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
from docker import errors
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):
script = io.BytesIO('\n'.join([
'FROM busybox',
......
......@@ -8,10 +8,10 @@ import warnings
import docker
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):
res = self.client.version()
self.assertIn('GoVersion', res)
......@@ -33,7 +33,7 @@ class InformationTest(BaseIntegrationTest):
self.assertIn('description', base_img[0])
class LinkTest(BaseIntegrationTest):
class LinkTest(BaseAPIIntegrationTest):
def test_remove_link(self):
# Create containers
container1 = self.client.create_container(
......@@ -75,7 +75,7 @@ class LinkTest(BaseIntegrationTest):
self.assertEqual(len(retrieved), 2)
class LoadConfigTest(BaseIntegrationTest):
class LoadConfigTest(BaseAPIIntegrationTest):
def test_load_legacy_config(self):
folder = tempfile.mkdtemp()
self.tmp_folders.append(folder)
......
......@@ -11,10 +11,10 @@ import six
from ..helpers import requires_api_version
from .. import helpers
from .base import BaseIntegrationTest, BUSYBOX
from .base import BaseAPIIntegrationTest, BUSYBOX
class ListContainersTest(BaseIntegrationTest):
class ListContainersTest(BaseAPIIntegrationTest):
def test_list_containers(self):
res0 = self.client.containers(all=True)
size = len(res0)
......@@ -34,7 +34,7 @@ class ListContainersTest(BaseIntegrationTest):
self.assertIn('Status', retrieved)
class CreateContainerTest(BaseIntegrationTest):
class CreateContainerTest(BaseAPIIntegrationTest):
def test_create(self):
res = self.client.create_container(BUSYBOX, 'true')
......@@ -409,7 +409,7 @@ class CreateContainerTest(BaseIntegrationTest):
assert config['HostConfig']['Isolation'] == 'default'
class VolumeBindTest(BaseIntegrationTest):
class VolumeBindTest(BaseAPIIntegrationTest):
def setUp(self):
super(VolumeBindTest, self).setUp()
......@@ -504,7 +504,7 @@ class VolumeBindTest(BaseIntegrationTest):
@requires_api_version('1.20')
class ArchiveTest(BaseIntegrationTest):
class ArchiveTest(BaseAPIIntegrationTest):
def test_get_file_archive_from_container(self):
data = 'The Maid and the Pocket Watch of Blood'
ctnr = self.client.create_container(
......@@ -584,7 +584,7 @@ class ArchiveTest(BaseIntegrationTest):
self.assertIn('bar/', results)
class RenameContainerTest(BaseIntegrationTest):
class RenameContainerTest(BaseAPIIntegrationTest):
def test_rename_container(self):
version = self.client.version()['Version']
name = 'hong_meiling'
......@@ -600,7 +600,7 @@ class RenameContainerTest(BaseIntegrationTest):
self.assertEqual('/{0}'.format(name), inspect['Name'])
class StartContainerTest(BaseIntegrationTest):
class StartContainerTest(BaseAPIIntegrationTest):
def test_start_container(self):
res = self.client.create_container(BUSYBOX, 'true')
self.assertIn('Id', res)
......@@ -654,7 +654,7 @@ class StartContainerTest(BaseIntegrationTest):
self.assertEqual(exitcode, 0, msg=cmd)
class WaitTest(BaseIntegrationTest):
class WaitTest(BaseAPIIntegrationTest):
def test_wait(self):
res = self.client.create_container(BUSYBOX, ['sleep', '3'])
id = res['Id']
......@@ -682,7 +682,7 @@ class WaitTest(BaseIntegrationTest):
self.assertEqual(inspect['State']['ExitCode'], exitcode)
class LogsTest(BaseIntegrationTest):
class LogsTest(BaseAPIIntegrationTest):
def test_logs(self):
snippet = 'Flowering Nights (Sakuya Iyazoi)'
container = self.client.create_container(
......@@ -754,7 +754,7 @@ Line2'''
self.assertEqual(logs, ''.encode(encoding='ascii'))
class DiffTest(BaseIntegrationTest):
class DiffTest(BaseAPIIntegrationTest):
def test_diff(self):
container = self.client.create_container(BUSYBOX, ['touch', '/test'])
id = container['Id']
......@@ -782,7 +782,7 @@ class DiffTest(BaseIntegrationTest):
self.assertEqual(test_diff[0]['Kind'], 1)
class StopTest(BaseIntegrationTest):
class StopTest(BaseAPIIntegrationTest):
def test_stop(self):
container = self.client.create_container(BUSYBOX, ['sleep', '9999'])
id = container['Id']
......@@ -809,7 +809,7 @@ class StopTest(BaseIntegrationTest):
self.assertEqual(state['Running'], False)
class KillTest(BaseIntegrationTest):
class KillTest(BaseAPIIntegrationTest):
def test_kill(self):
container = self.client.create_container(BUSYBOX, ['sleep', '9999'])
id = container['Id']
......@@ -886,7 +886,7 @@ class KillTest(BaseIntegrationTest):
self.assertEqual(state['Running'], False, state)
class PortTest(BaseIntegrationTest):
class PortTest(BaseAPIIntegrationTest):
def test_port(self):
port_bindings = {
......@@ -917,7 +917,7 @@ class PortTest(BaseIntegrationTest):
self.client.kill(id)
class ContainerTopTest(BaseIntegrationTest):
class ContainerTopTest(BaseAPIIntegrationTest):
def test_top(self):
container = self.client.create_container(
BUSYBOX, ['sleep', '60']
......@@ -957,7 +957,7 @@ class ContainerTopTest(BaseIntegrationTest):
self.assertEqual(res['Processes'][0][10], 'sleep 60')
class RestartContainerTest(BaseIntegrationTest):
class RestartContainerTest(BaseAPIIntegrationTest):
def test_restart(self):
container = self.client.create_container(BUSYBOX, ['sleep', '9999'])
id = container['Id']
......@@ -998,7 +998,7 @@ class RestartContainerTest(BaseIntegrationTest):
self.client.kill(id)
class RemoveContainerTest(BaseIntegrationTest):
class RemoveContainerTest(BaseAPIIntegrationTest):
def test_remove(self):
container = self.client.create_container(BUSYBOX, ['true'])
id = container['Id']
......@@ -1020,7 +1020,7 @@ class RemoveContainerTest(BaseIntegrationTest):
self.assertEqual(len(res), 0)
class AttachContainerTest(BaseIntegrationTest):
class AttachContainerTest(BaseAPIIntegrationTest):
def test_run_container_streaming(self):
container = self.client.create_container(BUSYBOX, '/bin/sh',
detach=True, stdin_open=True)
......@@ -1051,7 +1051,7 @@ class AttachContainerTest(BaseIntegrationTest):
self.assertEqual(data.decode('utf-8'), line)
class PauseTest(BaseIntegrationTest):
class PauseTest(BaseAPIIntegrationTest):
def test_pause_unpause(self):
container = self.client.create_container(BUSYBOX, ['sleep', '9999'])
id = container['Id']
......@@ -1080,7 +1080,7 @@ class PauseTest(BaseIntegrationTest):
self.assertEqual(state['Paused'], False)
class GetContainerStatsTest(BaseIntegrationTest):
class GetContainerStatsTest(BaseAPIIntegrationTest):
@requires_api_version('1.19')
def test_get_container_stats_no_stream(self):
container = self.client.create_container(
......@@ -1111,7 +1111,7 @@ class GetContainerStatsTest(BaseIntegrationTest):
self.assertIn(key, chunk)
class ContainerUpdateTest(BaseIntegrationTest):
class ContainerUpdateTest(BaseAPIIntegrationTest):
@requires_api_version('1.22')
def test_update_container(self):
old_mem_limit = 400 * 1024 * 1024
......@@ -1158,7 +1158,7 @@ class ContainerUpdateTest(BaseIntegrationTest):
)
class ContainerCPUTest(BaseIntegrationTest):
class ContainerCPUTest(BaseAPIIntegrationTest):
@requires_api_version('1.18')
def test_container_cpu_shares(self):
cpu_shares = 512
......
from docker.utils.socket import next_frame_size
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):
container = self.client.create_container(BUSYBOX, 'cat',
detach=True, stdin_open=True)
......
......@@ -14,10 +14,10 @@ from six.moves import socketserver
import docker
from .base import BaseIntegrationTest, BUSYBOX
from .base import BaseAPIIntegrationTest, BUSYBOX
class ListImagesTest(BaseIntegrationTest):
class ListImagesTest(BaseAPIIntegrationTest):
def test_images(self):
res1 = self.client.images(all=True)
self.assertIn('Id', res1[0])
......@@ -35,7 +35,7 @@ class ListImagesTest(BaseIntegrationTest):
self.assertEqual(type(res1[0]), six.text_type)
class PullImageTest(BaseIntegrationTest):
class PullImageTest(BaseAPIIntegrationTest):
def test_pull(self):
try:
self.client.remove_image('hello-world')
......@@ -66,7 +66,7 @@ class PullImageTest(BaseIntegrationTest):
self.assertIn('Id', img_info)
class CommitTest(BaseIntegrationTest):
class CommitTest(BaseAPIIntegrationTest):
def test_commit(self):
container = self.client.create_container(BUSYBOX, ['touch', '/test'])
id = container['Id']
......@@ -101,7 +101,7 @@ class CommitTest(BaseIntegrationTest):
assert img['Config']['Cmd'] == ['bash']
class RemoveImageTest(BaseIntegrationTest):
class RemoveImageTest(BaseAPIIntegrationTest):
def test_remove(self):
container = self.client.create_container(BUSYBOX, ['touch', '/test'])
id = container['Id']
......@@ -117,7 +117,7 @@ class RemoveImageTest(BaseIntegrationTest):
self.assertEqual(len(res), 0)
class ImportImageTest(BaseIntegrationTest):
class ImportImageTest(BaseAPIIntegrationTest):
'''Base class for `docker import` test cases.'''
TAR_SIZE = 512 * 1024
......
......@@ -6,10 +6,10 @@ from docker.utils import create_ipam_pool
import pytest
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):
net_name = u'dockerpy{}'.format(random.getrandbits(24))[:14]
net_id = self.client.create_network(net_name, *args, **kwargs)['Id']
......
......@@ -3,10 +3,10 @@ import random
import docker
from ..helpers import requires_api_version
from .base import BaseIntegrationTest
from .base import BaseAPIIntegrationTest
class ServiceTest(BaseIntegrationTest):
class ServiceTest(BaseAPIIntegrationTest):
def setUp(self):
super(ServiceTest, self).setUp()
self.client.leave_swarm(force=True)
......
......@@ -3,10 +3,10 @@ import docker
import pytest
from ..helpers import requires_api_version
from .base import BaseIntegrationTest
from .base import BaseAPIIntegrationTest
class SwarmTest(BaseIntegrationTest):
class SwarmTest(BaseAPIIntegrationTest):
def setUp(self):
super(SwarmTest, self).setUp()
self.client.leave_swarm(force=True)
......
......@@ -2,11 +2,11 @@ import docker
import pytest
from ..helpers import requires_api_version
from .base import BaseIntegrationTest
from .base import BaseAPIIntegrationTest
@requires_api_version('1.21')
class TestVolumes(BaseIntegrationTest):
class TestVolumes(BaseAPIIntegrationTest):
def test_create_volume(self):
name = 'perfectcherryblossom'
self.tmp_volumes.append(name)
......
......@@ -11,20 +11,14 @@ 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.
A base class for integration test cases. It 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.APIClient(timeout=60, **kwargs_from_env())
self.tmp_imgs = []
self.tmp_containers = []
self.tmp_folders = []
......@@ -32,32 +26,41 @@ class BaseIntegrationTest(unittest.TestCase):
self.tmp_networks = []
def tearDown(self):
client = docker.from_env()
for img in self.tmp_imgs:
try:
self.client.remove_image(img)
client.api.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)
client.api.remove_container(container, force=True)
except docker.errors.APIError:
pass
for network in self.tmp_networks:
try:
self.client.remove_network(network)
client.api.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)
client.api.remove_volume(volume)
except docker.errors.APIError:
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):
container = self.client.create_container(*args, **kwargs)
......
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):
container = self.client.create_container(BUSYBOX, ['sleep', '10'])
self.client.start(container['Id'])
......
......@@ -4,10 +4,10 @@ import random
import docker
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):
dfile = io.BytesIO()
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