Repository: XD-embedded/xd-docker

Issue:
client.commit()
Client API command to create an image from a container.

Discussion:

Patch:
diff --git a/CHANGELOG b/CHANGELOG
index 2d7da75..238dbfc 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,10 @@
-0.2.0 (2016-08-28)
+0.3.0 (in development)
 ----------------------
+* Add container_upload() method.
+* Add commit() method.
+
+0.2.0 (2016-08-28)
+------------------
 * Add container_remove() method.
 * Add container_start() method.
 * Add container_wait() method.
diff --git a/xd/docker/client.py b/xd/docker/client.py
index 879dea2..f16bc78 100644
--- a/xd/docker/client.py
+++ b/xd/docker/client.py
@@ -643,3 +643,40 @@ class DockerClient(object):
                 raise PermissionDenied(
                     "Volume or container rootfs is marked as read-only") \
                     from exc
+
+    def commit(self,
+               container: Union[Container, ContainerName, str],
+               repo: Optional[Union[Repository, str]]=None,
+               comment: Optional[str]=None,
+               author: Optional[str]=None,
+               pause: Optional[bool]=None):
+
+        # 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
+        if isinstance(repo, str):
+            repo = Repository(repo)
+
+        params = {'container': id_or_name}
+        if repo:
+            params['repo'] = repo.name
+            if repo.tag is not None:
+                params['tag'] = repo.tag
+        if comment is not None:
+            params['comment'] = comment
+        if author is not None:
+            params['author'] = author
+        if pause is not None:
+            params['pause'] = pause
+
+        # TODO: add support for 'config' JSON parameter
+        # The ContainerConfig class should be changed to allow image to be
+        # optional, so we can simply pass a instance of that
+
+        # TODO: add support for 'changes' query parameter
+        r = self._post('/commit', params=params)
+        return Image(self, id=r.json()['Id'])


Test patch:
diff --git a/tests/integration/commit_test.py b/tests/integration/commit_test.py
new file mode 100644
index 0000000..b2009b1
--- /dev/null
+++ b/tests/integration/commit_test.py
@@ -0,0 +1,57 @@
+import pytest
+import os
+import re
+import json
+
+
+from xd.docker.client import *
+
+
+def test_basic(docker, stdout):
+    os.system("docker create --name xd-docker-test busybox:latest")
+    foo = docker.commit('xd-docker-test')
+    assert foo.id
+    assert os.system('docker inspect {}'.format(foo.id) +
+                     '|grep \'"Comment": "foobar"\'') != 0
+    assert os.system('docker inspect {}'.format(foo.id) +
+                     '|grep \'"Author": "foobar"\'') != 0
+
+
+def test_with_repo(docker, stdout):
+    os.system("docker create --name xd-docker-test busybox:latest")
+    foo = docker.commit('xd-docker-test', repo='foo')
+    assert foo.id
+
+
+def test_with_tag(docker, stdout):
+    os.system("docker create --name xd-docker-test busybox:latest")
+    foo = docker.commit('xd-docker-test', repo='foo:bar')
+    assert foo.id
+
+
+def test_comment(docker, stdout):
+    os.system("docker create --name xd-docker-test busybox:latest")
+    foo = docker.commit('xd-docker-test', comment='foobar')
+    assert foo.id
+    assert os.system('docker inspect {}'.format(foo.id) +
+                     '|grep \'"Comment": "foobar"\'') == 0
+
+
+def test_author(docker, stdout):
+    os.system("docker create --name xd-docker-test busybox:latest")
+    foo = docker.commit('xd-docker-test', author='foobar')
+    assert foo.id
+    assert os.system('docker inspect {}'.format(foo.id) +
+                     '|grep \'"Author": "foobar"\'') == 0
+
+
+def test_pause_true(docker, stdout):
+    os.system("docker create --name xd-docker-test busybox:latest")
+    foo = docker.commit('xd-docker-test', pause=True)
+    assert foo.id
+
+
+def test_pause_false(docker, stdout):
+    os.system("docker create --name xd-docker-test busybox:latest")
+    foo = docker.commit('xd-docker-test', pause=False)
+    assert foo.id
diff --git a/tests/unit/client_test.py b/tests/unit/client_test.py
index 4e7fbb6..aef1579 100644
--- a/tests/unit/client_test.py
+++ b/tests/unit/client_test.py
@@ -1450,3 +1450,86 @@ class container_upload_tests(ContextClientTestCase):
             return_value=requests_mock.version_response("1.19", "1.7.1"))
         with pytest.raises(IncompatibleRemoteAPI):
             self.client.container_upload('foo', self.tar_file, 'bar')
