Skip to content

Commit

Permalink
Add --image-pull-policy option to zun create command
Browse files Browse the repository at this point in the history
With the commit 2c6ad5a119b97bc127d7ad3945adfd805c3ce4ba,
we can decide whether to pull the image from docker repos
according to the image pull policy. Add policy support on
client side.

Change-Id: I37c4653670c630fabe79b8ed3927db133ac5d0b5
Depends-On: #If2715d7724fd5336fb25d380f125e1485ca302d0
Closes-bug: 1644072
  • Loading branch information
zhangjlcn committed Nov 23, 2016
1 parent 899fe35 commit 11fc5e5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
6 changes: 6 additions & 0 deletions zunclient/tests/v1/shell_test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class TestCommandLineArgument(utils.TestCase):
".*?^Try 'zun help ",
]

_invalid_choice_error = [
'.*?^usage: ',
'.*?^error: argument .*: invalid choice:',
".*?^Try 'zun help ",
]

def setUp(self):
super(TestCommandLineArgument, self).setUp()
self.make_env(fake_env=FAKE_ENV)
Expand Down
53 changes: 53 additions & 0 deletions zunclient/tests/v1/test_containers_shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import mock

from zunclient.tests.v1 import shell_test_base


class ShellTest(shell_test_base.TestCommandLineArgument):

@mock.patch('zunclient.v1.containers_shell._show_container')
@mock.patch('zunclient.v1.containers.ContainerManager.create')
def test_zun_container_create_success_with_pull_policy(
self, mock_create, mock_show_container):
mock_create.return_value = 'container-never'
self._test_arg_success(
'create --image x --image-pull-policy never')
mock_show_container.assert_called_with('container-never')

mock_create.return_value = 'container-always'
self._test_arg_success(
'create --image x --image-pull-policy always')
mock_show_container.assert_called_with('container-always')

mock_create.return_value = 'container-ifnotpresent'
self._test_arg_success(
'create --image x --image-pull-policy ifnotpresent')
mock_show_container.assert_called_with('container-ifnotpresent')

@mock.patch('zunclient.v1.containers_shell._show_container')
@mock.patch('zunclient.v1.containers.ContainerManager.create')
def test_zun_container_create_success_without_pull_policy(
self, mock_create, mock_show_container):
mock_create.return_value = 'container'
self._test_arg_success('create --image x')
mock_show_container.assert_called_once_with('container')

@mock.patch('zunclient.v1.containers.ContainerManager.create')
def test_zun_container_create_failure_with_wrong_pull_policy(
self, mock_create):
self._test_arg_failure(
'create --image x --image-pull-policy wrong',
self._invalid_choice_error)
self.assertFalse(mock_create.called)
3 changes: 2 additions & 1 deletion zunclient/v1/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@


CREATION_ATTRIBUTES = ['name', 'image', 'command', 'cpu', 'memory',
'environment', 'workdir', 'ports', 'hostname', 'labels']
'environment', 'workdir', 'ports', 'hostname', 'labels',
'image_pull_policy']


class Container(base.Resource):
Expand Down
12 changes: 12 additions & 0 deletions zunclient/v1/containers_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ def _show_container(container):
action='append', default=[],
help='Adds a map of labels to a container. '
'May be used multiple times.')
@utils.arg('--image-pull-policy',
dest='image_pull_policy',
metavar='<policy>',
choices=['never', 'always', 'ifnotpresent'],
help='The policy which determines if the image should '
'be pulled prior to starting the container. '
'It can have following values: '
'"ifnotpresent": only pull the image if it does not '
'already exist on the node. '
'"always": Always pull the image from repositery.'
'"never": never pull the image')
def do_create(cs, args):
"""Create a container."""
opts = {}
Expand All @@ -72,6 +83,7 @@ def do_create(cs, args):
opts['ports'] = args.expose
opts['hostname'] = args.hostname
opts['labels'] = zun_utils.format_labels(args.label)
opts['image_pull_policy'] = args.image_pull_policy
_show_container(cs.containers.create(**opts))


Expand Down

0 comments on commit 11fc5e5

Please sign in to comment.