コード例 #1
0
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()
コード例 #3
0
def test_image_pullname_when_default_tag_given():
    """Ensures pullname is as expected when the image was build with a tag & a hash
    """
    # Given
    name = 'tralala'
    image = Image(name)

    # When
    assert image.pullname == f"{name}:{DEFAULT_IMAGE_TAG}"
コード例 #4
0
def test_image_pullname_when_no_tag_nor_hash_given():
    """Ensures pullname is as expected when the image was build with a tag & a hash
    """
    # Given
    name = 'tralala'
    image = Image(name, tag=None)

    # When
    assert image.pullname == f"{name}"
コード例 #5
0
def test_image_environment_is_complete(dummy_env):
    # Given
    img = Image('tralala', tag='itou', environment=dummy_env)

    # When
    default_env = img.default_environment

    # Then
    assert default_env == dummy_env
コード例 #6
0
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'
コード例 #7
0
def test_image_pullname_when_tag_given():
    """Ensures pullname is as expected when the image was build with a tag
    """
    # Given
    name = 'tralala'
    tag = 'itou'
    image = Image(name, tag=tag)

    # When
    assert image.pullname == f"{name}:{tag}"
コード例 #8
0
def test_image_pullname_when_tag_and_hash_given():
    """Ensures pullname is as expected when the image was build with a tag & a hash
    """
    # Given
    name = 'tralala'
    tag = 'itou'
    hash_ = 'sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
    image = Image(name, tag=tag, hash_=hash_)

    # When
    assert image.pullname == f"{name}@{hash_}"
コード例 #9
0
def test_image_pullname_when_hash_given():
    """Ensures basic attributes are assigned as expected
    """
    # Given
    name = 'tralala'
    tag = 'itou'
    hash_ = 'sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
    image = Image(name, tag=tag, hash_=hash_)

    # When
    assert image.pullname == f"{name}@{hash_}"
コード例 #10
0
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)
コード例 #11
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()
コード例 #12
0
def test_image_attributes():
    """Ensures basic attributes are assigned as expected
    """
    # Given
    name = 'tralala'
    tag = 'itou'
    hash_ = 'sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
    img = Image(name, tag=tag, hash_=hash_)

    # When
    assert img.tag == tag
    assert img.name == name
コード例 #13
0
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
コード例 #14
0
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()
コード例 #15
0
def test_image_environment_is_not_shared(dummy_env):
    """Ensure that an image default environment is copied

    Otherwise changing the value of default_environment may change
    the environment of other images
    """
    # Given
    img = Image('tralala', tag='itou', environment=dummy_env)

    # When
    default_env = img.default_environment

    # Then
    assert default_env is not dummy_env
コード例 #16
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()
コード例 #17
0
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
コード例 #18
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()
コード例 #19
0
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
コード例 #20
0
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
コード例 #21
0
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)
コード例 #22
0
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)
コード例 #23
0
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)
コード例 #24
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