Kaydet (Commit) 89a1ffa7 authored tarafından Joffrey F's avatar Joffrey F Kaydeden (comit) GitHub

Merge pull request #1217 from bfirsh/reorganisation-base-tests

Move BaseTestCase to BaseIntegrationTest
...@@ -108,8 +108,10 @@ class Client( ...@@ -108,8 +108,10 @@ class Client(
@classmethod @classmethod
def from_env(cls, **kwargs): def from_env(cls, **kwargs):
timeout = kwargs.pop('timeout', None)
version = kwargs.pop('version', None) version = kwargs.pop('version', None)
return cls(version=version, **kwargs_from_env(**kwargs)) return cls(timeout=timeout, version=version,
**kwargs_from_env(**kwargs))
def _retrieve_server_version(self): def _retrieve_server_version(self):
try: try:
......
import sys import sys
import unittest import unittest
import pytest
import six import six
import docker
class BaseTestCase(unittest.TestCase): class BaseTestCase(unittest.TestCase):
def assertIn(self, object, collection): def assertIn(self, object, collection):
...@@ -14,15 +11,6 @@ class BaseTestCase(unittest.TestCase): ...@@ -14,15 +11,6 @@ class BaseTestCase(unittest.TestCase):
return super(BaseTestCase, self).assertIn(object, collection) return super(BaseTestCase, self).assertIn(object, collection)
def requires_api_version(version):
return pytest.mark.skipif(
docker.utils.version_lt(
docker.constants.DEFAULT_DOCKER_API_VERSION, version
),
reason="API version is too low (< {0})".format(version)
)
class Cleanup(object): class Cleanup(object):
if sys.version_info < (2, 7): if sys.version_info < (2, 7):
# Provide a basic implementation of addCleanup for Python < 2.7 # Provide a basic implementation of addCleanup for Python < 2.7
......
import os import os
import os.path import os.path
import shutil
import tarfile import tarfile
import tempfile import tempfile
import unittest
import docker import docker
import six import pytest
BUSYBOX = 'busybox:buildroot-2014.02'
EXEC_DRIVER = []
def make_tree(dirs, files): def make_tree(dirs, files):
...@@ -45,86 +40,10 @@ def untar_file(tardata, filename): ...@@ -45,86 +40,10 @@ def untar_file(tardata, filename):
return result return result
def docker_client(**kwargs): def requires_api_version(version):
return docker.Client(**docker_client_kwargs(**kwargs)) return pytest.mark.skipif(
docker.utils.version_lt(
docker.constants.DEFAULT_DOCKER_API_VERSION, version
def docker_client_kwargs(**kwargs): ),
client_kwargs = docker.utils.kwargs_from_env(assert_hostname=False) reason="API version is too low (< {0})".format(version)
client_kwargs.update(kwargs) )
return client_kwargs
class BaseTestCase(unittest.TestCase):
tmp_imgs = []
tmp_containers = []
tmp_folders = []
tmp_volumes = []
def setUp(self):
if six.PY2:
self.assertRegex = self.assertRegexpMatches
self.assertCountEqual = self.assertItemsEqual
self.client = docker_client(timeout=60)
self.tmp_imgs = []
self.tmp_containers = []
self.tmp_folders = []
self.tmp_volumes = []
self.tmp_networks = []
def tearDown(self):
for img in self.tmp_imgs:
try:
self.client.remove_image(img)
except docker.errors.APIError:
pass
for container in self.tmp_containers:
try:
self.client.stop(container, timeout=1)
self.client.remove_container(container)
except docker.errors.APIError:
pass
for network in self.tmp_networks:
try:
self.client.remove_network(network)
except docker.errors.APIError:
pass
for folder in self.tmp_folders:
shutil.rmtree(folder)
for volume in self.tmp_volumes:
try:
self.client.remove_volume(volume)
except docker.errors.APIError:
pass
self.client.close()
def run_container(self, *args, **kwargs):
container = self.client.create_container(*args, **kwargs)
self.tmp_containers.append(container)
self.client.start(container)
exitcode = self.client.wait(container)
if exitcode != 0:
output = self.client.logs(container)
raise Exception(
"Container exited with code {}:\n{}"
.format(exitcode, output))
return container
def create_and_start(self, image='busybox', command='top', **kwargs):
container = self.client.create_container(
image=image, command=command, **kwargs)
self.tmp_containers.append(container)
self.client.start(container)
return container
def execute(self, container, cmd, exit_code=0, **kwargs):
exc = self.client.exec_create(container, cmd, **kwargs)
output = self.client.exec_start(exc)
actual_exit_code = self.client.exec_inspect(exc)['ExitCode']
msg = "Expected `{}` to exit with code {} but returned {}:\n{}".format(
" ".join(cmd), exit_code, actual_exit_code, output)
assert actual_exit_code == exit_code, msg
...@@ -6,11 +6,12 @@ import unittest ...@@ -6,11 +6,12 @@ import unittest
import warnings import warnings
import docker import docker
from docker.utils import kwargs_from_env
from .. import helpers from .base import BaseIntegrationTest, BUSYBOX
class InformationTest(helpers.BaseTestCase): class InformationTest(BaseIntegrationTest):
def test_version(self): def test_version(self):
res = self.client.version() res = self.client.version()
self.assertIn('GoVersion', res) self.assertIn('GoVersion', res)
...@@ -24,19 +25,19 @@ class InformationTest(helpers.BaseTestCase): ...@@ -24,19 +25,19 @@ class InformationTest(helpers.BaseTestCase):
self.assertIn('Debug', res) self.assertIn('Debug', res)
def test_search(self): def test_search(self):
self.client = helpers.docker_client(timeout=10) client = docker.from_env(timeout=10)
res = self.client.search('busybox') res = client.search('busybox')
self.assertTrue(len(res) >= 1) self.assertTrue(len(res) >= 1)
base_img = [x for x in res if x['name'] == 'busybox'] base_img = [x for x in res if x['name'] == 'busybox']
self.assertEqual(len(base_img), 1) self.assertEqual(len(base_img), 1)
self.assertIn('description', base_img[0]) self.assertIn('description', base_img[0])
class LinkTest(helpers.BaseTestCase): class LinkTest(BaseIntegrationTest):
def test_remove_link(self): def test_remove_link(self):
# Create containers # Create containers
container1 = self.client.create_container( container1 = self.client.create_container(
helpers.BUSYBOX, 'cat', detach=True, stdin_open=True BUSYBOX, 'cat', detach=True, stdin_open=True
) )
container1_id = container1['Id'] container1_id = container1['Id']
self.tmp_containers.append(container1_id) self.tmp_containers.append(container1_id)
...@@ -48,7 +49,7 @@ class LinkTest(helpers.BaseTestCase): ...@@ -48,7 +49,7 @@ class LinkTest(helpers.BaseTestCase):
link_alias = 'mylink' link_alias = 'mylink'
container2 = self.client.create_container( container2 = self.client.create_container(
helpers.BUSYBOX, 'cat', host_config=self.client.create_host_config( BUSYBOX, 'cat', host_config=self.client.create_host_config(
links={link_path: link_alias} links={link_path: link_alias}
) )
) )
...@@ -74,7 +75,7 @@ class LinkTest(helpers.BaseTestCase): ...@@ -74,7 +75,7 @@ class LinkTest(helpers.BaseTestCase):
self.assertEqual(len(retrieved), 2) self.assertEqual(len(retrieved), 2)
class LoadConfigTest(helpers.BaseTestCase): class LoadConfigTest(BaseIntegrationTest):
def test_load_legacy_config(self): def test_load_legacy_config(self):
folder = tempfile.mkdtemp() folder = tempfile.mkdtemp()
self.tmp_folders.append(folder) self.tmp_folders.append(folder)
...@@ -113,7 +114,7 @@ class LoadConfigTest(helpers.BaseTestCase): ...@@ -113,7 +114,7 @@ class LoadConfigTest(helpers.BaseTestCase):
class AutoDetectVersionTest(unittest.TestCase): class AutoDetectVersionTest(unittest.TestCase):
def test_client_init(self): def test_client_init(self):
client = helpers.docker_client(version='auto') client = docker.from_env(version='auto')
client_version = client._version client_version = client._version
api_version = client.version(api_version=False)['ApiVersion'] api_version = client.version(api_version=False)['ApiVersion']
self.assertEqual(client_version, api_version) self.assertEqual(client_version, api_version)
...@@ -122,7 +123,7 @@ class AutoDetectVersionTest(unittest.TestCase): ...@@ -122,7 +123,7 @@ class AutoDetectVersionTest(unittest.TestCase):
client.close() client.close()
def test_auto_client(self): def test_auto_client(self):
client = docker.AutoVersionClient(**helpers.docker_client_kwargs()) client = docker.AutoVersionClient(**kwargs_from_env())
client_version = client._version client_version = client._version
api_version = client.version(api_version=False)['ApiVersion'] api_version = client.version(api_version=False)['ApiVersion']
self.assertEqual(client_version, api_version) self.assertEqual(client_version, api_version)
...@@ -130,9 +131,7 @@ class AutoDetectVersionTest(unittest.TestCase): ...@@ -130,9 +131,7 @@ class AutoDetectVersionTest(unittest.TestCase):
self.assertEqual(client_version, api_version_2) self.assertEqual(client_version, api_version_2)
client.close() client.close()
with self.assertRaises(docker.errors.DockerException): with self.assertRaises(docker.errors.DockerException):
docker.AutoVersionClient( docker.AutoVersionClient(version='1.11', **kwargs_from_env())
**helpers.docker_client_kwargs(version='1.11')
)
class ConnectionTimeoutTest(unittest.TestCase): class ConnectionTimeoutTest(unittest.TestCase):
...@@ -167,7 +166,7 @@ class UnixconnTest(unittest.TestCase): ...@@ -167,7 +166,7 @@ class UnixconnTest(unittest.TestCase):
with warnings.catch_warnings(record=True) as w: with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always') warnings.simplefilter('always')
client = helpers.docker_client() client = docker.from_env()
client.images() client.images()
client.close() client.close()
del client del client
......
import shutil
import unittest
import docker
import six
BUSYBOX = 'busybox:buildroot-2014.02'
class BaseIntegrationTest(unittest.TestCase):
"""
A base class for integration test cases.
It sets up a Docker client and cleans up the Docker server after itself.
"""
tmp_imgs = []
tmp_containers = []
tmp_folders = []
tmp_volumes = []
def setUp(self):
if six.PY2:
self.assertRegex = self.assertRegexpMatches
self.assertCountEqual = self.assertItemsEqual
self.client = docker.from_env(timeout=60)
self.tmp_imgs = []
self.tmp_containers = []
self.tmp_folders = []
self.tmp_volumes = []
self.tmp_networks = []
def tearDown(self):
for img in self.tmp_imgs:
try:
self.client.remove_image(img)
except docker.errors.APIError:
pass
for container in self.tmp_containers:
try:
self.client.stop(container, timeout=1)
self.client.remove_container(container)
except docker.errors.APIError:
pass
for network in self.tmp_networks:
try:
self.client.remove_network(network)
except docker.errors.APIError:
pass
for folder in self.tmp_folders:
shutil.rmtree(folder)
for volume in self.tmp_volumes:
try:
self.client.remove_volume(volume)
except docker.errors.APIError:
pass
self.client.close()
def run_container(self, *args, **kwargs):
container = self.client.create_container(*args, **kwargs)
self.tmp_containers.append(container)
self.client.start(container)
exitcode = self.client.wait(container)
if exitcode != 0:
output = self.client.logs(container)
raise Exception(
"Container exited with code {}:\n{}"
.format(exitcode, output))
return container
def create_and_start(self, image='busybox', command='top', **kwargs):
container = self.client.create_container(
image=image, command=command, **kwargs)
self.tmp_containers.append(container)
self.client.start(container)
return container
def execute(self, container, cmd, exit_code=0, **kwargs):
exc = self.client.exec_create(container, cmd, **kwargs)
output = self.client.exec_start(exc)
actual_exit_code = self.client.exec_inspect(exc)['ExitCode']
msg = "Expected `{}` to exit with code {} but returned {}:\n{}".format(
" ".join(cmd), exit_code, actual_exit_code, output)
assert actual_exit_code == exit_code, msg
...@@ -8,11 +8,11 @@ import six ...@@ -8,11 +8,11 @@ import six
from docker import errors from docker import errors
from .. import helpers from ..helpers import requires_api_version
from ..base import requires_api_version from .base import BaseIntegrationTest
class BuildTest(helpers.BaseTestCase): class BuildTest(BaseIntegrationTest):
def test_build_streaming(self): def test_build_streaming(self):
script = io.BytesIO('\n'.join([ script = io.BytesIO('\n'.join([
'FROM busybox', 'FROM busybox',
......
...@@ -7,14 +7,13 @@ import warnings ...@@ -7,14 +7,13 @@ import warnings
import docker.errors import docker.errors
import pytest import pytest
from ..helpers import BUSYBOX from .base import BUSYBOX
from ..helpers import docker_client
@pytest.fixture(autouse=True, scope='session') @pytest.fixture(autouse=True, scope='session')
def setup_test_session(): def setup_test_session():
warnings.simplefilter('error') warnings.simplefilter('error')
c = docker_client() c = docker.from_env()
try: try:
c.inspect_image(BUSYBOX) c.inspect_image(BUSYBOX)
except docker.errors.NotFound: except docker.errors.NotFound:
......
...@@ -8,13 +8,12 @@ from docker.utils.socket import read_exactly ...@@ -8,13 +8,12 @@ from docker.utils.socket import read_exactly
import pytest import pytest
import six import six
from ..base import requires_api_version from ..helpers import requires_api_version
from .. import helpers from .. import helpers
from .base import BaseIntegrationTest, BUSYBOX
BUSYBOX = helpers.BUSYBOX
class ListContainersTest(BaseIntegrationTest):
class ListContainersTest(helpers.BaseTestCase):
def test_list_containers(self): def test_list_containers(self):
res0 = self.client.containers(all=True) res0 = self.client.containers(all=True)
size = len(res0) size = len(res0)
...@@ -34,7 +33,7 @@ class ListContainersTest(helpers.BaseTestCase): ...@@ -34,7 +33,7 @@ class ListContainersTest(helpers.BaseTestCase):
self.assertIn('Status', retrieved) self.assertIn('Status', retrieved)
class CreateContainerTest(helpers.BaseTestCase): class CreateContainerTest(BaseIntegrationTest):
def test_create(self): def test_create(self):
res = self.client.create_container(BUSYBOX, 'true') res = self.client.create_container(BUSYBOX, 'true')
...@@ -398,7 +397,7 @@ class CreateContainerTest(helpers.BaseTestCase): ...@@ -398,7 +397,7 @@ class CreateContainerTest(helpers.BaseTestCase):
assert config['HostConfig']['Tmpfs'] == tmpfs assert config['HostConfig']['Tmpfs'] == tmpfs
class VolumeBindTest(helpers.BaseTestCase): class VolumeBindTest(BaseIntegrationTest):
def setUp(self): def setUp(self):
super(VolumeBindTest, self).setUp() super(VolumeBindTest, self).setUp()
...@@ -487,7 +486,7 @@ class VolumeBindTest(helpers.BaseTestCase): ...@@ -487,7 +486,7 @@ class VolumeBindTest(helpers.BaseTestCase):
@requires_api_version('1.20') @requires_api_version('1.20')
class ArchiveTest(helpers.BaseTestCase): class ArchiveTest(BaseIntegrationTest):
def test_get_file_archive_from_container(self): def test_get_file_archive_from_container(self):
data = 'The Maid and the Pocket Watch of Blood' data = 'The Maid and the Pocket Watch of Blood'
ctnr = self.client.create_container( ctnr = self.client.create_container(
...@@ -567,7 +566,7 @@ class ArchiveTest(helpers.BaseTestCase): ...@@ -567,7 +566,7 @@ class ArchiveTest(helpers.BaseTestCase):
self.assertIn('bar/', results) self.assertIn('bar/', results)
class RenameContainerTest(helpers.BaseTestCase): class RenameContainerTest(BaseIntegrationTest):
def test_rename_container(self): def test_rename_container(self):
version = self.client.version()['Version'] version = self.client.version()['Version']
name = 'hong_meiling' name = 'hong_meiling'
...@@ -583,7 +582,7 @@ class RenameContainerTest(helpers.BaseTestCase): ...@@ -583,7 +582,7 @@ class RenameContainerTest(helpers.BaseTestCase):
self.assertEqual('/{0}'.format(name), inspect['Name']) self.assertEqual('/{0}'.format(name), inspect['Name'])
class StartContainerTest(helpers.BaseTestCase): class StartContainerTest(BaseIntegrationTest):
def test_start_container(self): def test_start_container(self):
res = self.client.create_container(BUSYBOX, 'true') res = self.client.create_container(BUSYBOX, 'true')
self.assertIn('Id', res) self.assertIn('Id', res)
...@@ -637,7 +636,7 @@ class StartContainerTest(helpers.BaseTestCase): ...@@ -637,7 +636,7 @@ class StartContainerTest(helpers.BaseTestCase):
self.assertEqual(exitcode, 0, msg=cmd) self.assertEqual(exitcode, 0, msg=cmd)
class WaitTest(helpers.BaseTestCase): class WaitTest(BaseIntegrationTest):
def test_wait(self): def test_wait(self):
res = self.client.create_container(BUSYBOX, ['sleep', '3']) res = self.client.create_container(BUSYBOX, ['sleep', '3'])
id = res['Id'] id = res['Id']
...@@ -665,7 +664,7 @@ class WaitTest(helpers.BaseTestCase): ...@@ -665,7 +664,7 @@ class WaitTest(helpers.BaseTestCase):
self.assertEqual(inspect['State']['ExitCode'], exitcode) self.assertEqual(inspect['State']['ExitCode'], exitcode)
class LogsTest(helpers.BaseTestCase): class LogsTest(BaseIntegrationTest):
def test_logs(self): def test_logs(self):
snippet = 'Flowering Nights (Sakuya Iyazoi)' snippet = 'Flowering Nights (Sakuya Iyazoi)'
container = self.client.create_container( container = self.client.create_container(
...@@ -737,7 +736,7 @@ Line2''' ...@@ -737,7 +736,7 @@ Line2'''
self.assertEqual(logs, ''.encode(encoding='ascii')) self.assertEqual(logs, ''.encode(encoding='ascii'))
class DiffTest(helpers.BaseTestCase): class DiffTest(BaseIntegrationTest):
def test_diff(self): def test_diff(self):
container = self.client.create_container(BUSYBOX, ['touch', '/test']) container = self.client.create_container(BUSYBOX, ['touch', '/test'])
id = container['Id'] id = container['Id']
...@@ -765,7 +764,7 @@ class DiffTest(helpers.BaseTestCase): ...@@ -765,7 +764,7 @@ class DiffTest(helpers.BaseTestCase):
self.assertEqual(test_diff[0]['Kind'], 1) self.assertEqual(test_diff[0]['Kind'], 1)
class StopTest(helpers.BaseTestCase): class StopTest(BaseIntegrationTest):
def test_stop(self): def test_stop(self):
container = self.client.create_container(BUSYBOX, ['sleep', '9999']) container = self.client.create_container(BUSYBOX, ['sleep', '9999'])
id = container['Id'] id = container['Id']
...@@ -792,7 +791,7 @@ class StopTest(helpers.BaseTestCase): ...@@ -792,7 +791,7 @@ class StopTest(helpers.BaseTestCase):
self.assertEqual(state['Running'], False) self.assertEqual(state['Running'], False)
class KillTest(helpers.BaseTestCase): class KillTest(BaseIntegrationTest):
def test_kill(self): def test_kill(self):
container = self.client.create_container(BUSYBOX, ['sleep', '9999']) container = self.client.create_container(BUSYBOX, ['sleep', '9999'])
id = container['Id'] id = container['Id']
...@@ -868,7 +867,7 @@ class KillTest(helpers.BaseTestCase): ...@@ -868,7 +867,7 @@ class KillTest(helpers.BaseTestCase):
self.assertEqual(state['Running'], False, state) self.assertEqual(state['Running'], False, state)
class PortTest(helpers.BaseTestCase): class PortTest(BaseIntegrationTest):
def test_port(self): def test_port(self):
port_bindings = { port_bindings = {
...@@ -899,7 +898,7 @@ class PortTest(helpers.BaseTestCase): ...@@ -899,7 +898,7 @@ class PortTest(helpers.BaseTestCase):
self.client.kill(id) self.client.kill(id)
class ContainerTopTest(helpers.BaseTestCase): class ContainerTopTest(BaseIntegrationTest):
def test_top(self): def test_top(self):
container = self.client.create_container( container = self.client.create_container(
BUSYBOX, ['sleep', '60']) BUSYBOX, ['sleep', '60'])
...@@ -934,7 +933,7 @@ class ContainerTopTest(helpers.BaseTestCase): ...@@ -934,7 +933,7 @@ class ContainerTopTest(helpers.BaseTestCase):
self.client.kill(id) self.client.kill(id)
class RestartContainerTest(helpers.BaseTestCase): class RestartContainerTest(BaseIntegrationTest):
def test_restart(self): def test_restart(self):
container = self.client.create_container(BUSYBOX, ['sleep', '9999']) container = self.client.create_container(BUSYBOX, ['sleep', '9999'])
id = container['Id'] id = container['Id']
...@@ -975,7 +974,7 @@ class RestartContainerTest(helpers.BaseTestCase): ...@@ -975,7 +974,7 @@ class RestartContainerTest(helpers.BaseTestCase):
self.client.kill(id) self.client.kill(id)
class RemoveContainerTest(helpers.BaseTestCase): class RemoveContainerTest(BaseIntegrationTest):
def test_remove(self): def test_remove(self):
container = self.client.create_container(BUSYBOX, ['true']) container = self.client.create_container(BUSYBOX, ['true'])
id = container['Id'] id = container['Id']
...@@ -997,7 +996,7 @@ class RemoveContainerTest(helpers.BaseTestCase): ...@@ -997,7 +996,7 @@ class RemoveContainerTest(helpers.BaseTestCase):
self.assertEqual(len(res), 0) self.assertEqual(len(res), 0)
class AttachContainerTest(helpers.BaseTestCase): class AttachContainerTest(BaseIntegrationTest):
def test_run_container_streaming(self): def test_run_container_streaming(self):
container = self.client.create_container(BUSYBOX, '/bin/sh', container = self.client.create_container(BUSYBOX, '/bin/sh',
detach=True, stdin_open=True) detach=True, stdin_open=True)
...@@ -1028,7 +1027,7 @@ class AttachContainerTest(helpers.BaseTestCase): ...@@ -1028,7 +1027,7 @@ class AttachContainerTest(helpers.BaseTestCase):
self.assertEqual(data.decode('utf-8'), line) self.assertEqual(data.decode('utf-8'), line)
class PauseTest(helpers.BaseTestCase): class PauseTest(BaseIntegrationTest):
def test_pause_unpause(self): def test_pause_unpause(self):
container = self.client.create_container(BUSYBOX, ['sleep', '9999']) container = self.client.create_container(BUSYBOX, ['sleep', '9999'])
id = container['Id'] id = container['Id']
...@@ -1057,7 +1056,7 @@ class PauseTest(helpers.BaseTestCase): ...@@ -1057,7 +1056,7 @@ class PauseTest(helpers.BaseTestCase):
self.assertEqual(state['Paused'], False) self.assertEqual(state['Paused'], False)
class GetContainerStatsTest(helpers.BaseTestCase): class GetContainerStatsTest(BaseIntegrationTest):
@requires_api_version('1.19') @requires_api_version('1.19')
def test_get_container_stats_no_stream(self): def test_get_container_stats_no_stream(self):
container = self.client.create_container( container = self.client.create_container(
...@@ -1088,7 +1087,7 @@ class GetContainerStatsTest(helpers.BaseTestCase): ...@@ -1088,7 +1087,7 @@ class GetContainerStatsTest(helpers.BaseTestCase):
self.assertIn(key, chunk) self.assertIn(key, chunk)
class ContainerUpdateTest(helpers.BaseTestCase): class ContainerUpdateTest(BaseIntegrationTest):
@requires_api_version('1.22') @requires_api_version('1.22')
def test_update_container(self): def test_update_container(self):
old_mem_limit = 400 * 1024 * 1024 old_mem_limit = 400 * 1024 * 1024
...@@ -1135,7 +1134,7 @@ class ContainerUpdateTest(helpers.BaseTestCase): ...@@ -1135,7 +1134,7 @@ class ContainerUpdateTest(helpers.BaseTestCase):
) )
class ContainerCPUTest(helpers.BaseTestCase): class ContainerCPUTest(BaseIntegrationTest):
@requires_api_version('1.18') @requires_api_version('1.18')
def test_container_cpu_shares(self): def test_container_cpu_shares(self):
cpu_shares = 512 cpu_shares = 512
......
from docker.errors import APIError from docker.errors import APIError
from .. import helpers from .base import BaseIntegrationTest, BUSYBOX
class ErrorsTest(helpers.BaseTestCase): class ErrorsTest(BaseIntegrationTest):
def test_api_error_parses_json(self): def test_api_error_parses_json(self):
container = self.client.create_container( container = self.client.create_container(BUSYBOX, ['sleep', '10'])
helpers.BUSYBOX,
['sleep', '10']
)
self.client.start(container['Id']) self.client.start(container['Id'])
with self.assertRaises(APIError) as cm: with self.assertRaises(APIError) as cm:
self.client.remove_container(container['Id']) self.client.remove_container(container['Id'])
......
from docker.utils.socket import next_frame_size from docker.utils.socket import next_frame_size
from docker.utils.socket import read_exactly from docker.utils.socket import read_exactly
from .. import helpers from .base import BaseIntegrationTest, BUSYBOX
BUSYBOX = helpers.BUSYBOX
class ExecTest(BaseIntegrationTest):
class ExecTest(helpers.BaseTestCase):
def test_execute_command(self): def test_execute_command(self):
container = self.client.create_container(BUSYBOX, 'cat', container = self.client.create_container(BUSYBOX, 'cat',
detach=True, stdin_open=True) detach=True, stdin_open=True)
......
...@@ -14,12 +14,10 @@ from six.moves import socketserver ...@@ -14,12 +14,10 @@ from six.moves import socketserver
import docker import docker
from .. import helpers from .base import BaseIntegrationTest, BUSYBOX
BUSYBOX = helpers.BUSYBOX
class ListImagesTest(BaseIntegrationTest):
class ListImagesTest(helpers.BaseTestCase):
def test_images(self): def test_images(self):
res1 = self.client.images(all=True) res1 = self.client.images(all=True)
self.assertIn('Id', res1[0]) self.assertIn('Id', res1[0])
...@@ -37,7 +35,7 @@ class ListImagesTest(helpers.BaseTestCase): ...@@ -37,7 +35,7 @@ class ListImagesTest(helpers.BaseTestCase):
self.assertEqual(type(res1[0]), six.text_type) self.assertEqual(type(res1[0]), six.text_type)
class PullImageTest(helpers.BaseTestCase): class PullImageTest(BaseIntegrationTest):
def test_pull(self): def test_pull(self):
try: try:
self.client.remove_image('hello-world') self.client.remove_image('hello-world')
...@@ -70,7 +68,7 @@ class PullImageTest(helpers.BaseTestCase): ...@@ -70,7 +68,7 @@ class PullImageTest(helpers.BaseTestCase):
self.assertIn('Id', img_info) self.assertIn('Id', img_info)
class CommitTest(helpers.BaseTestCase): class CommitTest(BaseIntegrationTest):
def test_commit(self): def test_commit(self):
container = self.client.create_container(BUSYBOX, ['touch', '/test']) container = self.client.create_container(BUSYBOX, ['touch', '/test'])
id = container['Id'] id = container['Id']
...@@ -105,7 +103,7 @@ class CommitTest(helpers.BaseTestCase): ...@@ -105,7 +103,7 @@ class CommitTest(helpers.BaseTestCase):
assert img['Config']['Cmd'] == ['bash'] assert img['Config']['Cmd'] == ['bash']
class RemoveImageTest(helpers.BaseTestCase): class RemoveImageTest(BaseIntegrationTest):
def test_remove(self): def test_remove(self):
container = self.client.create_container(BUSYBOX, ['touch', '/test']) container = self.client.create_container(BUSYBOX, ['touch', '/test'])
id = container['Id'] id = container['Id']
...@@ -121,7 +119,7 @@ class RemoveImageTest(helpers.BaseTestCase): ...@@ -121,7 +119,7 @@ class RemoveImageTest(helpers.BaseTestCase):
self.assertEqual(len(res), 0) self.assertEqual(len(res), 0)
class ImportImageTest(helpers.BaseTestCase): class ImportImageTest(BaseIntegrationTest):
'''Base class for `docker import` test cases.''' '''Base class for `docker import` test cases.'''
TAR_SIZE = 512 * 1024 TAR_SIZE = 512 * 1024
......
...@@ -5,11 +5,11 @@ from docker.utils import create_ipam_config ...@@ -5,11 +5,11 @@ from docker.utils import create_ipam_config
from docker.utils import create_ipam_pool from docker.utils import create_ipam_pool
import pytest import pytest
from .. import helpers from ..helpers import requires_api_version
from ..base import requires_api_version from .base import BaseIntegrationTest
class TestNetworks(helpers.BaseTestCase): class TestNetworks(BaseIntegrationTest):
def create_network(self, *args, **kwargs): def create_network(self, *args, **kwargs):
net_name = u'dockerpy{}'.format(random.getrandbits(24))[:14] net_name = u'dockerpy{}'.format(random.getrandbits(24))[:14]
net_id = self.client.create_network(net_name, *args, **kwargs)['Id'] net_id = self.client.create_network(net_name, *args, **kwargs)['Id']
......
...@@ -4,12 +4,10 @@ import random ...@@ -4,12 +4,10 @@ import random
import docker import docker
import six import six
from .. import helpers from .base import BaseIntegrationTest, BUSYBOX
BUSYBOX = helpers.BUSYBOX
class TestRegressions(BaseIntegrationTest):
class TestRegressions(helpers.BaseTestCase):
def test_443_handle_nonchunked_response_in_stream(self): def test_443_handle_nonchunked_response_in_stream(self):
dfile = io.BytesIO() dfile = io.BytesIO()
with self.assertRaises(docker.errors.APIError) as exc: with self.assertRaises(docker.errors.APIError) as exc:
......
...@@ -2,14 +2,11 @@ import random ...@@ -2,14 +2,11 @@ import random
import docker import docker
from ..base import requires_api_version from ..helpers import requires_api_version
from .. import helpers from .base import BaseIntegrationTest
BUSYBOX = helpers.BUSYBOX class ServiceTest(BaseIntegrationTest):
class ServiceTest(helpers.BaseTestCase):
def setUp(self): def setUp(self):
super(ServiceTest, self).setUp() super(ServiceTest, self).setUp()
self.client.leave_swarm(force=True) self.client.leave_swarm(force=True)
......
import docker import docker
import pytest import pytest
from ..base import requires_api_version from ..helpers import requires_api_version
from .. import helpers from .base import BaseIntegrationTest
BUSYBOX = helpers.BUSYBOX class SwarmTest(BaseIntegrationTest):
class SwarmTest(helpers.BaseTestCase):
def setUp(self): def setUp(self):
super(SwarmTest, self).setUp() super(SwarmTest, self).setUp()
self.client.leave_swarm(force=True) self.client.leave_swarm(force=True)
......
import docker import docker
import pytest import pytest
from .. import helpers from ..helpers import requires_api_version
from ..base import requires_api_version from .base import BaseIntegrationTest
@requires_api_version('1.21') @requires_api_version('1.21')
class TestVolumes(helpers.BaseTestCase): class TestVolumes(BaseIntegrationTest):
def test_create_volume(self): def test_create_volume(self):
name = 'perfectcherryblossom' name = 'perfectcherryblossom'
self.tmp_volumes.append(name) self.tmp_volumes.append(name)
......
...@@ -9,7 +9,7 @@ import pytest ...@@ -9,7 +9,7 @@ import pytest
import six import six
from . import fake_api from . import fake_api
from ..base import requires_api_version from ..helpers import requires_api_version
from .api_test import ( from .api_test import (
DockerClientTest, url_prefix, fake_request, DEFAULT_TIMEOUT_SECONDS, DockerClientTest, url_prefix, fake_request, DEFAULT_TIMEOUT_SECONDS,
fake_inspect_container fake_inspect_container
......
...@@ -2,7 +2,7 @@ import json ...@@ -2,7 +2,7 @@ import json
import six import six
from .. import base from ..helpers import requires_api_version
from .api_test import DockerClientTest, url_prefix, response from .api_test import DockerClientTest, url_prefix, response
from docker.utils import create_ipam_config, create_ipam_pool from docker.utils import create_ipam_config, create_ipam_pool
...@@ -13,7 +13,7 @@ except ImportError: ...@@ -13,7 +13,7 @@ except ImportError:
class NetworkTest(DockerClientTest): class NetworkTest(DockerClientTest):
@base.requires_api_version('1.21') @requires_api_version('1.21')
def test_list_networks(self): def test_list_networks(self):
networks = [ networks = [
{ {
...@@ -49,7 +49,7 @@ class NetworkTest(DockerClientTest): ...@@ -49,7 +49,7 @@ class NetworkTest(DockerClientTest):
filters = json.loads(get.call_args[1]['params']['filters']) filters = json.loads(get.call_args[1]['params']['filters'])
self.assertEqual(filters, {'id': ['123']}) self.assertEqual(filters, {'id': ['123']})
@base.requires_api_version('1.21') @requires_api_version('1.21')
def test_create_network(self): def test_create_network(self):
network_data = { network_data = {
"id": 'abc12345', "id": 'abc12345',
...@@ -104,7 +104,7 @@ class NetworkTest(DockerClientTest): ...@@ -104,7 +104,7 @@ class NetworkTest(DockerClientTest):
} }
}) })
@base.requires_api_version('1.21') @requires_api_version('1.21')
def test_remove_network(self): def test_remove_network(self):
network_id = 'abc12345' network_id = 'abc12345'
delete = mock.Mock(return_value=response(status_code=200)) delete = mock.Mock(return_value=response(status_code=200))
...@@ -116,7 +116,7 @@ class NetworkTest(DockerClientTest): ...@@ -116,7 +116,7 @@ class NetworkTest(DockerClientTest):
self.assertEqual(args[0][0], self.assertEqual(args[0][0],
url_prefix + 'networks/{0}'.format(network_id)) url_prefix + 'networks/{0}'.format(network_id))
@base.requires_api_version('1.21') @requires_api_version('1.21')
def test_inspect_network(self): def test_inspect_network(self):
network_id = 'abc12345' network_id = 'abc12345'
network_name = 'foo' network_name = 'foo'
...@@ -138,7 +138,7 @@ class NetworkTest(DockerClientTest): ...@@ -138,7 +138,7 @@ class NetworkTest(DockerClientTest):
self.assertEqual(args[0][0], self.assertEqual(args[0][0],
url_prefix + 'networks/{0}'.format(network_id)) url_prefix + 'networks/{0}'.format(network_id))
@base.requires_api_version('1.21') @requires_api_version('1.21')
def test_connect_container_to_network(self): def test_connect_container_to_network(self):
network_id = 'abc12345' network_id = 'abc12345'
container_id = 'def45678' container_id = 'def45678'
...@@ -167,7 +167,7 @@ class NetworkTest(DockerClientTest): ...@@ -167,7 +167,7 @@ class NetworkTest(DockerClientTest):
}, },
}) })
@base.requires_api_version('1.21') @requires_api_version('1.21')
def test_disconnect_container_from_network(self): def test_disconnect_container_from_network(self):
network_id = 'abc12345' network_id = 'abc12345'
container_id = 'def45678' container_id = 'def45678'
......
...@@ -2,12 +2,12 @@ import json ...@@ -2,12 +2,12 @@ import json
import pytest import pytest
from .. import base from ..helpers import requires_api_version
from .api_test import DockerClientTest, url_prefix, fake_request from .api_test import DockerClientTest, url_prefix, fake_request
class VolumeTest(DockerClientTest): class VolumeTest(DockerClientTest):
@base.requires_api_version('1.21') @requires_api_version('1.21')
def test_list_volumes(self): def test_list_volumes(self):
volumes = self.client.volumes() volumes = self.client.volumes()
self.assertIn('Volumes', volumes) self.assertIn('Volumes', volumes)
...@@ -17,7 +17,7 @@ class VolumeTest(DockerClientTest): ...@@ -17,7 +17,7 @@ class VolumeTest(DockerClientTest):
self.assertEqual(args[0][0], 'GET') self.assertEqual(args[0][0], 'GET')
self.assertEqual(args[0][1], url_prefix + 'volumes') self.assertEqual(args[0][1], url_prefix + 'volumes')
@base.requires_api_version('1.21') @requires_api_version('1.21')
def test_list_volumes_and_filters(self): def test_list_volumes_and_filters(self):
volumes = self.client.volumes(filters={'dangling': True}) volumes = self.client.volumes(filters={'dangling': True})
assert 'Volumes' in volumes assert 'Volumes' in volumes
...@@ -29,7 +29,7 @@ class VolumeTest(DockerClientTest): ...@@ -29,7 +29,7 @@ class VolumeTest(DockerClientTest):
assert args[1] == {'params': {'filters': '{"dangling": ["true"]}'}, assert args[1] == {'params': {'filters': '{"dangling": ["true"]}'},
'timeout': 60} 'timeout': 60}
@base.requires_api_version('1.21') @requires_api_version('1.21')
def test_create_volume(self): def test_create_volume(self):
name = 'perfectcherryblossom' name = 'perfectcherryblossom'
result = self.client.create_volume(name) result = self.client.create_volume(name)
...@@ -43,7 +43,7 @@ class VolumeTest(DockerClientTest): ...@@ -43,7 +43,7 @@ class VolumeTest(DockerClientTest):
self.assertEqual(args[0][1], url_prefix + 'volumes/create') self.assertEqual(args[0][1], url_prefix + 'volumes/create')
self.assertEqual(json.loads(args[1]['data']), {'Name': name}) self.assertEqual(json.loads(args[1]['data']), {'Name': name})
@base.requires_api_version('1.23') @requires_api_version('1.23')
def test_create_volume_with_labels(self): def test_create_volume_with_labels(self):
name = 'perfectcherryblossom' name = 'perfectcherryblossom'
result = self.client.create_volume(name, labels={ result = self.client.create_volume(name, labels={
...@@ -53,13 +53,13 @@ class VolumeTest(DockerClientTest): ...@@ -53,13 +53,13 @@ class VolumeTest(DockerClientTest):
{'com.example.some-label': 'some-value'} {'com.example.some-label': 'some-value'}
) )
@base.requires_api_version('1.23') @requires_api_version('1.23')
def test_create_volume_with_invalid_labels(self): def test_create_volume_with_invalid_labels(self):
name = 'perfectcherryblossom' name = 'perfectcherryblossom'
with pytest.raises(TypeError): with pytest.raises(TypeError):
self.client.create_volume(name, labels=1) self.client.create_volume(name, labels=1)
@base.requires_api_version('1.21') @requires_api_version('1.21')
def test_create_volume_with_driver(self): def test_create_volume_with_driver(self):
name = 'perfectcherryblossom' name = 'perfectcherryblossom'
driver_name = 'sshfs' driver_name = 'sshfs'
...@@ -72,7 +72,7 @@ class VolumeTest(DockerClientTest): ...@@ -72,7 +72,7 @@ class VolumeTest(DockerClientTest):
self.assertIn('Driver', data) self.assertIn('Driver', data)
self.assertEqual(data['Driver'], driver_name) self.assertEqual(data['Driver'], driver_name)
@base.requires_api_version('1.21') @requires_api_version('1.21')
def test_create_volume_invalid_opts_type(self): def test_create_volume_invalid_opts_type(self):
with pytest.raises(TypeError): with pytest.raises(TypeError):
self.client.create_volume( self.client.create_volume(
...@@ -89,7 +89,7 @@ class VolumeTest(DockerClientTest): ...@@ -89,7 +89,7 @@ class VolumeTest(DockerClientTest):
'perfectcherryblossom', driver_opts='' 'perfectcherryblossom', driver_opts=''
) )
@base.requires_api_version('1.21') @requires_api_version('1.21')
def test_inspect_volume(self): def test_inspect_volume(self):
name = 'perfectcherryblossom' name = 'perfectcherryblossom'
result = self.client.inspect_volume(name) result = self.client.inspect_volume(name)
...@@ -102,7 +102,7 @@ class VolumeTest(DockerClientTest): ...@@ -102,7 +102,7 @@ class VolumeTest(DockerClientTest):
self.assertEqual(args[0][0], 'GET') self.assertEqual(args[0][0], 'GET')
self.assertEqual(args[0][1], '{0}volumes/{1}'.format(url_prefix, name)) self.assertEqual(args[0][1], '{0}volumes/{1}'.format(url_prefix, name))
@base.requires_api_version('1.21') @requires_api_version('1.21')
def test_remove_volume(self): def test_remove_volume(self):
name = 'perfectcherryblossom' name = 'perfectcherryblossom'
self.client.remove_volume(name) self.client.remove_volume(name)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment