Skip to content

Commit ef452ed

Browse files
smothikismothiki
authored andcommitted
make memory units compatible with native docker cli
Signed-off-by: sivaram mothiki <[email protected]>
1 parent f28e90b commit ef452ed

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

docker/utils/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@ def parse_bytes(s):
322322
if len(s) == 0:
323323
s = 0
324324
else:
325+
if s[-2:-1].isalpha() and s[-1].isalpha():
326+
if (s[-1] == "b" or s[-1] == "B"):
327+
s = s[:-1]
325328
units = BYTE_UNITS
326329
suffix = s[-1].lower()
327330

tests/utils_test.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from docker.errors import DockerException
77
from docker.utils import (
88
parse_repository_tag, parse_host, convert_filters, kwargs_from_env,
9-
create_host_config, Ulimit, LogConfig
9+
create_host_config, Ulimit, LogConfig, parse_bytes
1010
)
1111
from docker.utils.ports import build_port_bindings, split_port
1212
from docker.auth import resolve_authconfig
@@ -37,6 +37,12 @@ def test_parse_repository_tag(self):
3737
self.assertEqual(parse_repository_tag("url:5000/repo:tag"),
3838
("url:5000/repo", "tag"))
3939

40+
def test_parse_bytes(self):
41+
self.assertEqual(parse_bytes("512MB"), (536870912))
42+
self.assertEqual(parse_bytes("512M"), (536870912))
43+
self.assertRaises(DockerException, parse_bytes, "512MK")
44+
self.assertRaises(DockerException, parse_bytes, "512L")
45+
4046
def test_parse_host(self):
4147
invalid_hosts = [
4248
'0.0.0.0',

0 commit comments

Comments
 (0)