From 8d770b012d6786ffb468e0fc929bde309a9500b1 Mon Sep 17 00:00:00 2001 From: Michael Hankin Date: Sun, 3 Dec 2017 14:54:28 -0600 Subject: [PATCH 1/2] Change format of extra hosts Signed-off-by: Michael Hankin --- docker/utils/utils.py | 2 +- tests/integration/api_service_test.py | 4 ++-- tests/unit/models_containers_test.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docker/utils/utils.py b/docker/utils/utils.py index a123fd8f..3e2a710d 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -566,7 +566,7 @@ def format_environment(environment): def format_extra_hosts(extra_hosts): return [ - '{}:{}'.format(k, v) for k, v in sorted(six.iteritems(extra_hosts)) + '{} {}'.format(v, k) for k, v in sorted(six.iteritems(extra_hosts)) ] diff --git a/tests/integration/api_service_test.py b/tests/integration/api_service_test.py index b9311549..05b5ba75 100644 --- a/tests/integration/api_service_test.py +++ b/tests/integration/api_service_test.py @@ -588,8 +588,8 @@ class ServiceTest(BaseAPIIntegrationTest): assert 'Hosts' in svc_info['Spec']['TaskTemplate']['ContainerSpec'] hosts = svc_info['Spec']['TaskTemplate']['ContainerSpec']['Hosts'] assert len(hosts) == 2 - assert 'foobar:127.0.0.1' in hosts - assert 'baz:8.8.8.8' in hosts + assert '127.0.0.1 foobar' in hosts + assert '8.8.8.8 baz' in hosts @requires_api_version('1.25') def test_create_service_with_hostname(self): diff --git a/tests/unit/models_containers_test.py b/tests/unit/models_containers_test.py index 5eaa45ac..29a5caad 100644 --- a/tests/unit/models_containers_test.py +++ b/tests/unit/models_containers_test.py @@ -141,7 +141,7 @@ class ContainerCollectionTest(unittest.TestCase): 'Dns': ['8.8.8.8'], 'DnsOptions': ['foo'], 'DnsSearch': ['example.com'], - 'ExtraHosts': ['foo:1.2.3.4'], + 'ExtraHosts': ['1.2.3.4 foo'], 'GroupAdd': ['blah'], 'IpcMode': 'foo', 'KernelMemory': 123, From 0134939c2c5cc6920339a65a69305227849a452d Mon Sep 17 00:00:00 2001 From: Michael Hankin Date: Tue, 5 Dec 2017 21:19:37 -0600 Subject: [PATCH 2/2] Change format in which hosts are being stored for Swarm services Signed-off-by: Michael Hankin --- docker/types/services.py | 2 +- docker/utils/utils.py | 10 ++++++++-- tests/unit/models_containers_test.py | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/docker/types/services.py b/docker/types/services.py index 9031e609..5b6af8f5 100644 --- a/docker/types/services.py +++ b/docker/types/services.py @@ -137,7 +137,7 @@ class ContainerSpec(dict): if labels is not None: self['Labels'] = labels if hosts is not None: - self['Hosts'] = format_extra_hosts(hosts) + self['Hosts'] = format_extra_hosts(hosts, task=True) if mounts is not None: parsed_mounts = [] diff --git a/docker/utils/utils.py b/docker/utils/utils.py index 3e2a710d..845af653 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -564,9 +564,15 @@ def format_environment(environment): return [format_env(*var) for var in six.iteritems(environment)] -def format_extra_hosts(extra_hosts): +def format_extra_hosts(extra_hosts, task=False): + # Use format dictated by Swarm API if container is part of a task + if task: + return [ + '{} {}'.format(v, k) for k, v in sorted(six.iteritems(extra_hosts)) + ] + return [ - '{} {}'.format(v, k) for k, v in sorted(six.iteritems(extra_hosts)) + '{}:{}'.format(k, v) for k, v in sorted(six.iteritems(extra_hosts)) ] diff --git a/tests/unit/models_containers_test.py b/tests/unit/models_containers_test.py index 29a5caad..5eaa45ac 100644 --- a/tests/unit/models_containers_test.py +++ b/tests/unit/models_containers_test.py @@ -141,7 +141,7 @@ class ContainerCollectionTest(unittest.TestCase): 'Dns': ['8.8.8.8'], 'DnsOptions': ['foo'], 'DnsSearch': ['example.com'], - 'ExtraHosts': ['1.2.3.4 foo'], + 'ExtraHosts': ['foo:1.2.3.4'], 'GroupAdd': ['blah'], 'IpcMode': 'foo', 'KernelMemory': 123,