def test_execute():
    docker_compose = DockerComposeExecutor("docker-compose.yml", "pytest123")
    with mock.patch("subprocess.check_output") as check_output:
        docker_compose.execute("up")
        assert check_output.call_args_list == [
            mock.call(
                'docker-compose -f "docker-compose.yml" -p "pytest123" up',
                shell=True,
                stderr=subprocess.STDOUT,
            )
        ]
def test_pypath_compose_files():
    compose_file = py.path.local("/tmp/docker-compose.yml")
    docker_compose = DockerComposeExecutor(compose_file, "pytest123")
    with mock.patch("subprocess.check_output") as check_output:
        docker_compose.execute("up")
        assert check_output.call_args_list == [
            mock.call(
                'docker-compose -f "/tmp/docker-compose.yml"'
                ' -p "pytest123" up',
                shell=True,
                stderr=subprocess.STDOUT,
            )
        ]
Ejemplo n.º 3
0
def docker_services(docker_compose_file, docker_compose_project_name,
                    tmp_path_factory):
    # overriding `docker_services` fixture from `pytest_docker` plugin to
    # only launch docker images once.

    from filelock import FileLock
    from pytest_docker.plugin import DockerComposeExecutor, Services

    executor = DockerComposeExecutor(docker_compose_file,
                                     docker_compose_project_name)

    # making sure we don't accidentally launch docker-compose in parallel,
    # as it might result in network conflicts. Inspired by:
    # https://github.com/pytest-dev/pytest-xdist#making-session-scoped-fixtures-execute-only-once
    lockfile = tmp_path_factory.getbasetemp().parent / "docker-compose.lock"
    with FileLock(str(lockfile)):  # pylint:disable=abstract-class-instantiated
        executor.execute("up --build -d")

    return Services(executor)
def test_wait_until_responsive_timeout():
    clock = mock.MagicMock()
    clock.side_effect = [0.0, 1.0, 2.0, 3.0]

    with mock.patch("time.sleep") as sleep:
        docker_compose = DockerComposeExecutor(
            compose_files="docker-compose.yml",
            compose_project_name="pytest123")
        services = Services(docker_compose)
        with pytest.raises(Exception) as exc:
            print(
                services.wait_until_responsive(check=lambda: False,
                                               timeout=3.0,
                                               pause=1.0,
                                               clock=clock))
        assert sleep.call_args_list == [
            mock.call(1.0), mock.call(1.0),
            mock.call(1.0)
        ]
    assert str(exc.value) == ("Timeout reached while waiting on service!")