From 8a58a7aecc02ddc47ce81f77f651a12b23053461 Mon Sep 17 00:00:00 2001 From: Lourens Veen Date: Sun, 10 Feb 2019 21:35:01 +0100 Subject: [PATCH] Move fixtures to conftest.py --- tests/clean_up.py | 17 ---------- tests/conftest.py | 77 +++++++++++++++++++++++++++++++++++++++++++ tests/fixtures.py | 26 --------------- tests/test_service.py | 61 +++++----------------------------- 4 files changed, 85 insertions(+), 96 deletions(-) delete mode 100644 tests/clean_up.py create mode 100644 tests/conftest.py delete mode 100644 tests/fixtures.py diff --git a/tests/clean_up.py b/tests/clean_up.py deleted file mode 100644 index e9023ac..0000000 --- a/tests/clean_up.py +++ /dev/null @@ -1,17 +0,0 @@ -import docker - -def clean_up_service(srv_name): - dc = docker.from_env() - try: - test_srv = dc.containers.get(srv_name) - test_srv.stop() - test_srv.remove() - except docker.errors.NotFound: - pass - -# Clean up any mess left over from previous failed tests. -def clean_up(): - clean_up_service('cerise_manager_test_service') - clean_up_service('cerise_manager_test_service2') - - diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..ee0e2db --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,77 @@ +import docker +import pytest +import random +import time + +import cerise_manager.service as cs + + +@pytest.fixture(scope='session') +def docker_client(): + return docker.from_env() + + +def clean_up_service(docker_client, srv_name): + try: + test_srv = docker_client.containers.get(srv_name) + test_srv.stop() + test_srv.remove() + except docker.errors.NotFound: + pass + + +@pytest.fixture(scope='session') +def clean_up(docker_client): + clean_up_service(docker_client, 'cerise_manager_test_service') + clean_up_service(docker_client, 'cerise_manager_test_service2') + clean_up_service(docker_client, 'cerise_manager_test_service3') + + +@pytest.fixture(scope='session') +def test_image(clean_up): + """Get a plain cerise image for testing. + + Ignores errors; we may have a local image available already, + in which case we want to continue, otherwise the other + tests will fail. + """ + docker_client = docker.from_env() + try: + docker_client.images.pull('mdstudio/cerise:develop') + except docker.errors.APIError: + pass + return 'mdstudio/cerise:develop' + + +def wait_for_status(container, status): + while container.status != status: + time.sleep(0.05) + container.reload() + + +@pytest.fixture(scope='session') +def test_port(): + return 29593 + random.randrange(100) + + +@pytest.fixture(scope='session') +def test_container(request, test_image, test_port, docker_client): + image = docker_client.images.get(test_image) + + container = docker_client.containers.run( + image, + name='cerise_manager_test_service', + ports={'29593/tcp': ('127.0.0.1', test_port) }, + detach=True) + + wait_for_status(container, 'running') + + yield container + + container.stop() + container.remove() + + +@pytest.fixture(scope='session') +def test_service(test_container): + return cs.get_service('cerise_manager_test_service') diff --git a/tests/fixtures.py b/tests/fixtures.py deleted file mode 100644 index 50c415f..0000000 --- a/tests/fixtures.py +++ /dev/null @@ -1,26 +0,0 @@ -import docker -import os -import pytest -import requests -import time - -import cerise_manager.service as cs - -@pytest.fixture(scope='session') -def docker_client(request): - return docker.from_env() - -@pytest.fixture(scope='session') -def test_image(request): - """Get a plain cerise image for testing. - - Ignores errors; we may have a local image available already, - in which case we want to continue, otherwise the other - tests will fail. - """ - docker_client = docker.from_env() - try: - docker_client.images.pull('mdstudio/cerise:develop') - except docker.errors.APIError: - pass - return 'mdstudio/cerise:develop' diff --git a/tests/test_service.py b/tests/test_service.py index 769c32a..c762b09 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -1,57 +1,12 @@ import docker import json -import os import pytest -import random -import requests -import sys import time from unittest.mock import patch - -# clean up any mess left over from previous failed tests -from .clean_up import clean_up -clean_up() - import cerise_manager.service as cs import cerise_manager.errors as ce -from .clean_up import clean_up_service - -from .fixtures import docker_client, test_image - - -def wait_for_status(container, status): - while container.status != status: - time.sleep(0.05) - container.reload() - - -@pytest.fixture(scope='session') -def test_port(): - return 29593 + random.randrange(100) - - -@pytest.fixture(scope='session') -def test_container(request, test_image, test_port, docker_client): - image = docker_client.images.get(test_image) - - container = docker_client.containers.run( - image, - name='cerise_manager_test_service', - ports={'29593/tcp': ('127.0.0.1', test_port) }, - detach=True) - - wait_for_status(container, 'running') - - yield container - - container.stop() - container.remove() - - -@pytest.fixture(scope='session') -def test_service(test_container): - return cs.get_service('cerise_manager_test_service') +from .conftest import clean_up_service, wait_for_status def test_service_exists(test_container): @@ -86,28 +41,28 @@ def test_create_service(docker_client): srv = cs.create_service('cerise_manager_test_service2', 'mdstudio/cerise:develop') assert isinstance(srv, cs.ManagedService) - clean_up_service('cerise_manager_test_service2') + clean_up_service(docker_client, 'cerise_manager_test_service2') def test_create_existing_service(test_container): with pytest.raises(ce.ServiceAlreadyExists): cs.create_service('cerise_manager_test_service', 'mdstudio/cerise:develop') -def test_create_service_port_occupied(test_container, test_port): +def test_create_service_port_occupied(docker_client, test_container, test_port): with pytest.raises(ce.PortNotAvailable): cs.create_service('cerise_manager_test_service2', 'mdstudio/cerise:develop', test_port) - clean_up_service('cerise_manager_test_service2') + clean_up_service(docker_client, 'cerise_manager_test_service2') -def test_create_service_auto_port_occupied(): +def test_create_service_auto_port_occupied(docker_client): with patch('cerise_manager.service._RAND_RANGE', 1): srv0 = cs.create_service('cerise_manager_test_service2', 'mdstudio/cerise:develop') srv1 = cs.create_service('cerise_manager_test_service3', 'mdstudio/cerise:develop') assert srv0._port != srv1._port - clean_up_service('cerise_manager_test_service2') - clean_up_service('cerise_manager_test_service3') + clean_up_service(docker_client, 'cerise_manager_test_service2') + clean_up_service(docker_client, 'cerise_manager_test_service3') def test_create_two_services_and_destroy(test_container, test_port, docker_client): srv = cs.create_service('cerise_manager_test_service2', @@ -132,7 +87,7 @@ def test_require_service(docker_client): srv = cs.require_service('cerise_manager_test_service2', 'mdstudio/cerise:develop') assert isinstance(srv, cs.ManagedService) - clean_up_service('cerise_manager_test_service2') + clean_up_service(docker_client, 'cerise_manager_test_service2') def test_require_existing_service(docker_client, test_container, test_port): srv = cs.require_service('cerise_manager_test_service',