From 86a82ce0b7c23b4dd7fd086745fbdb0cd69887fe Mon Sep 17 00:00:00 2001 From: David Sibley Date: Mon, 14 Oct 2024 15:46:36 +0100 Subject: [PATCH] finished refactor of unit tests --- outputs.tf | 2 +- test/module_test.go | 33 ++++++++++++++++----------------- test/unit-test/outputs.tf | 8 ++++---- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/outputs.tf b/outputs.tf index c7735bc..3b18dbe 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,6 +1,6 @@ output "bastion_security_group" { description = "Security group of bastion" - value = aws_security_group.bastion_linux.id + value = aws_security_group.bastion_linux } output "bastion_launch_template" { diff --git a/test/module_test.go b/test/module_test.go index 254662a..2bd270e 100644 --- a/test/module_test.go +++ b/test/module_test.go @@ -20,21 +20,20 @@ func TestBastionCreation(t *testing.T) { terraform.InitAndApply(t, terraformOptions) - bastionSecurityGroup1 := terraform.Output(t, terraformOptions, "bastion_security_group_1") - bastionLaunchTemplate1 := terraform.Output(t, terraformOptions, "bastion_launch_template_1") - bastionS3Bucket1 := terraform.Output(t, terraformOptions, "bastion_s3_bucket_1") - - assert.Regexp(t, regexp.MustCompile(`^sg-*`), bastionSecurityGroup1) - assert.Contains(t, bastionLaunchTemplate1, "arn:aws:ec2:eu-west-2:") - assert.Contains(t, bastionLaunchTemplate1, "instance_type:t3.micro") - assert.Contains(t, bastionS3Bucket1, "arn:aws:s3:::bastion-1-testing-test-") - - bastionSecurityGroup2 := terraform.Output(t, terraformOptions, "bastion_security_group_2") - bastionLaunchTemplate2 := terraform.Output(t, terraformOptions, "bastion_launch_template_2") - bastionS3Bucket2 := terraform.Output(t, terraformOptions, "bastion_s3_bucket_2") - - assert.Regexp(t, regexp.MustCompile(`^sg-*`), bastionSecurityGroup2) - assert.Contains(t, bastionLaunchTemplate2, "arn:aws:ec2:eu-west-2:") - assert.Contains(t, bastionLaunchTemplate2, "instance_type:t3.micro") - assert.Contains(t, bastionS3Bucket2, "arn:aws:s3:::bastion-2-testing-test-") + bastionKMS := terraform.OutputMap(t, terraformOptions, "bastion_kms_key") + bastionLaunchTemplate := terraform.OutputMap(t, terraformOptions, "bastion_launch_template") + bastionSecurityGroup := terraform.OutputMap(t, terraformOptions, "bastion_security_group") + bastionS3Bucket := terraform.OutputMap(t, terraformOptions, "bastion_s3_bucket") + + assert.Regexp(t, regexp.MustCompile(`[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}`), bastionKMS["bastion_0"]) + assert.Regexp(t, regexp.MustCompile(`[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}`), bastionKMS["bastion_1"]) + + assert.Regexp(t, regexp.MustCompile(`lt-[a-f0-9]{17}`), bastionLaunchTemplate["bastion_0"]) + assert.Regexp(t, regexp.MustCompile(`lt-[a-f0-9]{17}`), bastionLaunchTemplate["bastion_1"]) + + assert.Regexp(t, regexp.MustCompile(`sg-[a-f0-9]{17}`), bastionSecurityGroup["bastion_0"]) + assert.Regexp(t, regexp.MustCompile(`sg-[a-f0-9]{17}`), bastionSecurityGroup["bastion_1"]) + + assert.Regexp(t, regexp.MustCompile(`bastion-\d-testing-test-[a-z0-9]{6}`), bastionS3Bucket["bastion_0"]) + assert.Regexp(t, regexp.MustCompile(`bastion-\d-testing-test-[a-z0-9]{6}`), bastionS3Bucket["bastion_1"]) } diff --git a/test/unit-test/outputs.tf b/test/unit-test/outputs.tf index 5bb2635..2c5bbd8 100644 --- a/test/unit-test/outputs.tf +++ b/test/unit-test/outputs.tf @@ -1,19 +1,19 @@ output "bastion_launch_template" { description = "Bastion launch templates" - value = { for idx, bastion in module.bastion_linux : "bastion_${idx}" => bastion.bastion_launch_template } + value = { for idx, bastion in module.bastion_linux : "bastion_${idx}" => bastion.bastion_launch_template.id } } output "bastion_s3_bucket" { description = "Bastion S3 buckets" - value = { for idx, bastion in module.bastion_linux : "bastion_${idx}" => bastion.bastion_s3_bucket } + value = { for idx, bastion in module.bastion_linux : "bastion_${idx}" => bastion.bastion_s3_bucket.bucket.id } } output "bastion_security_group" { description = "Bastion security groups" - value = { for idx, bastion in module.bastion_linux : "bastion_${idx}" => bastion.bastion_security_group } + value = { for idx, bastion in module.bastion_linux : "bastion_${idx}" => bastion.bastion_security_group.id } } output "bastion_kms_key" { description = "Bastion KMS keys" - value = { for idx, bastion in module.bastion_linux : "bastion_${idx}" => bastion.bastion_kms_key } + value = { for idx, bastion in module.bastion_linux : "bastion_${idx}" => bastion.bastion_kms_key[0].id } }