diff options
author | Joffrey F <joffrey@docker.com> | 2015-08-25 16:58:16 -0700 |
---|---|---|
committer | Joffrey F <joffrey@docker.com> | 2015-08-25 16:58:16 -0700 |
commit | d60cb3172e767d77cb55c8b183ac0f7cea658d5f (patch) | |
tree | 2dc1f100524634169264c619677e60970081d464 | |
parent | 47e0ad6959dd08776f35e67ba4f26e0fa3d336e1 (diff) | |
parent | b5f1e64e8f18b313019fd47a431115cf881873e8 (diff) | |
download | docker-py-d60cb3172e767d77cb55c8b183ac0f7cea658d5f.tar.gz |
Merge branch 'aanand-pytest'
-rw-r--r-- | .dockerignore | 1 | ||||
-rw-r--r-- | Makefile | 8 | ||||
-rw-r--r-- | test-requirements.txt | 3 | ||||
-rw-r--r-- | tests/fake_api.py | 2 | ||||
-rw-r--r-- | tests/integration_test.py | 2 | ||||
-rw-r--r-- | tests/test.py | 319 | ||||
-rw-r--r-- | tests/utils_test.py | 17 | ||||
-rw-r--r-- | tox.ini | 6 |
8 files changed, 117 insertions, 241 deletions
diff --git a/.dockerignore b/.dockerignore index ababae3..c767879 100644 --- a/.dockerignore +++ b/.dockerignore @@ -10,6 +10,7 @@ dist .tox .coverage html/* +tests/__pycache__ # Compiled Documentation site/ @@ -13,13 +13,13 @@ build-py3: test: unit-test integration-test unit-test-py3 integration-test-py3 unit-test: build - docker run docker-py python tests/test.py + docker run docker-py py.test tests/test.py tests/utils_test.py unit-test-py3: build-py3 - docker run docker-py3 python tests/test.py + docker run docker-py3 py.test tests/test.py tests/utils_test.py integration-test: build - docker run -e NOT_ON_HOST=true -v `$(HOST_TMPDIR)`:/tmp -v /var/run/docker.sock:/var/run/docker.sock docker-py python tests/integration_test.py + docker run -e NOT_ON_HOST=true -v `$(HOST_TMPDIR)`:/tmp -v /var/run/docker.sock:/var/run/docker.sock docker-py py.test tests/integration_test.py integration-test-py3: build-py3 - docker run -e NOT_ON_HOST=true -v `$(HOST_TMPDIR)`:/tmp -v /var/run/docker.sock:/var/run/docker.sock docker-py3 python tests/integration_test.py + docker run -e NOT_ON_HOST=true -v `$(HOST_TMPDIR)`:/tmp -v /var/run/docker.sock:/var/run/docker.sock docker-py3 py.test tests/integration_test.py diff --git a/test-requirements.txt b/test-requirements.txt index 969f7a2..8461e36 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,2 +1,3 @@ mock==1.0.1 -coverage==3.7.1 +pytest==2.7.2 +pytest-cov==2.1.0 diff --git a/tests/fake_api.py b/tests/fake_api.py index 199b4f6..d663988 100644 --- a/tests/fake_api.py +++ b/tests/fake_api.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import fake_stat +from . import fake_stat CURRENT_VERSION = 'v1.19' diff --git a/tests/integration_test.py b/tests/integration_test.py index c1e6d30..815cd37 100644 --- a/tests/integration_test.py +++ b/tests/integration_test.py @@ -33,7 +33,7 @@ import six from six.moves import BaseHTTPServer from six.moves import socketserver -from test import Cleanup +from .test import Cleanup # FIXME: missing tests for # export; history; insert; port; push; tag; get; load; stats diff --git a/tests/test.py b/tests/test.py index 00ef0d4..da4d34d 100644 --- a/tests/test.py +++ b/tests/test.py @@ -27,16 +27,16 @@ import tarfile import tempfile import threading import time -import unittest -import warnings import random import docker import requests import six -import base -import fake_api +from . import base +from . import fake_api + +import pytest try: from unittest import mock @@ -46,9 +46,6 @@ except ImportError: DEFAULT_TIMEOUT_SECONDS = docker.client.constants.DEFAULT_TIMEOUT_SECONDS -warnings.simplefilter('error') -warnings.filterwarnings('error') - def response(status_code=200, content='', headers=None, reason=None, elapsed=0, request=None): @@ -138,15 +135,13 @@ class DockerClientTest(Cleanup, base.BaseTestCase): } def test_ctor(self): - try: + with pytest.raises(docker.errors.DockerException) as excinfo: docker.Client(version=1.12) - except Exception as e: - self.assertTrue(isinstance(e, docker.errors.DockerException)) - if not six.PY3: - self.assertEqual( - str(e), - 'Version parameter must be a string or None. Found float' - ) + + self.assertEqual( + str(excinfo.value), + 'Version parameter must be a string or None. Found float' + ) ######################### # INFORMATION TESTS # @@ -187,11 +182,9 @@ class DockerClientTest(Cleanup, base.BaseTestCase): ) def test_image_viz(self): - try: + with pytest.raises(Exception): self.client.images('busybox', viz=True) self.fail('Viz output should not be supported!') - except Exception: - pass def test_events(self): self.client.events() @@ -615,19 +608,21 @@ class DockerClientTest(Cleanup, base.BaseTestCase): ) def test_start_container_none(self): - try: + with pytest.raises(ValueError) as excinfo: self.client.start(container=None) - except ValueError as e: - self.assertEqual(str(e), 'image or container param is undefined') - else: - self.fail('Command should raise ValueError') - try: + self.assertEqual( + str(excinfo.value), + 'image or container param is undefined', + ) + + with pytest.raises(ValueError) as excinfo: self.client.start(None) - except ValueError as e: - self.assertEqual(str(e), 'image or container param is undefined') - else: - self.fail('Command should raise ValueError') + + self.assertEqual( + str(excinfo.value), + 'image or container param is undefined', + ) def test_start_container_regression_573(self): self.client.start(**{'container': fake_api.FAKE_CONTAINER_ID}) @@ -765,7 +760,7 @@ class DockerClientTest(Cleanup, base.BaseTestCase): ) def test_create_container_with_binds_mode_and_ro_error(self): - try: + with pytest.raises(ValueError): mount_dest = '/mnt' mount_origin = '/tmp' self.client.create_container( @@ -777,10 +772,6 @@ class DockerClientTest(Cleanup, base.BaseTestCase): }} ) ) - except ValueError: - return - - self.fail('Command should raise ValueError') def test_create_container_with_binds_list(self): self.client.create_container( @@ -962,192 +953,93 @@ class DockerClientTest(Cleanup, base.BaseTestCase): ) def test_start_container_with_lxc_conf(self): - if six.PY2: - try: - self.client.start( - fake_api.FAKE_CONTAINER_ID, - lxc_conf={'lxc.conf.k': 'lxc.conf.value'} - ) - except DeprecationWarning: - return - else: - self.fail('Expected a DeprecationWarning') - else: - with self.assertWarns(DeprecationWarning): - self.client.start( - fake_api.FAKE_CONTAINER_ID, - lxc_conf={'lxc.conf.k': 'lxc.conf.value'} - ) + def call_start(): + self.client.start( + fake_api.FAKE_CONTAINER_ID, + lxc_conf={'lxc.conf.k': 'lxc.conf.value'} + ) + + pytest.deprecated_call(call_start) def test_start_container_with_lxc_conf_compat(self): - if six.PY2: - try: - self.client.start( - fake_api.FAKE_CONTAINER_ID, - lxc_conf=[{'Key': 'lxc.conf.k', 'Value': 'lxc.conf.value'}] - ) - except DeprecationWarning: - return - else: - self.fail('Expected a DeprecationWarning') - else: - with self.assertWarns(DeprecationWarning): - self.client.start( - fake_api.FAKE_CONTAINER_ID, - lxc_conf=[{'Key': 'lxc.conf.k', 'Value': 'lxc.conf.value'}] - ) + def call_start(): + self.client.start( + fake_api.FAKE_CONTAINER_ID, + lxc_conf=[{'Key': 'lxc.conf.k', 'Value': 'lxc.conf.value'}] + ) - def test_start_container_with_binds_ro(self): - mount_dest = '/mnt' - mount_origin = '/tmp' + pytest.deprecated_call(call_start) - if six.PY2: - try: - self.client.start( - fake_api.FAKE_CONTAINER_ID, binds={ - mount_origin: { - "bind": mount_dest, - "ro": True - } - } - ) - except DeprecationWarning: - return - else: - self.fail('Expected a DeprecationWarning') - else: - with self.assertWarns(DeprecationWarning): - self.client.start( - fake_api.FAKE_CONTAINER_ID, binds={ - mount_origin: { - "bind": mount_dest, - "ro": True - } + def test_start_container_with_binds_ro(self): + def call_start(): + self.client.start( + fake_api.FAKE_CONTAINER_ID, binds={ + '/tmp': { + "bind": '/mnt', + "ro": True } - ) + } + ) + + pytest.deprecated_call(call_start) def test_start_container_with_binds_rw(self): - mount_dest = '/mnt' - mount_origin = '/tmp' - if six.PY2: - try: - self.client.start( - fake_api.FAKE_CONTAINER_ID, binds={ - mount_origin: {"bind": mount_dest, "ro": False} - } - ) - except DeprecationWarning: - return - else: - self.fail('Expected a DeprecationWarning') - else: - with self.assertWarns(DeprecationWarning): - self.client.start( - fake_api.FAKE_CONTAINER_ID, binds={ - mount_origin: {"bind": mount_dest, "ro": False} - } - ) + def call_start(): + self.client.start( + fake_api.FAKE_CONTAINER_ID, binds={ + '/tmp': {"bind": '/mnt', "ro": False} + } + ) + + pytest.deprecated_call(call_start) def test_start_container_with_port_binds(self): self.maxDiff = None - if six.PY2: - try: - self.client.start(fake_api.FAKE_CONTAINER_ID, port_bindings={ - 1111: None, - 2222: 2222, - '3333/udp': (3333,), - 4444: ('127.0.0.1',), - 5555: ('127.0.0.1', 5555), - 6666: [('127.0.0.1',), ('192.168.0.1',)] - }) - except DeprecationWarning: - return - else: - self.fail('Expected a DeprecationWarning') - else: - with self.assertWarns(DeprecationWarning): - self.client.start(fake_api.FAKE_CONTAINER_ID, port_bindings={ - 1111: None, - 2222: 2222, - '3333/udp': (3333,), - 4444: ('127.0.0.1',), - 5555: ('127.0.0.1', 5555), - 6666: [('127.0.0.1',), ('192.168.0.1',)] - }) + + def call_start(): + self.client.start(fake_api.FAKE_CONTAINER_ID, port_bindings={ + 1111: None, + 2222: 2222, + '3333/udp': (3333,), + 4444: ('127.0.0.1',), + 5555: ('127.0.0.1', 5555), + 6666: [('127.0.0.1',), ('192.168.0.1',)] + }) + + pytest.deprecated_call(call_start) def test_start_container_with_links(self): - # one link - link_path = 'path' - alias = 'alias' + def call_start(): + self.client.start( + fake_api.FAKE_CONTAINER_ID, links={'path': 'alias'} + ) - if six.PY2: - try: - self.client.start(fake_api.FAKE_CONTAINER_ID, - links={link_path: alias}) - except DeprecationWarning: - return - else: - self.fail('Expected a DeprecationWarning') - else: - with self.assertWarns(DeprecationWarning): - self.client.start( - fake_api.FAKE_CONTAINER_ID, links={link_path: alias} - ) + pytest.deprecated_call(call_start) def test_start_container_with_multiple_links(self): - link_path = 'path' - alias = 'alias' - if six.PY2: - try: - self.client.start( - fake_api.FAKE_CONTAINER_ID, - links={ - link_path + '1': alias + '1', - link_path + '2': alias + '2' - } - ) - except DeprecationWarning: - return - else: - self.fail('Expected a DeprecationWarning') - else: - with self.assertWarns(DeprecationWarning): - self.client.start( - fake_api.FAKE_CONTAINER_ID, - links={ - link_path + '1': alias + '1', - link_path + '2': alias + '2' - } - ) + def call_start(): + self.client.start( + fake_api.FAKE_CONTAINER_ID, + links={ + 'path1': 'alias1', + 'path2': 'alias2' + } + ) + + pytest.deprecated_call(call_start) def test_start_container_with_links_as_list_of_tuples(self): - # one link - link_path = 'path' - alias = 'alias' - if six.PY2: - try: - self.client.start(fake_api.FAKE_CONTAINER_ID, - links=[(link_path, alias)]) - except DeprecationWarning: - return - else: - self.fail('Expected a DeprecationWarning') - else: - with self.assertWarns(DeprecationWarning): - self.client.start(fake_api.FAKE_CONTAINER_ID, - links=[(link_path, alias)]) + def call_start(): + self.client.start(fake_api.FAKE_CONTAINER_ID, + links=[('path', 'alias')]) + + pytest.deprecated_call(call_start) def test_start_container_privileged(self): - if six.PY2: - try: - self.client.start(fake_api.FAKE_CONTAINER_ID, privileged=True) - except DeprecationWarning: - return - else: - self.fail('Expected a DeprecationWarning') - else: - with self.assertWarns(DeprecationWarning): - self.client.start(fake_api.FAKE_CONTAINER_ID, privileged=True) + def call_start(): + self.client.start(fake_api.FAKE_CONTAINER_ID, privileged=True) + + pytest.deprecated_call(call_start) def test_start_container_with_dict_instead_of_id(self): self.client.start({'Id': fake_api.FAKE_CONTAINER_ID}) @@ -1716,14 +1608,12 @@ class DockerClientTest(Cleanup, base.BaseTestCase): def test_inspect_container_undefined_id(self): for arg in None, '', {True: True}: - try: + with pytest.raises(docker.errors.NullResource) as excinfo: self.client.inspect_container(arg) - except docker.errors.NullResource as e: - self.assertEqual( - e.args[0], 'image or container param is undefined' - ) - else: - self.fail('Command expected NullResource exception') + + self.assertEqual( + excinfo.value.args[0], 'image or container param is undefined' + ) def test_container_stats(self): self.client.stats(fake_api.FAKE_CONTAINER_ID) @@ -1869,14 +1759,12 @@ class DockerClientTest(Cleanup, base.BaseTestCase): def test_inspect_image_undefined_id(self): for arg in None, '', {True: True}: - try: + with pytest.raises(docker.errors.NullResource) as excinfo: self.client.inspect_image(arg) - except docker.errors.NullResource as e: - self.assertEqual( - e.args[0], 'image or container param is undefined' - ) - else: - self.fail('Command expected NullResource exception') + + self.assertEqual( + excinfo.value.args[0], 'image or container param is undefined' + ) def test_insert_image(self): try: @@ -2331,6 +2219,3 @@ class StreamTest(Cleanup, base.BaseTestCase): self.assertEqual(list(stream), [ str(i).encode() for i in range(50)]) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/utils_test.py b/tests/utils_test.py index f10b0d0..a72a429 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -1,6 +1,5 @@ import os import os.path -import unittest import tempfile from docker.client import Client @@ -13,7 +12,9 @@ from docker.utils import ( from docker.utils.ports import build_port_bindings, split_port from docker.auth import resolve_repository_name, resolve_authconfig -import base +from . import base + +import pytest class UtilsTest(base.BaseTestCase): @@ -80,13 +81,8 @@ class UtilsTest(base.BaseTestCase): } for host in invalid_hosts: - try: - parsed = parse_host(host) - self.fail('Expected to fail but success: %s -> %s' % ( - host, parsed - )) - except DockerException: - pass + with pytest.raises(DockerException): + parse_host(host) for host, expected in valid_hosts.items(): self.assertEqual(parse_host(host), expected, msg=host) @@ -476,6 +472,3 @@ class UtilsTest(base.BaseTestCase): ["127.0.0.1:1000:1000", "127.0.0.1:2000:2000"]) self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")]) self.assertEqual(port_bindings["2000"], [("127.0.0.1", "2000")]) - -if __name__ == '__main__': - unittest.main() @@ -5,11 +5,7 @@ skipsdist=True [testenv] usedevelop=True commands = - {envbindir}/coverage run -p tests/test.py - {envbindir}/coverage run -p tests/utils_test.py - {envbindir}/coverage combine - {envbindir}/coverage report - {envbindir}/coverage html + py.test --cov=docker tests/test.py tests/utils_test.py deps = -r{toxinidir}/test-requirements.txt -r{toxinidir}/requirements.txt |