Merge "Fix handling of "Permission Denied" error from NamedTemporaryFile function"

This commit is contained in:
Jenkins 2016-01-26 14:17:34 +00:00 committed by Gerrit Code Review
commit f1989c5e8b
2 changed files with 30 additions and 5 deletions

View File

@ -2573,17 +2573,19 @@ def dump_recon_cache(cache_dict, cache_file, logger, lock_timeout=2):
pass pass
for cache_key, cache_value in cache_dict.items(): for cache_key, cache_value in cache_dict.items():
put_recon_cache_entry(cache_entry, cache_key, cache_value) put_recon_cache_entry(cache_entry, cache_key, cache_value)
tf = None
try: try:
with NamedTemporaryFile(dir=os.path.dirname(cache_file), with NamedTemporaryFile(dir=os.path.dirname(cache_file),
delete=False) as tf: delete=False) as tf:
tf.write(json.dumps(cache_entry) + '\n') tf.write(json.dumps(cache_entry) + '\n')
renamer(tf.name, cache_file, fsync=False) renamer(tf.name, cache_file, fsync=False)
finally: finally:
try: if tf is not None:
os.unlink(tf.name) try:
except OSError as err: os.unlink(tf.name)
if err.errno != errno.ENOENT: except OSError as err:
raise if err.errno != errno.ENOENT:
raise
except (Exception, Timeout): except (Exception, Timeout):
logger.exception(_('Exception dumping recon cache')) logger.exception(_('Exception dumping recon cache'))

View File

@ -1213,6 +1213,29 @@ class TestUtils(unittest.TestCase):
finally: finally:
rmtree(testdir_base) 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): def test_get_logger(self):
sio = StringIO() sio = StringIO()
logger = logging.getLogger('server') logger = logging.getLogger('server')