Fix handling of "Permission Denied" error from NamedTemporaryFile function
If "Permission Denied" has happen in NamedTemporaryFile function in dump_recon_cache method, swift will log a message of reference to a variable without assignment and not log a message of "Permission Denied". This patch fixes the handling and add an unit test. Co-Authored-By: Kota Tsuyuzaki <tsuyuzaki.kota@lab.ntt.co.jp> Change-Id: Iafdd94905e9e9c81f5966a923324b50c18fcf592
This commit is contained in:
parent
53c4735b02
commit
9ef15453fa
@ -2566,17 +2566,19 @@ def dump_recon_cache(cache_dict, cache_file, logger, lock_timeout=2):
|
||||
pass
|
||||
for cache_key, cache_value in cache_dict.items():
|
||||
put_recon_cache_entry(cache_entry, cache_key, cache_value)
|
||||
tf = None
|
||||
try:
|
||||
with NamedTemporaryFile(dir=os.path.dirname(cache_file),
|
||||
delete=False) as tf:
|
||||
tf.write(json.dumps(cache_entry) + '\n')
|
||||
renamer(tf.name, cache_file, fsync=False)
|
||||
finally:
|
||||
try:
|
||||
os.unlink(tf.name)
|
||||
except OSError as err:
|
||||
if err.errno != errno.ENOENT:
|
||||
raise
|
||||
if tf is not None:
|
||||
try:
|
||||
os.unlink(tf.name)
|
||||
except OSError as err:
|
||||
if err.errno != errno.ENOENT:
|
||||
raise
|
||||
except (Exception, Timeout):
|
||||
logger.exception(_('Exception dumping recon cache'))
|
||||
|
||||
|
@ -1216,6 +1216,29 @@ class TestUtils(unittest.TestCase):
|
||||
finally:
|
||||
rmtree(testdir_base)
|
||||
|
||||
def test_dump_recon_cache_permission_denied(self):
|
||||
testdir_base = mkdtemp()
|
||||
testcache_file = os.path.join(testdir_base, 'cache.recon')
|
||||
|
||||
class MockLogger(object):
|
||||
def __init__(self):
|
||||
self._excs = []
|
||||
|
||||
def exception(self, message):
|
||||
_junk, exc, _junk = sys.exc_info()
|
||||
self._excs.append(exc)
|
||||
|
||||
logger = MockLogger()
|
||||
try:
|
||||
submit_dict = {'key1': {'value1': 1, 'value2': 2}}
|
||||
with mock.patch(
|
||||
'swift.common.utils.NamedTemporaryFile',
|
||||
side_effect=IOError(13, 'Permission Denied')):
|
||||
utils.dump_recon_cache(submit_dict, testcache_file, logger)
|
||||
self.assertIsInstance(logger._excs[0], IOError)
|
||||
finally:
|
||||
rmtree(testdir_base)
|
||||
|
||||
def test_get_logger(self):
|
||||
sio = StringIO()
|
||||
logger = logging.getLogger('server')
|
||||
|
Loading…
Reference in New Issue
Block a user