From d9d84ce8d1b9a54cb32c1d69a672e50c42cad540 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 23 Feb 2016 15:55:52 -0800 Subject: [PATCH] Format environment variables to match docker-cli. Signed-off-by: Daniel Nephin --- docker/utils/utils.py | 13 +++++++++---- tests/integration/container_test.py | 10 ++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/docker/utils/utils.py b/docker/utils/utils.py index 6fcf037a..bde18b6b 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -821,6 +821,14 @@ def split_command(command): return shlex.split(command) +def format_environment(environment): + def format_env(key, value): + if not value: + return key + return '{key}={value}'.format(key=key, value=value) + return [format_env(*var) for var in six.iteritems(environment)] + + def create_container_config( version, image, command, hostname=None, user=None, detach=False, stdin_open=False, tty=False, mem_limit=None, ports=None, environment=None, @@ -836,10 +844,7 @@ def create_container_config( entrypoint = split_command(entrypoint) if isinstance(environment, dict): - environment = [ - six.text_type('{0}={1}').format(k, v) - for k, v in six.iteritems(environment) - ] + environment = format_environment(environment) if labels is not None and compare_version('1.18', version) < 0: raise errors.InvalidVersion( diff --git a/tests/integration/container_test.py b/tests/integration/container_test.py index 1714599b..eb5b7467 100644 --- a/tests/integration/container_test.py +++ b/tests/integration/container_test.py @@ -370,6 +370,16 @@ class CreateContainerTest(helpers.BaseTestCase): self.assertRaises(ValueError, self.client.create_host_config, pid_mode='40') + def test_create_with_environment_variable_no_value(self): + container = self.client.create_container( + BUSYBOX, + ['echo'], + environment={'Foo': None, 'Other': 'one'}, + ) + self.tmp_containers.append(container['Id']) + config = self.client.inspect_container(container['Id']) + assert sorted(config['Config']['Env']) == sorted(['Foo', 'Other=one']) + class VolumeBindTest(helpers.BaseTestCase): def setUp(self):