def test_create_get_kind_raises(mocked_get_kind: mock.MagicMock): """ GIVEN mocked body and mocked get_kind that raises KindMissingError WHEN create is called with the body THEN failure response is returned. """ mocked_get_kind.side_effect = exceptions.KindMissingError return_value = operations.create(body=mock.MagicMock()) assert return_value == operations.CreateReturn("FAILURE", "kind is required.", None)
def test_create_client_function_return(mocked_get_function: mock.MagicMock): """ GIVEN mocked get_function that returns a client function that returns the metadata with a name and False for namespaced WHEN create 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.create(body=mock.MagicMock()) assert return_value == operations.CreateReturn("SUCCESS", None, "name 1")
def test_create_client_function_namespace_raises(mocked_get_function: mock.MagicMock): """ GIVEN mocked get_function that returns a client function that raises ApiException and True for namespaced WHEN create 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, True ) return_value = operations.create(body=mock.MagicMock()) assert return_value == operations.CreateReturn( "FAILURE", "(400)\nReason: reason 1\n", None )
def test_create_put_failure( mocked_operations_create: mock.MagicMock, create_lambda_event, mocked_urllib3_pool_manager: mock.MagicMock, ): """ GIVEN mocked operations.create that returns failure response and urllib3.PoolManager and create 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_create.return_value = operations.CreateReturn( "FAILURE", "reason 1", None) event = { **create_lambda_event, **{ "RequestType": "Create", "ResponseURL": "response url 1", "StackId": "stack id 1", "RequestId": "request id 1", "LogicalResourceId": "logical 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", "Status": "FAILURE", "PhysicalResourceId": "[FAIL]logical resource id 1", "Reason": "reason 1", }).encode("utf-8"), )