diff options
author | Aanand Prasad <aanand.prasad@gmail.com> | 2015-06-05 12:55:03 +0100 |
---|---|---|
committer | Aanand Prasad <aanand.prasad@gmail.com> | 2015-06-05 13:25:59 +0100 |
commit | 7dd762539196eaade8da183d7588627c51d5a479 (patch) | |
tree | 77aa3f6faf15e1a2c884383a823ad7f63d804d63 | |
parent | be73aaf5401faf5ca64911fb6664036c7b7ec61b (diff) | |
download | docker-py-7dd762539196eaade8da183d7588627c51d5a479.tar.gz |
Allow any mode string to be passed into a volume bind
Volume binds now take a "mode" key, whose value can be any string.
"ro" is still supported. It is an error to specify both "ro" and "mode".
Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
-rw-r--r-- | docker/utils/utils.py | 15 | ||||
-rw-r--r-- | docs/volumes.md | 4 | ||||
-rw-r--r-- | tests/test.py | 47 |
3 files changed, 63 insertions, 3 deletions
diff --git a/docker/utils/utils.py b/docker/utils/utils.py index e4a3c9e..54b01ce 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -177,8 +177,21 @@ def convert_volume_binds(binds): result = [] for k, v in binds.items(): if isinstance(v, dict): + if 'ro' in v and 'mode' in v: + raise ValueError( + 'Binding cannot contain both "ro" and "mode": {}' + .format(repr(v)) + ) + + if 'ro' in v: + mode = 'ro' if v['ro'] else 'rw' + elif 'mode' in v: + mode = v['mode'] + else: + mode = 'rw' + result.append('{0}:{1}:{2}'.format( - k, v['bind'], 'ro' if v.get('ro', False) else 'rw' + k, v['bind'], mode )) else: result.append('{0}:{1}:rw'.format(k, v)) diff --git a/docs/volumes.md b/docs/volumes.md index de28214..86f8571 100644 --- a/docs/volumes.md +++ b/docs/volumes.md @@ -10,11 +10,11 @@ container_id = c.create_container( host_config=docker.utils.create_host_config(binds={ '/home/user1/': { 'bind': '/mnt/vol2', - 'ro': False + 'mode': 'rw', }, '/var/www': { 'bind': '/mnt/vol1', - 'ro': True + 'mode': 'ro', } }) ) diff --git a/tests/test.py b/tests/test.py index e0a9e34..1a88d47 100644 --- a/tests/test.py +++ b/tests/test.py @@ -808,6 +808,53 @@ class DockerClientTest(Cleanup, base.BaseTestCase): DEFAULT_TIMEOUT_SECONDS ) + def test_create_container_with_binds_mode(self): + try: + mount_dest = '/mnt' + mount_origin = '/tmp' + self.client.create_container( + 'busybox', 'true', host_config=create_host_config( + binds={mount_origin: { + "bind": mount_dest, + "mode": "z", + }} + ) + ) + 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:z"] + 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_binds_mode_and_ro_error(self): + try: + mount_dest = '/mnt' + mount_origin = '/tmp' + self.client.create_container( + 'busybox', 'true', host_config=create_host_config( + binds={mount_origin: { + "bind": mount_dest, + "mode": "z", + "ro": True, + }} + ) + ) + except ValueError: + return + + self.fail('Command should raise ValueError') + def test_create_container_with_port_binds(self): self.maxDiff = None try: |