diff --git a/README.rst b/README.rst index 36cc47b..5b57b17 100644 --- a/README.rst +++ b/README.rst @@ -79,6 +79,11 @@ It will use the container port and localhost instead:: Changelog ========= +Version 0.7.0 +------------- + +* Output from containers will now be printed for failed tests. + Version 0.6.0 ------------- diff --git a/setup.py b/setup.py index 1aab815..8ece401 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name='pytest-docker', url='https://github.com/AndreLouisCaron/pytest-docker', - version='0.6.1', + version='0.7.0', license='MIT', maintainer='Andre Caron', maintainer_email='andre.l.caron@gmail.com', diff --git a/src/pytest_docker/__init__.py b/src/pytest_docker/__init__.py index 9422958..05853eb 100644 --- a/src/pytest_docker/__init__.py +++ b/src/pytest_docker/__init__.py @@ -1,14 +1,16 @@ # -*- coding: utf-8 -*- - -import attr +import functools import os -import pytest import re +import shlex import subprocess import time import timeit +import attr +import pytest + def execute(command, success_codes=(0,)): """Run a shell command.""" @@ -68,9 +70,22 @@ def port_for(self, service, port): if cache is not None: return cache - output = self._docker_compose.execute( - 'port %s %d' % (service, port,) + get_port = functools.partial( + self._docker_compose.execute, + 'port %s %d' % (service, port,), ) + + def check_if_up(): + try: + get_port() + return True + except Exception as ex: + if 'No container found' in ex.args[0]: + return False + raise ex + self.wait_until_responsive(check_if_up, 5, 0.1) + + output = get_port() endpoint = output.strip() if not endpoint: raise ValueError( @@ -114,11 +129,20 @@ class DockerComposeExecutor(object): _compose_project_name = attr.ib() def execute(self, subcommand): + command = '{} {}'.format(self._get_command_prefix(), subcommand) + return execute(command) + + def get_up(self): + prepped_command = '{} up --build'.format(self._get_command_prefix()) + subprocess.Popen(shlex.split(prepped_command)) + + def _get_command_prefix(self): command = "docker-compose" for compose_file in self._compose_files: command += ' -f "{}"'.format(compose_file) - command += ' -p "{}" {}'.format(self._compose_project_name, subcommand) - return execute(command) + command += ' -p "{}"'.format(self._compose_project_name) + return command + @pytest.fixture(scope='session') @@ -175,7 +199,7 @@ def docker_services( return # Spawn containers. - docker_compose.execute('up --build -d') + docker_compose.get_up() # Let test(s) run. yield Services(docker_compose)