def test_run_docker_container_non_interactive_run_returns_nonzero(
    mock_get_healthcheck_for_instance,
    mock_get_container_id,
    mock_run,
    mock_get_cmd_string,
    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
def test_run_docker_container_terminates_with_healthcheck_only_fail(
    mock_get_healthcheck_for_instance,
    mock_get_container_id,
    mock_run,
    mock_get_cmd_string,
    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
def test_run_docker_container_non_interactive_keyboard_interrupt(
    mock_get_healthcheck_for_instance,
    mock_get_container_id,
    mock_run,
    mock_get_cmd_string,
    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
def test_run_docker_container_terminates_with_healthcheck_only_fail(
        mock_get_healthcheck_for_instance, mock_get_container_id, mock_run,
        mock_get_cmd_string, 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
def test_run_docker_container_non_interactive(
    mock_get_healthcheck_for_instance,
    mock_get_container_id,
    mock_run,
    mock_get_cmd_string,
    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_get_cmd_string.call_count == 0
        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_interactive(
    mock_get_healthcheck_for_instance,
    mock_get_container_id,
    mock_run,
    mock_get_cmd_string,
    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_get_cmd_string.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
def test_run_docker_container_non_interactive_run_returns_nonzero(
    mock_get_healthcheck_for_instance,
    mock_get_container_id,
    mock_run,
    mock_get_cmd_string,
    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