Blob Blame History Raw
From e2b5a06cfd5375efc9741ea6bdad1ed764a65e54 Mon Sep 17 00:00:00 2001
From: Tomas Tomecek <ttomecek@redhat.com>
Date: Wed, 6 Sep 2017 17:57:56 +0200
Subject: [PATCH] implement cpu_rt_* switches for create_host_config

Signed-off-by: Tomas Tomecek <ttomecek@redhat.com>
---
 docker/utils/utils.py               | 23 ++++++++++++++++++++++-
 docs/hostconfig.md                  |  2 ++
 tests/integration/container_test.py | 28 ++++++++++++++++++++++++++++
 tests/unit/container_test.py        | 25 +++++++++++++++++++++++++
 4 files changed, 77 insertions(+), 1 deletion(-)

diff --git a/docker/utils/utils.py b/docker/utils/utils.py
index 8d55b57..2a183a0 100644
--- a/docker/utils/utils.py
+++ b/docker/utils/utils.py
@@ -609,7 +609,8 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
                        device_write_iops=None, oom_kill_disable=False,
                        shm_size=None, sysctls=None, version=None, tmpfs=None,
                        oom_score_adj=None, dns_opt=None, cpu_shares=None,
-                       cpuset_cpus=None, userns_mode=None, pids_limit=None):
+                       cpuset_cpus=None, userns_mode=None, pids_limit=None,
+                       cpu_rt_period=None, cpu_rt_runtime=None):
 
     host_config = {}
 
@@ -897,6 +898,26 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
             raise host_config_version_error('pids_limit', '1.23')
         host_config["PidsLimit"] = pids_limit
 
+    if cpu_rt_period:
+        if version_lt(version, '1.25'):
+            raise host_config_version_error('cpu_rt_period', '1.25')
+
+        if not isinstance(cpu_rt_period, int):
+            raise host_config_type_error(
+                'cpu_rt_period', cpu_rt_period, 'int'
+            )
+        host_config['CPURealtimePeriod'] = cpu_rt_period
+
+    if cpu_rt_runtime:
+        if version_lt(version, '1.25'):
+            raise host_config_version_error('cpu_rt_runtime', '1.25')
+
+        if not isinstance(cpu_rt_runtime, int):
+            raise host_config_type_error(
+                'cpu_rt_runtime', cpu_rt_runtime, 'int'
+            )
+        host_config['CPURealtimeRuntime'] = cpu_rt_runtime
+
     return host_config
 
 
diff --git a/docs/hostconfig.md b/docs/hostconfig.md
index 008d5cf..c39a02c 100644
--- a/docs/hostconfig.md
+++ b/docs/hostconfig.md
@@ -130,6 +130,8 @@ for example:
 * userns_mode (str): Sets the user namespace mode for the container when user
   namespace remapping option is enabled. Supported values are: `host`
 * pids_limit (int): Tune a container’s pids limit. Set -1 for unlimited.
+* cpu_rt_runtime (int): Limit the CPU real-time runtime in microseconds.
+* cpu_rt_period (int): Limit the CPU real-time period in microseconds.
 
 **Returns** (dict) HostConfig dictionary
 
diff --git a/tests/integration/container_test.py b/tests/integration/container_test.py
index e390acb..7d179d5 100644
--- a/tests/integration/container_test.py
+++ b/tests/integration/container_test.py
@@ -1144,3 +1144,31 @@ class ContainerCPUTest(helpers.BaseTestCase):
         self.client.start(container)
         inspect_data = self.client.inspect_container(container)
         self.assertEqual(inspect_data['HostConfig']['CpusetCpus'], cpuset_cpus)
+
+
+class ContainerCPURT(helpers.BaseTestCase):
+    @requires_api_version('1.25')
+    def test_container_cpu_rt_runtime(self):
+        cpu_rt_runtime = 1000
+        container = self.client.create_container(
+            BUSYBOX, 'ls', host_config=self.client.create_host_config(
+                cpu_rt_runtime=cpu_rt_runtime
+            )
+        )
+        self.tmp_containers.append(container)
+        self.client.start(container)
+        inspect_data = self.client.inspect_container(container)
+        self.assertEqual(inspect_data['HostConfig']['CPURealtimeRuntime'], 1000)
+
+    @requires_api_version('1.285')
+    def test_container_rt_period(self):
+        cpu_rt_period = 2000
+        container = self.client.create_container(
+            BUSYBOX, 'ls', host_config=self.client.create_host_config(
+                cpu_rt_period=cpu_rt_period
+            )
+        )
+        self.tmp_containers.append(container)
+        self.client.start(container)
+        inspect_data = self.client.inspect_container(container)
+        self.assertEqual(inspect_data['HostConfig']['CPURealtimePeriod'], 2000)
diff --git a/tests/unit/container_test.py b/tests/unit/container_test.py
index db3dd74..8c7b2c1 100644
--- a/tests/unit/container_test.py
+++ b/tests/unit/container_test.py
@@ -335,6 +335,31 @@ class CreateContainerTest(DockerClientTest):
         self.assertEqual(args[1]['headers'],
                          {'Content-Type': 'application/json'})
 
+    @requires_api_version('1.25')
+    def test_create_container_with_rt(self):
+        self.client.create_container(
+            'busybox', 'ls', host_config=self.client.create_host_config(
+                cpu_rt_period=1000, cpu_rt_runtime=1000
+            )
+        )
+
+        args = fake_request.call_args
+        self.assertEqual(args[0][1],
+                         url_prefix + 'containers/create')
+        self.assertEqual(json.loads(args[1]['data']),
+                         json.loads('''
+                            {"Tty": false, "Image": "busybox",
+                             "Cmd": ["ls"], "AttachStdin": false,
+                             "AttachStderr": true,
+                             "AttachStdout": true, "OpenStdin": false,
+                             "StdinOnce": false,
+                             "NetworkDisabled": false,
+                             "HostConfig": {
+                               "CPURealtimePeriod": 1000,
+                               "CPURealtimeRuntime": 1000,
+                               "NetworkMode": "default"
+                             }}'''))
+
     @requires_api_version('1.18')
     def test_create_container_with_host_config_cpuset(self):
         self.client.create_container(
-- 
2.14.1