diff --git a/examples/custom-name-tag/main.tf b/examples/custom-name-tag/main.tf index cabd074..54333d8 100644 --- a/examples/custom-name-tag/main.tf +++ b/examples/custom-name-tag/main.tf @@ -1,6 +1,6 @@ // Basic Module Example // Creates a peering between VPCs in the same account in the same region -module "custiom_name" { +module "custom_name" { source = "../../" providers = { diff --git a/examples/custom-name-tag/outputs.tf b/examples/custom-name-tag/outputs.tf index 712781d..eac2696 100644 --- a/examples/custom-name-tag/outputs.tf +++ b/examples/custom-name-tag/outputs.tf @@ -1,8 +1,8 @@ // Required for tests output "vpc_peering_accept_status" { - value = module.custiom_name.vpc_peering_accept_status + value = module.custom_name.vpc_peering_accept_status } output "vpc_peering_connection" { - value = module.custiom_name.aws_vpc_peering_connection + value = module.custom_name.aws_vpc_peering_connection } diff --git a/test/fixtures/custom-name/main.tf b/test/fixtures/custom-name/main.tf new file mode 100644 index 0000000..3575314 --- /dev/null +++ b/test/fixtures/custom-name/main.tf @@ -0,0 +1,65 @@ +// Fixtures +// VPCs +resource "aws_vpc" "this" { + cidr_block = "172.22.0.0/16" + + tags = { + Name = "this_vpc" + Environment = "Test" + } +} + +resource "aws_vpc" "peer" { + cidr_block = "172.23.0.0/16" + + tags = { + Name = "peer_vpc" + Environment = "Test" + } +} + +// Route Tables +resource "aws_route_table" "this" { + count = length(var.this_subnets) + vpc_id = aws_vpc.this.id + + tags = { + Name = "This VPC RT" + Environment = "Test" + } +} + +resource "aws_route_table" "peer" { + count = length(var.peer_subnets) + vpc_id = aws_vpc.peer.id + + tags = { + Name = "Peer VPC RT" + Environment = "Test" + } +} + +// Subnets +resource "aws_subnet" "this" { + count = length(var.this_subnets) + vpc_id = aws_vpc.this.id + cidr_block = var.this_subnets[count.index] + availability_zone = element(var.azs, count.index) + + tags = { + Name = "This VPC Subnet" + Environment = "Test" + } +} + +resource "aws_subnet" "peer" { + count = length(var.peer_subnets) + vpc_id = aws_vpc.peer.id + cidr_block = var.peer_subnets[count.index] + availability_zone = element(var.azs, count.index) + + tags = { + Name = "This VPC Subnet" + Environment = "Test" + } +} diff --git a/test/fixtures/custom-name/outputs.tf b/test/fixtures/custom-name/outputs.tf new file mode 100644 index 0000000..9e20e58 --- /dev/null +++ b/test/fixtures/custom-name/outputs.tf @@ -0,0 +1,7 @@ +output "this_vpc_id" { + value = aws_vpc.this.id +} + +output "peer_vpc_id" { + value = aws_vpc.peer.id +} diff --git a/test/fixtures/custom-name/provider.tf b/test/fixtures/custom-name/provider.tf new file mode 100644 index 0000000..4aa3a6c --- /dev/null +++ b/test/fixtures/custom-name/provider.tf @@ -0,0 +1,13 @@ +provider "aws" { + endpoints { + ec2 = "http://localhost:4566" + s3 = "http://localhost:4566" + sts = "http://localhost:4566" + } + region = "eu-west-1" + access_key = "null" + secret_key = "null" + skip_credentials_validation = true + skip_metadata_api_check = true + skip_requesting_account_id = true +} diff --git a/test/fixtures/custom-name/variables.tf b/test/fixtures/custom-name/variables.tf new file mode 100644 index 0000000..8e75f92 --- /dev/null +++ b/test/fixtures/custom-name/variables.tf @@ -0,0 +1,17 @@ +variable "this_subnets" { + description = "Subnet list for _this_ VPC" + type = list(string) + default = ["172.22.0.0/24", "172.22.1.0/24", "172.22.2.0/24"] +} + +variable "peer_subnets" { + description = "Subnet list for _peer_ VPC" + type = list(string) + default = ["172.23.0.0/24", "172.23.1.0/24", "172.23.2.0/24"] +} + +variable "azs" { + description = "Availability Zones" + type = list(string) + default = ["eu-west-1a", "eu-west-1b", "eu-west-1c"] +} diff --git a/test/fixtures/default-name/main.tf b/test/fixtures/default-name/main.tf new file mode 100644 index 0000000..9f6bdd7 --- /dev/null +++ b/test/fixtures/default-name/main.tf @@ -0,0 +1,65 @@ +// Fixtures +// VPCs +resource "aws_vpc" "this" { + cidr_block = "172.24.0.0/16" + + tags = { + Name = "this_vpc" + Environment = "Test" + } +} + +resource "aws_vpc" "peer" { + cidr_block = "172.25.0.0/16" + + tags = { + Name = "peer_vpc" + Environment = "Test" + } +} + +// Route Tables +resource "aws_route_table" "this" { + count = length(var.this_subnets) + vpc_id = aws_vpc.this.id + + tags = { + Name = "This VPC RT" + Environment = "Test" + } +} + +resource "aws_route_table" "peer" { + count = length(var.peer_subnets) + vpc_id = aws_vpc.peer.id + + tags = { + Name = "Peer VPC RT" + Environment = "Test" + } +} + +// Subnets +resource "aws_subnet" "this" { + count = length(var.this_subnets) + vpc_id = aws_vpc.this.id + cidr_block = var.this_subnets[count.index] + availability_zone = element(var.azs, count.index) + + tags = { + Name = "This VPC Subnet" + Environment = "Test" + } +} + +resource "aws_subnet" "peer" { + count = length(var.peer_subnets) + vpc_id = aws_vpc.peer.id + cidr_block = var.peer_subnets[count.index] + availability_zone = element(var.azs, count.index) + + tags = { + Name = "This VPC Subnet" + Environment = "Test" + } +} diff --git a/test/fixtures/default-name/outputs.tf b/test/fixtures/default-name/outputs.tf new file mode 100644 index 0000000..9e20e58 --- /dev/null +++ b/test/fixtures/default-name/outputs.tf @@ -0,0 +1,7 @@ +output "this_vpc_id" { + value = aws_vpc.this.id +} + +output "peer_vpc_id" { + value = aws_vpc.peer.id +} diff --git a/test/fixtures/default-name/provider.tf b/test/fixtures/default-name/provider.tf new file mode 100644 index 0000000..4aa3a6c --- /dev/null +++ b/test/fixtures/default-name/provider.tf @@ -0,0 +1,13 @@ +provider "aws" { + endpoints { + ec2 = "http://localhost:4566" + s3 = "http://localhost:4566" + sts = "http://localhost:4566" + } + region = "eu-west-1" + access_key = "null" + secret_key = "null" + skip_credentials_validation = true + skip_metadata_api_check = true + skip_requesting_account_id = true +} diff --git a/test/fixtures/default-name/variables.tf b/test/fixtures/default-name/variables.tf new file mode 100644 index 0000000..88d43e3 --- /dev/null +++ b/test/fixtures/default-name/variables.tf @@ -0,0 +1,17 @@ +variable "this_subnets" { + description = "Subnet list for _this_ VPC" + type = list(string) + default = ["172.24.0.0/24", "172.24.1.0/24", "172.24.2.0/24"] +} + +variable "peer_subnets" { + description = "Subnet list for _peer_ VPC" + type = list(string) + default = ["172.25.0.0/24", "172.25.1.0/24", "172.25.2.0/24"] +} + +variable "azs" { + description = "Availability Zones" + type = list(string) + default = ["eu-west-1a", "eu-west-1b", "eu-west-1c"] +} diff --git a/test/peering-active_test.go b/test/peering-active_test.go index 5499abc..7191f66 100644 --- a/test/peering-active_test.go +++ b/test/peering-active_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/gruntwork-io/terratest/modules/terraform" + test_structure "github.com/gruntwork-io/terratest/modules/test-structure" "github.com/stretchr/testify/assert" ) @@ -15,6 +16,7 @@ type TestCase struct { } func TestPeeringActive(t *testing.T) { + t.Parallel() testCases := []TestCase{ {"SingleAccountSingleRegion", "./fixtures/single-account-single-region", "../examples/single-account-single-region"}, {"SingleAccountSingleRegionWithOptions", "./fixtures/single-account-single-region-with-options", "../examples/single-account-single-region-with-options"}, @@ -30,27 +32,36 @@ func TestPeeringActive(t *testing.T) { } for _, tc := range testCases { + tc := tc t.Run(tc.Name, func(t *testing.T) { + t.Parallel() terratestRun(tc, t) }) } } func TestConnectionName(t *testing.T) { + t.Parallel() var testCases = []struct { name string + fixtures string expected string }{ - {"DefaultName", "tf-single-account-single-region"}, - {"CustomName", "tf-custom-name"}, + {"DefaultName", "./fixtures/default-name", "tf-single-account-single-region"}, + {"CustomName", "./fixtures/custom-name", "tf-custom-name"}, } for _, tc := range testCases { + tc := tc t.Run(tc.name, func(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. + moduleFolder := test_structure.CopyTerraformFolderToTemp(t, "../", "examples/custom-name-tag") var tfVars = make(map[string]interface{}) // Apply the fixtures fixturesTerraformOptions := &terraform.Options{ - TerraformDir: "./fixtures/single-account-single-region", // hardcoded + TerraformDir: tc.fixtures, } // Remove the fixtures resources in the end of the test @@ -72,7 +83,7 @@ func TestConnectionName(t *testing.T) { // Terraform Options for module moduleTerraformOptions := &terraform.Options{ - TerraformDir: "../examples/custom-name-tag", // hardcoded + TerraformDir: moduleFolder, Vars: tfVars, }