Merge branch 'aanand-allow-binds-list' into 1.3.0-rc0

This commit is contained in:
Joffrey F 2015-06-12 11:29:56 -07:00
commit 6e3dba99b7
4 changed files with 57 additions and 2 deletions

View File

@ -174,6 +174,9 @@ def convert_port_bindings(port_bindings):
def convert_volume_binds(binds): def convert_volume_binds(binds):
if isinstance(binds, list):
return binds
result = [] result = []
for k, v in binds.items(): for k, v in binds.items():
if isinstance(v, dict): if isinstance(v, dict):
@ -322,6 +325,9 @@ def parse_bytes(s):
if len(s) == 0: if len(s) == 0:
s = 0 s = 0
else: else:
if s[-2:-1].isalpha() and s[-1].isalpha():
if (s[-1] == "b" or s[-1] == "B"):
s = s[:-1]
units = BYTE_UNITS units = BYTE_UNITS
suffix = s[-1].lower() suffix = s[-1].lower()
@ -380,7 +386,7 @@ def create_host_config(
host_config['PublishAllPorts'] = publish_all_ports host_config['PublishAllPorts'] = publish_all_ports
if read_only is not None: if read_only is not None:
host_config['ReadOnlyRootFs'] = read_only host_config['ReadonlyRootfs'] = read_only
if dns_search: if dns_search:
host_config['DnsSearch'] = dns_search host_config['DnsSearch'] = dns_search

View File

@ -19,3 +19,16 @@ container_id = c.create_container(
}) })
) )
``` ```
You can alternatively specify binds as a list. This code is equivalent to the
example above:
```python
container_id = c.create_container(
'busybox', 'ls', volumes=['/mnt/vol1', '/mnt/vol2'],
host_config=docker.utils.create_host_config(binds=[
'/home/user1/:/mnt/vol2',
'/var/www:/mnt/vol1:ro',
])
)
```

View File

@ -808,6 +808,36 @@ class DockerClientTest(Cleanup, base.BaseTestCase):
DEFAULT_TIMEOUT_SECONDS DEFAULT_TIMEOUT_SECONDS
) )
def test_create_container_with_binds_list(self):
try:
self.client.create_container(
'busybox', 'true', host_config=create_host_config(
binds=[
"/tmp:/mnt/1:ro",
"/tmp:/mnt/2",
],
)
)
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')
expected_payload = self.base_create_payload()
expected_payload['HostConfig'] = create_host_config()
expected_payload['HostConfig']['Binds'] = [
"/tmp:/mnt/1:ro",
"/tmp:/mnt/2",
]
self.assertEqual(json.loads(args[1]['data']), expected_payload)
self.assertEqual(args[1]['headers'],
{'Content-Type': 'application/json'})
self.assertEqual(
args[1]['timeout'],
DEFAULT_TIMEOUT_SECONDS
)
def test_create_container_with_port_binds(self): def test_create_container_with_port_binds(self):
self.maxDiff = None self.maxDiff = None
try: try:

View File

@ -6,7 +6,7 @@ from docker.client import Client
from docker.errors import DockerException from docker.errors import DockerException
from docker.utils import ( from docker.utils import (
parse_repository_tag, parse_host, convert_filters, kwargs_from_env, parse_repository_tag, parse_host, convert_filters, kwargs_from_env,
create_host_config, Ulimit, LogConfig create_host_config, Ulimit, LogConfig, parse_bytes
) )
from docker.utils.ports import build_port_bindings, split_port from docker.utils.ports import build_port_bindings, split_port
from docker.auth import resolve_authconfig from docker.auth import resolve_authconfig
@ -37,6 +37,12 @@ class UtilsTest(base.BaseTestCase):
self.assertEqual(parse_repository_tag("url:5000/repo:tag"), self.assertEqual(parse_repository_tag("url:5000/repo:tag"),
("url:5000/repo", "tag")) ("url:5000/repo", "tag"))
def test_parse_bytes(self):
self.assertEqual(parse_bytes("512MB"), (536870912))
self.assertEqual(parse_bytes("512M"), (536870912))
self.assertRaises(DockerException, parse_bytes, "512MK")
self.assertRaises(DockerException, parse_bytes, "512L")
def test_parse_host(self): def test_parse_host(self):
invalid_hosts = [ invalid_hosts = [
'0.0.0.0', '0.0.0.0',