From ab3dbb925b287f36691758a7076b6194761b8da9 Mon Sep 17 00:00:00 2001 From: Hope Aitchison Date: Tue, 2 Jan 2024 17:42:01 +0000 Subject: [PATCH] adding in some more go tests for new function invocation --- test/lambda_test.go | 17 +++++++++++------ test/unit-test/main.tf | 21 ++++++++++----------- test/unit-test/outputs.tf | 8 ++++++++ 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/test/lambda_test.go b/test/lambda_test.go index 4abee55..9d02483 100644 --- a/test/lambda_test.go +++ b/test/lambda_test.go @@ -1,10 +1,11 @@ package main import ( - "github.com/gruntwork-io/terratest/modules/terraform" - "github.com/stretchr/testify/assert" "regexp" "testing" + + "github.com/gruntwork-io/terratest/modules/terraform" + "github.com/stretchr/testify/assert" ) func TestLambdaCreation(t *testing.T) { @@ -19,18 +20,22 @@ func TestLambdaCreation(t *testing.T) { terraform.InitAndApply(t, terraformOptions) functionName := terraform.Output(t, terraformOptions, "function_name") + functionVpcName := terraform.Output(t, terraformOptions, "function_vpc_name") resultCode := terraform.Output(t, terraformOptions, "result_code") - + resultVpcCode := terraform.Output(t, terraformOptions, "vpc_result_code") subnetId := terraform.Output(t, terraformOptions, "subnet_ids") securityGroupId := terraform.Output(t, terraformOptions, "security_group_ids") - + re := regexp.MustCompile(`[{}\[\]\s]`) subnetId = re.ReplaceAllString(subnetId, "") securityGroupId = re.ReplaceAllString(securityGroupId, "") assert.Regexp(t, regexp.MustCompile(`^instance-scheduler-lambda-function*`), functionName) assert.Regexp(t, regexp.MustCompile(`^200*`), resultCode) - + assert.Regexp(t, regexp.MustCompile(`^subnet-\w+$`), subnetId) assert.Regexp(t, regexp.MustCompile(`^sg-\w+$`), securityGroupId) -} \ No newline at end of file + + assert.Regexp(t, regexp.MustCompile(`^lambda-function-in-vpc-test*`), functionVpcName) + assert.Regexp(t, regexp.MustCompile(`^200*`), resultVpcCode) +} diff --git a/test/unit-test/main.tf b/test/unit-test/main.tf index c7977db..1248749 100644 --- a/test/unit-test/main.tf +++ b/test/unit-test/main.tf @@ -64,12 +64,6 @@ resource "aws_cloudwatch_event_target" "instance_scheduler_weekly_start_in_the_m ) } -# resource "aws_cloudwatch_log_group" "fake" { -# name = "Lambda/Fake" -# retention_in_days = 365 -# kms_key_id = TODO.arn -# } - #tfsec:ignore:aws-iam-no-policy-wildcards data "aws_iam_policy_document" "instance-scheduler-lambda-function-policy" { # checkov:skip=CKV_AWS_107: "Limiting required permissions" @@ -80,12 +74,8 @@ data "aws_iam_policy_document" "instance-scheduler-lambda-function-policy" { "logs:CreateLogGroup" ] resources = [ - # format("arn:aws:logs:eu-west-2:%s:aws/lambda/%s", data.aws_caller_identity.current.account_id, module.module_test.lambda_function_name) - format("arn:aws:logs:eu-west-2:%s:aws/lambda/instance-scheduler-lambda-function", data.aws_caller_identity.current.account_id) + format("arn:aws:logs:eu-west-2:%s:aws/lambda/fake", data.aws_caller_identity.current.account_id) ] - # resources = [ - # "${aws_cloudwatch_log_group.fake.arn}" - # ] } statement { sid = "AllowLambdaToWriteLogsToGroup" @@ -239,3 +229,12 @@ data "archive_file" "lambda-zip" { source_file = "test-zip/test.py" output_path = "test.zip" } + +resource "aws_lambda_invocation" "test_vpc_invocation" { + function_name = module.lambda_function_in_vpc.lambda_function_name + + input = jsonencode( + { + action = "Test" + }) +} \ No newline at end of file diff --git a/test/unit-test/outputs.tf b/test/unit-test/outputs.tf index 523f12d..c0f99ca 100644 --- a/test/unit-test/outputs.tf +++ b/test/unit-test/outputs.tf @@ -16,4 +16,12 @@ output "security_group_ids" { output "subnet_ids" { value = module.lambda_function_in_vpc.vpc_subnet_ids +} + +output "function_vpc_name" { + value = module.lambda_function_in_vpc.lambda_function_name +} + +output "vpc_result_code" { + value = jsondecode(aws_lambda_invocation.test_vpc_invocation.result)["statusCode"] } \ No newline at end of file