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_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_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_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"
    )
Beispiel #5
0
def test_delete_put_failure(
    mocked_operations_delete: mock.MagicMock,
    exists_lambda_event,
    mocked_urllib3_pool_manager: mock.MagicMock,
):
    """
    GIVEN mocked operations.delete that returns failure response and
        urllib3.PoolManager and delete Cloudformation request
    WHEN lambda_handler is called with the request
    THEN PoolManager.request PUT is called with the ResponseURL from the request with
        the correct body.
    """
    mocked_operations_delete.return_value = operations.ExistsReturn(
        "FAILURE", "reason 1")
    event = {
        **exists_lambda_event,
        **{
            "RequestType": "Delete",
            "ResponseURL": "response url 1",
            "StackId": "stack id 1",
            "RequestId": "request id 1",
            "LogicalResourceId": "logical resource id 1",
            "PhysicalResourceId": "physical resource id 1",
        },
    }

    index.lambda_handler(event, mock.MagicMock())

    mocked_urllib3_pool_manager.return_value.request.assert_called_once_with(
        "PUT",
        "response url 1",
        body=json.dumps({
            "StackId": "stack id 1",
            "RequestId": "request id 1",
            "LogicalResourceId": "logical resource id 1",
            "PhysicalResourceId": "physical resource id 1",
            "Status": "FAILURE",
            "Reason": "reason 1",
        }).encode("utf-8"),
    )