Skip to content

Commit

Permalink
Use different .terraform dir for each lambda test
Browse files Browse the repository at this point in the history
  • Loading branch information
kbalk committed Apr 2, 2021
1 parent a6637ff commit f10e0c2
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions test/terraform_aws_lambda_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -15,6 +16,10 @@ import (
func TestTerraformAwsLambdaExample(t *testing.T) {
t.Parallel()

// Make a copy of the terraform module to a temporary directory. This allows running multiple tests in parallel
// against the same terraform module.
exampleFolder := test_structure.CopyTerraformFolderToTemp(t, "../", "examples/terraform-aws-lambda-example")

// Give this lambda function a unique ID for a name so we can distinguish it from any other lambdas
// in your AWS account
functionName := fmt.Sprintf("terratest-aws-lambda-example-%s", random.UniqueId())
Expand All @@ -26,7 +31,7 @@ func TestTerraformAwsLambdaExample(t *testing.T) {
// terraform testing.
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: "../examples/terraform-aws-lambda-example",
TerraformDir: exampleFolder,

// Variables to pass to our Terraform code using -var options
Vars: map[string]interface{}{
Expand Down Expand Up @@ -68,6 +73,10 @@ func TestTerraformAwsLambdaExample(t *testing.T) {
func TestTerraformAwsLambdaWithParamsExample(t *testing.T) {
t.Parallel()

// Make a copy of the terraform module to a temporary directory. This allows running multiple tests in parallel
// against the same terraform module.
exampleFolder := test_structure.CopyTerraformFolderToTemp(t, "../", "examples/terraform-aws-lambda-example")

// Give this lambda function a unique ID for a name so we can distinguish it from any other lambdas
// in your AWS account
functionName := fmt.Sprintf("terratest-aws-lambda-withparams-example-%s", random.UniqueId())
Expand All @@ -79,7 +88,7 @@ func TestTerraformAwsLambdaWithParamsExample(t *testing.T) {
// terraform testing.
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: "../examples/terraform-aws-lambda-example",
TerraformDir: exampleFolder,

// Variables to pass to our Terraform code using -var options
Vars: map[string]interface{}{
Expand Down Expand Up @@ -120,9 +129,7 @@ func TestTerraformAwsLambdaWithParamsExample(t *testing.T) {
out, err := aws.InvokeFunctionWithParamsE(t, awsRegion, functionName, input)

// The Lambda executed, but should have failed.
require.NotNil(t, err)
assert.Contains(t, err.Error(), "Unhandled")
assert.Equal(t, int(*out.StatusCode), 200)
assert.Error(t, err, "Unhandled")

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

0 comments on commit f10e0c2

Please sign in to comment.