Unverified Kaydet (Commit) 6334312e authored tarafından Ben Firshman's avatar Ben Firshman

Split out base integration test for APIClient

So the cleanup stuff can be reused for model tests.
Signed-off-by: 's avatarBen Firshman <ben@firshman.co.uk>
üst 39900c55
...@@ -8,10 +8,10 @@ import six ...@@ -8,10 +8,10 @@ import six
from docker import errors from docker import errors
from ..helpers import requires_api_version from ..helpers import requires_api_version
from .base import BaseIntegrationTest from .base import BaseAPIIntegrationTest
class BuildTest(BaseIntegrationTest): class BuildTest(BaseAPIIntegrationTest):
def test_build_streaming(self): def test_build_streaming(self):
script = io.BytesIO('\n'.join([ script = io.BytesIO('\n'.join([
'FROM busybox', 'FROM busybox',
......
...@@ -8,10 +8,10 @@ import warnings ...@@ -8,10 +8,10 @@ import warnings
import docker import docker
from docker.utils import kwargs_from_env from docker.utils import kwargs_from_env
from .base import BaseIntegrationTest, BUSYBOX from .base import BaseAPIIntegrationTest, BUSYBOX
class InformationTest(BaseIntegrationTest): class InformationTest(BaseAPIIntegrationTest):
def test_version(self): def test_version(self):
res = self.client.version() res = self.client.version()
self.assertIn('GoVersion', res) self.assertIn('GoVersion', res)
...@@ -33,7 +33,7 @@ class InformationTest(BaseIntegrationTest): ...@@ -33,7 +33,7 @@ class InformationTest(BaseIntegrationTest):
self.assertIn('description', base_img[0]) self.assertIn('description', base_img[0])
class LinkTest(BaseIntegrationTest): class LinkTest(BaseAPIIntegrationTest):
def test_remove_link(self): def test_remove_link(self):
# Create containers # Create containers
container1 = self.client.create_container( container1 = self.client.create_container(
...@@ -75,7 +75,7 @@ class LinkTest(BaseIntegrationTest): ...@@ -75,7 +75,7 @@ class LinkTest(BaseIntegrationTest):
self.assertEqual(len(retrieved), 2) self.assertEqual(len(retrieved), 2)
class LoadConfigTest(BaseIntegrationTest): class LoadConfigTest(BaseAPIIntegrationTest):
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)
......
...@@ -11,10 +11,10 @@ import six ...@@ -11,10 +11,10 @@ import six
from ..helpers import requires_api_version from ..helpers import requires_api_version
from .. import helpers from .. import helpers
from .base import BaseIntegrationTest, BUSYBOX from .base import BaseAPIIntegrationTest, BUSYBOX
class ListContainersTest(BaseIntegrationTest): class ListContainersTest(BaseAPIIntegrationTest):
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 +34,7 @@ class ListContainersTest(BaseIntegrationTest): ...@@ -34,7 +34,7 @@ class ListContainersTest(BaseIntegrationTest):
self.assertIn('Status', retrieved) self.assertIn('Status', retrieved)
class CreateContainerTest(BaseIntegrationTest): class CreateContainerTest(BaseAPIIntegrationTest):
def test_create(self): def test_create(self):
res = self.client.create_container(BUSYBOX, 'true') res = self.client.create_container(BUSYBOX, 'true')
...@@ -409,7 +409,7 @@ class CreateContainerTest(BaseIntegrationTest): ...@@ -409,7 +409,7 @@ class CreateContainerTest(BaseIntegrationTest):
assert config['HostConfig']['Isolation'] == 'default' assert config['HostConfig']['Isolation'] == 'default'
class VolumeBindTest(BaseIntegrationTest): class VolumeBindTest(BaseAPIIntegrationTest):
def setUp(self): def setUp(self):
super(VolumeBindTest, self).setUp() super(VolumeBindTest, self).setUp()
...@@ -504,7 +504,7 @@ class VolumeBindTest(BaseIntegrationTest): ...@@ -504,7 +504,7 @@ class VolumeBindTest(BaseIntegrationTest):
@requires_api_version('1.20') @requires_api_version('1.20')
class ArchiveTest(BaseIntegrationTest): class ArchiveTest(BaseAPIIntegrationTest):
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(
...@@ -584,7 +584,7 @@ class ArchiveTest(BaseIntegrationTest): ...@@ -584,7 +584,7 @@ class ArchiveTest(BaseIntegrationTest):
self.assertIn('bar/', results) self.assertIn('bar/', results)
class RenameContainerTest(BaseIntegrationTest): class RenameContainerTest(BaseAPIIntegrationTest):
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'
...@@ -600,7 +600,7 @@ class RenameContainerTest(BaseIntegrationTest): ...@@ -600,7 +600,7 @@ class RenameContainerTest(BaseIntegrationTest):
self.assertEqual('/{0}'.format(name), inspect['Name']) self.assertEqual('/{0}'.format(name), inspect['Name'])
class StartContainerTest(BaseIntegrationTest): class StartContainerTest(BaseAPIIntegrationTest):
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)
...@@ -654,7 +654,7 @@ class StartContainerTest(BaseIntegrationTest): ...@@ -654,7 +654,7 @@ class StartContainerTest(BaseIntegrationTest):
self.assertEqual(exitcode, 0, msg=cmd) self.assertEqual(exitcode, 0, msg=cmd)
class WaitTest(BaseIntegrationTest): class WaitTest(BaseAPIIntegrationTest):
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']
...@@ -682,7 +682,7 @@ class WaitTest(BaseIntegrationTest): ...@@ -682,7 +682,7 @@ class WaitTest(BaseIntegrationTest):
self.assertEqual(inspect['State']['ExitCode'], exitcode) self.assertEqual(inspect['State']['ExitCode'], exitcode)
class LogsTest(BaseIntegrationTest): class LogsTest(BaseAPIIntegrationTest):
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(
...@@ -754,7 +754,7 @@ Line2''' ...@@ -754,7 +754,7 @@ Line2'''
self.assertEqual(logs, ''.encode(encoding='ascii')) self.assertEqual(logs, ''.encode(encoding='ascii'))
class DiffTest(BaseIntegrationTest): class DiffTest(BaseAPIIntegrationTest):
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']
...@@ -782,7 +782,7 @@ class DiffTest(BaseIntegrationTest): ...@@ -782,7 +782,7 @@ class DiffTest(BaseIntegrationTest):
self.assertEqual(test_diff[0]['Kind'], 1) self.assertEqual(test_diff[0]['Kind'], 1)
class StopTest(BaseIntegrationTest): class StopTest(BaseAPIIntegrationTest):
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']
...@@ -809,7 +809,7 @@ class StopTest(BaseIntegrationTest): ...@@ -809,7 +809,7 @@ class StopTest(BaseIntegrationTest):
self.assertEqual(state['Running'], False) self.assertEqual(state['Running'], False)
class KillTest(BaseIntegrationTest): class KillTest(BaseAPIIntegrationTest):
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']
...@@ -886,7 +886,7 @@ class KillTest(BaseIntegrationTest): ...@@ -886,7 +886,7 @@ class KillTest(BaseIntegrationTest):
self.assertEqual(state['Running'], False, state) self.assertEqual(state['Running'], False, state)
class PortTest(BaseIntegrationTest): class PortTest(BaseAPIIntegrationTest):
def test_port(self): def test_port(self):
port_bindings = { port_bindings = {
...@@ -917,7 +917,7 @@ class PortTest(BaseIntegrationTest): ...@@ -917,7 +917,7 @@ class PortTest(BaseIntegrationTest):
self.client.kill(id) self.client.kill(id)
class ContainerTopTest(BaseIntegrationTest): class ContainerTopTest(BaseAPIIntegrationTest):
def test_top(self): def test_top(self):
container = self.client.create_container( container = self.client.create_container(
BUSYBOX, ['sleep', '60'] BUSYBOX, ['sleep', '60']
...@@ -957,7 +957,7 @@ class ContainerTopTest(BaseIntegrationTest): ...@@ -957,7 +957,7 @@ class ContainerTopTest(BaseIntegrationTest):
self.assertEqual(res['Processes'][0][10], 'sleep 60') self.assertEqual(res['Processes'][0][10], 'sleep 60')
class RestartContainerTest(BaseIntegrationTest): class RestartContainerTest(BaseAPIIntegrationTest):
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']
...@@ -998,7 +998,7 @@ class RestartContainerTest(BaseIntegrationTest): ...@@ -998,7 +998,7 @@ class RestartContainerTest(BaseIntegrationTest):
self.client.kill(id) self.client.kill(id)
class RemoveContainerTest(BaseIntegrationTest): class RemoveContainerTest(BaseAPIIntegrationTest):
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']
...@@ -1020,7 +1020,7 @@ class RemoveContainerTest(BaseIntegrationTest): ...@@ -1020,7 +1020,7 @@ class RemoveContainerTest(BaseIntegrationTest):
self.assertEqual(len(res), 0) self.assertEqual(len(res), 0)
class AttachContainerTest(BaseIntegrationTest): class AttachContainerTest(BaseAPIIntegrationTest):
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)
...@@ -1051,7 +1051,7 @@ class AttachContainerTest(BaseIntegrationTest): ...@@ -1051,7 +1051,7 @@ class AttachContainerTest(BaseIntegrationTest):
self.assertEqual(data.decode('utf-8'), line) self.assertEqual(data.decode('utf-8'), line)
class PauseTest(BaseIntegrationTest): class PauseTest(BaseAPIIntegrationTest):
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']
...@@ -1080,7 +1080,7 @@ class PauseTest(BaseIntegrationTest): ...@@ -1080,7 +1080,7 @@ class PauseTest(BaseIntegrationTest):
self.assertEqual(state['Paused'], False) self.assertEqual(state['Paused'], False)
class GetContainerStatsTest(BaseIntegrationTest): class GetContainerStatsTest(BaseAPIIntegrationTest):
@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(
...@@ -1111,7 +1111,7 @@ class GetContainerStatsTest(BaseIntegrationTest): ...@@ -1111,7 +1111,7 @@ class GetContainerStatsTest(BaseIntegrationTest):
self.assertIn(key, chunk) self.assertIn(key, chunk)
class ContainerUpdateTest(BaseIntegrationTest): class ContainerUpdateTest(BaseAPIIntegrationTest):
@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
...@@ -1158,7 +1158,7 @@ class ContainerUpdateTest(BaseIntegrationTest): ...@@ -1158,7 +1158,7 @@ class ContainerUpdateTest(BaseIntegrationTest):
) )
class ContainerCPUTest(BaseIntegrationTest): class ContainerCPUTest(BaseAPIIntegrationTest):
@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.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 .base import BaseIntegrationTest, BUSYBOX from .base import BaseAPIIntegrationTest, BUSYBOX
class ExecTest(BaseIntegrationTest): class ExecTest(BaseAPIIntegrationTest):
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,10 +14,10 @@ from six.moves import socketserver ...@@ -14,10 +14,10 @@ from six.moves import socketserver
import docker import docker
from .base import BaseIntegrationTest, BUSYBOX from .base import BaseAPIIntegrationTest, BUSYBOX
class ListImagesTest(BaseIntegrationTest): class ListImagesTest(BaseAPIIntegrationTest):
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])
...@@ -35,7 +35,7 @@ class ListImagesTest(BaseIntegrationTest): ...@@ -35,7 +35,7 @@ class ListImagesTest(BaseIntegrationTest):
self.assertEqual(type(res1[0]), six.text_type) self.assertEqual(type(res1[0]), six.text_type)
class PullImageTest(BaseIntegrationTest): class PullImageTest(BaseAPIIntegrationTest):
def test_pull(self): def test_pull(self):
try: try:
self.client.remove_image('hello-world') self.client.remove_image('hello-world')
...@@ -66,7 +66,7 @@ class PullImageTest(BaseIntegrationTest): ...@@ -66,7 +66,7 @@ class PullImageTest(BaseIntegrationTest):
self.assertIn('Id', img_info) self.assertIn('Id', img_info)
class CommitTest(BaseIntegrationTest): class CommitTest(BaseAPIIntegrationTest):
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']
...@@ -101,7 +101,7 @@ class CommitTest(BaseIntegrationTest): ...@@ -101,7 +101,7 @@ class CommitTest(BaseIntegrationTest):
assert img['Config']['Cmd'] == ['bash'] assert img['Config']['Cmd'] == ['bash']
class RemoveImageTest(BaseIntegrationTest): class RemoveImageTest(BaseAPIIntegrationTest):
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']
...@@ -117,7 +117,7 @@ class RemoveImageTest(BaseIntegrationTest): ...@@ -117,7 +117,7 @@ class RemoveImageTest(BaseIntegrationTest):
self.assertEqual(len(res), 0) self.assertEqual(len(res), 0)
class ImportImageTest(BaseIntegrationTest): class ImportImageTest(BaseAPIIntegrationTest):
'''Base class for `docker import` test cases.''' '''Base class for `docker import` test cases.'''
TAR_SIZE = 512 * 1024 TAR_SIZE = 512 * 1024
......
...@@ -6,10 +6,10 @@ from docker.utils import create_ipam_pool ...@@ -6,10 +6,10 @@ from docker.utils import create_ipam_pool
import pytest import pytest
from ..helpers import requires_api_version from ..helpers import requires_api_version
from .base import BaseIntegrationTest from .base import BaseAPIIntegrationTest
class TestNetworks(BaseIntegrationTest): class TestNetworks(BaseAPIIntegrationTest):
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']
......
...@@ -3,10 +3,10 @@ import random ...@@ -3,10 +3,10 @@ import random
import docker import docker
from ..helpers import requires_api_version from ..helpers import requires_api_version
from .base import BaseIntegrationTest from .base import BaseAPIIntegrationTest
class ServiceTest(BaseIntegrationTest): class ServiceTest(BaseAPIIntegrationTest):
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)
......
...@@ -3,10 +3,10 @@ import docker ...@@ -3,10 +3,10 @@ import docker
import pytest import pytest
from ..helpers import requires_api_version from ..helpers import requires_api_version
from .base import BaseIntegrationTest from .base import BaseAPIIntegrationTest
class SwarmTest(BaseIntegrationTest): class SwarmTest(BaseAPIIntegrationTest):
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)
......
...@@ -2,11 +2,11 @@ import docker ...@@ -2,11 +2,11 @@ import docker
import pytest import pytest
from ..helpers import requires_api_version from ..helpers import requires_api_version
from .base import BaseIntegrationTest from .base import BaseAPIIntegrationTest
@requires_api_version('1.21') @requires_api_version('1.21')
class TestVolumes(BaseIntegrationTest): class TestVolumes(BaseAPIIntegrationTest):
def test_create_volume(self): def test_create_volume(self):
name = 'perfectcherryblossom' name = 'perfectcherryblossom'
self.tmp_volumes.append(name) self.tmp_volumes.append(name)
......
...@@ -11,20 +11,14 @@ BUSYBOX = 'busybox:buildroot-2014.02' ...@@ -11,20 +11,14 @@ BUSYBOX = 'busybox:buildroot-2014.02'
class BaseIntegrationTest(unittest.TestCase): class BaseIntegrationTest(unittest.TestCase):
""" """
A base class for integration test cases. A base class for integration test cases. It cleans up the Docker server
after itself.
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): def setUp(self):
if six.PY2: if six.PY2:
self.assertRegex = self.assertRegexpMatches self.assertRegex = self.assertRegexpMatches
self.assertCountEqual = self.assertItemsEqual self.assertCountEqual = self.assertItemsEqual
self.client = docker.APIClient(timeout=60, **kwargs_from_env())
self.tmp_imgs = [] self.tmp_imgs = []
self.tmp_containers = [] self.tmp_containers = []
self.tmp_folders = [] self.tmp_folders = []
...@@ -32,32 +26,41 @@ class BaseIntegrationTest(unittest.TestCase): ...@@ -32,32 +26,41 @@ class BaseIntegrationTest(unittest.TestCase):
self.tmp_networks = [] self.tmp_networks = []
def tearDown(self): def tearDown(self):
client = docker.from_env()
for img in self.tmp_imgs: for img in self.tmp_imgs:
try: try:
self.client.remove_image(img) client.api.remove_image(img)
except docker.errors.APIError: except docker.errors.APIError:
pass pass
for container in self.tmp_containers: for container in self.tmp_containers:
try: try:
self.client.stop(container, timeout=1) client.api.remove_container(container, force=True)
self.client.remove_container(container)
except docker.errors.APIError: except docker.errors.APIError:
pass pass
for network in self.tmp_networks: for network in self.tmp_networks:
try: try:
self.client.remove_network(network) client.api.remove_network(network)
except docker.errors.APIError: except docker.errors.APIError:
pass pass
for folder in self.tmp_folders:
shutil.rmtree(folder)
for volume in self.tmp_volumes: for volume in self.tmp_volumes:
try: try:
self.client.remove_volume(volume) client.api.remove_volume(volume)
except docker.errors.APIError: except docker.errors.APIError:
pass pass
self.client.close() for folder in self.tmp_folders:
shutil.rmtree(folder)
class BaseAPIIntegrationTest(BaseIntegrationTest):
"""
A test case for `APIClient` integration tests. It sets up an `APIClient`
as `self.client`.
"""
def setUp(self):
super(BaseAPIIntegrationTest, self).setUp()
self.client = docker.APIClient(timeout=60, **kwargs_from_env())
def run_container(self, *args, **kwargs): def run_container(self, *args, **kwargs):
container = self.client.create_container(*args, **kwargs) container = self.client.create_container(*args, **kwargs)
......
from docker.errors import APIError from docker.errors import APIError
from .base import BaseIntegrationTest, BUSYBOX from .base import BaseAPIIntegrationTest, BUSYBOX
class ErrorsTest(BaseIntegrationTest): class ErrorsTest(BaseAPIIntegrationTest):
def test_api_error_parses_json(self): def test_api_error_parses_json(self):
container = self.client.create_container(BUSYBOX, ['sleep', '10']) container = self.client.create_container(BUSYBOX, ['sleep', '10'])
self.client.start(container['Id']) self.client.start(container['Id'])
......
...@@ -4,10 +4,10 @@ import random ...@@ -4,10 +4,10 @@ import random
import docker import docker
import six import six
from .base import BaseIntegrationTest, BUSYBOX from .base import BaseAPIIntegrationTest, BUSYBOX
class TestRegressions(BaseIntegrationTest): class TestRegressions(BaseAPIIntegrationTest):
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:
......
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