Repository: XD-embedded/xd-docker

Issue:
client.container_wait()
Client API command to wait for a container to stop.

Discussion:

Patch:
diff --git a/xd/docker/client.py b/xd/docker/client.py
index 3d2fe2e..ec42404 100644
--- a/xd/docker/client.py
+++ b/xd/docker/client.py
@@ -484,3 +484,27 @@ class DockerClient(object):
                 return False
             raise e
         return True
+
+    def container_wait(self,
+                       container: Union[Container, ContainerName, str]) -> int:
+        """Block until container stops.
+
+        Block until container stops, then returns the exit code.
+
+        Arguments:
+          container: The container to remove (id or name).
+
+        Returns:
+          Container exit code.
+        """
+
+        # 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
+
+        r = self._post('/containers/{}/wait'.format(id_or_name))
+        return r.json()['StatusCode']


Test patch:
diff --git a/tests/unit/client_test.py b/tests/unit/client_test.py
index 5b2eccd..0f1f130 100644
--- a/tests/unit/client_test.py
+++ b/tests/unit/client_test.py
@@ -1245,3 +1245,41 @@ class container_start_tests(ContextClientTestCase):
         with pytest.raises(ClientError) as clienterror:
             self.client.container_start('foobar')
         assert clienterror.value.code == 404
+
+
+class container_wait_tests(ContextClientTestCase):
+
+    @mock.patch('requests.post')
+    def test_0(self, post_mock):
+        post_mock.return_value = requests_mock.Response(json.dumps(
+            {'StatusCode': 0}), 200)
+        assert self.client.container_wait("foobar") == 0
+
+    @mock.patch('requests.post')
+    def test_42(self, post_mock):
+        post_mock.return_value = requests_mock.Response(json.dumps(
+            {'StatusCode': 42}), 200)
+        assert self.client.container_wait("foobar") == 42
+
+    @mock.patch('requests.post')
+    def test_no_such_container(self, post_mock):
+        post_mock.return_value = requests_mock.Response(json.dumps(
+            {'StatusCode': 42}), 200)
+        post_mock.return_value = requests_mock.Response(
+            "No such container", 404)
+        with pytest.raises(ClientError) as clienterror:
+            self.client.container_wait('foobar')
+        assert clienterror.value.code == 404
+
+    @mock.patch('requests.post')
+    def test_containername(self, post_mock):
+        post_mock.return_value = requests_mock.Response(json.dumps(
+            {'StatusCode': 0}), 200)
+        assert self.client.container_wait(ContainerName("foobar")) == 0
+
+    @mock.patch('requests.post')
+    def test_container(self, post_mock):
+        post_mock.return_value = requests_mock.Response(json.dumps(
+            {'StatusCode': 0}), 200)
+        assert self.client.container_wait(Container(
+            self.client,name="foobar")) == 0