def test_delete_get_kind_call(mocked_get_kind: mock.MagicMock):
    """
    GIVEN mocked body and mocked get_kind
    WHEN delete is called with the body
    THEN get_kind is called with the body.
    """
    mock_body = mock.MagicMock()

    operations.delete(body=mock_body, physical_name="physical name 1")

    mocked_get_kind.assert_called_once_with(body=mock_body)
def test_delete_client_function_call(mocked_get_function: mock.MagicMock):
    """
    GIVEN mocked get_function that returns a client function and False for namespaced
        and physical name
    WHEN delete is called with the physical name
    THEN the client function is called with the name as the physical name.
    """
    mock_client_function = mock.MagicMock()
    mocked_get_function.return_value = helpers.GetFunctionReturn(
        mock_client_function, False
    )
    physical_name = "name 1"

    operations.delete(body=mock.MagicMock(), physical_name=physical_name)

    mock_client_function.assert_called_once_with(name=physical_name)
def test_delete_client_function_namespace_call(mocked_get_function: mock.MagicMock):
    """
    GIVEN mocked get_function that returns a client function and True for namespaced
        and physical name with namespace and name
    WHEN delete is called with the body and physical name
    THEN the client function is called with name and physical name.
    """
    mock_client_function = mock.MagicMock()
    mocked_get_function.return_value = helpers.GetFunctionReturn(
        mock_client_function, True
    )
    name = "name 1"
    namespace = "namespace 1"

    operations.delete(body=mock.MagicMock(), physical_name=f"{namespace}/{name}")

    mock_client_function.assert_called_once_with(namespace=namespace, name=name)
def test_delete_get_function_call(
    mocked_get_api_version: mock.MagicMock,
    mocked_get_kind: mock.MagicMock,
    mocked_get_function: mock.MagicMock,
):
    """
    GIVEN mocked get_api_version, get_kind and get_function
    WHEN delete is called with the body
    THEN get_function is called with get_api_version and get_kind return value and the
        delete operation.
    """
    operations.delete(body=mock.MagicMock(), physical_name="physical name 1")

    mocked_get_function.assert_called_once_with(
        api_version=mocked_get_api_version.return_value,
        kind=mocked_get_kind.return_value,
        operation="delete",
    )
def test_delete_get_kind_raises(mocked_get_kind: mock.MagicMock):
    """
    GIVEN mocked body and mocked get_kind that raises KindMissingError
    WHEN delete is called with the body
    THEN failure response is returned.
    """
    mocked_get_kind.side_effect = exceptions.KindMissingError

    return_value = operations.delete(
        body=mock.MagicMock(), physical_name="physical name 1"
    )

    assert return_value == operations.ExistsReturn("FAILURE", "kind is required.")
def test_delete_get_api_version_raises(mocked_get_api_version: mock.MagicMock):
    """
    GIVEN mocked get_api_version that raises ApiVersionMissingError
    WHEN delete is called
    THEN failure response is returned.
    """
    mocked_get_api_version.side_effect = exceptions.ApiVersionMissingError

    return_value = operations.delete(
        body=mock.MagicMock(), physical_name="physical name 1"
    )

    assert return_value == operations.ExistsReturn("FAILURE", "apiVersion is required.")
def test_cluster_role_delete(cluster_role_info, _cluster_role):
    """
    GIVEN cluster role as dictionary that has been created
    WHEN delete is called with the cluster role dictionary as a body and physical name
    THEN the cluster role is deleted.
    """
    cluster_role_dict, name = cluster_role_info

    result = operations.delete(body=cluster_role_dict, physical_name=name)

    # Check result
    assert result.status == "SUCCESS"
    # Check that the cluster_role is created
    rbac_v1_api = kubernetes.client.RbacAuthorizationV1Api()
    response = rbac_v1_api.list_cluster_role()
    assert response.items
    assert name not in {item.metadata.name for item in response.items}
def test_delete_client_function_return(mocked_get_function: mock.MagicMock):
    """
    GIVEN mocked body and mocked get_function that returns a client function that
        returns the metadata with a name and False for namespaced
    WHEN delete is called
    THEN success response is returned.
    """
    mock_client_function = mock.MagicMock()
    mock_return = mock.MagicMock()
    mock_return.metadata.name = "name 1"
    mock_client_function.return_value = mock_return
    mocked_get_function.return_value = helpers.GetFunctionReturn(
        mock_client_function, False
    )

    return_value = operations.delete(body=mock.MagicMock(), physical_name="name 1")

    assert return_value == operations.ExistsReturn("SUCCESS", None)
def test_deployment_delete(nginx_deployment_info, _nginx_deployment):
    """
    GIVEN deployment as dictionary that has been created
    WHEN delete is called with the deployment dictionary as a body and physical name
    THEN the deployment is deleted.
    """
    deployment_dict, namespace, name = nginx_deployment_info

    result = operations.delete(
        body=deployment_dict, physical_name=f"{namespace}/{name}"
    )

    # Check result
    assert result.status == "SUCCESS"
    # Check that the deployment is deleted
    apps_v1_api = kubernetes.client.AppsV1Api()
    response = apps_v1_api.list_namespaced_deployment(namespace=namespace)
    assert not response.items
def test_delete_client_function_raises(mocked_get_function: mock.MagicMock):
    """
    GIVEN mocked body and mocked get_function that returns a client function that
        raises ApiException and False for namespaced
    WHEN delete is called
    THEN failure response is returned.
    """
    mock_client_function = mock.MagicMock()
    mock_client_function.side_effect = kubernetes.client.rest.ApiException(
        "400", "reason 1"
    )
    mocked_get_function.return_value = helpers.GetFunctionReturn(
        mock_client_function, False
    )

    return_value = operations.delete(body=mock.MagicMock(), physical_name="name 1")

    assert return_value == operations.ExistsReturn(
        "FAILURE", "(400)\nReason: reason 1\n"
    )