Merge "image_pull_policy does not work in glance driver"

This commit is contained in:
Zuul 2018-03-01 06:53:19 +00:00 committed by Gerrit Code Review
commit 2cd7ebc943
2 changed files with 20 additions and 27 deletions

View File

@ -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

View File

@ -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)