Skip to content

Commit 98a4aa9

Browse files
committed
Adding tests for mem_limit string conversion + adding default unit
1 parent 18f2b6f commit 98a4aa9

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ more information on how to create port bindings and volume mappings.
8181
The `environment` variable accepts a dictionary or a list of strings
8282
in the following format `["PASSWORD=xxx"]` or `{"PASSWORD": "xxx"}`.
8383

84-
The `mem_limit` variable accepts float values (which represent the memory limit of the created container in bytes) or a string with a units identification char ('1000k', 128m', '1g').
84+
The `mem_limit` variable accepts float values (which represent the memory limit of the created container in bytes) or a string with a units identification char ('100000b', 1000k', 128m', '1g'). If a string is specified without a units character, bytes are assumed as an intended unit.
8585

8686
`volumes_from` and `dns` arguments raise TypeError exception if they are used
8787
against v1.10 of docker remote API. Those arguments should be passed to

docker/client.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,35 @@ def _container_config(self, image, command, hostname=None, user=None,
115115
if len(mem_limit) == 0:
116116
mem_limit = 0
117117
else:
118-
units = {'k': 1024,
118+
units = {'b': 1,
119+
'k': 1024,
119120
'm': 1024*1024,
120121
'g': 1024*1024*1024}
121122
suffix = mem_limit[-1].lower()
122123

123-
if suffix in units.keys():
124+
# Check if the variable is a string representation of an int
125+
# without a units part. Assuming that the units are bytes.
126+
if suffix.isdigit():
127+
digits_part = mem_limit
128+
suffix = 'b'
129+
else:
130+
digits_part = mem_limit[:-1]
131+
132+
if suffix in units.keys() or suffix.isdigit():
124133
try:
125-
digits = int(mem_limit[:-1])
134+
digits = int(digits_part)
126135
except ValueError:
127136
message = ('Failed converting the string value for'
128137
' mem_limit ({0}) to a number.')
129-
formatted_message = message.format(mem_limit[:-1])
138+
formatted_message = message.format(digits_part)
130139
raise errors.DockerException(formatted_message)
131140

132141
mem_limit = digits * units[suffix]
133142
else:
134143
message = ('The specified value for mem_limit parameter'
135144
' ({0}) should specify the units. The postfix'
136-
' should be one of the `k` `m` `g` characters')
145+
' should be one of the `b` `k` `m` `g`'
146+
' characters')
137147
raise errors.DockerException(message.format(mem_limit))
138148

139149
if isinstance(ports, list):

tests/test.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,70 @@ def test_create_named_container(self):
418418
{'Content-Type': 'application/json'})
419419
self.assertEqual(args[1]['params'], {'name': 'marisa-kirisame'})
420420

421+
def test_create_container_with_mem_limit_as_int(self):
422+
try:
423+
self.client.create_container('busybox', 'true',
424+
mem_limit=128.0)
425+
except Exception as e:
426+
self.fail('Command should not raise exception: {0}'.format(e))
427+
428+
args = fake_request.call_args
429+
data = json.loads(args[1]['data'])
430+
self.assertEqual(data['Memory'], 128.0)
431+
432+
def test_create_container_with_mem_limit_as_string(self):
433+
try:
434+
self.client.create_container('busybox', 'true',
435+
mem_limit='128')
436+
except Exception as e:
437+
self.fail('Command should not raise exception: {0}'.format(e))
438+
439+
args = fake_request.call_args
440+
data = json.loads(args[1]['data'])
441+
self.assertEqual(data['Memory'], 128.0)
442+
443+
def test_create_container_with_mem_limit_as_string_with_k_unit(self):
444+
try:
445+
self.client.create_container('busybox', 'true',
446+
mem_limit='128k')
447+
except Exception as e:
448+
self.fail('Command should not raise exception: {0}'.format(e))
449+
450+
args = fake_request.call_args
451+
data = json.loads(args[1]['data'])
452+
self.assertEqual(data['Memory'], 128.0 * 1024)
453+
454+
def test_create_container_with_mem_limit_as_string_with_m_unit(self):
455+
try:
456+
self.client.create_container('busybox', 'true',
457+
mem_limit='128m')
458+
except Exception as e:
459+
self.fail('Command should not raise exception: {0}'.format(e))
460+
461+
args = fake_request.call_args
462+
data = json.loads(args[1]['data'])
463+
self.assertEqual(data['Memory'], 128.0 * 1024 * 1024)
464+
465+
def test_create_container_with_mem_limit_as_string_with_g_unit(self):
466+
try:
467+
self.client.create_container('busybox', 'true',
468+
mem_limit='128g')
469+
except Exception as e:
470+
self.fail('Command should not raise exception: {0}'.format(e))
471+
472+
args = fake_request.call_args
473+
data = json.loads(args[1]['data'])
474+
self.assertEqual(data['Memory'], 128.0 * 1024 * 1024 * 1024)
475+
476+
def test_create_container_with_mem_limit_as_string_with_wrong_value(self):
477+
self.assertRaises(docker.errors.DockerException,
478+
self.client.create_container,
479+
'busybox', 'true', mem_limit='128p')
480+
481+
self.assertRaises(docker.errors.DockerException,
482+
self.client.create_container,
483+
'busybox', 'true', mem_limit='1f28')
484+
421485
def test_start_container(self):
422486
try:
423487
self.client.start(fake_api.FAKE_CONTAINER_ID)

0 commit comments

Comments
 (0)