Skip to content

Commit 96bf4e0

Browse files
committed
Detect mount type in parse_mount_string
Signed-off-by: Joffrey F <[email protected]>
1 parent 26ff248 commit 96bf4e0

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

docker/types/services.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import six
22

33
from .. import errors
4+
from ..constants import IS_WINDOWS_PLATFORM
45
from ..utils import format_environment, split_command
56

67

@@ -175,8 +176,17 @@ def parse_mount_string(cls, string):
175176
else:
176177
target = parts[1]
177178
source = parts[0]
179+
mount_type = 'volume'
180+
if source.startswith('/') or (
181+
IS_WINDOWS_PLATFORM and source[0].isalpha() and
182+
source[1] == ':'
183+
):
184+
# FIXME: That windows condition will fail earlier since we
185+
# split on ':'. We should look into doing a smarter split
186+
# if we detect we are on Windows.
187+
mount_type = 'bind'
178188
read_only = not (len(parts) == 2 or parts[2] == 'rw')
179-
return cls(target, source, read_only=read_only)
189+
return cls(target, source, read_only=read_only, type=mount_type)
180190

181191

182192
class Resources(dict):

tests/unit/dockertypes_test.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
EndpointConfig, HostConfig, IPAMConfig, IPAMPool, LogConfig, Mount, Ulimit,
1111
)
1212

13+
try:
14+
from unittest import mock
15+
except:
16+
import mock
17+
1318

1419
def create_host_config(*args, **kwargs):
1520
return HostConfig(*args, **kwargs)
@@ -258,28 +263,48 @@ def test_create_ipam_config(self):
258263
class TestMounts(unittest.TestCase):
259264
def test_parse_mount_string_ro(self):
260265
mount = Mount.parse_mount_string("/foo/bar:/baz:ro")
261-
self.assertEqual(mount['Source'], "/foo/bar")
262-
self.assertEqual(mount['Target'], "/baz")
263-
self.assertEqual(mount['ReadOnly'], True)
266+
assert mount['Source'] == "/foo/bar"
267+
assert mount['Target'] == "/baz"
268+
assert mount['ReadOnly'] is True
264269

265270
def test_parse_mount_string_rw(self):
266271
mount = Mount.parse_mount_string("/foo/bar:/baz:rw")
267-
self.assertEqual(mount['Source'], "/foo/bar")
268-
self.assertEqual(mount['Target'], "/baz")
269-
self.assertEqual(mount['ReadOnly'], False)
272+
assert mount['Source'] == "/foo/bar"
273+
assert mount['Target'] == "/baz"
274+
assert not mount['ReadOnly']
270275

271276
def test_parse_mount_string_short_form(self):
272277
mount = Mount.parse_mount_string("/foo/bar:/baz")
273-
self.assertEqual(mount['Source'], "/foo/bar")
274-
self.assertEqual(mount['Target'], "/baz")
275-
self.assertEqual(mount['ReadOnly'], False)
278+
assert mount['Source'] == "/foo/bar"
279+
assert mount['Target'] == "/baz"
280+
assert not mount['ReadOnly']
276281

277282
def test_parse_mount_string_no_source(self):
278283
mount = Mount.parse_mount_string("foo/bar")
279-
self.assertEqual(mount['Source'], None)
280-
self.assertEqual(mount['Target'], "foo/bar")
281-
self.assertEqual(mount['ReadOnly'], False)
284+
assert mount['Source'] is None
285+
assert mount['Target'] == "foo/bar"
286+
assert not mount['ReadOnly']
282287

283288
def test_parse_mount_string_invalid(self):
284289
with pytest.raises(InvalidArgument):
285290
Mount.parse_mount_string("foo:bar:baz:rw")
291+
292+
def test_parse_mount_named_volume(self):
293+
mount = Mount.parse_mount_string("foobar:/baz")
294+
assert mount['Source'] == 'foobar'
295+
assert mount['Target'] == '/baz'
296+
assert mount['Type'] == 'volume'
297+
298+
def test_parse_mount_bind(self):
299+
mount = Mount.parse_mount_string('/foo/bar:/baz')
300+
assert mount['Source'] == "/foo/bar"
301+
assert mount['Target'] == "/baz"
302+
assert mount['Type'] == 'bind'
303+
304+
@pytest.mark.xfail
305+
def test_parse_mount_bind_windows(self):
306+
with mock.patch('docker.types.services.IS_WINDOWS_PLATFORM', True):
307+
mount = Mount.parse_mount_string('C:/foo/bar:/baz')
308+
assert mount['Source'] == "C:/foo/bar"
309+
assert mount['Target'] == "/baz"
310+
assert mount['Type'] == 'bind'

0 commit comments

Comments
 (0)