Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 76 additions & 2 deletions internal/service/lambda/invoke_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

"github.com/aws/aws-sdk-go-v2/service/lambda"
awstypes "github.com/aws/aws-sdk-go-v2/service/lambda/types"
"github.com/hashicorp/terraform-plugin-testing/actioncheck"

Check failure on line 15 in internal/service/lambda/invoke_action_test.go

View workflow job for this annotation

GitHub Actions / providerlint

could not import github.com/hashicorp/terraform-plugin-testing/actioncheck (invalid package name: "")

Check failure on line 15 in internal/service/lambda/invoke_action_test.go

View workflow job for this annotation

GitHub Actions / providerlint

no required module provides package github.com/hashicorp/terraform-plugin-testing/actioncheck; to add it:

Check failure on line 15 in internal/service/lambda/invoke_action_test.go

View workflow job for this annotation

GitHub Actions / Check for modern Go code

could not import github.com/hashicorp/terraform-plugin-testing/actioncheck (invalid package name: "")

Check failure on line 15 in internal/service/lambda/invoke_action_test.go

View workflow job for this annotation

GitHub Actions / Check for modern Go code

no required module provides package github.com/hashicorp/terraform-plugin-testing/actioncheck; to add it:

Check failure on line 15 in internal/service/lambda/invoke_action_test.go

View workflow job for this annotation

GitHub Actions / go test

no required module provides package github.com/hashicorp/terraform-plugin-testing/actioncheck; to add it:
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
Expand Down Expand Up @@ -138,13 +139,13 @@
CheckDestroy: acctest.CheckDestroyNoop,
Steps: []resource.TestStep{
{
Config: testAccInvokeActionConfig_logType(rName, testData, inputJSON, "None"),
Config: testAccInvokeActionConfig_logType(rName, testData, inputJSON, string(awstypes.LogTypeNone)),
Check: resource.ComposeTestCheckFunc(
testAccCheckInvokeActionLogType(ctx, rName, inputJSON, awstypes.LogTypeNone),
),
},
{
Config: testAccInvokeActionConfig_logType(rName, testData, inputJSON, "Tail"),
Config: testAccInvokeActionConfig_logType(rName, testData, inputJSON, string(awstypes.LogTypeTail)),
Check: resource.ComposeTestCheckFunc(
testAccCheckInvokeActionLogType(ctx, rName, inputJSON, awstypes.LogTypeTail),
),
Expand Down Expand Up @@ -182,6 +183,39 @@
})
}

func TestAccLambdaInvokeAction_logTypeTail(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
testData := "log_output_test"
inputJSON := `{"key1":"value1","key2":"value2"}`

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, names.LambdaEndpointID)
},
ErrorCheck: acctest.ErrorCheck(t, names.LambdaServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_14_0),
},
CheckDestroy: acctest.CheckDestroyNoop,
Steps: []resource.TestStep{
{
Config: testAccInvokeActionConfig_logType(rName, testData, inputJSON, string(awstypes.LogTypeTail)),
ActionChecks: []actioncheck.ActionCheck{

Check failure on line 206 in internal/service/lambda/invoke_action_test.go

View workflow job for this annotation

GitHub Actions / providerlint

unknown field ActionChecks in struct literal of type "github.com/hashicorp/terraform-plugin-testing/helper/resource".TestStep

Check failure on line 206 in internal/service/lambda/invoke_action_test.go

View workflow job for this annotation

GitHub Actions / Check for modern Go code

unknown field ActionChecks in struct literal of type "github.com/hashicorp/terraform-plugin-testing/helper/resource".TestStep
resource.TestCheckProgressMessageContains("aws_lambda_invoke.test", "Lambda function logs:"),

Check failure on line 207 in internal/service/lambda/invoke_action_test.go

View workflow job for this annotation

GitHub Actions / providerlint

undefined: resource.TestCheckProgressMessageContains

Check failure on line 207 in internal/service/lambda/invoke_action_test.go

View workflow job for this annotation

GitHub Actions / Check for modern Go code

undefined: resource.TestCheckProgressMessageContains
resource.TestCheckProgressMessageContains("aws_lambda_invoke.test", "invoked successfully"),

Check failure on line 208 in internal/service/lambda/invoke_action_test.go

View workflow job for this annotation

GitHub Actions / providerlint

undefined: resource.TestCheckProgressMessageContains

Check failure on line 208 in internal/service/lambda/invoke_action_test.go

View workflow job for this annotation

GitHub Actions / Check for modern Go code

undefined: resource.TestCheckProgressMessageContains
resource.TestCheckProgressMessageCount("aws_lambda_invoke.test", 2),

Check failure on line 209 in internal/service/lambda/invoke_action_test.go

View workflow job for this annotation

GitHub Actions / providerlint

undefined: resource.TestCheckProgressMessageCount

Check failure on line 209 in internal/service/lambda/invoke_action_test.go

View workflow job for this annotation

GitHub Actions / Check for modern Go code

undefined: resource.TestCheckProgressMessageCount
},
Check: resource.ComposeTestCheckFunc(
testAccCheckInvokeActionLogType(ctx, rName, inputJSON, awstypes.LogTypeTail),
),
},
},
})
}

func TestAccLambdaInvokeAction_complexPayload(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down Expand Up @@ -348,6 +382,46 @@
}
}

// testAccCheckInvokeActionLogOutput verifies that log output is captured and can be decoded
func testAccCheckInvokeActionLogOutput(ctx context.Context, functionName, inputJSON string) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).LambdaClient(ctx)

input := &lambda.InvokeInput{
FunctionName: &functionName,
InvocationType: awstypes.InvocationTypeRequestResponse,
Payload: []byte(inputJSON),
LogType: awstypes.LogTypeTail,
}

output, err := conn.Invoke(ctx, input)
if err != nil {
return fmt.Errorf("Failed to invoke Lambda function %s with log type Tail: %w", functionName, err)
}

if output.FunctionError != nil {
return fmt.Errorf("Lambda function %s returned an error: %s", functionName, string(output.Payload))
}

// Verify log result is present
if output.LogResult == nil {
return fmt.Errorf("Expected log result when log type is Tail, but got none")
}

logData, err := base64.StdEncoding.DecodeString(*output.LogResult)
if err != nil {
return fmt.Errorf("Failed to decode log result: %w", err)
}

logStr := string(logData)
if len(logStr) == 0 {
return fmt.Errorf("Decoded log result is empty")
}

return nil
}
}

// testAccCheckInvokeActionClientContext verifies client context is passed correctly
func testAccCheckInvokeActionClientContext(ctx context.Context, functionName, inputJSON, clientContext string) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down
Loading