def test_cancel_benchmark_with_cascade_with_not_found_exception(
    k8s_execution_engine: K8SExecutionEngine, mock_check_output_with_parent_not_found
):
    """
    Tests that when deleting a benchmark with cascade, an attempt to cascade the deletion
    is still made even in the case that the scheduled benchmark is not found. E.g. if the user
    deletes a scheduled benchmark without cascading, they should still be able to delete the
    scheduled job's spawned jobs.
    """
    k8s_execution_engine.cancel(CLIENT_ID, ACTION_ID, cascade=True)

    expected_calls = [
        # Deletes resources with label action-id=<action_id>
        call(
            [
                KUBECTL,
                "delete",
                JOINED_RESOURCE_TYPES,
                "--selector",
                K8SExecutionEngine._create_label_selector(CLIENT_ID, ACTION_ID, as_parent=False),
            ]
        ),
        # Also deletes resources with label parent-action-id=<action_id>
        call(
            [
                KUBECTL,
                "delete",
                JOINED_RESOURCE_TYPES,
                "--selector",
                K8SExecutionEngine._create_label_selector(CLIENT_ID, ACTION_ID, as_parent=True),
            ]
        ),
    ]

    mock_check_output_with_parent_not_found.assert_has_calls(expected_calls)
def test_cancel_benchmark_with_cascade(k8s_execution_engine: K8SExecutionEngine, mock_check_output):
    k8s_execution_engine.cancel(CLIENT_ID, ACTION_ID, cascade=True)

    expected_calls = [
        # Deletes resources with label action-id=<action_id>
        call(
            [
                KUBECTL,
                "delete",
                JOINED_RESOURCE_TYPES,
                "--selector",
                K8SExecutionEngine._create_label_selector(CLIENT_ID, ACTION_ID, as_parent=False),
            ]
        ),
        # Also deletes resources with label parent-action-id=<action_id>
        call(
            [
                KUBECTL,
                "delete",
                JOINED_RESOURCE_TYPES,
                "--selector",
                K8SExecutionEngine._create_label_selector(CLIENT_ID, ACTION_ID, as_parent=True),
            ]
        ),
    ]
    mock_check_output.assert_has_calls(expected_calls)
def test_cancel_benchmark_without_cascade(k8s_execution_engine: K8SExecutionEngine, mock_check_output):
    k8s_execution_engine.cancel(CLIENT_ID, ACTION_ID, cascade=False)

    expected_call = [
        KUBECTL,
        "delete",
        JOINED_RESOURCE_TYPES,
        "--selector",
        K8SExecutionEngine._create_label_selector(CLIENT_ID, ACTION_ID, as_parent=False),
    ]
    mock_check_output.assert_called_with(expected_call)
def test_cancel_with_cascade_fails(k8s_execution_engine: K8SExecutionEngine, mock_check_output_no_resource_found):
    with pytest.raises(NoResourcesFoundException):
        k8s_execution_engine.cancel(CLIENT_ID, ACTION_ID, cascade=False)