Skip to content

Commit

Permalink
Add a InvokceFunctionWithParamsE() func
Browse files Browse the repository at this point in the history
  • Loading branch information
kbalk committed Mar 29, 2021
1 parent 6d632a9 commit 8774e95
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
26 changes: 17 additions & 9 deletions modules/aws/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import (
type LambdaOptions struct {
// InvocationType can be one of lambda.InvocationTypeRequestResponse
// or lambda.InvocationTypeDryRun.
// * RequestResponse (default) - Invoke the function synchronously.
// Keep the connection open until the function returns a response
// or times out.
//
// * DryRun - Validate parameter values and verify that the user or
// role has permission to invoke the function.
// * InvocationTypeRequestResponse (default) - Invoke the function
// synchronously. Keep the connection open until the function
// returns a response or times out.
// * InvocationTypeDryRun - Validate parameter values and verify
// that the user or role has permission to invoke the function.
InvocationType *string `enum:"lambda.InvocationType"`

// Lambda function input; will be converted to JSON.
Expand Down Expand Up @@ -53,7 +52,7 @@ 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 := InvokeFunctionWithParams(t, region, functionName, input)
out, err := InvokeFunctionWithParamsE(t, region, functionName, input)
if err != nil {
return nil, err
}
Expand All @@ -67,8 +66,17 @@ func InvokeFunctionE(t testing.TestingT, region, functionName string, payload in

// InvokeFunctionWithParams invokes a lambda function using parameters
// supplied in the LambdaOptions struct and returns values in a LambdaOutput
// struct.
func InvokeFunctionWithParams(t testing.TestingT, region, functionName string, input *LambdaOptions) (*LambdaOutput, error) {
// struct. Checks for failure using "require".
func InvokeFunctionWithParams(t testing.TestingT, region, functionName string, input *LambdaOptions) *LambdaOutput {
out, err := InvokeFunctionWithParamsE(t, region, functionName, input)
require.NoError(t, err)
return out
}

// InvokeFunctionWithParamsE invokes a lambda function using parameters
// supplied in the LambdaOptions struct and returns values in a LambdaOutput
// struct and the error.
func InvokeFunctionWithParamsE(t testing.TestingT, region, functionName string, input *LambdaOptions) (*LambdaOutput, error) {
lambdaClient, err := NewLambdaClientE(t, region)
if err != nil {
return nil, err
Expand Down
19 changes: 17 additions & 2 deletions test/terraform_aws_lambda_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,28 @@ func TestTerraformAwsLambdaWithParamsExample(t *testing.T) {
// test function will not be checking the payload.
invocationType := lambda.InvocationTypeDryRun
input := &aws.LambdaOptions{InvocationType: &invocationType}
out, err := aws.InvokeFunctionWithParams(t, awsRegion, functionName, input)
out := aws.InvokeFunctionWithParams(t, awsRegion, functionName, input)

// With "DryRun", there's no message in the output, but there is
// a status code which will have a value of 204 for a successful
// invocation.
require.Nil(t, err)
assert.Equal(t, int(*out.StatusCode), 204)

// Invoke the function, this time causing the Lambda to error and
// capturing the error.
invocationType = lambda.InvocationTypeRequestResponse
input = &aws.LambdaOptions{
InvocationType: &invocationType,
Payload: ExampleFunctionPayload{ShouldFail: true, Echo: "hi!"},
}
out, err := aws.InvokeFunctionWithParamsE(t, awsRegion, functionName, input)

// No error in the invocation as Lambda was found and executed.
require.Nil(t, err)
assert.Equal(t, int(*out.StatusCode), 200)

// Make sure the function-specific error comes back
assert.Contains(t, string(out.Payload), "Failed to handle")
}

type ExampleFunctionPayload struct {
Expand Down

0 comments on commit 8774e95

Please sign in to comment.