Skip to content

Commit e0c6ec0

Browse files
committed
incorporate feedback
Signed-off-by: Dustin Falgout <[email protected]>
1 parent e0365fc commit e0c6ec0

File tree

3 files changed

+45
-46
lines changed

3 files changed

+45
-46
lines changed

docker/utils/utils.py

Lines changed: 40 additions & 30 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,17 +467,20 @@ def parse_bytes(s):
467467
return s
468468

469469

470-
def host_config_type_error(param_value=None, expected=None):
471-
return TypeError(
472-
'Invalid type for {0} param: expected {1} but found {2}'.format(param_value, expected,
473-
type(param_value))
474-
)
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)))
475473

476474

477-
def host_config_version_error(param=None, version=None):
478-
return errors.InvalidVersion(
479-
'{0} param is not supported in API versions < {1}'.format(param, version)
480-
)
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))
481484

482485

483486
def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
@@ -509,20 +512,21 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
509512
if memswap_limit is not None:
510513
if isinstance(memswap_limit, six.string_types):
511514
memswap_limit = parse_bytes(memswap_limit)
515+
512516
host_config['MemorySwap'] = memswap_limit
513517

514518
if mem_swappiness is not None:
515519
if version_lt(version, '1.20'):
516520
raise host_config_version_error('mem_swappiness', '1.20')
517521
if not isinstance(mem_swappiness, int):
518-
raise host_config_type_error(mem_swappiness, 'int')
522+
raise host_config_type_error(
523+
'mem_swappiness', mem_swappiness, 'int'
524+
)
519525

520526
host_config['MemorySwappiness'] = mem_swappiness
521527

522528
if pid_mode not in (None, 'host'):
523-
raise errors.DockerException(
524-
'Invalid value for pid param: {0}'.format(pid_mode)
525-
)
529+
raise host_config_value_error('pid_mode', pid_mode)
526530
elif pid_mode:
527531
host_config['PidMode'] = pid_mode
528532

@@ -533,10 +537,9 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
533537
host_config['Privileged'] = privileged
534538

535539
if oom_kill_disable:
536-
if version_lt(version, '1.19'):
537-
raise errors.InvalidVersion(
538-
'oom_kill_disable param not supported for API version < 1.19'
539-
)
540+
if version_lt(version, '1.20'):
541+
raise host_config_version_error('oom_kill_disable', '1.19')
542+
540543
host_config['OomKillDisable'] = oom_kill_disable
541544

542545
if publish_all_ports:
@@ -554,6 +557,11 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
554557
host_config['NetworkMode'] = 'default'
555558

556559
if restart_policy:
560+
if not isinstance(restart_policy, dict):
561+
raise host_config_type_error(
562+
'restart_policy', restart_policy, 'dict'
563+
)
564+
557565
host_config['RestartPolicy'] = restart_policy
558566

559567
if cap_add:
@@ -568,28 +576,29 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
568576
if group_add:
569577
if version_lt(version, '1.20'):
570578
raise host_config_version_error('group_add', '1.20')
579+
571580
host_config['GroupAdd'] = [six.text_type(grp) for grp in group_add]
572581

573582
if dns is not None:
574583
host_config['Dns'] = dns
575584

576585
if security_opt is not None:
577586
if not isinstance(security_opt, list):
578-
raise host_config_type_error(security_opt, 'list')
587+
raise host_config_type_error('security_opt', security_opt, 'list')
588+
579589
host_config['SecurityOpt'] = security_opt
580590

581591
if volumes_from is not None:
582592
if isinstance(volumes_from, six.string_types):
583593
volumes_from = volumes_from.split(',')
594+
584595
host_config['VolumesFrom'] = volumes_from
585596

586597
if binds is not None:
587598
host_config['Binds'] = convert_volume_binds(binds)
588599

589600
if port_bindings is not None:
590-
host_config['PortBindings'] = convert_port_bindings(
591-
port_bindings
592-
)
601+
host_config['PortBindings'] = convert_port_bindings(port_bindings)
593602

594603
if extra_hosts is not None:
595604
if isinstance(extra_hosts, dict):
@@ -604,9 +613,7 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
604613
if isinstance(links, dict):
605614
links = six.iteritems(links)
606615

607-
formatted_links = [
608-
'{0}:{1}'.format(k, v) for k, v in sorted(links)
609-
]
616+
formatted_links = ['{0}:{1}'.format(k, v) for k, v in sorted(links)]
610617

611618
host_config['Links'] = formatted_links
612619

@@ -624,7 +631,7 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
624631

625632
if ulimits is not None:
626633
if not isinstance(ulimits, list):
627-
raise host_config_type_error(ulimits, 'list')
634+
raise host_config_type_error('ulimits', ulimits, 'list')
628635
host_config['Ulimits'] = []
629636
for l in ulimits:
630637
if not isinstance(l, Ulimit):
@@ -634,21 +641,24 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
634641
if log_config is not None:
635642
if not isinstance(log_config, LogConfig):
636643
if not isinstance(log_config, dict):
637-
raise host_config_type_error(log_config, 'LogConfig')
644+
raise host_config_type_error(
645+
'log_config', log_config, 'LogConfig'
646+
)
638647
log_config = LogConfig(**log_config)
648+
639649
host_config['LogConfig'] = log_config
640650

641651
if cpu_quota:
642652
if not isinstance(cpu_quota, int):
643-
raise host_config_type_error(cpu_quota, 'int')
653+
raise host_config_type_error('cpu_quota', cpu_quota, 'int')
644654
if version_lt(version, '1.19'):
645655
raise host_config_version_error('cpu_quota', '1.19')
646656

647657
host_config['CpuQuota'] = cpu_quota
648658

649659
if cpu_period:
650660
if not isinstance(cpu_period, int):
651-
raise host_config_type_error(cpu_period, 'int')
661+
raise host_config_type_error('cpu_period', cpu_period, 'int')
652662
if version_lt(version, '1.19'):
653663
raise host_config_version_error('cpu_period', '1.19')
654664

tests/integration/container_test.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -365,21 +365,11 @@ def test_create_with_memory_constraints_with_int(self):
365365
self.assertIn('MemorySwappiness', host_config)
366366

367367
def test_create_host_config_exception_raising(self):
368-
with self.assertRaises(TypeError) as exc:
369-
ctnr1 = self.client.create_container(
370-
BUSYBOX, 'true',
371-
host_config=self.client.create_host_config(mem_swappiness='40')
372-
)
373-
self.assertIn('Invalid type for', exc.exception.response.text)
374-
self.client.remove_container(ctnr1.get('Id', ctnr1), force=True)
368+
self.assertRaises(TypeError,
369+
self.client.create_host_config, mem_swappiness='40')
375370

376-
with self.assertRaises(docker.errors.InvalidVersion) as exc:
377-
ctnr2 = self.client.create_container(
378-
BUSYBOX, 'true',
379-
host_config=self.client.create_host_config(version='1.18', mem_swappiness=40)
380-
)
381-
self.assertIn('param is not supported in', exc.exception.response.text)
382-
self.client.remove_container(ctnr2.get('Id', ctnr2), force=True)
371+
self.assertRaises(ValueError,
372+
self.client.create_host_config, pid_mode='40')
383373

384374

385375
class VolumeBindTest(helpers.BaseTestCase):

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)