This module contains tests of the 'orchestration' of dependent docker units. If a container depends on an image and a container, and that container depends on another container, and so on, then all the contains should be built in the right order. ''' from pytest_docker_tools import build, container, fetch, network, volume redis_image = fetch(repository='redis:latest') redis0 = container(image='{redis_image.id}', environment={ 'MARKER': 'redis0-0sider', }) foobar = build(path='tests/integration') mynetwork = network() myvolume = volume() mycontainer = container( image='{foobar.id}', network='{mynetwork.id}', volumes={ '{myvolume.id}': { 'bind': '/var/tmp' }, }, environment={ 'REDIS_IP': lambda redis0: redis0.ips.primary, }, dns=['{redis0.ips.primary}'], )
import os from http.client import HTTPConnection import pytest from pytest_docker_tools import build, container fakedns_image = build(path=os.path.join(os.path.dirname(__file__), 'dns'), ) fakedns = container(image='{fakedns_image.id}', environment={ 'DNS_EXAMPLE_COM__A': '127.0.0.1', }) apiserver_image = build(path=os.path.join(os.path.dirname(__file__), 'api'), ) apiserver = container(image='{apiserver_image.id}', ports={ '8080/tcp': None, }, dns=['{fakedns.ips.primary}']) @pytest.fixture def apiclient(apiserver): port = apiserver.ports['8080/tcp'][0] return HTTPConnection(f'localhost:{port}')
from http.client import HTTPConnection import pytest from pytest_docker_tools import fetch, build, container #myhost = "192.168.1.104" my_test_backend_app_image = build( path='/root/app-deploy', ) my_test_backend_app = container( image='{my_test_backend_app_image.id}', ports={ '8000/tcp': '9000/tcp', } ) my_test_redis_server_image = fetch(repository='redis:latest') my_test_redis_server = container( image='{my_test_redis_server_image.id}', ) @pytest.mark.parametrize("i", list(range(100))) def test_redist(i, my_test_redis_server): assert my_test_redis_server.status == "running" @pytest.mark.parametrize("i", list(range(100))) def test_app(i, my_test_backend_app): assert my_test_backend_app.status == "running"
# override again def cleanup(): nbexchange.stop() NbExchange.clear_instance() request.addfinalizer(cleanup) # convenience for accessing nbexchange in tests nbexchange.url = f"http://127.0.0.1:{nbexchange.port}{nbexchange.base_url}".rstrip( "/" ) return nbexchange @pytest.fixture(scope="session") def db(): """Get a db session""" _db = Session() # user = nbexchange.models.users.User( name=getuser(), org_id=1 ) # TODO: remove Magic number _db.add(user) _db.commit() return _db # Docker images nbexchange_image = build(path=".") container = container(image="{nbexchange_image.id}", ports={"9000/tcp": None})
from pytest_docker_tools import container, build import requests vmware_exporter_image = build(path='.') vmware_exporter = container( image='{vmware_exporter_image.id}', ports={ '9272/tcp': None, }, ) def test_container_starts(vmware_exporter): container_addr = vmware_exporter.get_addr('9272/tcp') assert requests.get( 'http://{}:{}/healthz'.format(*container_addr)).status_code == 200 def test_container_404(vmware_exporter): container_addr = vmware_exporter.get_addr('9272/tcp') assert requests.get( 'http://{}:{}/meetrics'.format(*container_addr)).status_code == 404
from http.client import HTTPConnection import pytest from pytest_docker_tools import build, container import testinfra from fastapi.testclient import TestClient from galaxy_exporter.galaxy_exporter import app TEST_COLLECTION = 'community.kubernetes' TEST_ROLE = 'mesaguy.prometheus' client = TestClient(app) galaxy_exporter_image = build( nocache=False, scope='session', path='.', ) galaxy_exporter_container = container(image='{galaxy_exporter_image.id}', scope='session', ports={ '9654/tcp': None, }) @pytest.fixture(scope='session') def galaxy_exporter_client(galaxy_exporter_container): port = galaxy_exporter_container.ports['9654/tcp'][0] return HTTPConnection(f'localhost:{port}')
on another container, and so on, then all the contains should be built in the right order. """ from pytest_docker_tools import build, container, fetch, network, volume redis_image = fetch(repository="redis:latest") redis0 = container( image="{redis_image.id}", environment={ "MARKER": "redis0-0sider", }, ) foobar = build(path="tests/integration") mynetwork = network() myvolume = volume() mycontainer = container( image="{foobar.id}", network="{mynetwork.id}", volumes={ "{myvolume.id}": { "bind": "/var/tmp" }, }, environment={ "REDIS_IP": lambda redis0: redis0.ips.primary, }, dns=["{redis0.ips.primary}"], )
import socket from pytest_docker_tools import build, container, fetch from pytest_docker_tools.utils import wait_for_callable test_container_1_image = fetch(repository='redis:latest') test_container_1 = container( image='{test_container_1_image.id}', ports={ '6379/tcp': None, }, ) ipv6_folder = os.path.join(os.path.dirname(__file__), 'fixtures/ipv6') ipv6_image = build(path=ipv6_folder) ipv6 = container(image='{ipv6_image.id}', ports={ '1234/udp': None, }) def test_container_created(docker_client, test_container_1): for c in docker_client.containers.list(ignore_removed=True): if c.id == test_container_1.id: # Looks like we managed to start one! break else: assert False, 'Looks like we failed to start a container' def test_container_ipv6(ipv6):
import ipaddress import time import requests from prometheus_client.parser import text_string_to_metric_families from pytest_docker_tools import build, container tshark_exporter_image = build(path='.') tshark_exporter = container( image='{tshark_exporter_image.id}', ports={ '9431/tcp': None, }, ) def test_metrics_server_responds_immediately(tshark_exporter): port = tshark_exporter.ports['9431/tcp'][0] response = requests.get(f'http://localhost:{port}/metrics') assert response.status_code == 200 assert b'# HELP tshark_exporter_match_bytes' in response.content assert b'# HELP tshark_exporter_match_count' in response.content def test_simple_capture(tshark_exporter): port = tshark_exporter.ports['9431/tcp'][0] while 'Capturing on \'eth0\'' not in tshark_exporter.logs(): time.sleep(0.5)
import functools from pathlib import Path import pytest from pytest_docker_tools import build import lm_zoo as Z # Fixture: lm-zoo template image (good for fast testing) build_context = Path(__name__).parent template_dockerfile = build_context / "models" / "_template" / "Dockerfile" template_image = build(path=str(build_context), dockerfile=str(template_dockerfile), rm=True, tag="lmzoo-template") @pytest.fixture(scope="session") def singularity_template_image(): path = Path(__file__).parent / "lmzoo-template.sif" if not path.exists(): pytest.xfail("Missing singularity image") return path @pytest.fixture(scope="session") def registry(): return Z.get_registry() @pytest.fixture(scope="session")
postgres_container = container( environment=dict( POSTGRES_USER=constants.POSTGRES_USER, POSTGRES_PASSWORD=constants.POSTGRES_PASSWORD, POSTGRES_DB=constants.POSTGRES_DB, ), image="postgres:14.1", restart_policy={"Name": "on-failure"}, scope="class", timeout=constants.READINESS_TIMEOUT_SECONDS, wrapper_class=PostgresContainer, ) qpc_server_image = build( path=constants.PROJECT_ROOT_DIR.as_posix(), rm=constants.CLEANUP_DOCKER_LAYERS, forcerm=constants.CLEANUP_DOCKER_LAYERS, ) qpc_server_container = container( environment=dict( ANSIBLE_LOG_LEVEL=constants.QPC_ANSIBLE_LOG_LEVEL, DJANGO_LOG_LEVEL=constants.QUIPUCORDS_LOG_LEVEL, QPC_DBMS="postgres", QPC_DBMS_DATABASE=constants.POSTGRES_DB, QPC_DBMS_HOST="{postgres_container.ips.primary}", QPC_DBMS_PASSWORD=constants.POSTGRES_PASSWORD, QPC_DBMS_USER=constants.POSTGRES_USER, QPC_SERVER_PASSWORD=constants.QPC_SERVER_PASSWORD, QPC_SERVER_USERNAME=constants.QPC_SERVER_USERNAME, QUIPUCORDS_COMMIT=constants.QPC_COMMIT, QUIPUCORDS_LOG_LEVEL=constants.QUIPUCORDS_LOG_LEVEL,
import os from pytest_docker_tools import build, container def get_base_path(): root_path = os.getcwd() print(root_path) if 'test_integration' in root_path: return '../' return '.' my_clai_installed_image = build( path=get_base_path(), dockerfile='./test_integration/docker/centos/Dockerfile') my_clai_module = container(image='{my_clai_installed_image.id}', scope='module') def pytest_generate_tests(metafunc): if "command" in metafunc.fixturenames: commands = getattr(metafunc.cls, 'get_commands_to_execute')(metafunc.cls) commands_expected = getattr(metafunc.cls, 'get_commands_expected')(metafunc.cls) metafunc.parametrize(["command", 'command_expected'], list(zip(commands, commands_expected)))