Merge "Add unique names support for cont_exec_args method"

This commit is contained in:
Zuul 2019-10-01 23:54:46 +00:00 committed by Gerrit Code Review
commit 54fc78cf31
2 changed files with 20 additions and 8 deletions

View File

@ -95,6 +95,8 @@ class BaseBuilder(object):
elif action == 'exec':
# for exec, the first argument is the fixed named container
# used when running the command into the running container.
# use the discovered container name to manipulate with the
# real (delagate) container representing the fixed named one
command = self.command_argument(cconfig.get('command'))
if command:
c_name = self.runner.discover_container_name(
@ -110,7 +112,9 @@ class BaseBuilder(object):
'container: %s' % container)
raise RuntimeError(msg)
cmd = [self.runner.cont_cmd, 'exec']
validations_passed = self.cont_exec_args(cmd, container)
validations_passed = self.cont_exec_args(cmd,
container,
c_name)
if not validations_passed:
self.log.debug('Validations failed. Skipping container: %s' %
@ -227,14 +231,17 @@ class BaseBuilder(object):
if v:
cmd.append('%s=%s' % (arg, v))
def cont_exec_args(self, cmd, container):
def cont_exec_args(self, cmd, container, delegate=None):
"""Prepare the exec command args, from the container configuration.
:param cmd: The list of command options to be modified
:param container: A dict with container configurations
:param delegate: A predictable/unique name of the actual container
:returns: True if configuration is valid, otherwise False
"""
if delegate and container != delegate:
self.log.debug("Container {} has a delegate "
"{}".format(container, delegate))
cconfig = self.config[container]
if 'privileged' in cconfig:
cmd.append('--privileged=%s' % str(cconfig['privileged']).lower())
@ -244,8 +251,11 @@ class BaseBuilder(object):
# for exec, the first argument is the container name,
# make sure the correct one is used
if command:
command[0] = self.runner.discover_container_name(
command[0], self.config_id)
if not delegate:
command[0] = self.runner.discover_container_name(
command[0], self.config_id)
else:
command[0] = delegate
cmd.extend(command)
return True

View File

@ -176,7 +176,8 @@ class TestBaseBuilder(base.TestCase):
),
# execute within four
mock.call(
['docker', 'exec', 'four-12345678', 'ls', '-l', '/'], mock.ANY
['docker', 'exec', 'four-12345678', 'ls', '-l',
'/'], mock.ANY
),
])
@ -328,7 +329,8 @@ three-12345678 three''', '', 0),
),
# execute within four
mock.call(
['docker', 'exec', 'four-12345678', 'ls', '-l', '/'], mock.ANY
['docker', 'exec', 'four-12345678', 'ls', '-l',
'/'], mock.ANY
),
])
@ -555,7 +557,7 @@ three-12345678 three''', '', 0),
'foo', config, runner.return_value)
cmd = ['docker', 'exec']
self.builder.cont_exec_args(cmd, 'one')
self.builder.cont_exec_args(cmd, 'one', 'one-12345678')
self.assertEqual(
['docker', 'exec',
'--privileged=true', '--user=bar',