Skip to content

Commit 19ef2dd

Browse files
authored
Merge pull request #1547 from shin-/1543-volume-driver-moved
Add support for volume_driver in HostConfig
2 parents 3076a9a + 79edcc2 commit 19ef2dd

File tree

3 files changed

+79
-48
lines changed

3 files changed

+79
-48
lines changed

docker/types/containers.py

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def __init__(self, version, binds=None, port_bindings=None,
118118
tmpfs=None, oom_score_adj=None, dns_opt=None, cpu_shares=None,
119119
cpuset_cpus=None, userns_mode=None, pids_limit=None,
120120
isolation=None, auto_remove=False, storage_opt=None,
121-
init=None, init_path=None):
121+
init=None, init_path=None, volume_driver=None):
122122

123123
if mem_limit is not None:
124124
self['Memory'] = parse_bytes(mem_limit)
@@ -428,6 +428,11 @@ def __init__(self, version, binds=None, port_bindings=None,
428428
raise host_config_version_error('init_path', '1.25')
429429
self['InitPath'] = init_path
430430

431+
if volume_driver is not None:
432+
if version_lt(version, '1.21'):
433+
raise host_config_version_error('volume_driver', '1.21')
434+
self['VolumeDriver'] = volume_driver
435+
431436

432437
def host_config_type_error(param, param_value, expected):
433438
error_msg = 'Invalid type for {0} param: expected {1} but found {2}'
@@ -456,43 +461,27 @@ def __init__(
456461
stop_signal=None, networking_config=None, healthcheck=None,
457462
stop_timeout=None
458463
):
459-
if isinstance(command, six.string_types):
460-
command = split_command(command)
461-
462-
if isinstance(entrypoint, six.string_types):
463-
entrypoint = split_command(entrypoint)
464-
465-
if isinstance(environment, dict):
466-
environment = format_environment(environment)
467-
468-
if labels is not None and version_lt(version, '1.18'):
469-
raise errors.InvalidVersion(
470-
'labels were only introduced in API version 1.18'
471-
)
464+
if version_gte(version, '1.10'):
465+
message = ('{0!r} parameter has no effect on create_container().'
466+
' It has been moved to host_config')
467+
if dns is not None:
468+
raise errors.InvalidVersion(message.format('dns'))
469+
if volumes_from is not None:
470+
raise errors.InvalidVersion(message.format('volumes_from'))
472471

473-
if cpuset is not None or cpu_shares is not None:
474-
if version_gte(version, '1.18'):
472+
if version_lt(version, '1.18'):
473+
if labels is not None:
474+
raise errors.InvalidVersion(
475+
'labels were only introduced in API version 1.18'
476+
)
477+
else:
478+
if cpuset is not None or cpu_shares is not None:
475479
warnings.warn(
476480
'The cpuset_cpus and cpu_shares options have been moved to'
477481
' host_config in API version 1.18, and will be removed',
478482
DeprecationWarning
479483
)
480484

481-
if stop_signal is not None and version_lt(version, '1.21'):
482-
raise errors.InvalidVersion(
483-
'stop_signal was only introduced in API version 1.21'
484-
)
485-
486-
if stop_timeout is not None and version_lt(version, '1.25'):
487-
raise errors.InvalidVersion(
488-
'stop_timeout was only introduced in API version 1.25'
489-
)
490-
491-
if healthcheck is not None and version_lt(version, '1.24'):
492-
raise errors.InvalidVersion(
493-
'Health options were only introduced in API version 1.24'
494-
)
495-
496485
if version_lt(version, '1.19'):
497486
if volume_driver is not None:
498487
raise errors.InvalidVersion(
@@ -513,6 +502,38 @@ def __init__(
513502
'version 1.19'
514503
)
515504

505+
if version_lt(version, '1.21'):
506+
if stop_signal is not None:
507+
raise errors.InvalidVersion(
508+
'stop_signal was only introduced in API version 1.21'
509+
)
510+
else:
511+
if volume_driver is not None:
512+
warnings.warn(
513+
'The volume_driver option has been moved to'
514+
' host_config in API version 1.21, and will be removed',
515+
DeprecationWarning
516+
)
517+
518+
if stop_timeout is not None and version_lt(version, '1.25'):
519+
raise errors.InvalidVersion(
520+
'stop_timeout was only introduced in API version 1.25'
521+
)
522+
523+
if healthcheck is not None and version_lt(version, '1.24'):
524+
raise errors.InvalidVersion(
525+
'Health options were only introduced in API version 1.24'
526+
)
527+
528+
if isinstance(command, six.string_types):
529+
command = split_command(command)
530+
531+
if isinstance(entrypoint, six.string_types):
532+
entrypoint = split_command(entrypoint)
533+
534+
if isinstance(environment, dict):
535+
environment = format_environment(environment)
536+
516537
if isinstance(labels, list):
517538
labels = dict((lbl, six.text_type('')) for lbl in labels)
518539

@@ -566,14 +587,6 @@ def __init__(
566587
attach_stdin = True
567588
stdin_once = True
568589

569-
if version_gte(version, '1.10'):
570-
message = ('{0!r} parameter has no effect on create_container().'
571-
' It has been moved to host_config')
572-
if dns is not None:
573-
raise errors.InvalidVersion(message.format('dns'))
574-
if volumes_from is not None:
575-
raise errors.InvalidVersion(message.format('volumes_from'))
576-
577590
self.update({
578591
'Hostname': hostname,
579592
'Domainname': domainname,

tests/unit/api_container_test.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,8 @@ def test_create_container_with_volumes_from(self):
407407
{'Content-Type': 'application/json'})
408408

409409
def test_create_container_empty_volumes_from(self):
410-
self.client.create_container('busybox', 'true', volumes_from=[])
411-
412-
args = fake_request.call_args
413-
data = json.loads(args[1]['data'])
414-
self.assertTrue('VolumesFrom' not in data)
410+
with pytest.raises(docker.errors.InvalidVersion):
411+
self.client.create_container('busybox', 'true', volumes_from=[])
415412

416413
def test_create_named_container(self):
417414
self.client.create_container('busybox', 'true',
@@ -978,20 +975,20 @@ def test_create_container_with_named_volume(self):
978975
self.client.create_container(
979976
'busybox', 'true',
980977
host_config=self.client.create_host_config(
978+
volume_driver='foodriver',
981979
binds={volume_name: {
982980
"bind": mount_dest,
983981
"ro": False
984982
}}),
985-
volume_driver='foodriver',
986983
)
987984

988985
args = fake_request.call_args
989986
self.assertEqual(
990987
args[0][1], url_prefix + 'containers/create'
991988
)
992989
expected_payload = self.base_create_payload()
993-
expected_payload['VolumeDriver'] = 'foodriver'
994990
expected_payload['HostConfig'] = self.client.create_host_config()
991+
expected_payload['HostConfig']['VolumeDriver'] = 'foodriver'
995992
expected_payload['HostConfig']['Binds'] = ["name:/mnt:rw"]
996993
self.assertEqual(json.loads(args[1]['data']), expected_payload)
997994
self.assertEqual(args[1]['headers'],

tests/unit/dockertypes_test.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# -*- coding: utf-8 -*-
22

33
import unittest
4+
import warnings
45

56
import pytest
67

78
from docker.constants import DEFAULT_DOCKER_API_VERSION
89
from docker.errors import InvalidArgument, InvalidVersion
910
from docker.types import (
10-
EndpointConfig, HostConfig, IPAMConfig, IPAMPool, LogConfig, Mount,
11-
ServiceMode, Ulimit,
11+
ContainerConfig, EndpointConfig, HostConfig, IPAMConfig, IPAMPool,
12+
LogConfig, Mount, ServiceMode, Ulimit,
1213
)
1314

1415
try:
@@ -165,6 +166,26 @@ def test_create_host_config_invalid_mem_swappiness(self):
165166
with pytest.raises(TypeError):
166167
create_host_config(version='1.24', mem_swappiness='40')
167168

169+
def test_create_host_config_with_volume_driver(self):
170+
with pytest.raises(InvalidVersion):
171+
create_host_config(version='1.20', volume_driver='local')
172+
173+
config = create_host_config(version='1.21', volume_driver='local')
174+
assert config.get('VolumeDriver') == 'local'
175+
176+
177+
class ContainerConfigTest(unittest.TestCase):
178+
def test_create_container_config_volume_driver_warning(self):
179+
with warnings.catch_warnings(record=True) as w:
180+
warnings.simplefilter('always')
181+
ContainerConfig(
182+
version='1.21', image='scratch', command=None,
183+
volume_driver='local'
184+
)
185+
186+
assert len(w) == 1
187+
assert 'The volume_driver option has been moved' in str(w[0].message)
188+
168189

169190
class UlimitTest(unittest.TestCase):
170191
def test_create_host_config_dict_ulimit(self):

0 commit comments

Comments
 (0)