PKLl  pytest_docker_tools/__init__.py''' A set of tools for creating declarative docker py.test integration fixtures. ''' from . import factories from .utils import wait_for_callable __version__ = '0.0.2' __all__ = [ 'factories', 'get_files', 'wait_for_callable', 'wait_for_port', ] PKL!pytest_docker_tools/plugin.pyimport docker import pytest @pytest.fixture(scope='session') def docker_client(request): return docker.from_env() @pytest.hookimpl(tryfirst=True, hookwrapper=True) def pytest_runtest_makereport(item, call): ''' This hook allows Docker containers to contribute their logs to the py.test report. ''' outcome = yield rep = outcome.get_result() if rep.when != 'call': return if not rep.failed: return for name, fixture in item.funcargs.items(): if isinstance(fixture, dict) and 'container' in fixture: container = fixture['container'] rep.sections.append(( name + ': ' + container.name, container.logs().decode('utf-8'), )) PK\L`͵pytest_docker_tools/utils.pyimport sys import time def wait_for_callable(message, callable, timeout=30): ''' Runs a callable once a second until it returns True or we hit the timeout. ''' sys.stdout.write(message) try: for i in range(timeout): sys.stdout.write('.') sys.stdout.flush() if callable(): return time.sleep(1) finally: sys.stdout.write('\n') raise RuntimeError('Timeout exceeded') PKJL{A)pytest_docker_tools/factories/__init__.pyfrom .container import container from .image import image from .network import network from .repository_image import repository_image from .volume import volume __all__ = [ 'container', 'image', 'network', 'repository_image', 'volume', ] PKLY$g##*pytest_docker_tools/factories/container.pyimport inspect from string import Formatter import pytest from pytest_docker_tools.utils import wait_for_callable from pytest_docker_tools.wrappers import Container def create_container(request, docker_client, *args, **kwargs): kwargs.update({'detach': True}) container = docker_client.containers.run(*args, **kwargs) request.addfinalizer(lambda: container.remove(force=True) and container.wait(timeout=10)) return Container(container) def _process_image(request, image): if hasattr(image, '_pytestfixturefunction'): return request.getfixturevalue(image._pytestfixturefunction.name).id return image def _process_network(request, network): if hasattr(network, '_pytestfixturefunction'): return request.getfixturevalue(network._pytestfixturefunction.name).id return network def _process_volumes(request, volumes): vols = {} for key, val in volumes.items(): if hasattr(key, '_pytestfixturefunction'): key = request.getfixturevalue(key._pytestfixturefunction.name).id vols[key] = val return vols def _process_environment(request, environment): env = {} for key, val in environment.items(): if callable(val): val = val(*[request.getfixturevalue(f) for f in inspect.getargspec(val)[0]]) env[key] = val return env class FixtureFormatter(Formatter): def __init__(self, request): self.request = request def get_value(self, key, args, kwargs): return self.request.getfixturevalue(key) def _process_val(request, val): if isinstance(val, str): return FixtureFormatter(request).format(val) elif callable(val): return val(*[request.getfixturevalue(f) for f in inspect.getargspec(val)[0]]) return val def _process_list(request, val): return [_process(request, v) for v in val] def _process_dict(request, mapping): return {k: _process(request, v) for (k, v) in mapping.items()} def _process(request, val): if isinstance(val, dict): return _process_dict(request, val) elif isinstance(val, list): return _process_list(request, val) else: return _process_val(request, val) def container(name, image, *, scope='function', **kwargs): ''' Fixture factory for creating containers. For example in your conftest.py you can: from pytest_docker_tools import container_fixture test_container = container_fixture('test_container', 'redis') This will create a container called 'test_container' from the 'redis' image. ''' def container(request, docker_client): local_kwargs = dict(kwargs) if 'network' in local_kwargs: local_kwargs['network'] = _process_network(request, local_kwargs.pop('network')) environment = _process_environment(request, local_kwargs.pop('environment', {})) volumes = _process_volumes(request, local_kwargs.pop('volumes', {})) container = create_container( request, docker_client, _process_image(request, image), environment=environment, volumes=volumes, **_process_dict(request, local_kwargs) ) wait_for_callable( f'Waiting for container {name} to be ready', lambda: container.reload() or container.ready(), ) return container container.__name__ = name pytest.fixture(scope=scope, name=name)(container) frame = inspect.stack()[1] module = inspect.getmodule(frame[0]) setattr(module, name, container) return container PKLG &pytest_docker_tools/factories/image.pyimport inspect import sys import pytest def image(name, path=None, scope='session'): ''' Fixture factory for creating container images from a Dockerfile. For example in your conftest.py you can: from pytest_docker_tools import image_fixture test_image = image_fixture('test_image', path='path/to/buildcontext') Where the path is a folder containing a Dockerfile. By default the fixture has a session scope. ''' def image(request, docker_client): sys.stdout.write(f'Building {name}') try: image, logs = docker_client.images.build( path=path or name, tag=f'{name}:latest' ) for line in logs: sys.stdout.write('.') sys.stdout.flush() finally: sys.stdout.write('\n') # request.addfinalizer(lambda: docker_client.images.remove(image.id)) return image image.__name__ = name pytest.fixture(scope=scope, name=name)(image) frame = inspect.stack()[1] module = inspect.getmodule(frame[0]) setattr(module, name, image) return image PKL{k(pytest_docker_tools/factories/network.pyimport inspect import uuid import pytest def network(name, scope='function'): ''' Fixture factory for creating networks. For example in your conftest.py you can: from pytest_docker_tools import network_fixture test_storage = network_fixture('test_storage') Then you can reference that network from your test: def test_a_docker_network(test_storage): print(test_storage.id) The fixture has a function scope - it will be destroyed after your test exits. ''' def network(request, docker_client): vol_id = name + '-' + str(uuid.uuid4()) print(f'Creating network {vol_id}') network = docker_client.networks.create(vol_id) request.addfinalizer(lambda: network.remove()) return network network.__name__ = name pytest.fixture(scope=scope, name=name)(network) frame = inspect.stack()[1] module = inspect.getmodule(frame[0]) setattr(module, name, network) return network PKKL\j1pytest_docker_tools/factories/repository_image.pyimport inspect import sys import pytest def repository_image(name, tag=None, scope='session'): ''' Fixture factory for fetching container images from a repository. For example in your conftest.py you can: from pytest_docker_tools import factories factories.repository_image('test_image', 'redis:latest') By default the fixture has a session scope. ''' tag = tag or name if ':' not in tag: tag += ':latest' def repository_image(request, docker_client): sys.stdout.write(f'Fetching {name}\n') image = docker_client.images.pull(tag) # request.addfinalizer(lambda: docker_client.images.remove(image.id)) return image repository_image.__name__ = name pytest.fixture(scope=scope, name=name)(repository_image) frame = inspect.stack()[1] module = inspect.getmodule(frame[0]) setattr(module, name, repository_image) return repository_image PKLR'pytest_docker_tools/factories/volume.pyimport inspect import uuid import pytest def volume(name, scope='function'): ''' Fixture factory for creating volumes. For example in your conftest.py you can: from pytest_docker_tools import volume_fixture test_storage = volume_fixture('test_storage') Then you can reference that volume from your test: def test_a_docker_volume(test_storage): print(test_storage.id) The fixture has a function scope - it will be destroyed after your test exits. ''' def volume(request, docker_client): vol_id = name + '-' + str(uuid.uuid4()) print(f'Creating volume {vol_id}') volume = docker_client.volumes.create(vol_id) request.addfinalizer(lambda: volume.remove(True)) return volume volume.__name__ = name pytest.fixture(scope=scope, name=name)(volume) frame = inspect.stack()[1] module = inspect.getmodule(frame[0]) setattr(module, name, volume) return volume PKU_L'kBB(pytest_docker_tools/wrappers/__init__.pyfrom .container import Container __all__ = [ 'Container', ] PKL%)pytest_docker_tools/wrappers/container.py''' This module contains a wrapper that adds some helpers to a Docker Container object that are useful for integration testing. ''' import io import tarfile class _Map(object): def __init__(self, container): self._container = container def values(self): return [self[k] for k in self.keys()] def items(self): return [(k, self[k]) for k in self.keys()] def __iter__(self): return iter(self.keys()) class IpMap(_Map): @property def primary(self): return next(iter(self.values())) def keys(self): return self._container.attrs['NetworkSettings']['Networks'].keys() def __getitem__(self, key): if not isinstance(key, str): key = key.name networks = self._container.attrs['NetworkSettings']['Networks'] if key not in networks: raise KeyError(f'Unknown network: {key}') return networks[key]['IPAddress'] class PortMap(_Map): def __init__(self, container): self._container = container def keys(self): return self._container.attrs['NetworkSettings']['Ports'].keys() def __getitem__(self, key): ports = self._container.attrs['NetworkSettings']['Ports'] if key not in ports: raise KeyError(f'Unknown port: {key}') if not ports[key]: return [] return [p['HostPort'] for p in ports[key]] class Container(object): def __init__(self, container): self._container = container self.ips = IpMap(container) self.ports = PortMap(container) def ready(self): if self.status == 'exited': raise RuntimeError(f'Container {self.name} has already exited before we noticed it was ready') if self.status != 'running': return False networks = self._container.attrs['NetworkSettings']['Networks'] for name, network in networks.items(): if not network['IPAddress']: return False # If a user has exposed a port then wait for LISTEN socket to show up in netstat ports = self._container.attrs['NetworkSettings']['Ports'] for port, listeners in ports.items(): if not listeners: continue port, proto = port.split('/') assert proto in ('tcp', 'udp') if proto == 'tcp' and port not in self.get_open_tcp_ports(): return False if proto == 'udp' and port not in self.get_open_udp_ports(): return False return True @property def attrs(self): return self._container.attrs @property def id(self): return self._container.id @property def status(self): return self._container.status def reload(self): return self._container.reload() def kill(self, signal=None): return self._container.kill(signal) def remove(self, *args, **kwargs): raise RuntimeError('Do not remove this container manually. It will be removed automatically by py.test after the test finishes.') def logs(self): return self._container.logs().decode('utf-8') def get_files(self, path): ''' Retrieve files from a container at a given path. This is meant for extracting log files from a container where it is not using the docker logging capabilities. ''' archive_iter, _ = self._container.get_archive(path) archive_stream = io.BytesIO() [archive_stream.write(chunk) for chunk in archive_iter] archive_stream.seek(0) archive = tarfile.TarFile(fileobj=archive_stream) files = {} for info in archive.getmembers(): if not info.isfile(): continue reader = archive.extractfile(info.name) files[info.name] = reader.read().decode('utf-8') return files def get_open_tcp_ports(self): ''' Gets all TCP sockets in the LISTEN state ''' netstat = self._container.exec_run('cat /proc/net/tcp')[1].decode('utf-8').strip() ports = [] for line in netstat.split('\n'): # Not interested in empty lines if not line: continue line = line.split() # Only interested in listen sockets if line[3] != '0A': continue ports.append(str(int(line[1].split(':', 1)[1], 16))) return ports def get_open_udp_ports(self): ''' Gets all UDP sockets in the LISTEN state ''' netstat = self._container.exec_run('cat /proc/net/udp')[1].decode('utf-8').strip() ports = [] for line in netstat.split('\n'): # Not interested in empty lines if not line: continue line = line.split() # Only interested in listen sockets if line[3] != '0A': continue ports.append(str(int(line[1].split(':', 1)[1], 16))) return ports PK!HG^(44pytest_docker_tools-0.0.2.dist-info/entry_points.txt.,I-.14JON-/)#䔦gqqPK!HNO)pytest_docker_tools-0.0.2.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,zd&Y)r$[)T&UrPK!HO[n,pytest_docker_tools-0.0.2.dist-info/METADATASMoFڅI%nQIF6(@#q+.U#Kɖ?.μy'آON]iRQQ ֓4k oL]ڋEq*.Rh%LKc^h`,ƥXWFR`Qb +']ƨɇt=c|Q6?cs1 3p"9)} ph8:hka-~d(Ŗy_~*~3rCD}~?a&֭GRцz3(΅K>8(Kx9a^X\(9z<5x qI6Xa(̂煎%MRtxF-T;BL5k`k/bVwʴ'qݑM\9{ze~&6SdaiF"UIǦ*Rqw+#t5G! _ %k .;I"wgY#,wvNq3nZ6+!lNc8$>G6q f"hW!Z^3)JZr<Ɍ!n-&f*qӋe\C϶Hu ״HP G[Zi !fӫ գ[ߙ/O]k4q/~KnAOz^BطcPK!HJj?i*pytest_docker_tools-0.0.2.dist-info/RECORDқPF{? P AA" =ƶfF.9gݯ[ ai m5~)`mo1A3?Ęs۳vI-d|_Pöd:^ [vՔ昄RrH4μT텄v@Wc `QoL2s`kɢy"}tqۙfs]_ z .)cLKtXEZ;i0x㱶ihf[׈r!gUq4. IlY!oN!$E oz)WH)Z.vgL.4hMpC~B*PյjKO 5_EH1y];_%cP\-dJa)=OKWJ ?j{HHi~/a:iV^|StDxnjY8CL QK)eψŷ|]|hA3'qYVf