Esempio n. 1
0
def test_run_docker_container_terminates_with_healthcheck_only_fail(
    mock_get_healthcheck_for_instance,
    mock_get_container_id,
    mock_run,
    mock_execlp,
    mock_get_docker_run_cmd,
    mock_pick_random_port,
    mock_simulate_healthcheck
):
    mock_pick_random_port.return_value = 666
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_docker_client.attach = mock.MagicMock(spec_set=docker.Client.attach)
    mock_docker_client.stop = mock.MagicMock(spec_set=docker.Client.stop)
    mock_docker_client.remove_container = mock.MagicMock(spec_set=docker.Client.remove_container)
    mock_service_manifest = mock.MagicMock(spec_set=MarathonServiceConfig)
    with raises(SystemExit) as excinfo:
        run_docker_container(
            mock_docker_client,
            'fake_service',
            'fake_instance',
            'fake_hash',
            [],
            False,  # interactive
            'fake_command',
            False,  # healthcheck
            True,  # terminate after healthcheck
            mock_service_manifest,
        )
    assert mock_docker_client.stop.call_count == 1
    assert mock_docker_client.remove_container.call_count == 1
    assert excinfo.value.code == 1
Esempio n. 2
0
def test_run_docker_container_non_interactive_keyboard_interrupt(
    mock_get_healthcheck_for_instance,
    mock_get_container_id,
    mock_run,
    mock_execlp,
    mock_get_docker_run_cmd,
    mock_pick_random_port,
):
    mock_pick_random_port.return_value = 666
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_docker_client.attach = mock.MagicMock(
        spec_set=docker.Client.attach,
        side_effect=KeyboardInterrupt,
    )
    mock_docker_client.stop = mock.MagicMock(spec_set=docker.Client.stop)
    mock_docker_client.remove_container = mock.MagicMock(spec_set=docker.Client.remove_container)
    mock_service_manifest = mock.MagicMock(spec_set=MarathonServiceConfig)
    mock_docker_client.inspect_container.return_value = {"State": {"ExitCode": 99}}
    with raises(SystemExit) as excinfo:
        run_docker_container(
            mock_docker_client,
            'fake_service',
            'fake_instance',
            'fake_hash',
            [],
            False,  # interactive
            'fake_command',
            False,  # healthcheck
            False,  # terminate after healthcheck
            mock_service_manifest,
        )
        assert mock_docker_client.stop.call_count == 1
        assert mock_docker_client.remove_container.call_count == 1
        assert excinfo.value.code == 99
Esempio n. 3
0
def test_run_docker_container_non_interactive_run_returns_nonzero(
    mock_get_healthcheck_for_instance,
    mock_get_container_id,
    mock_run,
    mock_execlp,
    mock_get_docker_run_cmd,
    mock_pick_random_port,
):
    mock_pick_random_port.return_value = 666
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_docker_client.attach = mock.MagicMock(
        spec_set=docker.Client.attach,
    )
    mock_docker_client.stop = mock.MagicMock(spec_set=docker.Client.stop)
    mock_docker_client.remove_container = mock.MagicMock(spec_set=docker.Client.remove_container)
    mock_service_manifest = mock.MagicMock(spec_set=MarathonServiceConfig)
    mock_docker_client.inspect_container.return_value = {"State": {"ExitCode": 99}}
    with raises(SystemExit) as excinfo:
        run_docker_container(
            mock_docker_client,
            'fake_service',
            'fake_instance',
            'fake_hash',
            [],
            False,  # interactive
            'fake_command',
            False,  # healthcheck
            False,  # terminate after healthcheck
            mock_service_manifest,
        )
        # Cleanup wont' be necessary and the function should bail out early.
        assert mock_docker_client.stop.call_count == 0
        assert mock_docker_client.remove_container.call_count == 0
        assert excinfo.value.code == 99
Esempio n. 4
0
def test_run_docker_container_terminates_with_healthcheck_only_fail(
    mock_get_healthcheck_for_instance,
    mock_get_container_id,
    mock_run,
    mock_execlp,
    mock_get_docker_run_cmd,
    mock_pick_random_port,
    mock_simulate_healthcheck
):
    mock_pick_random_port.return_value = 666
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_docker_client.attach = mock.MagicMock(spec_set=docker.Client.attach)
    mock_docker_client.stop = mock.MagicMock(spec_set=docker.Client.stop)
    mock_docker_client.remove_container = mock.MagicMock(spec_set=docker.Client.remove_container)
    mock_service_manifest = mock.MagicMock(spec_set=MarathonServiceConfig)
    with raises(SystemExit) as excinfo:
        run_docker_container(
            mock_docker_client,
            'fake_service',
            'fake_instance',
            'fake_hash',
            [],
            False,  # interactive
            'fake_command',
            'fake_hostname',
            False,  # healthcheck
            True,  # terminate after healthcheck
            mock_service_manifest,
        )
    assert mock_docker_client.stop.call_count == 1
    assert mock_docker_client.remove_container.call_count == 1
    assert excinfo.value.code == 1
