Skip to content

Commit 6987326

Browse files
committed
Merge pull request #287 from leonidlm/feature/adding-memory-limit-string-support
Adding units specification to mem_limit variable
2 parents f2ea67b + 98a4aa9 commit 6987326

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ 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 ('100000b', 1000k', 128m', '1g'). If a string is specified without a units character, bytes are assumed as an intended unit.
85+
8486
`volumes_from` and `dns` arguments raise TypeError exception if they are used
8587
against v1.10 of docker remote API. Those arguments should be passed to
8688
`start()` instead.

docker/client.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,41 @@ def _container_config(self, image, command, hostname=None, user=None,
111111
'{0}={1}'.format(k, v) for k, v in environment.items()
112112
]
113113

114+
if isinstance(mem_limit, six.string_types):
115+
if len(mem_limit) == 0:
116+
mem_limit = 0
117+
else:
118+
units = {'b': 1,
119+
'k': 1024,
120+
'm': 1024*1024,
121+
'g': 1024*1024*1024}
122+
suffix = mem_limit[-1].lower()
123+
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():
133+
try:
134+
digits = int(digits_part)
135+
except ValueError:
136+
message = ('Failed converting the string value for'
137+
' mem_limit ({0}) to a number.')
138+
formatted_message = message.format(digits_part)
139+
raise errors.DockerException(formatted_message)
140+
141+
mem_limit = digits * units[suffix]
142+
else:
143+
message = ('The specified value for mem_limit parameter'
144+
' ({0}) should specify the units. The postfix'
145+
' should be one of the `b` `k` `m` `g`'
146+
' characters')
147+
raise errors.DockerException(message.format(mem_limit))
148+
114149
if isinstance(ports, list):
115150
exposed_ports = {}
116151
for port_definition in ports:

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)