Add unique names support for cont_exec_args method

In I5617e11f5d315f408d818e1ce47aa68f4a0d777a we switched
container_run_args to run the container cli run into a unique container
name, but we forgot to do it for the container cli execs.

So this patch will run the exec using the delegated container_name if we
can otherwise fall back on the fixed container name.

Co-Authored-By: Bogdan Dobrelya <bdobreli@redhat.com>
Change-Id: I2654148d566f62b3e3620baf84f504113cb7312d
Closes-Bug: #1840992
This commit is contained in:
Emilien Macchi 2019-08-21 19:31:11 -04:00 committed by Bogdan Dobrelya
parent 3dcbe5e68c
commit 2609ef6870
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

@ -170,7 +170,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
),
])
@ -318,7 +319,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
),
])
@ -503,7 +505,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',