From 8561544e30d88e44930fcd7505529d26f3023efd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gra=C3=B1a?= Date: Wed, 13 Nov 2013 15:34:19 -0200 Subject: [PATCH] Add signal argument to kill command --- README.md | 2 +- docker/client.py | 7 +++++-- tests/integration_test.py | 19 +++++++++++++++++++ tests/test.py | 19 +++++++++++++++++-- 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 024012a0..0e1f9129 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Identical to the `docker inspect` command, but only for containers. * `c.inspect_image(image_id)` Identical to the `docker inspect` command, but only for images. -* `c.kill(container)` +* `c.kill(container, signal=None)` Kill a container. Similar to the `docker kill` command. * `c.login(username, password=None, email=None)` diff --git a/docker/client.py b/docker/client.py index 7c838ee5..af7e5aec 100644 --- a/docker/client.py +++ b/docker/client.py @@ -408,11 +408,14 @@ class Client(requests.Session): self._url("/images/{0}/json".format(image_id)) ), True) - def kill(self, container): + def kill(self, container, signal=None): if isinstance(container, dict): container = container.get('Id') url = self._url("/containers/{0}/kill".format(container)) - res = self.post(url, None) + params = {} + if signal is not None: + params['signal'] = signal + res = self.post(url, None, params=params) self._raise_for_status(res) def login(self, username, password=None, email=None, registry=None): diff --git a/tests/integration_test.py b/tests/integration_test.py index 4db21808..3d9efbee 100644 --- a/tests/integration_test.py +++ b/tests/integration_test.py @@ -15,6 +15,7 @@ import base64 import io import os +import signal import tempfile import unittest @@ -369,6 +370,24 @@ class TestKillWithDictInsteadOfId(BaseTestCase): self.assertEqual(state['Running'], False) +class TestKillWithSignal(BaseTestCase): + def runTest(self): + container = self.client.create_container('busybox', ['sleep', '60']) + id = container['Id'] + self.client.start(id) + self.tmp_containers.append(id) + self.client.kill(id, signal=signal.SIGTERM) + exitcode = self.client.wait(id) + self.assertNotEqual(exitcode, 0) + container_info = self.client.inspect_container(id) + self.assertIn('State', container_info) + state = container_info['State'] + self.assertIn('ExitCode', state) + self.assertNotEqual(state['ExitCode'], 0) + self.assertIn('Running', state) + self.assertEqual(state['Running'], False, state) + + class TestRestart(BaseTestCase): def runTest(self): container = self.client.create_container('busybox', ['sleep', '9999']) diff --git a/tests/test.py b/tests/test.py index 3a62f7f2..79fa50ed 100644 --- a/tests/test.py +++ b/tests/test.py @@ -17,6 +17,7 @@ import datetime import io import json import os +import signal import tempfile import unittest @@ -399,7 +400,8 @@ class DockerClientTest(unittest.TestCase): fake_request.assert_called_with( 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/kill', - None + None, + params={} ) def test_kill_container_with_dict_instead_of_id(self): @@ -410,7 +412,20 @@ class DockerClientTest(unittest.TestCase): fake_request.assert_called_with( 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/kill', - None + None, + params={} + ) + + def test_kill_container_with_signal(self): + try: + self.client.kill(fake_api.FAKE_CONTAINER_ID, signal=signal.SIGTERM) + except Exception as e: + self.fail('Command should not raise exception: {0}'.format(e)) + + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/kill', + None, + params={'signal': signal.SIGTERM} ) def test_restart_container(self):