From a2e7ae23627fc742d5c72f24d46c6be575dd38a6 Mon Sep 17 00:00:00 2001 From: Karri Balk Date: Thu, 1 Apr 2021 11:26:12 -0400 Subject: [PATCH] Remove FunctionError field from LambdaOutput Restore original InvokeFunctionE(). --- modules/aws/lambda.go | 55 ++++++++++++++++------- test/terraform_aws_lambda_example_test.go | 9 ++-- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/modules/aws/lambda.go b/modules/aws/lambda.go index 9ed1cfacb0..634d7290a3 100644 --- a/modules/aws/lambda.go +++ b/modules/aws/lambda.go @@ -53,10 +53,6 @@ type LambdaOptions struct { // fields may or may not have a value depending on the invocation type and // whether an error occurred or not. type LambdaOutput struct { - // If present, indicates that an error occurred during function execution. - // Error details are included in the response payload. - FunctionError *string - // The response from the function, or an error object. Payload []byte @@ -75,8 +71,26 @@ func InvokeFunction(t testing.TestingT, region, functionName string, payload int // InvokeFunctionE invokes a lambda function. func InvokeFunctionE(t testing.TestingT, region, functionName string, payload interface{}) ([]byte, error) { - input := &LambdaOptions{Payload: &payload} - out, err := InvokeFunctionWithParamsE(t, region, functionName, input) + lambdaClient, err := NewLambdaClientE(t, region) + if err != nil { + return nil, err + } + + invokeInput := &lambda.InvokeInput{ + FunctionName: &functionName, + } + + if payload != nil { + payloadJson, err := json.Marshal(payload) + + if err != nil { + return nil, err + } + invokeInput.Payload = payloadJson + } + + out, err := lambdaClient.Invoke(invokeInput) + require.NoError(t, err) if err != nil { return nil, err } @@ -98,8 +112,10 @@ func InvokeFunctionWithParams(t testing.TestingT, region, functionName string, i } // InvokeFunctionWithParamsE invokes a lambda function using parameters -// supplied in the LambdaOptions struct and returns values in a LambdaOutput -// struct and the error. +// supplied in the LambdaOptions struct. Returns the status code and payload +// in a LambdaOutput struct and the error. A non-nil error will either reflect +// a problem with the parameters supplied to this function or an error returned +// by the Lambda. func InvokeFunctionWithParamsE(t testing.TestingT, region, functionName string, input *LambdaOptions) (*LambdaOutput, error) { lambdaClient, err := NewLambdaClientE(t, region) if err != nil { @@ -111,8 +127,7 @@ func InvokeFunctionWithParamsE(t testing.TestingT, region, functionName string, // "RequestResponse". invocationType, err := input.InvocationType.Value() if err != nil { - msg := err.Error() - return &LambdaOutput{FunctionError: &msg}, err + return nil, err } invokeInput := &lambda.InvokeInput{ @@ -129,15 +144,23 @@ func InvokeFunctionWithParamsE(t testing.TestingT, region, functionName string, } out, err := lambdaClient.Invoke(invokeInput) + if err != nil { + return nil, err + } - // As this function supports different invocation types, so it must - // support different combinations of output. + // As this function supports different invocation types, it must + // then support different combinations of output other than just + // payload. lambdaOutput := LambdaOutput{ - FunctionError: out.FunctionError, - Payload: out.Payload, - StatusCode: out.StatusCode, + Payload: out.Payload, + StatusCode: out.StatusCode, } - return &lambdaOutput, err + + if out.FunctionError != nil { + return &lambdaOutput, errors.New(*out.FunctionError) + } + + return &lambdaOutput, nil } type FunctionError struct { diff --git a/test/terraform_aws_lambda_example_test.go b/test/terraform_aws_lambda_example_test.go index 9da241d918..fab47b8dd1 100644 --- a/test/terraform_aws_lambda_example_test.go +++ b/test/terraform_aws_lambda_example_test.go @@ -119,8 +119,9 @@ func TestTerraformAwsLambdaWithParamsExample(t *testing.T) { } out, err := aws.InvokeFunctionWithParamsE(t, awsRegion, functionName, input) - // No error in the invocation as Lambda was found and executed. - require.Nil(t, err) + // The Lambda executed, but should have failed. + require.NotNil(t, err) + assert.Contains(t, err.Error(), "Unhandled") assert.Equal(t, int(*out.StatusCode), 200) // Make sure the function-specific error comes back @@ -135,9 +136,7 @@ func TestTerraformAwsLambdaWithParamsExample(t *testing.T) { } out, err = aws.InvokeFunctionWithParamsE(t, awsRegion, functionName, input) require.NotNil(t, err) - msg := "LambdaOptions.InvocationType, if specified, must either be \"RequestResponse\" or \"DryRun\"" - assert.Contains(t, err.Error(), msg) - assert.Contains(t, *out.FunctionError, msg) + assert.Contains(t, err.Error(), "LambdaOptions.InvocationType, if specified, must either be \"RequestResponse\" or \"DryRun\"") } type ExampleFunctionPayload struct {