From 847f209865cb7c9234cd19a9ae8cc9a158d9a140 Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Thu, 26 Jan 2017 16:14:57 -0800 Subject: [PATCH] Add stop_timeout to create_container Fix requires_api_version test decorator Signed-off-by: Joffrey F --- Jenkinsfile | 6 ++---- docker/api/container.py | 5 ++++- docker/types/containers.py | 7 +++++++ tests/helpers.py | 8 +++++--- tests/integration/api_build_test.py | 4 ++++ tests/integration/api_container_test.py | 9 +++++++++ 6 files changed, 31 insertions(+), 8 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index bc4cc06d..b8b932ae 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -36,15 +36,14 @@ def buildImages = { -> def getAPIVersion = { engineVersion -> def versionMap = ['1.12': '1.24', '1.13': '1.25'] - - engineVersion = engineVersion.substring(0, 4) - return versionMap[engineVersion] + return versionMap[engineVersion.substring(0, 4)] } def runTests = { Map settings -> def dockerVersion = settings.get("dockerVersion", null) def pythonVersion = settings.get("pythonVersion", null) def testImage = settings.get("testImage", null) + def apiVersion = getAPIVersion(dockerVersion) if (!testImage) { throw new Exception("Need test image object, e.g.: `runTests(testImage: img)`") @@ -62,7 +61,6 @@ def runTests = { Map settings -> checkout(scm) def dindContainerName = "dpy-dind-\$BUILD_NUMBER-\$EXECUTOR_NUMBER-${pythonVersion}-${dockerVersion}" def testContainerName = "dpy-tests-\$BUILD_NUMBER-\$EXECUTOR_NUMBER-${pythonVersion}-${dockerVersion}" - def apiVersion = getAPIVersion(dockerVersion) try { sh """docker run -d --name ${dindContainerName} -v /tmp --privileged \\ dockerswarm/dind:${dockerVersion} docker daemon -H tcp://0.0.0.0:2375 diff --git a/docker/api/container.py b/docker/api/container.py index 482b7b64..acb0ffa8 100644 --- a/docker/api/container.py +++ b/docker/api/container.py @@ -238,7 +238,7 @@ class ContainerApiMixin(object): memswap_limit=None, cpuset=None, host_config=None, mac_address=None, labels=None, volume_driver=None, stop_signal=None, networking_config=None, - healthcheck=None): + healthcheck=None, stop_timeout=None): """ Creates a container. Parameters are similar to those for the ``docker run`` command except it doesn't support the attach options (``-a``). @@ -411,6 +411,8 @@ class ContainerApiMixin(object): volume_driver (str): The name of a volume driver/plugin. stop_signal (str): The stop signal to use to stop the container (e.g. ``SIGINT``). + stop_timeout (int): Timeout to stop the container, in seconds. + Default: 10 networking_config (dict): A networking configuration generated by :py:meth:`create_networking_config`. @@ -437,6 +439,7 @@ class ContainerApiMixin(object): network_disabled, entrypoint, cpu_shares, working_dir, domainname, memswap_limit, cpuset, host_config, mac_address, labels, volume_driver, stop_signal, networking_config, healthcheck, + stop_timeout ) return self.create_container_from_config(config, name) diff --git a/docker/types/containers.py b/docker/types/containers.py index 7e7d9eaa..3c0e41e0 100644 --- a/docker/types/containers.py +++ b/docker/types/containers.py @@ -438,6 +438,7 @@ class ContainerConfig(dict): working_dir=None, domainname=None, memswap_limit=None, cpuset=None, host_config=None, mac_address=None, labels=None, volume_driver=None, stop_signal=None, networking_config=None, healthcheck=None, + stop_timeout=None ): if isinstance(command, six.string_types): command = split_command(command) @@ -466,6 +467,11 @@ class ContainerConfig(dict): 'stop_signal was only introduced in API version 1.21' ) + if stop_timeout is not None and version_lt(version, '1.25'): + raise errors.InvalidVersion( + 'stop_timeout was only introduced in API version 1.25' + ) + if healthcheck is not None and version_lt(version, '1.24'): raise errors.InvalidVersion( 'Health options were only introduced in API version 1.24' @@ -584,4 +590,5 @@ class ContainerConfig(dict): 'VolumeDriver': volume_driver, 'StopSignal': stop_signal, 'Healthcheck': healthcheck, + 'StopTimeout': stop_timeout }) diff --git a/tests/helpers.py b/tests/helpers.py index 1e423631..b742c960 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -43,10 +43,12 @@ def untar_file(tardata, filename): def requires_api_version(version): + test_version = os.environ.get( + 'DOCKER_TEST_API_VERSION', docker.constants.DEFAULT_DOCKER_API_VERSION + ) + return pytest.mark.skipif( - docker.utils.version_lt( - docker.constants.DEFAULT_DOCKER_API_VERSION, version - ), + docker.utils.version_lt(test_version, version), reason="API version is too low (< {0})".format(version) ) diff --git a/tests/integration/api_build_test.py b/tests/integration/api_build_test.py index e7479bfb..c2fd26c1 100644 --- a/tests/integration/api_build_test.py +++ b/tests/integration/api_build_test.py @@ -3,6 +3,7 @@ import os import shutil import tempfile +import pytest import six from docker import errors @@ -154,9 +155,11 @@ class BuildTest(BaseAPIIntegrationTest): self.assertEqual(info['Config']['Labels'], labels) @requires_api_version('1.25') + @pytest.mark.xfail(reason='Bad test') def test_build_cachefrom(self): script = io.BytesIO('\n'.join([ 'FROM scratch', + 'CMD sh -c "echo \'Hello, World!\'"', ]).encode('ascii')) cachefrom = ['build1'] @@ -169,6 +172,7 @@ class BuildTest(BaseAPIIntegrationTest): pass info = self.client.inspect_image('cachefrom') + # FIXME: Config.CacheFrom is not a real thing self.assertEqual(info['Config']['CacheFrom'], cachefrom) def test_build_stderr_data(self): diff --git a/tests/integration/api_container_test.py b/tests/integration/api_container_test.py index fc748f1c..3cede45d 100644 --- a/tests/integration/api_container_test.py +++ b/tests/integration/api_container_test.py @@ -413,6 +413,15 @@ class CreateContainerTest(BaseAPIIntegrationTest): config = self.client.inspect_container(container) assert config['HostConfig']['AutoRemove'] is True + @requires_api_version('1.25') + def test_create_with_stop_timeout(self): + container = self.client.create_container( + BUSYBOX, ['echo', 'test'], stop_timeout=25 + ) + self.tmp_containers.append(container['Id']) + config = self.client.inspect_container(container) + assert config['Config']['StopTimeout'] == 25 + class VolumeBindTest(BaseAPIIntegrationTest): def setUp(self):