builder: allow to pass a dict to 'environment'

To allow to pass container environment as a dict instead of a list.
It's backward compatible, a list can still be passed.

We introduce a new function "list_or_dict_arg" so we can re-use it for
other parameters later if needed.

Related-Bug: #1855932
Change-Id: I85999889d3328dc9d2116b8539ac959b39cb833a
(cherry picked from commit cfa2fc51ab)
This commit is contained in:
Emilien Macchi 2019-10-07 22:03:47 -04:00 committed by Alex Schultz
parent 65f063f946
commit 0f20a8eb1d
4 changed files with 20 additions and 11 deletions

View File

@ -238,6 +238,21 @@ class BaseBuilder(object):
if v:
cmd.append('%s=%s' % (arg, v))
def list_or_dict_arg(self, cconfig, cmd, key, arg):
if key not in cconfig:
return
value = cconfig[key]
if isinstance(value, dict):
for k, v in sorted(value.items()):
if v:
cmd.append('%s=%s=%s' % (arg, k, v))
elif k:
cmd.append('%s=%s' % (arg, k))
else:
for v in value:
if v:
cmd.append('%s=%s' % (arg, v))
def cont_exec_args(self, cmd, container):
"""Prepare the exec command args, from the container configuration.

View File

@ -37,10 +37,7 @@ class ComposeV1Builder(base.BaseBuilder):
if cconfig.get('detach', True):
cmd.append('--detach=true')
self.list_or_string_arg(cconfig, cmd, 'env_file', '--env-file')
# TODO(sbaker): support the dict layout for this property
for v in cconfig.get('environment', []):
if v:
cmd.append('--env=%s' % v)
self.list_or_dict_arg(cconfig, cmd, 'environment', '--env')
self.boolean_arg(cconfig, cmd, 'remove', '--rm')
self.boolean_arg(cconfig, cmd, 'interactive', '--interactive')
self.boolean_arg(cconfig, cmd, 'tty', '--tty')

View File

@ -59,10 +59,7 @@ class PodmanBuilder(base.BaseBuilder):
raise ValueError('cont_log_path passed but not absolute.')
self.list_or_string_arg(cconfig, cmd, 'env_file', '--env-file')
# TODO(sbaker): support the dict layout for this property
for v in cconfig.get('environment', []):
if v:
cmd.append('--env=%s' % v)
self.list_or_dict_arg(cconfig, cmd, 'environment', '--env')
self.boolean_arg(cconfig, cmd, 'remove', '--rm')
self.boolean_arg(cconfig, cmd, 'interactive', '--interactive')
self.boolean_arg(cconfig, cmd, 'tty', '--tty')

View File

@ -536,7 +536,7 @@ three-12345678 three''', '', 0),
)
@mock.patch('paunch.runner.DockerRunner', autospec=True)
def test_container_run_args_lists_with_cpu(self, runner):
def test_container_run_args_lists_with_cpu_and_dict_env(self, runner):
config = {
'one': {
'image': 'centos:7',
@ -545,7 +545,7 @@ three-12345678 three''', '', 0),
'remove': True,
'tty': True,
'interactive': True,
'environment': ['FOO=BAR', 'BAR=BAZ'],
'environment': {'BAR': 'BAZ', 'FOO': 'BAR', 'SINGLE': ''},
'env_file': ['/tmp/foo.env', '/tmp/bar.env'],
'ulimit': ['nofile=1024', 'nproc=1024'],
'volumes': ['/foo:/foo:rw', '/bar:/bar:ro'],
@ -563,7 +563,7 @@ three-12345678 three''', '', 0),
self.assertEqual(
['docker', 'run', '--name', 'one',
'--env-file=/tmp/foo.env', '--env-file=/tmp/bar.env',
'--env=FOO=BAR', '--env=BAR=BAZ',
'--env=BAR=BAZ', '--env=FOO=BAR', '--env=SINGLE',
'--rm', '--interactive', '--tty',
'--ulimit=nofile=1024', '--ulimit=nproc=1024',
'--group-add=docker', '--group-add=zuul',