Skip to content

Commit 28864df

Browse files
committed
Merge pull request #854 from lots0logs/840-add-exception-utility-method-for-create-host-config
840 add exception utility method for create host config
2 parents 9ebecb5 + e0c6ec0 commit 28864df

File tree

3 files changed

+67
-61
lines changed

3 files changed

+67
-61
lines changed

docker/utils/utils.py

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def convert_port_bindings(port_bindings):
236236
for k, v in six.iteritems(port_bindings):
237237
key = str(k)
238238
if '/' not in key:
239-
key = key + '/tcp'
239+
key += '/tcp'
240240
if isinstance(v, list):
241241
result[key] = [_convert_port_binding(binding) for binding in v]
242242
else:
@@ -434,7 +434,7 @@ def parse_bytes(s):
434434
s = 0
435435
else:
436436
if s[-2:-1].isalpha() and s[-1].isalpha():
437-
if (s[-1] == "b" or s[-1] == "B"):
437+
if s[-1] == "b" or s[-1] == "B":
438438
s = s[:-1]
439439
units = BYTE_UNITS
440440
suffix = s[-1].lower()
@@ -467,16 +467,32 @@ def parse_bytes(s):
467467
return s
468468

469469

470-
def create_host_config(
471-
binds=None, port_bindings=None, lxc_conf=None, publish_all_ports=False,
472-
links=None, privileged=False, dns=None, dns_search=None, volumes_from=None,
473-
network_mode=None, restart_policy=None, cap_add=None, cap_drop=None,
474-
devices=None, extra_hosts=None, read_only=None, pid_mode=None,
475-
ipc_mode=None, security_opt=None, ulimits=None, log_config=None,
476-
mem_limit=None, memswap_limit=None, mem_swappiness=None,
477-
cgroup_parent=None, group_add=None, cpu_quota=None, cpu_period=None,
478-
oom_kill_disable=False, version=None
479-
):
470+
def host_config_type_error(param, param_value, expected):
471+
error_msg = 'Invalid type for {0} param: expected {1} but found {2}'
472+
return TypeError(error_msg.format(param, expected, type(param_value)))
473+
474+
475+
def host_config_version_error(param, version, less_than=True):
476+
operator = '<' if less_than else '>'
477+
error_msg = '{0} param is not supported in API versions {1} {2}'
478+
return errors.InvalidVersion(error_msg.format(param, operator, version))
479+
480+
481+
def host_config_value_error(param, param_value):
482+
error_msg = 'Invalid value for {0} param: {1}'
483+
return ValueError(error_msg.format(param, param_value))
484+
485+
486+
def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
487+
publish_all_ports=False, links=None, privileged=False,
488+
dns=None, dns_search=None, volumes_from=None,
489+
network_mode=None, restart_policy=None, cap_add=None,
490+
cap_drop=None, devices=None, extra_hosts=None,
491+
read_only=None, pid_mode=None, ipc_mode=None,
492+
security_opt=None, ulimits=None, log_config=None,
493+
mem_limit=None, memswap_limit=None, mem_swappiness=None,
494+
cgroup_parent=None, group_add=None, cpu_quota=None,
495+
cpu_period=None, oom_kill_disable=False, version=None):
480496

481497
host_config = {}
482498

