Skip to content

Commit f2bdd01

Browse files
committed
Merge branch 'aanand-allow-any-mode'
2 parents b78397d + e05d06b commit f2bdd01

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

docker/utils/utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,21 @@ def convert_volume_binds(binds):
180180
result = []
181181
for k, v in binds.items():
182182
if isinstance(v, dict):
183+
if 'ro' in v and 'mode' in v:
184+
raise ValueError(
185+
'Binding cannot contain both "ro" and "mode": {}'
186+
.format(repr(v))
187+
)
188+
189+
if 'ro' in v:
190+
mode = 'ro' if v['ro'] else 'rw'
191+
elif 'mode' in v:
192+
mode = v['mode']
193+
else:
194+
mode = 'rw'
195+
183196
result.append('{0}:{1}:{2}'.format(
184-
k, v['bind'], 'ro' if v.get('ro', False) else 'rw'
197+
k, v['bind'], mode
185198
))
186199
else:
187200
result.append('{0}:{1}:rw'.format(k, v))

docs/volumes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ container_id = c.create_container(
1010
host_config=docker.utils.create_host_config(binds={
1111
'/home/user1/': {
1212
'bind': '/mnt/vol2',
13-
'ro': False
13+
'mode': 'rw',
1414
},
1515
'/var/www': {
1616
'bind': '/mnt/vol1',
17-
'ro': True
17+
'mode': 'ro',
1818
}
1919
})
2020
)

tests/test.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,53 @@ def test_create_container_with_binds_rw(self):
808808
DEFAULT_TIMEOUT_SECONDS
809809
)
810810

811+
def test_create_container_with_binds_mode(self):
812+
try:
813+
mount_dest = '/mnt'
814+
mount_origin = '/tmp'
815+
self.client.create_container(
816+
'busybox', 'true', host_config=create_host_config(
817+
binds={mount_origin: {
818+
"bind": mount_dest,
819+
"mode": "z",
820+
}}
821+
)
822+
)
823+
except Exception as e:
824+
self.fail('Command should not raise exception: {0}'.format(e))
825+
826+
args = fake_request.call_args
827+
self.assertEqual(args[0][0], url_prefix +
828+
'containers/create')
829+
expected_payload = self.base_create_payload()
830+
expected_payload['HostConfig'] = create_host_config()
831+
expected_payload['HostConfig']['Binds'] = ["/tmp:/mnt:z"]
832+
self.assertEqual(json.loads(args[1]['data']), expected_payload)
833+
self.assertEqual(args[1]['headers'],
834+
{'Content-Type': 'application/json'})
835+
self.assertEqual(
836+
args[1]['timeout'],
837+
DEFAULT_TIMEOUT_SECONDS
838+
)
839+
840+
def test_create_container_with_binds_mode_and_ro_error(self):
841+
try:
842+
mount_dest = '/mnt'
843+
mount_origin = '/tmp'
844+
self.client.create_container(
845+
'busybox', 'true', host_config=create_host_config(
846+
binds={mount_origin: {
847+
"bind": mount_dest,
848+
"mode": "z",
849+
"ro": True,
850+
}}
851+
)
852+
)
853+
except ValueError:
854+
return
855+
856+
self.fail('Command should raise ValueError')
857+
811858
def test_create_container_with_binds_list(self):
812859
try:
813860
self.client.create_container(

0 commit comments

Comments
 (0)