From 75c7931f3da2bb9407d113048af16ec46d2a66f4 Mon Sep 17 00:00:00 2001 From: Yohan Graterol Date: Sun, 17 Aug 2014 02:23:32 -0500 Subject: [PATCH 1/6] Added validation for volumes parameter - Issue #276 --- docker/client.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docker/client.py b/docker/client.py index 1ff5a25a..70055a70 100644 --- a/docker/client.py +++ b/docker/client.py @@ -500,6 +500,9 @@ class Client(requests.Session): cpu_shares=None, working_dir=None, domainname=None, memswap_limit=0): + if isinstance(volumes, (str, unicode)): + volumes = [volumes, ] + config = self._container_config( image, command, hostname, user, detach, stdin_open, tty, mem_limit, ports, environment, dns, volumes, volumes_from, network_disabled, From 80b3688e81c377753884d48bea2dcc69b5e00363 Mon Sep 17 00:00:00 2001 From: Yohan Graterol Date: Sun, 17 Aug 2014 02:24:22 -0500 Subject: [PATCH 2/6] PEP8 compliance - space between numbers and operators --- docker/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/client.py b/docker/client.py index 70055a70..075e41c1 100644 --- a/docker/client.py +++ b/docker/client.py @@ -117,8 +117,8 @@ class Client(requests.Session): else: units = {'b': 1, 'k': 1024, - 'm': 1024*1024, - 'g': 1024*1024*1024} + 'm': 1024 * 1024, + 'g': 1024 * 1024 * 1024} suffix = mem_limit[-1].lower() # Check if the variable is a string representation of an int From 0a2940a8f06ac1d469a2441711bd1b0eda510d45 Mon Sep 17 00:00:00 2001 From: Yohan Graterol Date: Sun, 17 Aug 2014 02:27:00 -0500 Subject: [PATCH 3/6] Validation for volumes parameters as string in _container_config --- docker/client.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docker/client.py b/docker/client.py index 075e41c1..9af57e34 100644 --- a/docker/client.py +++ b/docker/client.py @@ -158,6 +158,9 @@ class Client(requests.Session): exposed_ports['{0}/{1}'.format(port, proto)] = {} ports = exposed_ports + if isinstance(volumes, (str, unicode)): + volumes = [volumes, ] + if isinstance(volumes, list): volumes_dict = {} for vol in volumes: From 10b11a3f9807d25b5d2e08dd32a14a079c5f5d00 Mon Sep 17 00:00:00 2001 From: Yohan Graterol Date: Sun, 17 Aug 2014 02:33:38 -0500 Subject: [PATCH 4/6] Remove unicode type in isinstance --- docker/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/client.py b/docker/client.py index 9af57e34..c265dc59 100644 --- a/docker/client.py +++ b/docker/client.py @@ -158,7 +158,7 @@ class Client(requests.Session): exposed_ports['{0}/{1}'.format(port, proto)] = {} ports = exposed_ports - if isinstance(volumes, (str, unicode)): + if isinstance(volumes, str): volumes = [volumes, ] if isinstance(volumes, list): @@ -503,7 +503,7 @@ class Client(requests.Session): cpu_shares=None, working_dir=None, domainname=None, memswap_limit=0): - if isinstance(volumes, (str, unicode)): + if isinstance(volumes, str): volumes = [volumes, ] config = self._container_config( From 28e27eb81ae2291c172a35ef19b511884538a62d Mon Sep 17 00:00:00 2001 From: Yohan Graterol Date: Sun, 17 Aug 2014 02:42:35 -0500 Subject: [PATCH 5/6] Create test for volume as string --- tests/test.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/test.py b/tests/test.py index 80f7f3d5..f1b6312d 100644 --- a/tests/test.py +++ b/tests/test.py @@ -248,6 +248,31 @@ class DockerClientTest(Cleanup, unittest.TestCase): self.assertEqual(args[1]['headers'], {'Content-Type': 'application/json'}) + def test_create_container_with_volume_string(self): + mount_dest = '/mnt' + + try: + self.client.create_container('busybox', ['ls', mount_dest], + volumes=mount_dest) + except Exception as e: + self.fail('Command should not raise exception: {0}'.format(e)) + + args = fake_request.call_args + self.assertEqual(args[0][0], + url_prefix + 'containers/create') + self.assertEqual(json.loads(args[1]['data']), + json.loads(''' + {"Tty": false, "Image": "busybox", + "Cmd": ["ls", "/mnt"], "AttachStdin": false, + "Volumes": {"/mnt": {}}, "Memory": 0, + "AttachStderr": true, + "AttachStdout": true, "OpenStdin": false, + "StdinOnce": false, + "NetworkDisabled": false, + "MemorySwap": 0}''')) + self.assertEqual(args[1]['headers'], + {'Content-Type': 'application/json'}) + def test_create_container_with_ports(self): try: self.client.create_container('busybox', 'ls', From 23a57c29ad7c92cb22acd21e2199fe024d284596 Mon Sep 17 00:00:00 2001 From: Yohan Graterol Date: Wed, 20 Aug 2014 10:28:56 -0500 Subject: [PATCH 6/6] Change str type for six.string_types to py3 compatibility --- docker/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/client.py b/docker/client.py index c265dc59..eac7b287 100644 --- a/docker/client.py +++ b/docker/client.py @@ -158,7 +158,7 @@ class Client(requests.Session): exposed_ports['{0}/{1}'.format(port, proto)] = {} ports = exposed_ports - if isinstance(volumes, str): + if isinstance(volumes, six.string_types): volumes = [volumes, ] if isinstance(volumes, list): @@ -503,7 +503,7 @@ class Client(requests.Session): cpu_shares=None, working_dir=None, domainname=None, memswap_limit=0): - if isinstance(volumes, str): + if isinstance(volumes, six.string_types): volumes = [volumes, ] config = self._container_config(