Repository: XD-embedded/xd-docker

Issue:
client.container_restart()
Client API command to restart a container.

Discussion:
coveralls: 
[![Coverage Status](https://coveralls.io/builds/7638771/badge)](https://coveralls.io/builds/7638771)

Coverage remained the same at 100.0% when pulling **b5f87cb77d21bf715abb3a74102224baeffccd53 on esben:container-restart** into **1f7353342a8eda120d4fddb87a14e63f5da799b3 on XD-embedded:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/7638771/badge)](https://coveralls.io/builds/7638771)

Coverage remained the same at 100.0% when pulling **b5f87cb77d21bf715abb3a74102224baeffccd53 on esben:container-restart** into **1f7353342a8eda120d4fddb87a14e63f5da799b3 on XD-embedded:master**.


Patch:
diff --git a/xd/docker/client.py b/xd/docker/client.py
index 382c1ec..a0c7398 100644
--- a/xd/docker/client.py
+++ b/xd/docker/client.py
@@ -519,6 +519,10 @@ class DockerClient(object):
         Arguments:
           container: The container to remove (id or name).
           timeout: Number of seconds to wait before killing the container.
+
+        Returns:
+          True if container was stopped.
+          False if container was already stopped.
         """
 
         # Handle convenience argument types
@@ -540,3 +544,31 @@ class DockerClient(object):
                 return False
             raise e
         return True
+
+    def container_restart(self,
+                          container: Union[Container, ContainerName, str],
+                          timeout: Optional[int]=None):
+        """Restart container.
+
+        Restart the container, and optionally killing the container after a
+        timeout waiting for the container to stop.
+
+        Arguments:
+          container: The container to remove (id or name).
+          timeout: Number of seconds to wait before killing the container.
+        """
+
+        # Handle convenience argument types
+        if isinstance(container, str):
+            id_or_name = container
+        elif isinstance(container, ContainerName):
+            id_or_name = container.name
+        else:
+            id_or_name = container.id or container.name
+
+        params = {}
+        if timeout is not None:
+            params['t'] = timeout
+
+        self._post('/containers/{}/restart'.format(id_or_name),
+                   params=params)


Test patch:
diff --git a/tests/integration/container_restart_test.py b/tests/integration/container_restart_test.py
new file mode 100644
index 0000000..53d998b
--- /dev/null
+++ b/tests/integration/container_restart_test.py
@@ -0,0 +1,26 @@
+import pytest
+import os
+
+from xd.docker.client import *
+
+
+def test_restart(docker, stdout):
+    os.system("docker run -d --name xd-docker-test busybox:latest sleep 5")
+    docker.container_restart('xd-docker-test')
+
+
+def test_already_stopped(docker, stdout):
+    os.system("docker run --name xd-docker-test busybox:latest true")
+    docker.container_restart('xd-docker-test')
+
+
+def test_not_started(docker, stdout):
+    os.system("docker create --name xd-docker-test busybox:latest true")
+    docker.container_restart('xd-docker-test')
+
+
+def test_no_such_container(docker, stdout):
+    with pytest.raises(ClientError) as clienterror:
+        with stdout.redirect():
+            docker.container_stop('xd-docker-test')
+    assert clienterror.value.code == 404
diff --git a/tests/integration/container_stop_test.py b/tests/integration/container_stop_test.py
index e11e8f3..3aaa43b 100644
--- a/tests/integration/container_stop_test.py
+++ b/tests/integration/container_stop_test.py
@@ -1,6 +1,5 @@
 import pytest
 import os
-import time
 
 from xd.docker.client import *
 
diff --git a/tests/unit/client_test.py b/tests/unit/client_test.py
index 6bf24f2..e890e2d 100644
--- a/tests/unit/client_test.py
+++ b/tests/unit/client_test.py
@@ -1320,3 +1320,37 @@ class container_stop_tests(ContextClientTestCase):
         params = post_mock.call_args[1]['params']
         assert 't' in params
         assert params['t'] == 42
+
+
+class container_restart_tests(ContextClientTestCase):
+
+    @mock.patch('requests.post')
+    def test_normal(self, post_mock):
+        post_mock.return_value = requests_mock.Response(None, 204)
+        self.client.container_restart("foobar")
+
+    @mock.patch('requests.post')
+    def test_no_such_container(self, post_mock):
+        post_mock.return_value = requests_mock.Response(None, 404)
+        with pytest.raises(ClientError) as clienterror:
+            self.client.container_restart('foobar')
+        assert clienterror.value.code == 404
+
+    @mock.patch('requests.post')
+    def test_containername(self, post_mock):
+        post_mock.return_value = requests_mock.Response(None, 204)
+        self.client.container_restart(ContainerName("foobar"))
+
+    @mock.patch('requests.post')
+    def test_container(self, post_mock):
+        post_mock.return_value = requests_mock.Response(None, 204)
+        self.client.container_restart(Container(self.client,name="foobar"))
+
+    @mock.patch('requests.post')
+    def test_timeout(self, post_mock):
+        post_mock.return_value = requests_mock.Response(None, 204)
+        self.client.container_restart("foobar", timeout=42)
+        assert 'params' in post_mock.call_args[1]
+        params = post_mock.call_args[1]['params']
+        assert 't' in params
+        assert params['t'] == 42