From 8047fb4cb40c6c2da0b8c01b1e98fa5f947b31ba Mon Sep 17 00:00:00 2001 From: Deni Bertovic Date: Wed, 25 Sep 2013 13:55:17 +0200 Subject: [PATCH 1/9] reamed test.py to integration_test.py --- tests/{test.py => integration_test.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{test.py => integration_test.py} (100%) diff --git a/tests/test.py b/tests/integration_test.py similarity index 100% rename from tests/test.py rename to tests/integration_test.py From 80e11964f18b8684c8f2ccab1ffbe8b6ad634492 Mon Sep 17 00:00:00 2001 From: Deni Bertovic Date: Wed, 25 Sep 2013 23:50:46 +0200 Subject: [PATCH 2/9] initial import of unit test and mocks for the docker api --- tests/fake_api.py | 231 ++++++++++++++++++++++++++++ tests/test.py | 377 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 608 insertions(+) create mode 100644 tests/fake_api.py create mode 100644 tests/test.py diff --git a/tests/fake_api.py b/tests/fake_api.py new file mode 100644 index 00000000..b9956a7c --- /dev/null +++ b/tests/fake_api.py @@ -0,0 +1,231 @@ +import json + +CURRENT_VERSION = 'v1.4' + +FAKE_CONTAINER_ID = '3cc2351ab11b' +FAKE_IMAGE_ID = 'e9aa60c60128' + +FAKE_INSPECT_DATA = { + "ID": "3cc2351ab11bca2e319f49b547834f550eb0c0505a636dc4d24c5b5e03845927", + "Created": "2013-09-25T14:01:18.867354259+02:00", + "Path": "/bin/sh", + "Args": [ + "-c", + "mkdir -p /tmp/test" + ], + "Config": { + "Hostname": FAKE_CONTAINER_ID, + "Domainname": "", + "User": "", + "Memory": 0, + "MemorySwap": 0, + "CpuShares": 0, + "AttachStdin": False, + "AttachStdout": False, + "AttachStderr": False, + "PortSpecs": [ + "8080" + ], + "Tty": False, + "OpenStdin": False, + "StdinOnce": False, + "Env": [ + "HOME=/", + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + ], + "Cmd": None, + "Dns": None, + "Image": "b9517f6e2d833745eb5f893ea901abe5ea8247fab8e569e1d3280881ab84284b", + "Volumes": None, + "VolumesFrom": "", + "WorkingDir": "", + "Entrypoint": None, + "NetworkDisabled": False, + "Privileged": False + }, + "State": { + "Running": False, + "Pid": 0, + "ExitCode": 0, + "StartedAt": "2013-09-25T14:01:18.869545111+02:00", + "Ghost": False + }, + "Image": "9330a90e5753f16df757d865700365263946bd7e6b6439e44622e5952bb5033e", + "NetworkSettings": { + "IPAddress": "", + "IPPrefixLen": 0, + "Gateway": "", + "Bridge": "", + "PortMapping": None + }, + "SysInitPath": "/opt/docker/docker", + "ResolvConfPath": "/etc/resolv.conf", + "HostnamePath": "/var/lib/docker/containers/800680f2e4ccca2e319f49b547834f550eb0c0505a636dc4d24c5b5e03845927/hostname", + "HostsPath": "/var/lib/docker/containers/800680f2e4ccca2e319f49b547834f550eb0c0505a636dc4d24c5b5e03845927/hosts", + "Volumes": {}, + "VolumesRW": {} +} + + +### Each method is prefixed with HTTP method (get, post...) +### for clarity and readability + + +def get_fake_version(): + status_code = 200 + response = json.dumps({'GoVersion': '1', 'Version': '1.1.1'}) + return status_code, response + + +def get_fake_info(): + status_code = 200 + response = json.dumps({'Containers': 1, 'Images': 1, 'Debug': ''}) + return status_code, response + + +def get_fake_search(): + status_code = 200 + response = json.dumps([{'Name': 'busybox', 'Description': 'Fake Description'}]) + return status_code, response + + +def get_fake_images(): + status_code = 200 + response = json.dumps([ + {'Id': FAKE_IMAGE_ID, 'Created': '2 days ago', 'Repository': 'busybox', 'Tag': 'latest'} + ]) + return status_code, response + + +def get_fake_containers(): + status_code = 200 + response = json.dumps([ + {'Id': FAKE_CONTAINER_ID, + 'Image': 'busybox:latest', + 'Created': '2 days ago', + 'Command': 'true', + 'Status': 'fake status'} + ]) + return status_code, response + + +def post_fake_start_container(): + status_code = 200 + response = json.dumps({'Id': FAKE_CONTAINER_ID}) + return status_code, response + + +def post_fake_create_container(): + status_code = 200 + response = json.dumps({'Id': FAKE_CONTAINER_ID}) + return status_code, response + + +def get_fake_inspect_container(): + status_code = 200 + response = json.dumps({ + 'Id': FAKE_CONTAINER_ID, + 'Config': {'Privileged': True}, + 'ID': FAKE_CONTAINER_ID, + 'Image': 'busybox:latest', + "State": { + "Running": True, + "Pid": 0, + "ExitCode": 0, + "StartedAt": "2013-09-25T14:01:18.869545111+02:00", + "Ghost": False + }, + + + }) + return status_code, response + + +def get_fake_wait(): + status_code = 200 + response = json.dumps({'StatusCode': 0}) + return status_code, response + + +def get_fake_logs(): + status_code = 200 + response = 'Flowering Nights (Sakuya Iyazoi)' + return status_code, response + + +def get_fake_diff(): + status_code = 200 + response = json.dumps([{'Path': '/test', 'Kind': 1}]) + return status_code, response + + +def post_fake_stop_container(): + status_code = 200 + response = json.dumps({'Id': FAKE_CONTAINER_ID}) + return status_code, response + + +def post_fake_kill_container(): + status_code = 200 + response = json.dumps({'Id': FAKE_CONTAINER_ID}) + return status_code, response + + +def post_fake_restart_container(): + status_code = 200 + response = json.dumps({'Id': FAKE_CONTAINER_ID}) + return status_code, response + + +def delete_fake_remove_container(): + status_code = 200 + response = json.dumps({'Id': FAKE_CONTAINER_ID}) + return status_code, response + + +def post_fake_image_create(): + status_code = 200 + response = json.dumps({'Id': FAKE_IMAGE_ID}) + return status_code, response + + +def delete_fake_remove_image(): + status_code = 200 + response = json.dumps({'Id': FAKE_IMAGE_ID}) + return status_code, response + + +def post_fake_commit(): + status_code = 200 + response = json.dumps({'Id': FAKE_CONTAINER_ID}) + return status_code, response + + +def post_fake_build_container(): + status_code = 200 + response = json.dumps({'Id': FAKE_CONTAINER_ID}) + return status_code, response + + +## maps real api url to fake response callback +fake_responses = { + 'unix://var/run/docker.sock/{0}/version'.format(CURRENT_VERSION): get_fake_version, + 'unix://var/run/docker.sock/{0}/info'.format(CURRENT_VERSION): get_fake_info, + 'unix://var/run/docker.sock/{0}/images/search'.format(CURRENT_VERSION): get_fake_search, + 'unix://var/run/docker.sock/{0}/images/json'.format(CURRENT_VERSION): get_fake_images, + 'unix://var/run/docker.sock/{0}/containers/ps'.format(CURRENT_VERSION): get_fake_containers, + 'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/start'.format(CURRENT_VERSION): post_fake_start_container, + 'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/json'.format(CURRENT_VERSION): get_fake_inspect_container, + 'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/wait'.format(CURRENT_VERSION): get_fake_wait, + 'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/attach'.format(CURRENT_VERSION): get_fake_logs, + 'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/changes'.format(CURRENT_VERSION): get_fake_diff, + 'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/stop'.format(CURRENT_VERSION): post_fake_stop_container, + 'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/kill'.format(CURRENT_VERSION): post_fake_kill_container, + 'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/restart'.format(CURRENT_VERSION): post_fake_restart_container, + 'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b'.format(CURRENT_VERSION): delete_fake_remove_container, + 'unix://var/run/docker.sock/{0}/images/create'.format(CURRENT_VERSION): post_fake_image_create, + 'unix://var/run/docker.sock/{0}/images/e9aa60c60128'.format(CURRENT_VERSION): delete_fake_remove_image, + 'unix://var/run/docker.sock/{0}/commit'.format(CURRENT_VERSION): post_fake_commit, + 'unix://var/run/docker.sock/{0}/containers/create'.format(CURRENT_VERSION): post_fake_create_container, + 'unix://var/run/docker.sock/{0}/build'.format(CURRENT_VERSION): post_fake_build_container +} diff --git a/tests/test.py b/tests/test.py new file mode 100644 index 00000000..cf4bdb28 --- /dev/null +++ b/tests/test.py @@ -0,0 +1,377 @@ +# Copyright 2013 dotCloud inc. + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import base64 +import os +from StringIO import StringIO +import tempfile +import unittest + +import docker +import six + + +import requests +from requests import structures +import datetime +import json +from fake_api import fake_responses, FAKE_CONTAINER_ID, FAKE_IMAGE_ID + +# FIXME: missing tests for +# export; history; import_image; insert; port; push; tag + + +def response(status_code=200, content='', headers=None, reason=None, elapsed=0, + request=None): + res = requests.Response() + res.status_code = status_code + if isinstance(content, dict): + content = json.dumps(content) + res._content = content + res.headers = structures.CaseInsensitiveDict(headers or {}) + res.reason = reason + res.elapsed = datetime.timedelta(elapsed) + return res + + +def fake_get(self, url, **kwargs): + status_code, content = fake_responses[url]() + return response(status_code=status_code, content=content) + + +def fake_post(self, url, data=None, **kwargs): + status_code, content = fake_responses[url]() + return response(status_code=status_code, content=content) + + +def fake_put(self, url, data=None, **kwargs): + status_code, content = fake_responses[url]() + return response(status_code=status_code, content=content) + + +def fake_delete(self, url, data=None, **kwargs): + status_code, content = fake_responses[url]() + return response(status_code=status_code, content=content) + + +docker.Client.get = fake_get +docker.Client.post = fake_post +docker.Client.put = fake_put +docker.Client.delete = fake_delete + + +class BaseTestCase(unittest.TestCase): + + def setUp(self): + self.client = docker.Client() + + +######################### +## INFORMATION TESTS ## +######################### + + +class TestVersion(BaseTestCase): + def runTest(self): + try: + self.client.version() + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestInfo(BaseTestCase): + def runTest(self): + try: + self.client.info() + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestSearch(BaseTestCase): + def runTest(self): + try: + self.client.search('busybox') + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +# ################### +# ## LISTING TESTS ## +# ################### + + +class TestImages(BaseTestCase): + def runTest(self): + try: + self.client.images(all=True) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestImageIds(BaseTestCase): + def runTest(self): + try: + self.client.images(quiet=True) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestListContainers(BaseTestCase): + def runTest(self): + try: + self.client.containers(all=True) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +##################### +## CONTAINER TESTS ## +##################### + + +class TestCreateContainer(BaseTestCase): + def runTest(self): + try: + self.client.create_container('busybox', 'true') + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestCreateContainerWithBinds(BaseTestCase): + def runTest(self): + mount_dest = '/mnt' + mount_origin = '/tmp' + + try: + self.client.create_container('busybox', + ['ls', mount_dest], volumes={mount_dest: {}}) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestCreateContainerPrivileged(BaseTestCase): + def runTest(self): + try: + self.client.create_container('busybox', 'true', privileged=True) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestStartContainer(BaseTestCase): + def runTest(self): + try: + self.client.start(FAKE_CONTAINER_ID) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestStartContainerWithBinds(BaseTestCase): + def runTest(self): + try: + mount_dest = '/mnt' + mount_origin = '/tmp' + self.client.start(FAKE_CONTAINER_ID, binds={mount_origin: mount_dest}) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestStartContainerWithDictInsteadOfId(BaseTestCase): + def runTest(self): + try: + self.client.start({'Id': FAKE_CONTAINER_ID}) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestWait(BaseTestCase): + def runTest(self): + try: + self.client.wait(FAKE_CONTAINER_ID) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestWaitWithDictInsteadOfId(BaseTestCase): + def runTest(self): + try: + self.client.wait({'Id': FAKE_CONTAINER_ID}) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestLogs(BaseTestCase): + def runTest(self): + try: + self.client.logs(FAKE_CONTAINER_ID) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestLogsWithDictInsteadOfId(BaseTestCase): + def runTest(self): + try: + self.client.logs({'Id': FAKE_CONTAINER_ID}) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestDiff(BaseTestCase): + def runTest(self): + try: + self.client.diff(FAKE_CONTAINER_ID) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestDiffWithDictInsteadOfId(BaseTestCase): + def runTest(self): + try: + self.client.diff({'Id': FAKE_CONTAINER_ID}) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestStop(BaseTestCase): + def runTest(self): + try: + self.client.stop(FAKE_CONTAINER_ID, timeout=2) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestStopWithDictInsteadOfId(BaseTestCase): + def runTest(self): + try: + self.client.stop({'Id': FAKE_CONTAINER_ID}, timeout=2) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestKill(BaseTestCase): + def runTest(self): + try: + self.client.kill(FAKE_CONTAINER_ID) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestKillWithDictInsteadOfId(BaseTestCase): + def runTest(self): + try: + self.client.kill({'Id': FAKE_CONTAINER_ID}) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestRestart(BaseTestCase): + def runTest(self): + try: + self.client.restart(FAKE_CONTAINER_ID, timeout=2) + except Exception as e: + self.fail('Command should not raise exception : ' + str(e)) + + +class TestRestartWithDictInsteadOfId(BaseTestCase): + def runTest(self): + try: + self.client.restart({'Id': FAKE_CONTAINER_ID}, timeout=2) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestRemoveContainer(BaseTestCase): + def runTest(self): + try: + self.client.remove_container(FAKE_CONTAINER_ID) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestRemoveContainerWithDictInsteadOfId(BaseTestCase): + def runTest(self): + try: + self.client.remove_container({'Id': FAKE_CONTAINER_ID}) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +# ################## +# ## IMAGES TESTS ## +# ################## + +class TestPull(BaseTestCase): + def runTest(self): + try: + self.client.pull('joffrey/test001') + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestCommit(BaseTestCase): + def runTest(self): + try: + self.client.commit(FAKE_CONTAINER_ID) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +class TestRemoveImage(BaseTestCase): + def runTest(self): + try: + self.client.remove_image(FAKE_IMAGE_ID) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +# ################# +# # BUILDER TESTS # +# ################# + +class TestBuild(BaseTestCase): + def runTest(self): + script = StringIO('\n'.join([ + 'FROM busybox', + 'MAINTAINER docker-py', + 'RUN mkdir -p /tmp/test', + 'EXPOSE 8080', + 'ADD https://dl.dropboxusercontent.com/u/20637798/silence.tar.gz /tmp/silence.tar.gz' + ])) + try: + self.client.build(fileobj=script) + except Exception as e: + self.fail('Command should not raise exception: ' + str(e)) + + +# ####################### +# ## PY SPECIFIC TESTS ## +# ####################### + +class TestLoadConfig(BaseTestCase): + def runTest(self): + folder = tempfile.mkdtemp() + f = open(os.path.join(folder, '.dockercfg'), 'w') + auth_ = base64.b64encode('sakuya:izayoi') + f.write('auth = {0}\n'.format(auth_)) + f.write('email = sakuya@scarlet.net') + f.close() + cfg = docker.auth.load_config(folder) + self.assertNotEqual(cfg['Configs'][docker.auth.INDEX_URL], None) + cfg = cfg['Configs'][docker.auth.INDEX_URL] + self.assertEqual(cfg['Username'], 'sakuya') + self.assertEqual(cfg['Password'], 'izayoi') + self.assertEqual(cfg['Email'], 'sakuya@scarlet.net') + self.assertEqual(cfg.get('Auth'), None) + +if __name__ == '__main__': + unittest.main() From 320fcdeed9db4f5f3bbd9d30344920eb9f6a8798 Mon Sep 17 00:00:00 2001 From: Deni Bertovic Date: Fri, 27 Sep 2013 20:43:38 +0200 Subject: [PATCH 3/9] added mock to requirements --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index e5209b74..e399bbe7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ requests==1.2.0 six==1.3.0 +mock==1.0.1 From 7c80127eb7ff10da289ffc297de9eca9a74f0ad8 Mon Sep 17 00:00:00 2001 From: Deni Bertovic Date: Fri, 27 Sep 2013 20:45:02 +0200 Subject: [PATCH 4/9] refactored tests Using the mock library. Checking that every Client method gets tested for not raising an Exception and for construting the request params correctly. --- tests/test.py | 298 +++++++++++++++++++++++++++++++------------------- 1 file changed, 185 insertions(+), 113 deletions(-) diff --git a/tests/test.py b/tests/test.py index cf4bdb28..109f3efa 100644 --- a/tests/test.py +++ b/tests/test.py @@ -14,20 +14,30 @@ import base64 import os -from StringIO import StringIO import tempfile import unittest import docker import six - import requests from requests import structures import datetime import json from fake_api import fake_responses, FAKE_CONTAINER_ID, FAKE_IMAGE_ID +try: + from StringIO import StringIO +except ImportError: + from io import StringIO + + +try: + import mock +except ImportError: + from unittest import mock + + # FIXME: missing tests for # export; history; import_image; insert; port; push; tag @@ -42,114 +52,103 @@ def response(status_code=200, content='', headers=None, reason=None, elapsed=0, res.headers = structures.CaseInsensitiveDict(headers or {}) res.reason = reason res.elapsed = datetime.timedelta(elapsed) + res.request = request return res -def fake_get(self, url, **kwargs): +def fake_resp(url, data=None, **kwargs): status_code, content = fake_responses[url]() return response(status_code=status_code, content=content) - -def fake_post(self, url, data=None, **kwargs): - status_code, content = fake_responses[url]() - return response(status_code=status_code, content=content) +fake_request = mock.Mock(side_effect=fake_resp) -def fake_put(self, url, data=None, **kwargs): - status_code, content = fake_responses[url]() - return response(status_code=status_code, content=content) - - -def fake_delete(self, url, data=None, **kwargs): - status_code, content = fake_responses[url]() - return response(status_code=status_code, content=content) - - -docker.Client.get = fake_get -docker.Client.post = fake_post -docker.Client.put = fake_put -docker.Client.delete = fake_delete - - -class BaseTestCase(unittest.TestCase): - +@mock.patch.multiple('docker.Client', get=fake_request, post=fake_request, put=fake_request, delete=fake_request) +class DockerClientTest(unittest.TestCase): def setUp(self): self.client = docker.Client() - -######################### -## INFORMATION TESTS ## -######################### - - -class TestVersion(BaseTestCase): - def runTest(self): + ######################### + ## INFORMATION TESTS ## + ######################### + def test_version(self): try: self.client.version() except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/version') -class TestInfo(BaseTestCase): - def runTest(self): + def test_info(self): try: self.client.info() except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/info') -class TestSearch(BaseTestCase): - def runTest(self): + def test_search(self): try: self.client.search('busybox') except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/images/search', + params={'term': 'busybox'}) -# ################### -# ## LISTING TESTS ## -# ################### + ################### + ## LISTING TESTS ## + ################### - -class TestImages(BaseTestCase): - def runTest(self): + def test_images(self): try: self.client.images(all=True) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/images/json', + params={'filter': None, 'only_ids': 0, 'all': 1}) - -class TestImageIds(BaseTestCase): - def runTest(self): + def test_image_ids(self): try: self.client.images(quiet=True) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/images/json', + params={'filter': None, 'only_ids': 1, 'all': 0}) -class TestListContainers(BaseTestCase): - def runTest(self): + def test_list_containers(self): try: self.client.containers(all=True) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/containers/ps', + params={ + 'only_ids': 0, + 'all': 1, + 'since': None, + 'limit': -1, + 'trunc_cmd': 1, + 'before': None + } + ) -##################### -## CONTAINER TESTS ## -##################### + ##################### + ## CONTAINER TESTS ## + ##################### - -class TestCreateContainer(BaseTestCase): - def runTest(self): + def test_create_container(self): try: self.client.create_container('busybox', 'true') except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/containers/create', + '{"Tty": false, "Image": "busybox", "Cmd": ["true"], "AttachStdin": false, "Memory": 0, "AttachStderr": true, "Privileged": false, "AttachStdout": true, "OpenStdin": false}', + headers={'Content-Type': 'application/json'}) -class TestCreateContainerWithBinds(BaseTestCase): - def runTest(self): + def test_create_container_with_binds(self): mount_dest = '/mnt' mount_origin = '/tmp' @@ -159,25 +158,33 @@ class TestCreateContainerWithBinds(BaseTestCase): except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/containers/create', + '{"Tty": false, "Image": "busybox", "Cmd": ["ls", "/mnt"], "AttachStdin": false, "Volumes": {"/mnt": {}}, "Memory": 0, "AttachStderr": true, "Privileged": false, "AttachStdout": true, "OpenStdin": false}', + headers={'Content-Type': 'application/json'}) -class TestCreateContainerPrivileged(BaseTestCase): - def runTest(self): + def test_create_container_privileged(self): try: self.client.create_container('busybox', 'true', privileged=True) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/containers/create', + '{"Tty": false, "Image": "busybox", "Cmd": ["true"], "AttachStdin": false, "Memory": 0, "AttachStderr": true, "Privileged": true, "AttachStdout": true, "OpenStdin": false}', + headers={'Content-Type': 'application/json'}) -class TestStartContainer(BaseTestCase): - def runTest(self): + def test_start_container(self): try: self.client.start(FAKE_CONTAINER_ID) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/start', + '{}', + headers={'Content-Type': 'application/json'} + ) -class TestStartContainerWithBinds(BaseTestCase): - def runTest(self): + def test_start_container_with_binds(self): try: mount_dest = '/mnt' mount_origin = '/tmp' @@ -185,161 +192,227 @@ class TestStartContainerWithBinds(BaseTestCase): except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/start', + '{"Binds": ["/tmp:/mnt"]}', + headers={'Content-Type': 'application/json'} + ) -class TestStartContainerWithDictInsteadOfId(BaseTestCase): - def runTest(self): + def test_start_container_with_dict_instead_of_id(self): try: self.client.start({'Id': FAKE_CONTAINER_ID}) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/start', + '{}', headers={'Content-Type': 'application/json'} + ) - -class TestWait(BaseTestCase): - def runTest(self): + def test_wait(self): try: self.client.wait(FAKE_CONTAINER_ID) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/wait', + None, + timeout=None + ) -class TestWaitWithDictInsteadOfId(BaseTestCase): - def runTest(self): + def test_wait_with_dict_instead_of_id(self): try: self.client.wait({'Id': FAKE_CONTAINER_ID}) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/wait', + None, + timeout=None + ) -class TestLogs(BaseTestCase): - def runTest(self): + def test_logs(self): try: self.client.logs(FAKE_CONTAINER_ID) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/attach', + None, + params={'logs': 1, 'stderr': 1, 'stdout': 1} + ) -class TestLogsWithDictInsteadOfId(BaseTestCase): - def runTest(self): + def test_logs_with_dict_instead_of_id(self): try: self.client.logs({'Id': FAKE_CONTAINER_ID}) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/attach', + None, + params={'logs': 1, 'stderr': 1, 'stdout': 1} + ) -class TestDiff(BaseTestCase): - def runTest(self): + def test_diff(self): try: self.client.diff(FAKE_CONTAINER_ID) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/changes') -class TestDiffWithDictInsteadOfId(BaseTestCase): - def runTest(self): + def test_diff_with_dict_instead_of_id(self): try: self.client.diff({'Id': FAKE_CONTAINER_ID}) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/changes') -class TestStop(BaseTestCase): - def runTest(self): + def test_stop_container(self): try: self.client.stop(FAKE_CONTAINER_ID, timeout=2) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/stop', + None, + params={'t': 2} + ) -class TestStopWithDictInsteadOfId(BaseTestCase): - def runTest(self): + def test_stop_container_with_dict_instead_of_id(self): try: self.client.stop({'Id': FAKE_CONTAINER_ID}, timeout=2) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/stop', + None, + params={'t': 2} + ) -class TestKill(BaseTestCase): - def runTest(self): + def test_kill_container(self): try: self.client.kill(FAKE_CONTAINER_ID) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/kill', + None + ) -class TestKillWithDictInsteadOfId(BaseTestCase): - def runTest(self): + def test_kill_container_with_dict_instead_of_id(self): try: self.client.kill({'Id': FAKE_CONTAINER_ID}) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/kill', + None + ) -class TestRestart(BaseTestCase): - def runTest(self): + def test_restart_container(self): try: self.client.restart(FAKE_CONTAINER_ID, timeout=2) except Exception as e: self.fail('Command should not raise exception : ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/restart', + None, + params={'t': 2} + ) -class TestRestartWithDictInsteadOfId(BaseTestCase): - def runTest(self): + def test_restart_container_with_dict_instead_of_id(self): try: self.client.restart({'Id': FAKE_CONTAINER_ID}, timeout=2) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/restart', + None, + params={'t': 2} + ) -class TestRemoveContainer(BaseTestCase): - def runTest(self): + def test_remove_container(self): try: self.client.remove_container(FAKE_CONTAINER_ID) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b', + params={'v': False} + ) -class TestRemoveContainerWithDictInsteadOfId(BaseTestCase): - def runTest(self): + def test_remove_container_with_dict_instead_of_id(self): try: self.client.remove_container({'Id': FAKE_CONTAINER_ID}) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b', + params={'v': False} + ) -# ################## -# ## IMAGES TESTS ## -# ################## + ################## + ## IMAGES TESTS ## + ################## -class TestPull(BaseTestCase): - def runTest(self): + def test_pull(self): try: self.client.pull('joffrey/test001') except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/images/create', + headers={}, + params={'tag': None, 'fromImage': 'joffrey/test001'} + ) -class TestCommit(BaseTestCase): - def runTest(self): + def test_commit(self): try: self.client.commit(FAKE_CONTAINER_ID) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with( + 'unix://var/run/docker.sock/v1.4/commit', + '{}', + headers={'Content-Type': 'application/json'}, + params={ + 'repo': None, + 'comment': None, + 'tag': None, + 'container': '3cc2351ab11b', + 'author': None + } + ) -class TestRemoveImage(BaseTestCase): - def runTest(self): + def test_remove_image(self): try: self.client.remove_image(FAKE_IMAGE_ID) except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/images/e9aa60c60128') -# ################# -# # BUILDER TESTS # -# ################# + ################# + # BUILDER TESTS # + ################# -class TestBuild(BaseTestCase): - def runTest(self): + def test_build_container(self): script = StringIO('\n'.join([ 'FROM busybox', 'MAINTAINER docker-py', @@ -352,13 +425,11 @@ class TestBuild(BaseTestCase): except Exception as e: self.fail('Command should not raise exception: ' + str(e)) + ####################### + ## PY SPECIFIC TESTS ## + ####################### -# ####################### -# ## PY SPECIFIC TESTS ## -# ####################### - -class TestLoadConfig(BaseTestCase): - def runTest(self): + def test_load_config(self): folder = tempfile.mkdtemp() f = open(os.path.join(folder, '.dockercfg'), 'w') auth_ = base64.b64encode('sakuya:izayoi') @@ -373,5 +444,6 @@ class TestLoadConfig(BaseTestCase): self.assertEqual(cfg['Email'], 'sakuya@scarlet.net') self.assertEqual(cfg.get('Auth'), None) + if __name__ == '__main__': unittest.main() From 5fe16773d98b803c2653f8b9404f2eae7b24405a Mon Sep 17 00:00:00 2001 From: Deni Bertovic Date: Fri, 27 Sep 2013 20:47:23 +0200 Subject: [PATCH 5/9] fixed exception handling in _raise_for_status --- docker/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/client.py b/docker/client.py index eb6df8b0..10f44fba 100644 --- a/docker/client.py +++ b/docker/client.py @@ -75,7 +75,7 @@ class Client(requests.Session): """Raises stored :class:`APIError`, if one occurred.""" try: response.raise_for_status() - except requests.exceptions.HTTPError, e: + except requests.exceptions.HTTPError as e: raise APIError(e, response=response, explanation=explanation) def _result(self, response, json=False): From 7fbc41ea7f80a66085923ecbd6815f2712586b4f Mon Sep 17 00:00:00 2001 From: Deni Bertovic Date: Fri, 27 Sep 2013 20:50:59 +0200 Subject: [PATCH 6/9] removed unused FAKE_INSPECT_DATA --- tests/fake_api.py | 61 ----------------------------------------------- 1 file changed, 61 deletions(-) diff --git a/tests/fake_api.py b/tests/fake_api.py index b9956a7c..125d9407 100644 --- a/tests/fake_api.py +++ b/tests/fake_api.py @@ -5,67 +5,6 @@ CURRENT_VERSION = 'v1.4' FAKE_CONTAINER_ID = '3cc2351ab11b' FAKE_IMAGE_ID = 'e9aa60c60128' -FAKE_INSPECT_DATA = { - "ID": "3cc2351ab11bca2e319f49b547834f550eb0c0505a636dc4d24c5b5e03845927", - "Created": "2013-09-25T14:01:18.867354259+02:00", - "Path": "/bin/sh", - "Args": [ - "-c", - "mkdir -p /tmp/test" - ], - "Config": { - "Hostname": FAKE_CONTAINER_ID, - "Domainname": "", - "User": "", - "Memory": 0, - "MemorySwap": 0, - "CpuShares": 0, - "AttachStdin": False, - "AttachStdout": False, - "AttachStderr": False, - "PortSpecs": [ - "8080" - ], - "Tty": False, - "OpenStdin": False, - "StdinOnce": False, - "Env": [ - "HOME=/", - "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" - ], - "Cmd": None, - "Dns": None, - "Image": "b9517f6e2d833745eb5f893ea901abe5ea8247fab8e569e1d3280881ab84284b", - "Volumes": None, - "VolumesFrom": "", - "WorkingDir": "", - "Entrypoint": None, - "NetworkDisabled": False, - "Privileged": False - }, - "State": { - "Running": False, - "Pid": 0, - "ExitCode": 0, - "StartedAt": "2013-09-25T14:01:18.869545111+02:00", - "Ghost": False - }, - "Image": "9330a90e5753f16df757d865700365263946bd7e6b6439e44622e5952bb5033e", - "NetworkSettings": { - "IPAddress": "", - "IPPrefixLen": 0, - "Gateway": "", - "Bridge": "", - "PortMapping": None - }, - "SysInitPath": "/opt/docker/docker", - "ResolvConfPath": "/etc/resolv.conf", - "HostnamePath": "/var/lib/docker/containers/800680f2e4ccca2e319f49b547834f550eb0c0505a636dc4d24c5b5e03845927/hostname", - "HostsPath": "/var/lib/docker/containers/800680f2e4ccca2e319f49b547834f550eb0c0505a636dc4d24c5b5e03845927/hosts", - "Volumes": {}, - "VolumesRW": {} -} - ### Each method is prefixed with HTTP method (get, post...) ### for clarity and readability From 1ce8dc962c71251110fc465286a665723754e6cb Mon Sep 17 00:00:00 2001 From: Deni Bertovic Date: Thu, 3 Oct 2013 11:47:22 +0200 Subject: [PATCH 7/9] python3 fixes Fixed imports to be compatible with python3 on various places in the client lib and in the tests themselves. --- docker/__init__.py | 1 - docker/auth.py | 4 +-- docker/client.py | 39 ++++++++++++++------------- docker/unixconn.py | 6 ++++- tests/test.py | 66 +++++++++++++++++++++++----------------------- 5 files changed, 61 insertions(+), 55 deletions(-) diff --git a/docker/__init__.py b/docker/__init__.py index 3c6a8b47..96338bb0 100644 --- a/docker/__init__.py +++ b/docker/__init__.py @@ -13,4 +13,3 @@ # limitations under the License. from .client import Client, APIError -import auth diff --git a/docker/auth.py b/docker/auth.py index aab96035..7467aafa 100644 --- a/docker/auth.py +++ b/docker/auth.py @@ -18,7 +18,7 @@ import os import six -import utils +from .utils import ping INDEX_URL = 'https://index.docker.io/v1/' @@ -35,7 +35,7 @@ def expand_registry_url(hostname): if '/' not in hostname[9:]: hostname = hostname + '/v1/' return hostname - if utils.ping('https://' + hostname + '_ping'): + if ping('https://' + hostname + '_ping'): return 'https://' + hostname + '/v1/' return 'http://' + hostname + '/v1/' diff --git a/docker/client.py b/docker/client.py index 10f44fba..ece7581a 100644 --- a/docker/client.py +++ b/docker/client.py @@ -20,9 +20,11 @@ import requests import requests.exceptions import six -import auth -import unixconn -import utils +from .auth import (load_config, resolve_repository_name, + resolve_authconfig, encode_header) + +from .unixconn import UnixAdapter +from .utils import tar, compare_version, mkbuildcontext class APIError(requests.exceptions.HTTPError): @@ -60,11 +62,11 @@ class APIError(requests.exceptions.HTTPError): class Client(requests.Session): def __init__(self, base_url="unix://var/run/docker.sock", version="1.4"): super(Client, self).__init__() - self.mount('unix://', unixconn.UnixAdapter(base_url)) + self.mount('unix://', UnixAdapter(base_url)) self.base_url = base_url self._version = version try: - self._cfg = auth.load_config() + self._cfg = load_config() except Exception: pass @@ -172,12 +174,12 @@ class Client(requests.Session): raise Exception("Either path or fileobj needs to be provided.") if fileobj is not None: - context = utils.mkbuildcontext(fileobj) + context = mkbuildcontext(fileobj) elif (path.startswith('http://') or path.startswith('https://') or path.startswith('git://') or path.startswith('github.com/')): remote = path else: - context = utils.tar(path) + context = tar(path) u = self._url('/build') params = { 't': tag, 'remote': remote, 'q': quiet, 'nocache': nocache, 'rm': rm } @@ -336,7 +338,7 @@ class Client(requests.Session): registry = auth.INDEX_URL if getattr(self, '_cfg', None) is None: self._cfg = auth.load_config() - authcfg = auth.resolve_authconfig(self._cfg, registry) + authcfg = resolve_authconfig(self._cfg, registry) if 'username' in authcfg and authcfg['username'] == username: return authcfg req_data = { @@ -376,7 +378,7 @@ class Client(requests.Session): return f_port def pull(self, repository, tag=None): - registry, repo_name = auth.resolve_repository_name(repository) + registry, repo_name = resolve_repository_name(repository) if repo_name.count(":") == 1: repository, tag = repository.rsplit(":", 1) @@ -385,29 +387,30 @@ class Client(requests.Session): 'fromImage': repository } headers = {} - if utils.compare_version('1.5', self._version) >= 0: + + if compare_version('1.5', self._version) >= 0: if getattr(self, '_cfg', None) is None: - self._cfg = auth.load_config() - authcfg = auth.resolve_authconfig(self._cfg, registry) + self._cfg = load_config() + authcfg = resolve_authconfig(self._cfg, registry) # do not fail if no atuhentication exists # for this specific registry as we can have a readonly pull if authcfg: - headers['X-Registry-Auth'] = auth.encode_header(authcfg) + headers['X-Registry-Auth'] = encode_header(authcfg) u = self._url("/images/create") return self._result(self.post(u, params=params, headers=headers)) def push(self, repository): - registry, repository = auth.resolve_repository_name(repository) + registry, repository = resolve_repository_name(repository) u = self._url("/images/{0}/push".format(repository)) headers = {} if getattr(self, '_cfg', None) is None: - self._cfg = auth.load_config() - authcfg = auth.resolve_authconfig(self._cfg, registry) - if utils.compare_version('1.5', self._version) >= 0: + self._cfg = load_config() + authcfg = resolve_authconfig(self._cfg, registry) + if compare_version('1.5', self._version) >= 0: # do not fail if no atuhentication exists # for this specific registry as we can have an anon push if authcfg: - headers['X-Registry-Auth'] = auth.encode_header(authcfg) + headers['X-Registry-Auth'] = encode_header(authcfg) return self._result(self._post_json(u, None, headers=headers)) return self._result(self._post_json(u, authcfg)) diff --git a/docker/unixconn.py b/docker/unixconn.py index 561230c5..b6f3264e 100644 --- a/docker/unixconn.py +++ b/docker/unixconn.py @@ -11,8 +11,12 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import six -import httplib +if six.PY3: + from http import client as httplib +else: + import httplib import requests.adapters import socket diff --git a/tests/test.py b/tests/test.py index 109f3efa..8f335b7d 100644 --- a/tests/test.py +++ b/tests/test.py @@ -32,10 +32,10 @@ except ImportError: from io import StringIO -try: - import mock -except ImportError: +if six.PY3: from unittest import mock +else: + import mock # FIXME: missing tests for @@ -75,7 +75,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.version() except Exception as e: - self.fail('Command should not raise exception: ' + str(e)) + self.fail('Command should not raise exception: {0}'.format(e)) fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/version') @@ -83,7 +83,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.info() except Exception as e: - self.fail('Command should not raise exception: ' + str(e)) + self.fail('Command should not raise exception: {0}'.format(e)) fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/info') @@ -91,7 +91,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.search('busybox') except Exception as e: - self.fail('Command should not raise exception: ' + str(e)) + self.fail('Command should not raise exception: {0}'.format(e)) fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/images/search', params={'term': 'busybox'}) @@ -104,7 +104,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.images(all=True) except Exception as e: - self.fail('Command should not raise exception: ' + str(e)) + self.fail('Command should not raise exception: {0}'.format(e)) fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/images/json', params={'filter': None, 'only_ids': 0, 'all': 1}) @@ -112,7 +112,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.images(quiet=True) except Exception as e: - self.fail('Command should not raise exception: ' + str(e)) + self.fail('Command should not raise exception: {0}'.format(e)) fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/images/json', params={'filter': None, 'only_ids': 1, 'all': 0}) @@ -121,7 +121,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.containers(all=True) except Exception as e: - self.fail('Command should not raise exception: ' + str(e)) + self.fail('Command should not raise exception: {0}'.format(e)) fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/containers/ps', params={ @@ -142,7 +142,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.create_container('busybox', 'true') except Exception as e: - self.fail('Command should not raise exception: ' + str(e)) + self.fail('Command should not raise exception: {0}'.format(e)) fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/containers/create', '{"Tty": false, "Image": "busybox", "Cmd": ["true"], "AttachStdin": false, "Memory": 0, "AttachStderr": true, "Privileged": false, "AttachStdout": true, "OpenStdin": false}', @@ -156,7 +156,7 @@ class DockerClientTest(unittest.TestCase): self.client.create_container('busybox', ['ls', mount_dest], volumes={mount_dest: {}}) except Exception as e: - self.fail('Command should not raise exception: ' + str(e)) + self.fail('Command should not raise exception: {0}'.format(e)) fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/containers/create', '{"Tty": false, "Image": "busybox", "Cmd": ["ls", "/mnt"], "AttachStdin": false, "Volumes": {"/mnt": {}}, "Memory": 0, "AttachStderr": true, "Privileged": false, "AttachStdout": true, "OpenStdin": false}', @@ -166,7 +166,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.create_container('busybox', 'true', privileged=True) except Exception as e: - self.fail('Command should not raise exception: ' + str(e)) + self.fail('Command should not raise exception: {0}'.format(e)) fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/containers/create', '{"Tty": false, "Image": "busybox", "Cmd": ["true"], "AttachStdin": false, "Memory": 0, "AttachStderr": true, "Privileged": true, "AttachStdout": true, "OpenStdin": false}', @@ -176,7 +176,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.start(FAKE_CONTAINER_ID) except Exception as e: - self.fail('Command should not raise exception: ' + str(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/start', @@ -190,7 +190,7 @@ class DockerClientTest(unittest.TestCase): mount_origin = '/tmp' self.client.start(FAKE_CONTAINER_ID, binds={mount_origin: mount_dest}) except Exception as e: - self.fail('Command should not raise exception: ' + str(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/start', @@ -202,7 +202,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.start({'Id': FAKE_CONTAINER_ID}) except Exception as e: - self.fail('Command should not raise exception: ' + str(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/start', '{}', headers={'Content-Type': 'application/json'} @@ -212,7 +212,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.wait(FAKE_CONTAINER_ID) except Exception as e: - self.fail('Command should not raise exception: ' + str(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/wait', @@ -224,7 +224,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.wait({'Id': FAKE_CONTAINER_ID}) except Exception as e: - self.fail('Command should not raise exception: ' + str(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/wait', @@ -236,7 +236,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.logs(FAKE_CONTAINER_ID) except Exception as e: - self.fail('Command should not raise exception: ' + str(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/attach', @@ -248,7 +248,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.logs({'Id': FAKE_CONTAINER_ID}) except Exception as e: - self.fail('Command should not raise exception: ' + str(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/attach', @@ -260,7 +260,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.diff(FAKE_CONTAINER_ID) except Exception as e: - self.fail('Command should not raise exception: ' + str(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/changes') @@ -269,7 +269,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.diff({'Id': FAKE_CONTAINER_ID}) except Exception as e: - self.fail('Command should not raise exception: ' + str(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/changes') @@ -278,7 +278,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.stop(FAKE_CONTAINER_ID, timeout=2) except Exception as e: - self.fail('Command should not raise exception: ' + str(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/stop', @@ -290,7 +290,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.stop({'Id': FAKE_CONTAINER_ID}, timeout=2) except Exception as e: - self.fail('Command should not raise exception: ' + str(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/stop', @@ -302,7 +302,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.kill(FAKE_CONTAINER_ID) except Exception as e: - self.fail('Command should not raise exception: ' + str(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', @@ -313,7 +313,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.kill({'Id': FAKE_CONTAINER_ID}) except Exception as e: - self.fail('Command should not raise exception: ' + str(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', @@ -324,7 +324,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.restart(FAKE_CONTAINER_ID, timeout=2) except Exception as e: - self.fail('Command should not raise exception : ' + str(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/restart', @@ -336,7 +336,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.restart({'Id': FAKE_CONTAINER_ID}, timeout=2) except Exception as e: - self.fail('Command should not raise exception: ' + str(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/restart', @@ -348,7 +348,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.remove_container(FAKE_CONTAINER_ID) except Exception as e: - self.fail('Command should not raise exception: ' + str(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', @@ -359,7 +359,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.remove_container({'Id': FAKE_CONTAINER_ID}) except Exception as e: - self.fail('Command should not raise exception: ' + str(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', @@ -374,7 +374,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.pull('joffrey/test001') except Exception as e: - self.fail('Command should not raise exception: ' + str(e)) + self.fail('Command should not raise exception: {0}'.format(e)) fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/images/create', headers={}, @@ -385,7 +385,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.commit(FAKE_CONTAINER_ID) except Exception as e: - self.fail('Command should not raise exception: ' + str(e)) + self.fail('Command should not raise exception: {0}'.format(e)) fake_request.assert_called_with( 'unix://var/run/docker.sock/v1.4/commit', @@ -404,7 +404,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.remove_image(FAKE_IMAGE_ID) except Exception as e: - self.fail('Command should not raise exception: ' + str(e)) + self.fail('Command should not raise exception: {0}'.format(e)) fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/images/e9aa60c60128') @@ -423,7 +423,7 @@ class DockerClientTest(unittest.TestCase): try: self.client.build(fileobj=script) except Exception as e: - self.fail('Command should not raise exception: ' + str(e)) + self.fail('Command should not raise exception: {0}'.format(e)) ####################### ## PY SPECIFIC TESTS ## From afbfc703caaeff7ccd21c913b91716bb5b6aa281 Mon Sep 17 00:00:00 2001 From: Deni Bertovic Date: Thu, 3 Oct 2013 11:48:18 +0200 Subject: [PATCH 8/9] integration test fix make the mount point for shared.txt reside in the /tmp/ folder because if it resides in the lib folder docker sets the owner of the folder as root inside the container so then when the container exists the test fails to cleanup and remove the shared.txt file, and breaks git as well. --- tests/integration_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration_test.py b/tests/integration_test.py index 295a2500..8130c3d6 100644 --- a/tests/integration_test.py +++ b/tests/integration_test.py @@ -131,7 +131,7 @@ class TestCreateContainer(BaseTestCase): class TestCreateContainerWithBinds(BaseTestCase): def runTest(self): mount_dest = '/mnt' - mount_origin = os.getcwd() + mount_origin = '/tmp' filename = 'shared.txt' shared_file = os.path.join(mount_origin, filename) From 128cdb9112389f18d864dd2cb11bc584a0af55da Mon Sep 17 00:00:00 2001 From: Deni Bertovic Date: Thu, 3 Oct 2013 11:56:44 +0200 Subject: [PATCH 9/9] fixed test list_containers to account for upstream changes --- tests/test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test.py b/tests/test.py index 8f335b7d..fa3d99df 100644 --- a/tests/test.py +++ b/tests/test.py @@ -125,7 +125,6 @@ class DockerClientTest(unittest.TestCase): fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/containers/ps', params={ - 'only_ids': 0, 'all': 1, 'since': None, 'limit': -1,