diff --git a/zun/image/glance/driver.py b/zun/image/glance/driver.py index c6228680a..131b350f2 100644 --- a/zun/image/glance/driver.py +++ b/zun/image/glance/driver.py @@ -60,27 +60,23 @@ class GlanceDriver(driver.ContainerImageDriver): # once metadata is stored in db then handle tags image_loaded = False image = self._search_image_on_host(context, repo) - if image: - image_path = image['path'] - image_checksum = image['checksum'] - md5sum = hashlib.md5() - with open(image_path, 'rb') as fd: - while True: - # read 10MB of data each time - data = fd.read(10 * 1024 * 1024) - if not data: - break - md5sum.update(data) - md5sum = md5sum.hexdigest() - if md5sum == image_checksum: - image_loaded = True - return image, image_loaded if not common_utils.should_pull_image(image_pull_policy, bool(image)): if image: - LOG.debug('Image %s present locally', repo) - image_loaded = True - return image, image_loaded + image_path = image['path'] + image_checksum = image['checksum'] + md5sum = hashlib.md5() + with open(image_path, 'rb') as fd: + while True: + # read 10MB of data each time + data = fd.read(10 * 1024 * 1024) + if not data: + break + md5sum.update(data) + md5sum = md5sum.hexdigest() + if md5sum == image_checksum: + image_loaded = True + return image, image_loaded else: message = _('Image %s not present with pull policy of Never' ) % repo diff --git a/zun/tests/unit/image/glance/test_driver.py b/zun/tests/unit/image/glance/test_driver.py index 7fbb8f2ba..5a2643cd9 100644 --- a/zun/tests/unit/image/glance/test_driver.py +++ b/zun/tests/unit/image/glance/test_driver.py @@ -52,12 +52,13 @@ class TestDriver(base.BaseTestCase): def test_pull_image_should_pull_no_image_present_locally( self, mock_should_pull_image, mock_search): mock_should_pull_image.return_value = False + checksum = 'd41d8cd98f00b204e9800998ecf8427e' mock_search.return_value = {'image': 'nginx', 'path': 'xyz', - 'checksum': 'xxx'} + 'checksum': checksum} mock_open_file = mock.mock_open() with mock.patch('zun.image.glance.driver.open', mock_open_file): self.assertEqual(({'image': 'nginx', 'path': 'xyz', - 'checksum': 'xxx'}, True), + 'checksum': checksum}, True), self.driver.pull_image(None, 'nonexisting', 'tag', 'never')) mock_open_file.assert_any_call('xyz', 'rb') @@ -72,12 +73,9 @@ class TestDriver(base.BaseTestCase): mock_should_pull_image.return_value = True mock_search.return_value = {'image': 'nginx', 'path': 'xyz', 'checksum': 'xxx'} - mock_open_file = mock.mock_open() - with mock.patch('zun.image.glance.driver.open', mock_open_file): - mock_find_image.side_effect = Exception - self.assertRaises(exception.ZunException, self.driver.pull_image, - None, 'nonexisting', 'tag', 'always') - mock_open_file.assert_any_call('xyz', 'rb') + mock_find_image.side_effect = Exception + self.assertRaises(exception.ZunException, self.driver.pull_image, + None, 'nonexisting', 'tag', 'always') @mock.patch.object(driver.GlanceDriver, '_search_image_on_host') @@ -98,7 +96,6 @@ class TestDriver(base.BaseTestCase): mock_open_file = mock.mock_open() with mock.patch('zun.image.glance.driver.open', mock_open_file): ret = self.driver.pull_image(None, 'image', 'latest', 'always') - mock_open_file.assert_any_call('xyz', 'rb') mock_open_file.assert_any_call(out_path, 'wb') self.assertTrue(mock_search_on_host.called) self.assertTrue(mock_should_pull_image.called)