def test_container_address_raises_runtime_error_when_not_started():
    # Given
    cntr = Container(Image(''))

    # Ensure
    with pytest.raises(RuntimeError):
        # When
        cntr.address
예제 #2
0
def test_container_remove_when_not_started():
    # Given
    cntr = Container(Image(''), startup_poll_interval=0.0)

    # Ensure
    with pytest.warns(RuntimeWarning, match='never.*started'):
        # When
        cntr.remove()
def test_container_address_when_no_network_settings(client, container_lacking_network_settings):
    # Given
    cntr = Container(Image(''), dockerclient=client)
    cntr.run()

    # When
    address = cntr.address

    # Then
    assert address == 'localhost'
예제 #4
0
def test_container_kill_catches_when_response_is_409(mocker, client,
                                                     running_container):
    # Given
    response = mocker.Mock()
    response.status_code = 409
    running_container.kill = mocker.Mock(
        side_effect=exceptions.HTTPError('', response=response))
    cntr = Container(Image(''), dockerclient=client)
    cntr.run()

    # When
    cntr.kill()
def test_container_run_raises_image_not_found_when_registry_says_so(
        mocker, client):
    # Given
    image = Image('')
    mocker.patch.object(client.images,
                        'pull',
                        side_effect=docker.errors.ImageNotFound(''))

    # Ensure
    with pytest.raises(ImageNotFound):
        # When
        Container(image).run(dockerclient=client)
def test_container_wait_when_service_starts_fast_enough(mocker):
    # Given
    mocker.patch('dockerfixtures.container.socket')
    mocker.patch('dockerfixtures.container.Container.address',
                 new_callable=mocker.PropertyMock,
                 return_value='localhost')
    cntr = Container(Image('unknown', max_wait=10.0))

    # When
    res = cntr.wait((1234, 'tcp'))

    # Then
    assert res
def test_container_running(client, container_running_after_reload):
    """
    """
    # Given
    image = Image('')
    cntr = Container(image)

    # When
    result = cntr.run(dockerclient=client)

    # Then
    assert result == container_running_after_reload.id
    container_running_after_reload.reload.assert_called_once_with()
예제 #8
0
def test_container_kill_raises_when_response_is_404(mocker, client,
                                                    running_container):
    # Given
    response = mocker.Mock()
    response.status_code = 404
    running_container.kill = mocker.Mock(
        side_effect=exceptions.HTTPError('', response=response))
    cntr = Container(Image(''), dockerclient=client)
    cntr.run()

    # Ensure
    with pytest.raises(exceptions.HTTPError):
        # When
        cntr.kill()
def test_container_run_times_out(mocker, client, container):
    # Given
    cntr = Container(Image(''),
                     dockerclient=client,
                     max_wait=0.000000001,
                     startup_poll_interval=0.0)
    mocker.patch('dockerfixtures.container.sleep')  # bypass time.sleep()

    # Ensure
    with pytest.raises(TimeOut):
        # When
        cntr.run()

    # Then
    assert container.reload.call_count == 0
def test_container_run_raises_runtime_error_when_exited(
        client, container_exited_after_reload):
    """
    """
    # Given
    image = Image('')
    cntr = Container(image)

    # Ensure
    with pytest.raises(RuntimeError):
        # When
        cntr.run(dockerclient=client)

    # Then
    container_exited_after_reload.reload.assert_called_once_with()
def test_container_run_raises_runtime_warning_when_unkwown_status(
        client, container_unknown_on_1st_reload):
    """
    """
    # Given
    image = Image('')
    cntr = Container(image, startup_poll_interval=0.0)

    # Ensure
    with pytest.warns(RuntimeWarning):
        # When
        cntr.run(dockerclient=client)

    # Then
    assert container_unknown_on_1st_reload.reload.call_count == 2
def test_container_run_when_image_as_command(client, container_w_command):
    # TODO: review this test, name does not fit content
    # Given
    image = Image('')
    img = client.images.pull()
    cntr = Container(image, startup_poll_interval=0.0)

    # When
    cntr.run(dockerclient=client)

    # Then
    client.containers.run.assert_called_once_with(
        image=img,
        # command=None,
        environment={},
        **cntr.options)
def test_container_wait_when_service_starts_fast_enough_but_no_port(mocker):
    # Given
    # Requirement: time_mock.side_effect[-1] - time_mock.side_effect[0] > max_wait
    mocker.patch('dockerfixtures.container.socket')
    mocker.patch('dockerfixtures.container.Container.address',
                 new_callable=mock.PropertyMock,
                 return_value='localhost')
    mocker.patch('dockerfixtures.container.Container.ports',
                 new_callable=mock.PropertyMock,
                 return_value={'1234/tcp': (1234, 'tcp')})
    cntr = Container(Image('unknown', max_wait=5.0))

    # When
    res = cntr.wait()

    assert res
