Skip to content

Commit

Permalink
Run tests in parallel (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
grem11n authored Nov 4, 2024
1 parent fc13f57 commit 15de829
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/custom-name-tag/main.tf
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions examples/custom-name-tag/outputs.tf
Original file line number Diff line number Diff line change
@@ -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
}
65 changes: 65 additions & 0 deletions test/fixtures/custom-name/main.tf
Original file line number Diff line number Diff line change
@@ -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"
}
}
7 changes: 7 additions & 0 deletions test/fixtures/custom-name/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "this_vpc_id" {
value = aws_vpc.this.id
}

output "peer_vpc_id" {
value = aws_vpc.peer.id
}
13 changes: 13 additions & 0 deletions test/fixtures/custom-name/provider.tf
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 17 additions & 0 deletions test/fixtures/custom-name/variables.tf
Original file line number Diff line number Diff line change
@@ -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"]
}
65 changes: 65 additions & 0 deletions test/fixtures/default-name/main.tf
Original file line number Diff line number Diff line change
@@ -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"
}
}
7 changes: 7 additions & 0 deletions test/fixtures/default-name/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "this_vpc_id" {
value = aws_vpc.this.id
}

output "peer_vpc_id" {
value = aws_vpc.peer.id
}
13 changes: 13 additions & 0 deletions test/fixtures/default-name/provider.tf
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 17 additions & 0 deletions test/fixtures/default-name/variables.tf
Original file line number Diff line number Diff line change
@@ -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"]
}
19 changes: 15 additions & 4 deletions test/peering-active_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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"},
Expand All @@ -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
Expand All @@ -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,
}

Expand Down

0 comments on commit 15de829

Please sign in to comment.