image_pull_policy does not work in glance driver

When I use the following command to create a container:
zun create --image-pull-policy always --image-driver glance ubuntu
the '--image-pull-policy always' does not work

Change-Id: I9429ac28a620ace3f3894f127388c8131cfbbcba
Closes-Bug: #1752015
This commit is contained in:
JiWei 2018-02-27 17:13:58 +08:00
parent aa3d4a3c8c
commit 0abcb5e773
2 changed files with 20 additions and 27 deletions

View File

@ -60,6 +60,8 @@ class GlanceDriver(driver.ContainerImageDriver):
# once metadata is stored in db then handle tags # once metadata is stored in db then handle tags
image_loaded = False image_loaded = False
image = self._search_image_on_host(context, repo) image = self._search_image_on_host(context, repo)
if not common_utils.should_pull_image(image_pull_policy, bool(image)):
if image: if image:
image_path = image['path'] image_path = image['path']
image_checksum = image['checksum'] image_checksum = image['checksum']
@ -75,12 +77,6 @@ class GlanceDriver(driver.ContainerImageDriver):
if md5sum == image_checksum: if md5sum == image_checksum:
image_loaded = True image_loaded = True
return image, image_loaded 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
else: else:
message = _('Image %s not present with pull policy of Never' message = _('Image %s not present with pull policy of Never'
) % repo ) % repo

View File

@ -52,12 +52,13 @@ class TestDriver(base.BaseTestCase):
def test_pull_image_should_pull_no_image_present_locally( def test_pull_image_should_pull_no_image_present_locally(
self, mock_should_pull_image, mock_search): self, mock_should_pull_image, mock_search):
mock_should_pull_image.return_value = False mock_should_pull_image.return_value = False
checksum = 'd41d8cd98f00b204e9800998ecf8427e'
mock_search.return_value = {'image': 'nginx', 'path': 'xyz', mock_search.return_value = {'image': 'nginx', 'path': 'xyz',
'checksum': 'xxx'} 'checksum': checksum}
mock_open_file = mock.mock_open() mock_open_file = mock.mock_open()
with mock.patch('zun.image.glance.driver.open', mock_open_file): with mock.patch('zun.image.glance.driver.open', mock_open_file):
self.assertEqual(({'image': 'nginx', 'path': 'xyz', self.assertEqual(({'image': 'nginx', 'path': 'xyz',
'checksum': 'xxx'}, True), 'checksum': checksum}, True),
self.driver.pull_image(None, 'nonexisting', self.driver.pull_image(None, 'nonexisting',
'tag', 'never')) 'tag', 'never'))
mock_open_file.assert_any_call('xyz', 'rb') mock_open_file.assert_any_call('xyz', 'rb')
@ -72,12 +73,9 @@ class TestDriver(base.BaseTestCase):
mock_should_pull_image.return_value = True mock_should_pull_image.return_value = True
mock_search.return_value = {'image': 'nginx', 'path': 'xyz', mock_search.return_value = {'image': 'nginx', 'path': 'xyz',
'checksum': 'xxx'} '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 mock_find_image.side_effect = Exception
self.assertRaises(exception.ZunException, self.driver.pull_image, self.assertRaises(exception.ZunException, self.driver.pull_image,
None, 'nonexisting', 'tag', 'always') None, 'nonexisting', 'tag', 'always')
mock_open_file.assert_any_call('xyz', 'rb')
@mock.patch.object(driver.GlanceDriver, @mock.patch.object(driver.GlanceDriver,
'_search_image_on_host') '_search_image_on_host')
@ -98,7 +96,6 @@ class TestDriver(base.BaseTestCase):
mock_open_file = mock.mock_open() mock_open_file = mock.mock_open()
with mock.patch('zun.image.glance.driver.open', mock_open_file): with mock.patch('zun.image.glance.driver.open', mock_open_file):
ret = self.driver.pull_image(None, 'image', 'latest', 'always') 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') mock_open_file.assert_any_call(out_path, 'wb')
self.assertTrue(mock_search_on_host.called) self.assertTrue(mock_search_on_host.called)
self.assertTrue(mock_should_pull_image.called) self.assertTrue(mock_should_pull_image.called)