@@ -496,24 +512,21 @@ def create_host_config(
496512
if memswap_limit is not None:
497513
if isinstance(memswap_limit, six.string_types):
498514
memswap_limit = parse_bytes(memswap_limit)
515+
499516
host_config['MemorySwap'] = memswap_limit
500517

501518
if mem_swappiness is not None:
502519
if version_lt(version, '1.20'):
503-
raise errors.InvalidVersion(
504-
'mem_swappiness param not supported for API version < 1.20'
505-
)
520+
raise host_config_version_error('mem_swappiness', '1.20')
506521
if not isinstance(mem_swappiness, int):
507-
raise TypeError(
508-
'Invalid type for mem_swappiness param: expected int but'
509-
' found {0}'.format(type(mem_swappiness))
522+
raise host_config_type_error(
523+
'mem_swappiness', mem_swappiness, 'int'
510524
)
525+
511526
host_config['MemorySwappiness'] = mem_swappiness
512527

513528
if pid_mode not in (None, 'host'):
514-
raise errors.DockerException(
515-
'Invalid value for pid param: {0}'.format(pid_mode)
516-
)
529+
raise host_config_value_error('pid_mode', pid_mode)
517530
elif pid_mode:
518531
host_config['PidMode'] = pid_mode
519532

@@ -524,10 +537,9 @@ def create_host_config(
524537
host_config['Privileged'] = privileged
525538

526539
if oom_kill_disable:
527-
if version_lt(version, '1.19'):
528-
raise errors.InvalidVersion(
529-
'oom_kill_disable param not supported for API version < 1.19'
530-
)
540+
if version_lt(version, '1.20'):
541+
raise host_config_version_error('oom_kill_disable', '1.19')
542+
531543
host_config['OomKillDisable'] = oom_kill_disable
532544

533545
if publish_all_ports:
@@ -545,6 +557,11 @@ def create_host_config(
545557
host_config['NetworkMode'] = 'default'
546558

547559
if restart_policy:
560+
if not isinstance(restart_policy, dict):
561+
raise host_config_type_error(
562+
'restart_policy', restart_policy, 'dict'
563+
)
564+
548565
host_config['RestartPolicy'] = restart_policy
549566

550567
if cap_add:
@@ -558,34 +575,30 @@ def create_host_config(
558575

559576
if group_add:
560577
if version_lt(version, '1.20'):
561-
raise errors.InvalidVersion(
562-
'group_add param not supported for API version < 1.20'
563-
)
578+
raise host_config_version_error('group_add', '1.20')
579+
564580
host_config['GroupAdd'] = [six.text_type(grp) for grp in group_add]
565581

566582
if dns is not None:
567583
host_config['Dns'] = dns
568584

569585
if security_opt is not None:
570586
if not isinstance(security_opt, list):
571-
raise errors.DockerException(
572-
'Invalid type for security_opt param: expected list but found'
573-
' {0}'.format(type(security_opt))
574-
)
587+
raise host_config_type_error('security_opt', security_opt, 'list')
588+
575589
host_config['SecurityOpt'] = security_opt
576590

577591
if volumes_from is not None:
578592
if isinstance(volumes_from, six.string_types):
579593
volumes_from = volumes_from.split(',')
594+
580595
host_config['VolumesFrom'] = volumes_from
581596

582597
if binds is not None:
583598
host_config['Binds'] = convert_volume_binds(binds)
584599

585600
if port_bindings is not None:
586-
host_config['PortBindings'] = convert_port_bindings(
587-
port_bindings
588-
)
601+
host_config['PortBindings'] = convert_port_bindings(port_bindings)
589602

590603
if extra_hosts is not None:
591604
if isinstance(extra_hosts, dict):
@@ -600,9 +613,7 @@ def create_host_config(
600613
if isinstance(links, dict):
601614
links = six.iteritems(links)
602615

603-
formatted_links = [
604-
'{0}:{1}'.format(k, v) for k, v in sorted(links)
605-
]
616+
formatted_links = ['{0}:{1}'.format(k, v) for k, v in sorted(links)]
606617

607618
host_config['Links'] = formatted_links
608619

@@ -620,10 +631,7 @@ def create_host_config(
620631

621632
if ulimits is not None:
622633
if not isinstance(ulimits, list):
623-
raise errors.DockerException(
624-
'Invalid type for ulimits param: expected list but found'
625-
' {0}'.format(type(ulimits))
626-
)
634+
raise host_config_type_error('ulimits', ulimits, 'list')
627635
host_config['Ulimits'] = []
628636
for l in ulimits:
629637
if not isinstance(l, Ulimit):
@@ -633,35 +641,27 @@ def create_host_config(
633641
if log_config is not None:
634642
if not isinstance(log_config, LogConfig):
635643
if not isinstance(log_config, dict):
636-
raise errors.DockerException(
637-
'Invalid type for log_config param: expected LogConfig but'
638-
' found {0}'.format(type(log_config))
644+
raise host_config_type_error(
645+
'log_config', log_config, 'LogConfig'
639646
)
640647
log_config = LogConfig(**log_config)
648+
641649
host_config['LogConfig'] = log_config
642650

643651
if cpu_quota:
644652
if not isinstance(cpu_quota, int):
645-
raise TypeError(
646-
'Invalid type for cpu_quota param: expected int but'
647-
' found {0}'.format(type(cpu_quota))
648-
)
653+
raise host_config_type_error('cpu_quota', cpu_quota, 'int')
649654
if version_lt(version, '1.19'):
650-
raise errors.InvalidVersion(
651-
'cpu_quota param not supported for API version < 1.19'
652-
)
655+
raise host_config_version_error('cpu_quota', '1.19')
656+
653657
host_config['CpuQuota'] = cpu_quota
654658

655659
if cpu_period:
656660
if not isinstance(cpu_period, int):
657-
raise TypeError(
658-
'Invalid type for cpu_period param: expected int but'
659-
' found {0}'.format(type(cpu_period))
660-
)
661+
raise host_config_type_error('cpu_period', cpu_period, 'int')
661662
if version_lt(version, '1.19'):
662-
raise errors.InvalidVersion(
663-
'cpu_period param not supported for API version < 1.19'
664-
)
663+
raise host_config_version_error('cpu_period', '1.19')
664+
665665
host_config['CpuPeriod'] = cpu_period
666666

667667
return host_config

tests/integration/container_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,13 @@ def test_create_with_memory_constraints_with_int(self):
364364
host_config = inspect['HostConfig']
365365
self.assertIn('MemorySwappiness', host_config)
366366

367+
def test_create_host_config_exception_raising(self):
368+
self.assertRaises(TypeError,
369+
self.client.create_host_config, mem_swappiness='40')
370+
371+
self.assertRaises(ValueError,
372+
self.client.create_host_config, pid_mode='40')
373+
367374

368375
class VolumeBindTest(helpers.BaseTestCase):
369376
def setUp(self):

tests/unit/api_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,7 @@ def test_create_host_config_secopt(self):
314314
self.assertIn('SecurityOpt', result)
315315
self.assertEqual(result['SecurityOpt'], security_opt)
316316
self.assertRaises(
317-
docker.errors.DockerException, self.client.create_host_config,
318-
security_opt='wrong'
317+
TypeError, self.client.create_host_config, security_opt='wrong'
319318
)
320319

321320

0 commit comments

Comments
 (0)