def test_container_running_after_second_reload(
        mocker, client, container_running_after_2_reload):
    """
    """
    # Given
    poll_interval = 10.0
    image = Image('')
    cntr = Container(image, startup_poll_interval=poll_interval)
    sleep = mocker.patch('dockerfixtures.container.sleep')

    # When
    result = cntr.run(dockerclient=client)

    # Then
    assert container_running_after_2_reload.id == result
    assert container_running_after_2_reload.reload.call_count == 2
    sleep.assert_called_once_with(poll_interval)
def test_container_paused_then_running(mocker, client,
                                       container_paused_after_1st_reload):
    """
    """
    # Given
    poll_interval = 10.0
    image = Image('')
    cntr = Container(image, max_wait=2, startup_poll_interval=poll_interval)
    sleep = mocker.patch('dockerfixtures.container.sleep')

    # When
    result = cntr.run(dockerclient=client)

    # Then
    # call count is 2 because reload is call one last time before raising
    assert container_paused_after_1st_reload.id == result
    assert container_paused_after_1st_reload.reload.call_count == 2
    sleep.assert_called_once_with(poll_interval)
예제 #16
0
def test_container_from_kafka_image_all_defaults(client):
    topic = 'topic'
    message_count = 20

    with Container(catalog.PAPERLIB_KAFKA_2_3_1,
                   dockerclient=client,
                   max_wait=100,
                   options={
                       'network_mode': 'host',
                   }) as cntr:
        broker = '{}:9092'.format(cntr.address)
        # Wait for the container to be ready
        cntr.wait((9092, 'tcp'), readyness_poll_interval=0.5)

        # Create a topic to publish messages to
        kafka = KafkaAdminClient(bootstrap_servers=broker)
        topics = []
        topics.append(
            NewTopic(name=topic, num_partitions=1, replication_factor=1))
        kafka.create_topics(topics)

        # Produce a few message
        producer = KafkaProducer(bootstrap_servers=broker, acks=0)

        for ndx in range(message_count):
            producer.send(
                topic,
                json.dumps({
                    'hello': 'nanny {}'.format(ndx)
                }).encode('utf-8'))

        # Make sure the messages were produced by consuming them
        consumer = KafkaConsumer(
            auto_offset_reset='earliest',
            bootstrap_servers=broker,
            consumer_timeout_ms=5000,
            # request_timeout_ms=5000, # 5 seconds max.
            value_deserializer=json.loads)
        consumer.subscribe([topic])
        for idx, message in enumerate(consumer):
            logging.getLogger().info(message)
        assert idx > 0
def test_container_wait_when_service_does_not_start_fast_enough(
        mocker, container_max_wait):
    # Given
    # Requirement: time_mock.side_effect[-1] - time_mock.side_effect[0] > max_wait
    cntr = Container(Image('unknown', max_wait=5.0),
                     max_wait=container_max_wait)
    mocker.patch('dockerfixtures.container.Container.address',
                 new_callable=mock.PropertyMock,
                 return_value='localhost')
    time_mock = mocker.patch('dockerfixtures.container.time',
                             side_effect=[0.1, 0.2, 10.0, 10.0, 10.0])
    mocker.patch('dockerfixtures.container.socket',
                 side_effect=builtins.ConnectionError())

    # Ensure
    with pytest.raises(TimeOut):
        # When
        cntr.wait((1234, 'tcp'))

    assert time_mock.call_count == 4
예제 #18
0
def test_container_from_pg_image_all_defaults(client):
    env = {
        **catalog.PG_11.default_environment, 'POSTGRES_PASSWORD':
        secrets.token_urlsafe()
    }
    with Container(catalog.PG_11,
                   dockerclient=client,
                   max_wait=100,
                   environment=env) as cntr:
        cntr.wait((5432, 'tcp'), max_wait=10.0)
        # time.sleep(1.5)  # See https://github.com/docker-library/postgres/issues/146
        subprocess.check_call([
            'docker',
            'exec',
            '-i',
            cntr.id,
            'psql',
            '-U',
            catalog.PG_10.default_environment['POSTGRES_USER'],
            # '-d', catalog.PG_10.default_environment['POSTGRES_DB'],
            '-c',
            'SELECT 1'
        ])