Skip to content

Commit

Permalink
finished refactor of unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dms1981 committed Oct 14, 2024
1 parent e2c40c1 commit 86a82ce
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
@@ -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" {
Expand Down
33 changes: 16 additions & 17 deletions test/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
}
8 changes: 4 additions & 4 deletions test/unit-test/outputs.tf
Original file line number Diff line number Diff line change
@@ -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 }
}

0 comments on commit 86a82ce

Please sign in to comment.