Fix memory conversion to bytes (#2645)

* Fix memory conversion to bytes

Co-authored-by: Ulysses Souza <ulysses.souza@gmail.com>

Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
Anca Iordache 2020-08-17 18:32:48 +02:00 committed by GitHub
parent 30ff9f339c
commit 2c522fb362
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 21 deletions

View File

@ -506,7 +506,7 @@ class ContainerApiMixin(object):
bytes) or a string with a units identification char bytes) or a string with a units identification char
(``100000b``, ``1000k``, ``128m``, ``1g``). If a string is (``100000b``, ``1000k``, ``128m``, ``1g``). If a string is
specified without a units character, bytes are assumed as an specified without a units character, bytes are assumed as an
mem_reservation (int or str): Memory soft limit. mem_reservation (float or str): Memory soft limit.
mem_swappiness (int): Tune a container's memory swappiness mem_swappiness (int): Tune a container's memory swappiness
behavior. Accepts number between 0 and 100. behavior. Accepts number between 0 and 100.
memswap_limit (str or int): Maximum amount of memory + swap a memswap_limit (str or int): Maximum amount of memory + swap a
@ -1219,8 +1219,8 @@ class ContainerApiMixin(object):
cpu_shares (int): CPU shares (relative weight) cpu_shares (int): CPU shares (relative weight)
cpuset_cpus (str): CPUs in which to allow execution cpuset_cpus (str): CPUs in which to allow execution
cpuset_mems (str): MEMs in which to allow execution cpuset_mems (str): MEMs in which to allow execution
mem_limit (int or str): Memory limit mem_limit (float or str): Memory limit
mem_reservation (int or str): Memory soft limit mem_reservation (float or str): Memory soft limit
memswap_limit (int or str): Total memory (memory + swap), -1 to memswap_limit (int or str): Total memory (memory + swap), -1 to
disable swap disable swap
kernel_memory (int or str): Kernel memory limit kernel_memory (int or str): Kernel memory limit

View File

@ -412,7 +412,7 @@ def parse_bytes(s):
if suffix in units.keys() or suffix.isdigit(): if suffix in units.keys() or suffix.isdigit():
try: try:
digits = int(digits_part) digits = float(digits_part)
except ValueError: except ValueError:
raise errors.DockerException( raise errors.DockerException(
'Failed converting the string value for memory ({0}) to' 'Failed converting the string value for memory ({0}) to'

View File

@ -5,27 +5,21 @@ import json
import os import os
import os.path import os.path
import shutil import shutil
import sys
import tempfile import tempfile
import unittest import unittest
import pytest
import six
from docker.api.client import APIClient from docker.api.client import APIClient
from docker.constants import IS_WINDOWS_PLATFORM from docker.constants import IS_WINDOWS_PLATFORM
from docker.errors import DockerException from docker.errors import DockerException
from docker.utils import ( from docker.utils import (convert_filters, convert_volume_binds,
convert_filters, convert_volume_binds, decode_json_header, kwargs_from_env, decode_json_header, kwargs_from_env, parse_bytes,
parse_bytes, parse_devices, parse_env_file, parse_host, parse_devices, parse_env_file, parse_host,
parse_repository_tag, split_command, update_headers, parse_repository_tag, split_command, update_headers)
)
from docker.utils.ports import build_port_bindings, split_port from docker.utils.ports import build_port_bindings, split_port
from docker.utils.utils import format_environment from docker.utils.utils import format_environment
import pytest
import six
TEST_CERT_DIR = os.path.join( TEST_CERT_DIR = os.path.join(
os.path.dirname(__file__), os.path.dirname(__file__),
'testdata/certs', 'testdata/certs',
@ -447,11 +441,7 @@ class ParseBytesTest(unittest.TestCase):
parse_bytes("127.0.0.1K") parse_bytes("127.0.0.1K")
def test_parse_bytes_float(self): def test_parse_bytes_float(self):
with pytest.raises(DockerException): assert parse_bytes("1.5k") == 1536
parse_bytes("1.5k")
def test_parse_bytes_maxint(self):
assert parse_bytes("{0}k".format(sys.maxsize)) == sys.maxsize * 1024
class UtilsTest(unittest.TestCase): class UtilsTest(unittest.TestCase):