Esempio n. 5
0
def test_run_docker_container_non_interactive_run_returns_nonzero(
    mock_get_healthcheck_for_instance,
    mock_get_container_id,
    mock_run,
    mock_execlp,
    mock_get_docker_run_cmd,
    mock_pick_random_port,
):
    mock_pick_random_port.return_value = 666
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_docker_client.attach = mock.MagicMock(
        spec_set=docker.Client.attach,
    )
    mock_docker_client.stop = mock.MagicMock(spec_set=docker.Client.stop)
    mock_docker_client.remove_container = mock.MagicMock(spec_set=docker.Client.remove_container)
    mock_service_manifest = mock.MagicMock(spec_set=MarathonServiceConfig)
    mock_docker_client.inspect_container.return_value = {'State': {'ExitCode': 99, 'Running': True}}
    with raises(SystemExit) as excinfo:
        run_docker_container(
            mock_docker_client,
            'fake_service',
            'fake_instance',
            'fake_hash',
            [],
            False,  # interactive
            'fake_command',
            'fake_hostname',
            False,  # healthcheck
            False,  # terminate after healthcheck
            mock_service_manifest,
        )
        # Cleanup wont' be necessary and the function should bail out early.
        assert mock_docker_client.stop.call_count == 0
        assert mock_docker_client.remove_container.call_count == 0
        assert excinfo.value.code == 99
Esempio n. 6
0
def test_run_docker_container_non_interactive_keyboard_interrupt(
    mock_get_healthcheck_for_instance,
    mock_get_container_id,
    mock_run,
    mock_execlp,
    mock_get_docker_run_cmd,
    mock_pick_random_port,
):
    mock_pick_random_port.return_value = 666
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_docker_client.attach = mock.MagicMock(
        spec_set=docker.Client.attach,
        side_effect=KeyboardInterrupt,
    )
    mock_docker_client.stop = mock.MagicMock(spec_set=docker.Client.stop)
    mock_docker_client.remove_container = mock.MagicMock(spec_set=docker.Client.remove_container)
    mock_service_manifest = mock.MagicMock(spec_set=MarathonServiceConfig)
    mock_docker_client.inspect_container.return_value = {'State': {'ExitCode': 99, 'Running': True}}
    with raises(SystemExit) as excinfo:
        run_docker_container(
            mock_docker_client,
            'fake_service',
            'fake_instance',
            'fake_hash',
            [],
            False,  # interactive
            'fake_command',
            'fake_hostname',
            False,  # healthcheck
            False,  # terminate after healthcheck
            mock_service_manifest,
        )
        assert mock_docker_client.stop.call_count == 1
        assert mock_docker_client.remove_container.call_count == 1
        assert excinfo.value.code == 99
Esempio n. 7
0
def test_run_docker_container_non_interactive(
    mock_get_healthcheck_for_instance,
    mock_get_container_id,
    mock_run,
    mock_execlp,
    mock_get_docker_run_cmd,
    mock_pick_random_port,
):
    mock_pick_random_port.return_value = 666
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_docker_client.attach = mock.MagicMock(spec_set=docker.Client.attach)
    mock_docker_client.stop = mock.MagicMock(spec_set=docker.Client.stop)
    mock_docker_client.remove_container = mock.MagicMock(
        spec_set=docker.Client.remove_container)
    mock_docker_client.inspect_container.return_value = {
        "State": {
            "ExitCode": 666
        }
    }
    mock_service_manifest = mock.MagicMock(spec_set=MarathonServiceConfig)
    with raises(SystemExit) as excinfo:
        run_docker_container(
            mock_docker_client,
            'fake_service',
            'fake_instance',
            'fake_hash',
            [],
            False,  # interactive
            'fake_command',
            False,  # healthcheck
            False,  # terminate after healthcheck
            mock_service_manifest,
        )
        mock_service_manifest.get_mem.assert_called_once_with()
        mock_pick_random_port.assert_called_once_with()
        assert mock_get_docker_run_cmd.call_count == 1
        assert mock_get_healthcheck_for_instance.call_count == 1
        assert mock_execlp.call_count == 0
        assert mock_run.call_count == 1
        assert mock_get_container_id.call_count == 1
        assert mock_docker_client.attach.call_count == 1
        assert mock_docker_client.stop.call_count == 1
        assert mock_docker_client.remove_container.call_count == 1
        assert excinfo.value.code == 666
