Cached file should not be deleted if time equal to master

The cached file was being deleted when the modification times were
identical. This causes an issue especially with local storage of the
master images as a hard link is done and the cache file would always be
flagged as out of date.

Also give more details when the cached file is deleted.

Closes-Bug: #1499601
Change-Id: I0964c84028271b8a11e1178463bb2d513ce26410
This commit is contained in:
John L. Villalovos 2015-09-24 17:05:54 -07:00 committed by John L. Villalovos
parent ecf6fc17f0
commit e964614679
2 changed files with 21 additions and 4 deletions

View File

@ -407,14 +407,15 @@ def _delete_master_path_if_stale(master_path, href, ctx):
"cached image up to date."), {'href': href})
return True
master_mtime = utils.unix_file_modification_datetime(master_path)
if img_mtime < master_mtime:
if img_mtime <= master_mtime:
return True
# Delete image from cache as it is outdated
LOG.info(_LI('Image %(href)s was last modified at %(remote_time)s. '
'Deleting the cached copy since it was last modified at '
'%(local_time)s and may be outdated.'),
'Deleting the cached copy "%(cached_file)s since it was '
'last modified at %(local_time)s and may be outdated.'),
{'href': href, 'remote_time': img_mtime,
'local_time': master_mtime})
'local_time': master_mtime, 'cached_file': master_path})
os.unlink(master_path)
return False

View File

@ -243,6 +243,22 @@ class TestUpdateImages(base.TestCase):
self.assertFalse(mock_unlink.called)
self.assertTrue(res)
@mock.patch.object(image_service, 'get_image_service', autospec=True)
def test__delete_master_path_if_stale_master_same_time(self, mock_gis,
mock_unlink):
# When times identical should not delete cached file
touch(self.master_path)
mtime = utils.unix_file_modification_datetime(self.master_path)
href = 'http://awesomefreeimages.al/img999'
mock_gis.return_value.show.return_value = {
'updated_at': mtime
}
res = image_cache._delete_master_path_if_stale(self.master_path, href,
None)
mock_gis.assert_called_once_with(href, context=None)
self.assertFalse(mock_unlink.called)
self.assertTrue(res)
@mock.patch.object(image_service, 'get_image_service', autospec=True)
def test__delete_master_path_if_stale_out_of_date(self, mock_gis,
mock_unlink):