mirror of https://github.com/docker/docker-py.git
Add signal argument to kill command
This commit is contained in:
parent
14946ed56b
commit
8561544e30
|
@ -71,7 +71,7 @@ Identical to the `docker inspect` command, but only for containers.
|
||||||
* `c.inspect_image(image_id)`
|
* `c.inspect_image(image_id)`
|
||||||
Identical to the `docker inspect` command, but only for images.
|
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.
|
Kill a container. Similar to the `docker kill` command.
|
||||||
|
|
||||||
* `c.login(username, password=None, email=None)`
|
* `c.login(username, password=None, email=None)`
|
||||||
|
|
|
@ -408,11 +408,14 @@ class Client(requests.Session):
|
||||||
self._url("/images/{0}/json".format(image_id))
|
self._url("/images/{0}/json".format(image_id))
|
||||||
), True)
|
), True)
|
||||||
|
|
||||||
def kill(self, container):
|
def kill(self, container, signal=None):
|
||||||
if isinstance(container, dict):
|
if isinstance(container, dict):
|
||||||
container = container.get('Id')
|
container = container.get('Id')
|
||||||
url = self._url("/containers/{0}/kill".format(container))
|
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)
|
self._raise_for_status(res)
|
||||||
|
|
||||||
def login(self, username, password=None, email=None, registry=None):
|
def login(self, username, password=None, email=None, registry=None):
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
import base64
|
import base64
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
|
import signal
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
@ -369,6 +370,24 @@ class TestKillWithDictInsteadOfId(BaseTestCase):
|
||||||
self.assertEqual(state['Running'], False)
|
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):
|
class TestRestart(BaseTestCase):
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
container = self.client.create_container('busybox', ['sleep', '9999'])
|
container = self.client.create_container('busybox', ['sleep', '9999'])
|
||||||
|
|
|
@ -17,6 +17,7 @@ import datetime
|
||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import signal
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
@ -399,7 +400,8 @@ class DockerClientTest(unittest.TestCase):
|
||||||
|
|
||||||
fake_request.assert_called_with(
|
fake_request.assert_called_with(
|
||||||
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/kill',
|
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/kill',
|
||||||
None
|
None,
|
||||||
|
params={}
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_kill_container_with_dict_instead_of_id(self):
|
def test_kill_container_with_dict_instead_of_id(self):
|
||||||
|
@ -410,7 +412,20 @@ class DockerClientTest(unittest.TestCase):
|
||||||
|
|
||||||
fake_request.assert_called_with(
|
fake_request.assert_called_with(
|
||||||
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/kill',
|
'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):
|
def test_restart_container(self):
|
||||||
|
|
Loading…
Reference in New Issue