def test_run_docker_container_non_interactive(
    mock_get_healthcheck_for_instance,
    mock_get_container_id,
    mock_run,
    mock_execlp,
    mock_get_docker_run_cmd,
    mock_pick_random_port,
):
    mock_pick_random_port.return_value = 666
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_docker_client.attach = mock.MagicMock(spec_set=docker.Client.attach)
    mock_docker_client.stop = mock.MagicMock(spec_set=docker.Client.stop)
    mock_docker_client.remove_container = mock.MagicMock(spec_set=docker.Client.remove_container)
    mock_docker_client.inspect_container.return_value = {"State": {"ExitCode": 666}}
    mock_service_manifest = mock.MagicMock(spec_set=MarathonServiceConfig)
    with raises(SystemExit) as excinfo:
        run_docker_container(
            mock_docker_client,
            'fake_service',
            'fake_instance',
            'fake_hash',
            [],
            False,  # interactive
            'fake_command',
            'fake_hostname',
            False,  # healthcheck
            False,  # terminate after healthcheck
            mock_service_manifest,
        )
        mock_service_manifest.get_mem.assert_called_once_with()
        mock_pick_random_port.assert_called_once_with()
        assert mock_get_docker_run_cmd.call_count == 1
        assert mock_get_healthcheck_for_instance.call_count == 1
        assert mock_execlp.call_count == 0
        assert mock_run.call_count == 1
        assert mock_get_container_id.call_count == 1
        assert mock_docker_client.attach.call_count == 1
        assert mock_docker_client.stop.call_count == 1
        assert mock_docker_client.remove_container.call_count == 1
        assert excinfo.value.code == 666
Esempio n. 9
0
def test_run_docker_container_interactive(
    mock_get_healthcheck_for_instance,
    mock_get_container_id,
    mock_run,
    mock_execlp,
    mock_get_docker_run_cmd,
    mock_pick_random_port,
):
    mock_pick_random_port.return_value = 666
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_docker_client.attach = mock.MagicMock(spec_set=docker.Client.attach)
    mock_docker_client.stop = mock.MagicMock(spec_set=docker.Client.stop)
    mock_docker_client.remove_container = mock.MagicMock(
        spec_set=docker.Client.remove_container)
    mock_service_manifest = mock.MagicMock(spec_set=MarathonServiceConfig)
    run_docker_container(
        mock_docker_client,
        'fake_service',
        'fake_instance',
        'fake_hash',
        [],
        True,  # interactive
        'fake_command',
        'fake_hostname',
        False,  # healthcheck
        False,  # terminate after healthcheck
        mock_service_manifest,
    )
    mock_service_manifest.get_mem.assert_called_once_with()
    mock_pick_random_port.assert_called_once_with()
    assert mock_get_docker_run_cmd.call_count == 1
    assert mock_get_healthcheck_for_instance.call_count == 1
    assert mock_execlp.call_count == 1
    assert mock_run.call_count == 0
    assert mock_get_container_id.call_count == 0
    assert mock_docker_client.attach.call_count == 0
    assert mock_docker_client.stop.call_count == 0
    assert mock_docker_client.remove_container.call_count == 0
Esempio n. 10
0
def test_run_docker_container_interactive(
    mock_get_healthcheck_for_instance,
    mock_get_container_id,
    mock_run,
    mock_execlp,
    mock_get_docker_run_cmd,
    mock_pick_random_port,
):
    mock_pick_random_port.return_value = 666
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_docker_client.attach = mock.MagicMock(spec_set=docker.Client.attach)
    mock_docker_client.stop = mock.MagicMock(spec_set=docker.Client.stop)
    mock_docker_client.remove_container = mock.MagicMock(spec_set=docker.Client.remove_container)
    mock_service_manifest = mock.MagicMock(spec_set=MarathonServiceConfig)
    run_docker_container(
        mock_docker_client,
        'fake_service',
        'fake_instance',
        'fake_hash',
        [],
        True,  # interactive
        'fake_command',
        False,  # healthcheck
        False,  # terminate after healthcheck
        mock_service_manifest,
    )
    mock_service_manifest.get_mem.assert_called_once_with()
    mock_pick_random_port.assert_called_once_with()
    assert mock_get_docker_run_cmd.call_count == 1
    assert mock_get_healthcheck_for_instance.call_count == 1
    assert mock_execlp.call_count == 1
    assert mock_run.call_count == 0
    assert mock_get_container_id.call_count == 0
    assert mock_docker_client.attach.call_count == 0
    assert mock_docker_client.stop.call_count == 0
    assert mock_docker_client.remove_container.call_count == 0