+
+
+class commit_tests(ContextClientTestCase):
+
+    @mock.patch('requests.post')
+    def test_str(self, post_mock):
+        post_mock.return_value = requests_mock.Response(
+            '{"Id": "596069db4bf5"}', 201)
+        self.client.commit('foo')
+
+    @mock.patch('requests.post')
+    def test_containername(self, post_mock):
+        post_mock.return_value = requests_mock.Response(
+            '{"Id": "596069db4bf5"}', 201)
+        self.client.commit(ContainerName('foo'))
+
+    @mock.patch('requests.post')
+    def test_container(self, post_mock):
+        post_mock.return_value = requests_mock.Response(
+            '{"Id": "596069db4bf5"}', 201)
+        self.client.commit(Container('foo'))
+
+    @mock.patch('requests.post')
+    def test_repo_str(self, post_mock):
+        post_mock.return_value = requests_mock.Response(
+            '{"Id": "596069db4bf5"}', 201)
+        self.client.commit('foobar', repo='foo')
+        assert 'params' in post_mock.call_args[1]
+        params = post_mock.call_args[1]['params']
+        assert 'repo' in params
+        assert params['repo'] == 'foo'
+
+    @mock.patch('requests.post')
+    def test_repotag_str(self, post_mock):
+        post_mock.return_value = requests_mock.Response(
+            '{"Id": "596069db4bf5"}', 201)
+        self.client.commit('foobar', repo='foo:bar')
+        assert 'params' in post_mock.call_args[1]
+        params = post_mock.call_args[1]['params']
+        assert 'repo' in params
+        assert params['repo'] == 'foo'
+        assert 'tag' in params
+        assert params['tag'] == 'bar'
+
+    @mock.patch('requests.post')
+    def test_comment(self, post_mock):
+        post_mock.return_value = requests_mock.Response(
+            '{"Id": "596069db4bf5"}', 201)
+        self.client.commit('foobar', comment='foo')
+        assert 'params' in post_mock.call_args[1]
+        params = post_mock.call_args[1]['params']
+        assert 'comment' in params
+        assert params['comment'] == 'foo'
+
+    @mock.patch('requests.post')
+    def test_author(self, post_mock):
+        post_mock.return_value = requests_mock.Response(
+            '{"Id": "596069db4bf5"}', 201)
+        self.client.commit('foobar', author='foo')
+        assert 'params' in post_mock.call_args[1]
+        params = post_mock.call_args[1]['params']
+        assert 'author' in params
+        assert params['author'] == 'foo'
+
+    @mock.patch('requests.post')
+    def test_pause_true(self, post_mock):
+        post_mock.return_value = requests_mock.Response(
+            '{"Id": "596069db4bf5"}', 201)
+        self.client.commit('foo', pause=True)
+        assert 'params' in post_mock.call_args[1]
+        params = post_mock.call_args[1]['params']
+        assert 'pause' in params
+        assert params['pause'] == True
+
+    @mock.patch('requests.post')
+    def test_pause_false(self, post_mock):
+        post_mock.return_value = requests_mock.Response(
+            '{"Id": "596069db4bf5"}', 201)
+        self.client.commit('foo', pause=False)
+        assert 'params' in post_mock.call_args[1]
+        params = post_mock.call_args[1]['params']
+        assert 'pause' in params
+        assert params['pause'] == False