added run command support at client side

Closes-bug: #1644900

Change-Id: I1797ae98c2649d211802ddb69c3180d98a96c28c
This commit is contained in:
prameswar 2016-11-30 11:41:46 +05:30
parent ae04a77b87
commit fd44c2beea
3 changed files with 107 additions and 0 deletions

View File

@ -51,3 +51,38 @@ class ShellTest(shell_test_base.TestCommandLineArgument):
'create --image x --image-pull-policy wrong',
self._invalid_choice_error)
self.assertFalse(mock_create.called)
@mock.patch('zunclient.v1.containers_shell._show_container')
@mock.patch('zunclient.v1.containers.ContainerManager.run')
def test_zun_container_run_success_with_pull_policy(
self, mock_run, mock_show_container):
mock_run.return_value = 'container-never'
self._test_arg_success(
'run --image x --image-pull-policy never')
mock_show_container.assert_called_with('container-never')
mock_run.return_value = 'container-always'
self._test_arg_success(
'run --image x --image-pull-policy always')
mock_show_container.assert_called_with('container-always')
mock_run.return_value = 'container-ifnotpresent'
self._test_arg_success(
'run --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.run')
def test_zun_container_run_success_without_pull_policy(
self, mock_run, mock_show_container):
mock_run.return_value = 'container'
self._test_arg_success('run --image x')
mock_show_container.assert_called_once_with('container')
@mock.patch('zunclient.v1.containers.ContainerManager.run')
def test_zun_container_run_failure_with_wrong_pull_policy(
self, mock_run):
self._test_arg_failure(
'run --image x --image-pull-policy wrong',
self._invalid_choice_error)
self.assertFalse(mock_run.called)

View File

@ -142,3 +142,10 @@ class ContainerManager(base.Manager):
def kill(self, id, signal=None):
return self._action(id, '/kill',
qparams={'signal': signal})[1]
def run(self, **kwargs):
if not set(kwargs).issubset(CREATION_ATTRIBUTES):
raise exceptions.InvalidAttribute(
"Key must be in %s" % ','.join(CREATION_ATTRIBUTES))
else:
return self._create(self._path() + '/run', kwargs)

View File

@ -276,3 +276,68 @@ def do_kill(cs, args):
print(
"kill signal for container %(container)s failed: %(e)s" %
{'container': args.container, 'e': e})
@utils.arg('-n', '--name',
metavar='<name>',
help='name of the container')
@utils.arg('-i', '--image',
required=True,
metavar='<image>',
help='name or ID of the image')
@utils.arg('-c', '--command',
metavar='<command>',
help='Send command to the container')
@utils.arg('--cpu',
metavar='<cpu>',
help='The number of virtual cpus.')
@utils.arg('-m', '--memory',
metavar='<memory>',
help='The container memory size (format: <number><optional unit>, '
'where unit = b, k, m or g)')
@utils.arg('-e', '--environment',
metavar='<KEY=VALUE>',
action='append', default=[],
help='The environment variabled')
@utils.arg('--workdir',
metavar='<workdir>',
help='The working directory for commands to run in')
@utils.arg('--expose',
metavar='<port>',
action='append', default=[],
help='A port or a list of ports to expose. '
'May be used multiple times.')
@utils.arg('--hostname',
metavar='<hostname>',
help='The hostname to use for the container')
@utils.arg('--label',
metavar='<KEY=VALUE>',
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_run(cs, args):
"""Run a command in a new container"""
opts = {}
opts['name'] = args.name
opts['image'] = args.image
opts['command'] = args.command
opts['memory'] = args.memory
opts['cpu'] = args.cpu
opts['environment'] = zun_utils.format_labels(args.environment)
opts['workdir'] = args.workdir
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.run(**opts))