Exemple #1
0
def test_get_port_docker_allow_fallback_docker_offline():
    with mock.patch('subprocess.check_output') as check_output:
        check_output.side_effect = [
            subprocess.CalledProcessError(
                1,
                'the command',
                b'the output',
            ),
        ]
        check_output.returncode = 1

        gen = docker_services('docker-compose.yml', docker_allow_fallback=True)
        services = next(gen)
        assert services.port_for('abc', 123) == 123

        # Next yield is last.
        with pytest.raises(StopIteration):
            print(next(gen))
def test_docker_services_failure():
    """Propagate failure to start service."""

    with mock.patch('subprocess.check_output') as check_output:
        check_output.side_effect = [
            subprocess.CalledProcessError(
                1,
                'the command',
                b'the output',
            ),
        ]
        check_output.returncode = 1

        # The fixture is a context-manager.
        gen = docker_services(
            'docker-compose.yml',
            docker_compose_project_name='pytest123',
            docker_allow_fallback=False,
        )

        assert check_output.call_count == 0

        # Failure propagates with improved diagnoatics.
        with pytest.raises(Exception) as exc:
            print(next(gen))
        assert str(exc.value) == ('Command %r returned %d: """%s""".' % (
            "the command",
            1,
            'the output',
        ))

        assert check_output.call_count == 1

    # Tear down code should not be called.
    assert check_output.call_args_list == [
        mock.call(
            'docker-compose -f "docker-compose.yml" -p "pytest123" '
            'up --build -d',
            shell=True,
            stderr=subprocess.STDOUT,
        ),
    ]
def test_docker_services_unused_port():
    """Complain loudly when the requested port is not used by the service."""
    gen = docker_services(COMPOSE_FILE,
                          docker_compose_project_name='pytest123',
                          docker_allow_fallback=False,
                          docker_compose_project_dir='.')
    services = next(gen)

    # Can request port for services.
    with pytest.raises(NoSuchService) as exc:
        print(services.port_for('abc', 123))
    assert str(exc.value) == 'No such service: abc'

    with pytest.raises(ValueError) as exc:
        print(services.port_for('hello', 123))
    assert str(exc.value) == 'Could not detect port for "hello:123".'

    # Next yield is last.
    with pytest.raises(StopIteration):
        print(next(gen))
def test_get_port_docker_allow_fallback_docker_offline():

    with mock.patch('subprocess.call') as check_output:
        check_output.side_effect = [Exception('')]

        assert check_output.call_count == 0

        # The fixture is a context-manager.
        gen = docker_services(COMPOSE_FILE,
                              docker_compose_project_name='pytest123',
                              docker_allow_fallback=True,
                              docker_compose_project_dir='.')
        services = next(gen)
        assert isinstance(services, Services)

        assert check_output.call_count == 1

        # Can request port for services.
        port = services.port_for('hello', 80)
        assert port == 80

        # Next yield is last.
        with pytest.raises(StopIteration):
            print(next(gen))