Skip to content

Commit

Permalink
adding in some more go tests for new function invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
haitchison committed Jan 2, 2024
1 parent 6018c88 commit ab3dbb9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
17 changes: 11 additions & 6 deletions test/lambda_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)
}

assert.Regexp(t, regexp.MustCompile(`^lambda-function-in-vpc-test*`), functionVpcName)
assert.Regexp(t, regexp.MustCompile(`^200*`), resultVpcCode)
}
21 changes: 10 additions & 11 deletions test/unit-test/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
})
}
8 changes: 8 additions & 0 deletions test/unit-test/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}

0 comments on commit ab3dbb9

Please sign in to comment.