diff --git a/ironic/drivers/modules/image_cache.py b/ironic/drivers/modules/image_cache.py index 8bb1e2397b..ae3e5670f1 100644 --- a/ironic/drivers/modules/image_cache.py +++ b/ironic/drivers/modules/image_cache.py @@ -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 diff --git a/ironic/tests/drivers/test_image_cache.py b/ironic/tests/drivers/test_image_cache.py index 3d666cd6c2..89dbaabecd 100644 --- a/ironic/tests/drivers/test_image_cache.py +++ b/ironic/tests/drivers/test_image_cache.py @@ -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):