mirror of https://github.com/docker/docker-py.git
Use pytest helpers to assert exceptions and deprecated warnings
Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
parent
6e27c2a3d0
commit
d5bcdaf2f8
241
tests/test.py
241
tests/test.py
|
@ -27,7 +27,6 @@ import tarfile
|
||||||
import tempfile
|
import tempfile
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import warnings
|
|
||||||
import random
|
import random
|
||||||
|
|
||||||
import docker
|
import docker
|
||||||
|
@ -37,6 +36,8 @@ import six
|
||||||
from . import base
|
from . import base
|
||||||
from . import fake_api
|
from . import fake_api
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -45,9 +46,6 @@ except ImportError:
|
||||||
|
|
||||||
DEFAULT_TIMEOUT_SECONDS = docker.client.constants.DEFAULT_TIMEOUT_SECONDS
|
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,
|
def response(status_code=200, content='', headers=None, reason=None, elapsed=0,
|
||||||
request=None):
|
request=None):
|
||||||
|
@ -137,13 +135,11 @@ class DockerClientTest(Cleanup, base.BaseTestCase):
|
||||||
}
|
}
|
||||||
|
|
||||||
def test_ctor(self):
|
def test_ctor(self):
|
||||||
try:
|
with pytest.raises(docker.errors.DockerException) as excinfo:
|
||||||
docker.Client(version=1.12)
|
docker.Client(version=1.12)
|
||||||
except Exception as e:
|
|
||||||
self.assertTrue(isinstance(e, docker.errors.DockerException))
|
|
||||||
if not six.PY3:
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(e),
|
str(excinfo.value),
|
||||||
'Version parameter must be a string or None. Found float'
|
'Version parameter must be a string or None. Found float'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -186,11 +182,9 @@ class DockerClientTest(Cleanup, base.BaseTestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_image_viz(self):
|
def test_image_viz(self):
|
||||||
try:
|
with pytest.raises(Exception):
|
||||||
self.client.images('busybox', viz=True)
|
self.client.images('busybox', viz=True)
|
||||||
self.fail('Viz output should not be supported!')
|
self.fail('Viz output should not be supported!')
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def test_events(self):
|
def test_events(self):
|
||||||
self.client.events()
|
self.client.events()
|
||||||
|
@ -614,19 +608,21 @@ class DockerClientTest(Cleanup, base.BaseTestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_start_container_none(self):
|
def test_start_container_none(self):
|
||||||
try:
|
with pytest.raises(ValueError) as excinfo:
|
||||||
self.client.start(container=None)
|
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)
|
self.client.start(None)
|
||||||
except ValueError as e:
|
|
||||||
self.assertEqual(str(e), 'image or container param is undefined')
|
self.assertEqual(
|
||||||
else:
|
str(excinfo.value),
|
||||||
self.fail('Command should raise ValueError')
|
'image or container param is undefined',
|
||||||
|
)
|
||||||
|
|
||||||
def test_start_container_regression_573(self):
|
def test_start_container_regression_573(self):
|
||||||
self.client.start(**{'container': fake_api.FAKE_CONTAINER_ID})
|
self.client.start(**{'container': fake_api.FAKE_CONTAINER_ID})
|
||||||
|
@ -764,7 +760,7 @@ class DockerClientTest(Cleanup, base.BaseTestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_create_container_with_binds_mode_and_ro_error(self):
|
def test_create_container_with_binds_mode_and_ro_error(self):
|
||||||
try:
|
with pytest.raises(ValueError):
|
||||||
mount_dest = '/mnt'
|
mount_dest = '/mnt'
|
||||||
mount_origin = '/tmp'
|
mount_origin = '/tmp'
|
||||||
self.client.create_container(
|
self.client.create_container(
|
||||||
|
@ -776,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):
|
def test_create_container_with_binds_list(self):
|
||||||
self.client.create_container(
|
self.client.create_container(
|
||||||
|
@ -961,110 +953,50 @@ class DockerClientTest(Cleanup, base.BaseTestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_start_container_with_lxc_conf(self):
|
def test_start_container_with_lxc_conf(self):
|
||||||
if six.PY2:
|
def call_start():
|
||||||
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(
|
self.client.start(
|
||||||
fake_api.FAKE_CONTAINER_ID,
|
fake_api.FAKE_CONTAINER_ID,
|
||||||
lxc_conf={'lxc.conf.k': 'lxc.conf.value'}
|
lxc_conf={'lxc.conf.k': 'lxc.conf.value'}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
pytest.deprecated_call(call_start)
|
||||||
|
|
||||||
def test_start_container_with_lxc_conf_compat(self):
|
def test_start_container_with_lxc_conf_compat(self):
|
||||||
if six.PY2:
|
def call_start():
|
||||||
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(
|
self.client.start(
|
||||||
fake_api.FAKE_CONTAINER_ID,
|
fake_api.FAKE_CONTAINER_ID,
|
||||||
lxc_conf=[{'Key': 'lxc.conf.k', 'Value': 'lxc.conf.value'}]
|
lxc_conf=[{'Key': 'lxc.conf.k', 'Value': 'lxc.conf.value'}]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
pytest.deprecated_call(call_start)
|
||||||
|
|
||||||
def test_start_container_with_binds_ro(self):
|
def test_start_container_with_binds_ro(self):
|
||||||
mount_dest = '/mnt'
|
def call_start():
|
||||||
mount_origin = '/tmp'
|
self.client.start(
|
||||||
|
fake_api.FAKE_CONTAINER_ID, binds={
|
||||||
|
'/tmp': {
|
||||||
|
"bind": '/mnt',
|
||||||
|
"ro": True
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
if six.PY2:
|
pytest.deprecated_call(call_start)
|
||||||
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_rw(self):
|
def test_start_container_with_binds_rw(self):
|
||||||
mount_dest = '/mnt'
|
def call_start():
|
||||||
mount_origin = '/tmp'
|
|
||||||
if six.PY2:
|
|
||||||
try:
|
|
||||||
self.client.start(
|
self.client.start(
|
||||||
fake_api.FAKE_CONTAINER_ID, binds={
|
fake_api.FAKE_CONTAINER_ID, binds={
|
||||||
mount_origin: {"bind": mount_dest, "ro": False}
|
'/tmp': {"bind": '/mnt', "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}
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
pytest.deprecated_call(call_start)
|
||||||
|
|
||||||
def test_start_container_with_port_binds(self):
|
def test_start_container_with_port_binds(self):
|
||||||
self.maxDiff = None
|
self.maxDiff = None
|
||||||
if six.PY2:
|
|
||||||
try:
|
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',)]
|
|
||||||
})
|
|
||||||
except DeprecationWarning:
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
self.fail('Expected a DeprecationWarning')
|
|
||||||
else:
|
|
||||||
with self.assertWarns(DeprecationWarning):
|
|
||||||
self.client.start(fake_api.FAKE_CONTAINER_ID, port_bindings={
|
self.client.start(fake_api.FAKE_CONTAINER_ID, port_bindings={
|
||||||
1111: None,
|
1111: None,
|
||||||
2222: 2222,
|
2222: 2222,
|
||||||
|
@ -1074,80 +1006,41 @@ class DockerClientTest(Cleanup, base.BaseTestCase):
|
||||||
6666: [('127.0.0.1',), ('192.168.0.1',)]
|
6666: [('127.0.0.1',), ('192.168.0.1',)]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
pytest.deprecated_call(call_start)
|
||||||
|
|
||||||
def test_start_container_with_links(self):
|
def test_start_container_with_links(self):
|
||||||
# one link
|
def call_start():
|
||||||
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(
|
self.client.start(
|
||||||
fake_api.FAKE_CONTAINER_ID, links={link_path: alias}
|
fake_api.FAKE_CONTAINER_ID, links={'path': 'alias'}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
pytest.deprecated_call(call_start)
|
||||||
|
|
||||||
def test_start_container_with_multiple_links(self):
|
def test_start_container_with_multiple_links(self):
|
||||||
link_path = 'path'
|
def call_start():
|
||||||
alias = 'alias'
|
|
||||||
if six.PY2:
|
|
||||||
try:
|
|
||||||
self.client.start(
|
self.client.start(
|
||||||
fake_api.FAKE_CONTAINER_ID,
|
fake_api.FAKE_CONTAINER_ID,
|
||||||
links={
|
links={
|
||||||
link_path + '1': alias + '1',
|
'path1': 'alias1',
|
||||||
link_path + '2': alias + '2'
|
'path2': 'alias2'
|
||||||
}
|
|
||||||
)
|
|
||||||
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'
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
pytest.deprecated_call(call_start)
|
||||||
|
|
||||||
def test_start_container_with_links_as_list_of_tuples(self):
|
def test_start_container_with_links_as_list_of_tuples(self):
|
||||||
# one link
|
def call_start():
|
||||||
link_path = 'path'
|
|
||||||
alias = 'alias'
|
|
||||||
if six.PY2:
|
|
||||||
try:
|
|
||||||
self.client.start(fake_api.FAKE_CONTAINER_ID,
|
self.client.start(fake_api.FAKE_CONTAINER_ID,
|
||||||
links=[(link_path, alias)])
|
links=[('path', 'alias')])
|
||||||
except DeprecationWarning:
|
|
||||||
return
|
pytest.deprecated_call(call_start)
|
||||||
else:
|
|
||||||
self.fail('Expected a DeprecationWarning')
|
|
||||||
else:
|
|
||||||
with self.assertWarns(DeprecationWarning):
|
|
||||||
self.client.start(fake_api.FAKE_CONTAINER_ID,
|
|
||||||
links=[(link_path, alias)])
|
|
||||||
|
|
||||||
def test_start_container_privileged(self):
|
def test_start_container_privileged(self):
|
||||||
if six.PY2:
|
def call_start():
|
||||||
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)
|
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):
|
def test_start_container_with_dict_instead_of_id(self):
|
||||||
self.client.start({'Id': fake_api.FAKE_CONTAINER_ID})
|
self.client.start({'Id': fake_api.FAKE_CONTAINER_ID})
|
||||||
|
|
||||||
|
@ -1715,14 +1608,12 @@ class DockerClientTest(Cleanup, base.BaseTestCase):
|
||||||
|
|
||||||
def test_inspect_container_undefined_id(self):
|
def test_inspect_container_undefined_id(self):
|
||||||
for arg in None, '', {True: True}:
|
for arg in None, '', {True: True}:
|
||||||
try:
|
with pytest.raises(docker.errors.NullResource) as excinfo:
|
||||||
self.client.inspect_container(arg)
|
self.client.inspect_container(arg)
|
||||||
except docker.errors.NullResource as e:
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
e.args[0], 'image or container param is undefined'
|
excinfo.value.args[0], 'image or container param is undefined'
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
self.fail('Command expected NullResource exception')
|
|
||||||
|
|
||||||
def test_container_stats(self):
|
def test_container_stats(self):
|
||||||
self.client.stats(fake_api.FAKE_CONTAINER_ID)
|
self.client.stats(fake_api.FAKE_CONTAINER_ID)
|
||||||
|
@ -1868,14 +1759,12 @@ class DockerClientTest(Cleanup, base.BaseTestCase):
|
||||||
|
|
||||||
def test_inspect_image_undefined_id(self):
|
def test_inspect_image_undefined_id(self):
|
||||||
for arg in None, '', {True: True}:
|
for arg in None, '', {True: True}:
|
||||||
try:
|
with pytest.raises(docker.errors.NullResource) as excinfo:
|
||||||
self.client.inspect_image(arg)
|
self.client.inspect_image(arg)
|
||||||
except docker.errors.NullResource as e:
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
e.args[0], 'image or container param is undefined'
|
excinfo.value.args[0], 'image or container param is undefined'
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
self.fail('Command expected NullResource exception')
|
|
||||||
|
|
||||||
def test_insert_image(self):
|
def test_insert_image(self):
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -14,6 +14,8 @@ from docker.auth import resolve_repository_name, resolve_authconfig
|
||||||
|
|
||||||
from . import base
|
from . import base
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
class UtilsTest(base.BaseTestCase):
|
class UtilsTest(base.BaseTestCase):
|
||||||
longMessage = True
|
longMessage = True
|
||||||
|
@ -79,13 +81,8 @@ class UtilsTest(base.BaseTestCase):
|
||||||
}
|
}
|
||||||
|
|
||||||
for host in invalid_hosts:
|
for host in invalid_hosts:
|
||||||
try:
|
with pytest.raises(DockerException):
|
||||||
parsed = parse_host(host)
|
parse_host(host)
|
||||||
self.fail('Expected to fail but success: %s -> %s' % (
|
|
||||||
host, parsed
|
|
||||||
))
|
|
||||||
except DockerException:
|
|
||||||
pass
|
|
||||||
|
|
||||||
for host, expected in valid_hosts.items():
|
for host, expected in valid_hosts.items():
|
||||||
self.assertEqual(parse_host(host), expected, msg=host)
|
self.assertEqual(parse_host(host), expected, msg=host)
|
||||||
|
|
Loading…
Reference in New Issue