From 561a61ceee0915f2b78a17cb23122754a714ffbe Mon Sep 17 00:00:00 2001 From: jsclarridge <2491291+jsclarridge@users.noreply.github.com> Date: Mon, 11 Nov 2019 08:20:59 -0800 Subject: [PATCH 1/5] Add terratests for all supported services https://www.pivotaltracker.com/story/show/169682491 --- Makefile | 2 +- examples/alb/main.tf | 27 +++++++ examples/alb/variables.tf | 15 ++++ examples/combined/main.tf | 94 ++++++++++++++++++++++ examples/combined/variables.tf | 20 +++++ examples/config/main.tf | 15 ++++ examples/config/variables.tf | 7 ++ examples/elb/main.tf | 32 ++++++++ examples/elb/variables.tf | 15 ++++ examples/nlb/main.tf | 27 +++++++ examples/nlb/variables.tf | 15 ++++ examples/redshift/main.tf | 21 +++++ examples/redshift/variables.tf | 7 ++ examples/s3/main.tf | 14 ++++ examples/s3/variables.tf | 7 ++ test/terraform_aws_logs_alb_test.go | 38 +++++++++ test/terraform_aws_logs_cloudtrail_test.go | 11 +-- test/terraform_aws_logs_combined_test.go | 41 ++++++++++ test/terraform_aws_logs_config_test.go | 34 ++++++++ test/terraform_aws_logs_elb_test.go | 38 +++++++++ test/terraform_aws_logs_nlb_test.go | 38 +++++++++ test/terraform_aws_logs_redshift_test.go | 38 +++++++++ test/terraform_aws_logs_s3_test.go | 34 ++++++++ test/terraform_aws_logs_test.go | 7 +- 24 files changed, 587 insertions(+), 10 deletions(-) create mode 100644 examples/alb/main.tf create mode 100644 examples/alb/variables.tf create mode 100644 examples/combined/main.tf create mode 100644 examples/combined/variables.tf create mode 100644 examples/config/main.tf create mode 100644 examples/config/variables.tf create mode 100644 examples/elb/main.tf create mode 100644 examples/elb/variables.tf create mode 100644 examples/nlb/main.tf create mode 100644 examples/nlb/variables.tf create mode 100644 examples/redshift/main.tf create mode 100644 examples/redshift/variables.tf create mode 100644 examples/s3/main.tf create mode 100644 examples/s3/variables.tf create mode 100644 test/terraform_aws_logs_alb_test.go create mode 100644 test/terraform_aws_logs_combined_test.go create mode 100644 test/terraform_aws_logs_config_test.go create mode 100644 test/terraform_aws_logs_elb_test.go create mode 100644 test/terraform_aws_logs_nlb_test.go create mode 100644 test/terraform_aws_logs_redshift_test.go create mode 100644 test/terraform_aws_logs_s3_test.go diff --git a/Makefile b/Makefile index 9bf3468..8002ae7 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ pre_commit_tests: ensure_pre_commit ## Run pre-commit tests .PHONY: test test: pre_commit_tests - go test -count 1 -v -timeout 90m ./test/... + go test -short -count 1 -v -timeout 90m ./test/... .PHONY: clean clean: diff --git a/examples/alb/main.tf b/examples/alb/main.tf new file mode 100644 index 0000000..a9d8e81 --- /dev/null +++ b/examples/alb/main.tf @@ -0,0 +1,27 @@ +module "aws_logs" { + source = "../../" + s3_bucket_name = var.logs_bucket + region = var.region + allow_alb = "true" +} + +resource "aws_lb" "test_lb" { + internal = false + load_balancer_type = "application" + subnets = module.vpc.public_subnets + + access_logs { + bucket = module.aws_logs.aws_logs_bucket + prefix = "alb" + enabled = true + } +} + +module "vpc" { + source = "terraform-aws-modules/vpc/aws" + version = "~> 2" + name = var.vpc_name + cidr = "10.0.0.0/16" + azs = var.vpc_azs + public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] +} \ No newline at end of file diff --git a/examples/alb/variables.tf b/examples/alb/variables.tf new file mode 100644 index 0000000..ec8d2a0 --- /dev/null +++ b/examples/alb/variables.tf @@ -0,0 +1,15 @@ +variable "logs_bucket" { + type = string +} + +variable "region" { + type = string +} + +variable "vpc_name" { + type = string +} + +variable "vpc_azs" { + type = list(string) +} \ No newline at end of file diff --git a/examples/combined/main.tf b/examples/combined/main.tf new file mode 100644 index 0000000..1ef8e3d --- /dev/null +++ b/examples/combined/main.tf @@ -0,0 +1,94 @@ +module "aws_logs" { + source = "../../" + s3_bucket_name = var.logs_bucket + region = var.region +} + +resource "aws_lb" "test_alb" { + internal = false + load_balancer_type = "application" + subnets = module.vpc.public_subnets + + access_logs { + bucket = module.aws_logs.aws_logs_bucket + prefix = "alb" + enabled = true + } +} + +module "aws_cloudtrail" { + source = "trussworks/cloudtrail/aws" + version = "~> 2" + s3_bucket_name = module.aws_logs.aws_logs_bucket +} + +module "config" { + source = "trussworks/config/aws" + version = "~> 2" + + config_logs_bucket = module.aws_logs.aws_logs_bucket + config_logs_prefix = "config" +} + +resource "aws_elb" "test_elb" { + subnets = module.vpc.public_subnets + + access_logs { + bucket = module.aws_logs.aws_logs_bucket + bucket_prefix = "elb" + enabled = true + } + + listener { + instance_port = 8000 + instance_protocol = "http" + lb_port = 80 + lb_protocol = "http" + } +} + +resource "aws_lb" "test_nlb" { + internal = false + load_balancer_type = "network" + subnets = module.vpc.public_subnets + + access_logs { + bucket = module.aws_logs.aws_logs_bucket + prefix = "nlb" + enabled = true + } +} + +resource "aws_redshift_cluster" "test_redshift" { + count = var.test_redshift ? 1 : 0 + cluster_identifier = "tf-redshift-cluster" + node_type = "dc2.large" + cluster_type = "single-node" + master_username = "testredshiftuser" + master_password = "TestRedshiftpw123" + skip_final_snapshot = "true" + + logging { + bucket_name = module.aws_logs.aws_logs_bucket + s3_key_prefix = "redshift" + enable = true + } +} + +resource "aws_s3_bucket" "log_source_bucket" { + acl = "private" + + logging { + target_bucket = module.aws_logs.aws_logs_bucket + target_prefix = "log/" + } +} + +module "vpc" { + source = "terraform-aws-modules/vpc/aws" + version = "~> 2" + name = var.vpc_name + cidr = "10.0.0.0/16" + azs = var.vpc_azs + public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] +} diff --git a/examples/combined/variables.tf b/examples/combined/variables.tf new file mode 100644 index 0000000..97201a7 --- /dev/null +++ b/examples/combined/variables.tf @@ -0,0 +1,20 @@ +variable "logs_bucket" { + type = string +} + +variable "region" { + type = string +} + +variable "vpc_name" { + type = string +} + +variable "vpc_azs" { + type = list(string) +} + +variable "test_redshift" { + type = bool + default = true +} \ No newline at end of file diff --git a/examples/config/main.tf b/examples/config/main.tf new file mode 100644 index 0000000..3a6d6b6 --- /dev/null +++ b/examples/config/main.tf @@ -0,0 +1,15 @@ +module "aws_logs" { + source = "../../" + s3_bucket_name = var.logs_bucket + region = var.region + allow_config = "true" + config_logs_prefix = "config" +} + +module "config" { + source = "trussworks/config/aws" + version = "~> 2" + + config_logs_bucket = module.aws_logs.aws_logs_bucket + config_logs_prefix = "config" +} diff --git a/examples/config/variables.tf b/examples/config/variables.tf new file mode 100644 index 0000000..bc57a37 --- /dev/null +++ b/examples/config/variables.tf @@ -0,0 +1,7 @@ +variable "logs_bucket" { + type = "string" +} + +variable "region" { + type = "string" +} diff --git a/examples/elb/main.tf b/examples/elb/main.tf new file mode 100644 index 0000000..075f2ae --- /dev/null +++ b/examples/elb/main.tf @@ -0,0 +1,32 @@ +module "aws_logs" { + source = "../../" + s3_bucket_name = var.logs_bucket + region = var.region + allow_elb = "true" +} + +resource "aws_elb" "test_elb" { + subnets = module.vpc.public_subnets + + access_logs { + bucket = module.aws_logs.aws_logs_bucket + bucket_prefix = "elb" + enabled = true + } + + listener { + instance_port = 8000 + instance_protocol = "http" + lb_port = 80 + lb_protocol = "http" + } +} + +module "vpc" { + source = "terraform-aws-modules/vpc/aws" + version = "~> 2" + name = var.vpc_name + cidr = "10.0.0.0/16" + azs = var.vpc_azs + public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] +} \ No newline at end of file diff --git a/examples/elb/variables.tf b/examples/elb/variables.tf new file mode 100644 index 0000000..ec8d2a0 --- /dev/null +++ b/examples/elb/variables.tf @@ -0,0 +1,15 @@ +variable "logs_bucket" { + type = string +} + +variable "region" { + type = string +} + +variable "vpc_name" { + type = string +} + +variable "vpc_azs" { + type = list(string) +} \ No newline at end of file diff --git a/examples/nlb/main.tf b/examples/nlb/main.tf new file mode 100644 index 0000000..2cdeec1 --- /dev/null +++ b/examples/nlb/main.tf @@ -0,0 +1,27 @@ +module "aws_logs" { + source = "../../" + s3_bucket_name = var.logs_bucket + region = var.region + allow_nlb = "true" +} + +resource "aws_lb" "test_lb" { + internal = false + load_balancer_type = "network" + subnets = module.vpc.public_subnets + + access_logs { + bucket = module.aws_logs.aws_logs_bucket + prefix = "nlb" + enabled = true + } +} + +module "vpc" { + source = "terraform-aws-modules/vpc/aws" + version = "~> 2" + name = var.vpc_name + cidr = "10.0.0.0/16" + azs = var.vpc_azs + public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] +} \ No newline at end of file diff --git a/examples/nlb/variables.tf b/examples/nlb/variables.tf new file mode 100644 index 0000000..ec8d2a0 --- /dev/null +++ b/examples/nlb/variables.tf @@ -0,0 +1,15 @@ +variable "logs_bucket" { + type = string +} + +variable "region" { + type = string +} + +variable "vpc_name" { + type = string +} + +variable "vpc_azs" { + type = list(string) +} \ No newline at end of file diff --git a/examples/redshift/main.tf b/examples/redshift/main.tf new file mode 100644 index 0000000..cba026e --- /dev/null +++ b/examples/redshift/main.tf @@ -0,0 +1,21 @@ +module "aws_logs" { + source = "../../" + s3_bucket_name = var.logs_bucket + region = var.region + allow_redshift = "true" +} + +resource "aws_redshift_cluster" "test_redshift" { + cluster_identifier = "tf-redshift-cluster" + node_type = "dc2.large" + cluster_type = "single-node" + master_username = "testredshiftuser" + master_password = "TestRedshiftpw123" + skip_final_snapshot = "true" + + logging { + bucket_name = module.aws_logs.aws_logs_bucket + s3_key_prefix = "redshift" + enable = true + } +} diff --git a/examples/redshift/variables.tf b/examples/redshift/variables.tf new file mode 100644 index 0000000..3f4b117 --- /dev/null +++ b/examples/redshift/variables.tf @@ -0,0 +1,7 @@ +variable "logs_bucket" { + type = string +} + +variable "region" { + type = string +} diff --git a/examples/s3/main.tf b/examples/s3/main.tf new file mode 100644 index 0000000..44a5664 --- /dev/null +++ b/examples/s3/main.tf @@ -0,0 +1,14 @@ +module "aws_logs" { + source = "../../" + s3_bucket_name = var.logs_bucket + region = var.region +} + +resource "aws_s3_bucket" "log_source_bucket" { + acl = "private" + + logging { + target_bucket = module.aws_logs.aws_logs_bucket + target_prefix = "log/" + } +} \ No newline at end of file diff --git a/examples/s3/variables.tf b/examples/s3/variables.tf new file mode 100644 index 0000000..3f4b117 --- /dev/null +++ b/examples/s3/variables.tf @@ -0,0 +1,7 @@ +variable "logs_bucket" { + type = string +} + +variable "region" { + type = string +} diff --git a/test/terraform_aws_logs_alb_test.go b/test/terraform_aws_logs_alb_test.go new file mode 100644 index 0000000..ea29004 --- /dev/null +++ b/test/terraform_aws_logs_alb_test.go @@ -0,0 +1,38 @@ +package test + +import ( + "fmt" + "strings" + "testing" + + "github.com/gruntwork-io/terratest/modules/aws" + "github.com/gruntwork-io/terratest/modules/random" + "github.com/gruntwork-io/terratest/modules/terraform" +) + +func TestTerraformAwsLogsAlb(t *testing.T) { + t.Parallel() + + expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-alb-%s", strings.ToLower(random.UniqueId())) + vpcName := fmt.Sprintf("terratest-vpc-alb-%s", strings.ToLower(random.UniqueId())) + awsRegion := "us-west-2" + vpcAzs := aws.GetAvailabilityZones(t, awsRegion)[:3] + + terraformOptions := &terraform.Options{ + TerraformDir: "../examples/alb/", + Vars: map[string]interface{}{ + "region": awsRegion, + "vpc_azs": vpcAzs, + "logs_bucket": expectedLogsBucket, + "vpc_name": vpcName, + }, + EnvVars: map[string]string{ + "AWS_DEFAULT_REGION": awsRegion, + }, + } + + defer terraform.Destroy(t, terraformOptions) + // Empty logs_bucket before terraform destroy + defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + terraform.InitAndApply(t, terraformOptions) +} diff --git a/test/terraform_aws_logs_cloudtrail_test.go b/test/terraform_aws_logs_cloudtrail_test.go index cb4a881..fa1f706 100644 --- a/test/terraform_aws_logs_cloudtrail_test.go +++ b/test/terraform_aws_logs_cloudtrail_test.go @@ -11,10 +11,12 @@ import ( ) func TestTerraformAwsLogsCloudtrail(t *testing.T) { - t.Parallel() + // Note: do not run this test in t.Parallel() mode. + // Running this test in parallel with other tests in the module + // often causes issues when attempting to empty and delete the bucket. expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-cloudtrail-%s", strings.ToLower(random.UniqueId())) - awsRegion := aws.GetRandomStableRegion(t, nil, nil) + awsRegion := "us-west-2" terraformOptions := &terraform.Options{ TerraformDir: "../examples/cloudtrail/", @@ -28,8 +30,7 @@ func TestTerraformAwsLogsCloudtrail(t *testing.T) { } defer terraform.Destroy(t, terraformOptions) - terraform.InitAndApply(t, terraformOptions) - // Empty logs_bucket before terraform destroy - aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + terraform.InitAndApply(t, terraformOptions) } diff --git a/test/terraform_aws_logs_combined_test.go b/test/terraform_aws_logs_combined_test.go new file mode 100644 index 0000000..173e2cd --- /dev/null +++ b/test/terraform_aws_logs_combined_test.go @@ -0,0 +1,41 @@ +package test + +import ( + "fmt" + "strings" + "testing" + + "github.com/gruntwork-io/terratest/modules/aws" + "github.com/gruntwork-io/terratest/modules/random" + "github.com/gruntwork-io/terratest/modules/terraform" +) + +func TestTerraformAwsLogsCombined(t *testing.T) { + // Note: do not run this test in t.Parallel() mode. + + expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-combined-%s", strings.ToLower(random.UniqueId())) + vpcName := fmt.Sprintf("terratest-vpc-combined-%s", strings.ToLower(random.UniqueId())) + awsRegion := "us-west-2" + vpcAzs := aws.GetAvailabilityZones(t, awsRegion)[:3] + testRedshift := !testing.Short() + + terraformOptions := &terraform.Options{ + TerraformDir: "../examples/combined/", + Vars: map[string]interface{}{ + "region": awsRegion, + "vpc_azs": vpcAzs, + "logs_bucket": expectedLogsBucket, + "vpc_name": vpcName, + "test_redshift": testRedshift, + }, + EnvVars: map[string]string{ + "AWS_DEFAULT_REGION": awsRegion, + }, + } + + defer terraform.Destroy(t, terraformOptions) + // Empty and delete logs_bucket before terraform destroy + defer aws.DeleteS3Bucket(t, awsRegion, expectedLogsBucket) + defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + terraform.InitAndApply(t, terraformOptions) +} diff --git a/test/terraform_aws_logs_config_test.go b/test/terraform_aws_logs_config_test.go new file mode 100644 index 0000000..ee047f3 --- /dev/null +++ b/test/terraform_aws_logs_config_test.go @@ -0,0 +1,34 @@ +package test + +import ( + "fmt" + "strings" + "testing" + + "github.com/gruntwork-io/terratest/modules/aws" + "github.com/gruntwork-io/terratest/modules/random" + "github.com/gruntwork-io/terratest/modules/terraform" +) + +func TestTerraformAwsLogsConfig(t *testing.T) { + t.Parallel() + + expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-config-%s", strings.ToLower(random.UniqueId())) + awsRegion := "us-west-2" + + terraformOptions := &terraform.Options{ + TerraformDir: "../examples/config/", + Vars: map[string]interface{}{ + "region": awsRegion, + "logs_bucket": expectedLogsBucket, + }, + EnvVars: map[string]string{ + "AWS_DEFAULT_REGION": awsRegion, + }, + } + + defer terraform.Destroy(t, terraformOptions) + // Empty logs_bucket before terraform destroy + defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + terraform.InitAndApply(t, terraformOptions) +} diff --git a/test/terraform_aws_logs_elb_test.go b/test/terraform_aws_logs_elb_test.go new file mode 100644 index 0000000..34da8ed --- /dev/null +++ b/test/terraform_aws_logs_elb_test.go @@ -0,0 +1,38 @@ +package test + +import ( + "fmt" + "strings" + "testing" + + "github.com/gruntwork-io/terratest/modules/aws" + "github.com/gruntwork-io/terratest/modules/random" + "github.com/gruntwork-io/terratest/modules/terraform" +) + +func TestTerraformAwsLogsElb(t *testing.T) { + t.Parallel() + + expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-elb-%s", strings.ToLower(random.UniqueId())) + vpcName := fmt.Sprintf("terratest-vpc-elb-%s", strings.ToLower(random.UniqueId())) + awsRegion := "us-west-2" + vpcAzs := aws.GetAvailabilityZones(t, awsRegion)[:3] + + terraformOptions := &terraform.Options{ + TerraformDir: "../examples/elb/", + Vars: map[string]interface{}{ + "region": awsRegion, + "vpc_azs": vpcAzs, + "logs_bucket": expectedLogsBucket, + "vpc_name": vpcName, + }, + EnvVars: map[string]string{ + "AWS_DEFAULT_REGION": awsRegion, + }, + } + + defer terraform.Destroy(t, terraformOptions) + // Empty logs_bucket before terraform destroy + defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + terraform.InitAndApply(t, terraformOptions) +} diff --git a/test/terraform_aws_logs_nlb_test.go b/test/terraform_aws_logs_nlb_test.go new file mode 100644 index 0000000..f624b94 --- /dev/null +++ b/test/terraform_aws_logs_nlb_test.go @@ -0,0 +1,38 @@ +package test + +import ( + "fmt" + "strings" + "testing" + + "github.com/gruntwork-io/terratest/modules/aws" + "github.com/gruntwork-io/terratest/modules/random" + "github.com/gruntwork-io/terratest/modules/terraform" +) + +func TestTerraformAwsLogsNlb(t *testing.T) { + t.Parallel() + + expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-nlb-%s", strings.ToLower(random.UniqueId())) + vpcName := fmt.Sprintf("terratest-vpc-nlb-%s", strings.ToLower(random.UniqueId())) + awsRegion := "us-west-2" + vpcAzs := aws.GetAvailabilityZones(t, awsRegion)[:3] + + terraformOptions := &terraform.Options{ + TerraformDir: "../examples/nlb/", + Vars: map[string]interface{}{ + "region": awsRegion, + "vpc_azs": vpcAzs, + "logs_bucket": expectedLogsBucket, + "vpc_name": vpcName, + }, + EnvVars: map[string]string{ + "AWS_DEFAULT_REGION": awsRegion, + }, + } + + defer terraform.Destroy(t, terraformOptions) + // Empty logs_bucket before terraform destroy + defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + terraform.InitAndApply(t, terraformOptions) +} diff --git a/test/terraform_aws_logs_redshift_test.go b/test/terraform_aws_logs_redshift_test.go new file mode 100644 index 0000000..5f5e1e2 --- /dev/null +++ b/test/terraform_aws_logs_redshift_test.go @@ -0,0 +1,38 @@ +package test + +import ( + "fmt" + "strings" + "testing" + + "github.com/gruntwork-io/terratest/modules/aws" + "github.com/gruntwork-io/terratest/modules/random" + "github.com/gruntwork-io/terratest/modules/terraform" +) + +func TestTerraformAwsLogsRedshift(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + + t.Parallel() + + expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-redshift-%s", strings.ToLower(random.UniqueId())) + awsRegion := "us-west-2" + + terraformOptions := &terraform.Options{ + TerraformDir: "../examples/redshift/", + Vars: map[string]interface{}{ + "region": awsRegion, + "logs_bucket": expectedLogsBucket, + }, + EnvVars: map[string]string{ + "AWS_DEFAULT_REGION": awsRegion, + }, + } + + defer terraform.Destroy(t, terraformOptions) + // Empty logs_bucket before terraform destroy + defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + terraform.InitAndApply(t, terraformOptions) +} diff --git a/test/terraform_aws_logs_s3_test.go b/test/terraform_aws_logs_s3_test.go new file mode 100644 index 0000000..50b1cb5 --- /dev/null +++ b/test/terraform_aws_logs_s3_test.go @@ -0,0 +1,34 @@ +package test + +import ( + "fmt" + "strings" + "testing" + + "github.com/gruntwork-io/terratest/modules/aws" + "github.com/gruntwork-io/terratest/modules/random" + "github.com/gruntwork-io/terratest/modules/terraform" +) + +func TestTerraformAwsLogsS3(t *testing.T) { + t.Parallel() + + expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-s3-%s", strings.ToLower(random.UniqueId())) + awsRegion := "us-west-2" + + terraformOptions := &terraform.Options{ + TerraformDir: "../examples/s3/", + Vars: map[string]interface{}{ + "region": awsRegion, + "logs_bucket": expectedLogsBucket, + }, + EnvVars: map[string]string{ + "AWS_DEFAULT_REGION": awsRegion, + }, + } + + defer terraform.Destroy(t, terraformOptions) + // Empty logs_bucket before terraform destroy + defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + terraform.InitAndApply(t, terraformOptions) +} diff --git a/test/terraform_aws_logs_test.go b/test/terraform_aws_logs_test.go index 7ab5531..cd2d643 100644 --- a/test/terraform_aws_logs_test.go +++ b/test/terraform_aws_logs_test.go @@ -14,7 +14,7 @@ func TestTerraformAwsLogs(t *testing.T) { t.Parallel() expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-%s", strings.ToLower(random.UniqueId())) - awsRegion := aws.GetRandomStableRegion(t, nil, nil) + awsRegion := "us-west-2" terraformOptions := &terraform.Options{ TerraformDir: "../examples/simple/", @@ -28,8 +28,7 @@ func TestTerraformAwsLogs(t *testing.T) { } defer terraform.Destroy(t, terraformOptions) - terraform.InitAndApply(t, terraformOptions) - // Empty logs_bucket before terraform destroy - aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + terraform.InitAndApply(t, terraformOptions) } From 81538998aab15c52a353dd8904adf5136c69d42f Mon Sep 17 00:00:00 2001 From: jsclarridge <2491291+jsclarridge@users.noreply.github.com> Date: Thu, 14 Nov 2019 08:09:09 -0800 Subject: [PATCH 2/5] Add bucket deletion to config terratest https://www.pivotaltracker.com/story/show/169682491 --- test/terraform_aws_logs_config_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/terraform_aws_logs_config_test.go b/test/terraform_aws_logs_config_test.go index ee047f3..6c79c8a 100644 --- a/test/terraform_aws_logs_config_test.go +++ b/test/terraform_aws_logs_config_test.go @@ -28,7 +28,8 @@ func TestTerraformAwsLogsConfig(t *testing.T) { } defer terraform.Destroy(t, terraformOptions) - // Empty logs_bucket before terraform destroy + // Empty and delete logs_bucket before terraform destroy + defer aws.DeleteS3Bucket(t, awsRegion, expectedLogsBucket) defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) terraform.InitAndApply(t, terraformOptions) } From 7eca594108880215e5f45b04dee20b9ac9fc0455 Mon Sep 17 00:00:00 2001 From: jsclarridge <2491291+jsclarridge@users.noreply.github.com> Date: Thu, 14 Nov 2019 08:24:59 -0800 Subject: [PATCH 3/5] Insert final newlines --- examples/alb/main.tf | 2 +- examples/alb/variables.tf | 2 +- examples/combined/variables.tf | 2 +- examples/elb/variables.tf | 2 +- examples/nlb/main.tf | 2 +- examples/nlb/variables.tf | 2 +- examples/s3/main.tf | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/alb/main.tf b/examples/alb/main.tf index a9d8e81..fe5764d 100644 --- a/examples/alb/main.tf +++ b/examples/alb/main.tf @@ -24,4 +24,4 @@ module "vpc" { cidr = "10.0.0.0/16" azs = var.vpc_azs public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] -} \ No newline at end of file +} diff --git a/examples/alb/variables.tf b/examples/alb/variables.tf index ec8d2a0..720e1d8 100644 --- a/examples/alb/variables.tf +++ b/examples/alb/variables.tf @@ -12,4 +12,4 @@ variable "vpc_name" { variable "vpc_azs" { type = list(string) -} \ No newline at end of file +} diff --git a/examples/combined/variables.tf b/examples/combined/variables.tf index 97201a7..f59c09f 100644 --- a/examples/combined/variables.tf +++ b/examples/combined/variables.tf @@ -17,4 +17,4 @@ variable "vpc_azs" { variable "test_redshift" { type = bool default = true -} \ No newline at end of file +} diff --git a/examples/elb/variables.tf b/examples/elb/variables.tf index ec8d2a0..720e1d8 100644 --- a/examples/elb/variables.tf +++ b/examples/elb/variables.tf @@ -12,4 +12,4 @@ variable "vpc_name" { variable "vpc_azs" { type = list(string) -} \ No newline at end of file +} diff --git a/examples/nlb/main.tf b/examples/nlb/main.tf index 2cdeec1..4ca8b95 100644 --- a/examples/nlb/main.tf +++ b/examples/nlb/main.tf @@ -24,4 +24,4 @@ module "vpc" { cidr = "10.0.0.0/16" azs = var.vpc_azs public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] -} \ No newline at end of file +} diff --git a/examples/nlb/variables.tf b/examples/nlb/variables.tf index ec8d2a0..720e1d8 100644 --- a/examples/nlb/variables.tf +++ b/examples/nlb/variables.tf @@ -12,4 +12,4 @@ variable "vpc_name" { variable "vpc_azs" { type = list(string) -} \ No newline at end of file +} diff --git a/examples/s3/main.tf b/examples/s3/main.tf index 44a5664..615751f 100644 --- a/examples/s3/main.tf +++ b/examples/s3/main.tf @@ -11,4 +11,4 @@ resource "aws_s3_bucket" "log_source_bucket" { target_bucket = module.aws_logs.aws_logs_bucket target_prefix = "log/" } -} \ No newline at end of file +} From aa745c8a697e86fc275ee6cedd56c784af1bfb86 Mon Sep 17 00:00:00 2001 From: jsclarridge <2491291+jsclarridge@users.noreply.github.com> Date: Thu, 14 Nov 2019 08:27:13 -0800 Subject: [PATCH 4/5] Insert final newline --- examples/elb/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/elb/main.tf b/examples/elb/main.tf index 075f2ae..d2cfd9a 100644 --- a/examples/elb/main.tf +++ b/examples/elb/main.tf @@ -29,4 +29,4 @@ module "vpc" { cidr = "10.0.0.0/16" azs = var.vpc_azs public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] -} \ No newline at end of file +} From 44ebbece1120f702aed38da408bab04792c0c46d Mon Sep 17 00:00:00 2001 From: jsclarridge <2491291+jsclarridge@users.noreply.github.com> Date: Thu, 14 Nov 2019 15:56:38 -0800 Subject: [PATCH 5/5] Standardize test resource names https://www.pivotaltracker.com/story/show/169682491 --- examples/alb/main.tf | 5 +++-- examples/alb/variables.tf | 6 +----- examples/cloudtrail/main.tf | 9 ++++---- examples/cloudtrail/variables.tf | 2 +- examples/combined/main.tf | 25 +++++++++++++--------- examples/combined/variables.tf | 6 +----- examples/config/main.tf | 8 +++---- examples/config/variables.tf | 2 +- examples/elb/main.tf | 5 +++-- examples/elb/variables.tf | 6 +----- examples/nlb/main.tf | 5 +++-- examples/nlb/variables.tf | 6 +----- examples/redshift/main.tf | 4 ++-- examples/redshift/variables.tf | 2 +- examples/s3/main.tf | 5 +++-- examples/s3/variables.tf | 2 +- examples/simple/main.tf | 2 +- examples/simple/variables.tf | 2 +- test/terraform_aws_logs_alb_test.go | 12 +++++------ test/terraform_aws_logs_cloudtrail_test.go | 11 +++++----- test/terraform_aws_logs_combined_test.go | 14 ++++++------ test/terraform_aws_logs_config_test.go | 14 ++++++------ test/terraform_aws_logs_elb_test.go | 12 +++++------ test/terraform_aws_logs_nlb_test.go | 12 +++++------ test/terraform_aws_logs_redshift_test.go | 8 +++---- test/terraform_aws_logs_s3_test.go | 8 +++---- test/terraform_aws_logs_test.go | 8 +++---- 27 files changed, 96 insertions(+), 105 deletions(-) diff --git a/examples/alb/main.tf b/examples/alb/main.tf index fe5764d..f8d20ae 100644 --- a/examples/alb/main.tf +++ b/examples/alb/main.tf @@ -1,11 +1,12 @@ module "aws_logs" { source = "../../" - s3_bucket_name = var.logs_bucket + s3_bucket_name = var.test_name region = var.region allow_alb = "true" } resource "aws_lb" "test_lb" { + name = var.test_name internal = false load_balancer_type = "application" subnets = module.vpc.public_subnets @@ -20,7 +21,7 @@ resource "aws_lb" "test_lb" { module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "~> 2" - name = var.vpc_name + name = var.test_name cidr = "10.0.0.0/16" azs = var.vpc_azs public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] diff --git a/examples/alb/variables.tf b/examples/alb/variables.tf index 720e1d8..e58643c 100644 --- a/examples/alb/variables.tf +++ b/examples/alb/variables.tf @@ -1,4 +1,4 @@ -variable "logs_bucket" { +variable "test_name" { type = string } @@ -6,10 +6,6 @@ variable "region" { type = string } -variable "vpc_name" { - type = string -} - variable "vpc_azs" { type = list(string) } diff --git a/examples/cloudtrail/main.tf b/examples/cloudtrail/main.tf index d630c58..b05ed13 100644 --- a/examples/cloudtrail/main.tf +++ b/examples/cloudtrail/main.tf @@ -1,11 +1,12 @@ module "aws_logs" { source = "../../" - s3_bucket_name = var.logs_bucket + s3_bucket_name = var.test_name region = var.region } module "aws_cloudtrail" { - source = "trussworks/cloudtrail/aws" - version = "~> 2" - s3_bucket_name = module.aws_logs.aws_logs_bucket + source = "trussworks/cloudtrail/aws" + version = "~> 2" + s3_bucket_name = module.aws_logs.aws_logs_bucket + cloudwatch_log_group_name = var.test_name } diff --git a/examples/cloudtrail/variables.tf b/examples/cloudtrail/variables.tf index 3f4b117..cb2abac 100644 --- a/examples/cloudtrail/variables.tf +++ b/examples/cloudtrail/variables.tf @@ -1,4 +1,4 @@ -variable "logs_bucket" { +variable "test_name" { type = string } diff --git a/examples/combined/main.tf b/examples/combined/main.tf index 1ef8e3d..6f8bb4a 100644 --- a/examples/combined/main.tf +++ b/examples/combined/main.tf @@ -1,10 +1,11 @@ module "aws_logs" { source = "../../" - s3_bucket_name = var.logs_bucket + s3_bucket_name = var.test_name region = var.region } resource "aws_lb" "test_alb" { + name = var.test_name internal = false load_balancer_type = "application" subnets = module.vpc.public_subnets @@ -17,20 +18,22 @@ resource "aws_lb" "test_alb" { } module "aws_cloudtrail" { - source = "trussworks/cloudtrail/aws" - version = "~> 2" - s3_bucket_name = module.aws_logs.aws_logs_bucket + source = "trussworks/cloudtrail/aws" + version = "~> 2" + s3_bucket_name = module.aws_logs.aws_logs_bucket + cloudwatch_log_group_name = var.test_name } module "config" { - source = "trussworks/config/aws" - version = "~> 2" - + source = "trussworks/config/aws" + version = "~> 2" + config_name = var.test_name config_logs_bucket = module.aws_logs.aws_logs_bucket config_logs_prefix = "config" } resource "aws_elb" "test_elb" { + name = var.test_name subnets = module.vpc.public_subnets access_logs { @@ -48,6 +51,7 @@ resource "aws_elb" "test_elb" { } resource "aws_lb" "test_nlb" { + name = var.test_name internal = false load_balancer_type = "network" subnets = module.vpc.public_subnets @@ -61,7 +65,7 @@ resource "aws_lb" "test_nlb" { resource "aws_redshift_cluster" "test_redshift" { count = var.test_redshift ? 1 : 0 - cluster_identifier = "tf-redshift-cluster" + cluster_identifier = var.test_name node_type = "dc2.large" cluster_type = "single-node" master_username = "testredshiftuser" @@ -76,7 +80,8 @@ resource "aws_redshift_cluster" "test_redshift" { } resource "aws_s3_bucket" "log_source_bucket" { - acl = "private" + bucket = "${var.test_name}-source" + acl = "private" logging { target_bucket = module.aws_logs.aws_logs_bucket @@ -87,7 +92,7 @@ resource "aws_s3_bucket" "log_source_bucket" { module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "~> 2" - name = var.vpc_name + name = var.test_name cidr = "10.0.0.0/16" azs = var.vpc_azs public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] diff --git a/examples/combined/variables.tf b/examples/combined/variables.tf index f59c09f..b44ce94 100644 --- a/examples/combined/variables.tf +++ b/examples/combined/variables.tf @@ -1,4 +1,4 @@ -variable "logs_bucket" { +variable "test_name" { type = string } @@ -6,10 +6,6 @@ variable "region" { type = string } -variable "vpc_name" { - type = string -} - variable "vpc_azs" { type = list(string) } diff --git a/examples/config/main.tf b/examples/config/main.tf index 3a6d6b6..48242bf 100644 --- a/examples/config/main.tf +++ b/examples/config/main.tf @@ -1,15 +1,15 @@ module "aws_logs" { source = "../../" - s3_bucket_name = var.logs_bucket + s3_bucket_name = var.test_name region = var.region allow_config = "true" config_logs_prefix = "config" } module "config" { - source = "trussworks/config/aws" - version = "~> 2" - + source = "trussworks/config/aws" + version = "~> 2" + config_name = var.test_name config_logs_bucket = module.aws_logs.aws_logs_bucket config_logs_prefix = "config" } diff --git a/examples/config/variables.tf b/examples/config/variables.tf index bc57a37..5063baf 100644 --- a/examples/config/variables.tf +++ b/examples/config/variables.tf @@ -1,4 +1,4 @@ -variable "logs_bucket" { +variable "test_name" { type = "string" } diff --git a/examples/elb/main.tf b/examples/elb/main.tf index d2cfd9a..dd3d0d8 100644 --- a/examples/elb/main.tf +++ b/examples/elb/main.tf @@ -1,11 +1,12 @@ module "aws_logs" { source = "../../" - s3_bucket_name = var.logs_bucket + s3_bucket_name = var.test_name region = var.region allow_elb = "true" } resource "aws_elb" "test_elb" { + name = var.test_name subnets = module.vpc.public_subnets access_logs { @@ -25,7 +26,7 @@ resource "aws_elb" "test_elb" { module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "~> 2" - name = var.vpc_name + name = var.test_name cidr = "10.0.0.0/16" azs = var.vpc_azs public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] diff --git a/examples/elb/variables.tf b/examples/elb/variables.tf index 720e1d8..e58643c 100644 --- a/examples/elb/variables.tf +++ b/examples/elb/variables.tf @@ -1,4 +1,4 @@ -variable "logs_bucket" { +variable "test_name" { type = string } @@ -6,10 +6,6 @@ variable "region" { type = string } -variable "vpc_name" { - type = string -} - variable "vpc_azs" { type = list(string) } diff --git a/examples/nlb/main.tf b/examples/nlb/main.tf index 4ca8b95..7745df9 100644 --- a/examples/nlb/main.tf +++ b/examples/nlb/main.tf @@ -1,11 +1,12 @@ module "aws_logs" { source = "../../" - s3_bucket_name = var.logs_bucket + s3_bucket_name = var.test_name region = var.region allow_nlb = "true" } resource "aws_lb" "test_lb" { + name = var.test_name internal = false load_balancer_type = "network" subnets = module.vpc.public_subnets @@ -20,7 +21,7 @@ resource "aws_lb" "test_lb" { module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "~> 2" - name = var.vpc_name + name = var.test_name cidr = "10.0.0.0/16" azs = var.vpc_azs public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] diff --git a/examples/nlb/variables.tf b/examples/nlb/variables.tf index 720e1d8..e58643c 100644 --- a/examples/nlb/variables.tf +++ b/examples/nlb/variables.tf @@ -1,4 +1,4 @@ -variable "logs_bucket" { +variable "test_name" { type = string } @@ -6,10 +6,6 @@ variable "region" { type = string } -variable "vpc_name" { - type = string -} - variable "vpc_azs" { type = list(string) } diff --git a/examples/redshift/main.tf b/examples/redshift/main.tf index cba026e..7e33da9 100644 --- a/examples/redshift/main.tf +++ b/examples/redshift/main.tf @@ -1,12 +1,12 @@ module "aws_logs" { source = "../../" - s3_bucket_name = var.logs_bucket + s3_bucket_name = var.test_name region = var.region allow_redshift = "true" } resource "aws_redshift_cluster" "test_redshift" { - cluster_identifier = "tf-redshift-cluster" + cluster_identifier = var.test_name node_type = "dc2.large" cluster_type = "single-node" master_username = "testredshiftuser" diff --git a/examples/redshift/variables.tf b/examples/redshift/variables.tf index 3f4b117..cb2abac 100644 --- a/examples/redshift/variables.tf +++ b/examples/redshift/variables.tf @@ -1,4 +1,4 @@ -variable "logs_bucket" { +variable "test_name" { type = string } diff --git a/examples/s3/main.tf b/examples/s3/main.tf index 615751f..34d9035 100644 --- a/examples/s3/main.tf +++ b/examples/s3/main.tf @@ -1,11 +1,12 @@ module "aws_logs" { source = "../../" - s3_bucket_name = var.logs_bucket + s3_bucket_name = var.test_name region = var.region } resource "aws_s3_bucket" "log_source_bucket" { - acl = "private" + bucket = "${var.test_name}-source" + acl = "private" logging { target_bucket = module.aws_logs.aws_logs_bucket diff --git a/examples/s3/variables.tf b/examples/s3/variables.tf index 3f4b117..cb2abac 100644 --- a/examples/s3/variables.tf +++ b/examples/s3/variables.tf @@ -1,4 +1,4 @@ -variable "logs_bucket" { +variable "test_name" { type = string } diff --git a/examples/simple/main.tf b/examples/simple/main.tf index 59cd580..102476a 100644 --- a/examples/simple/main.tf +++ b/examples/simple/main.tf @@ -1,5 +1,5 @@ module "aws_logs" { source = "../../" - s3_bucket_name = var.logs_bucket + s3_bucket_name = var.test_name region = var.region } diff --git a/examples/simple/variables.tf b/examples/simple/variables.tf index 3f4b117..cb2abac 100644 --- a/examples/simple/variables.tf +++ b/examples/simple/variables.tf @@ -1,4 +1,4 @@ -variable "logs_bucket" { +variable "test_name" { type = string } diff --git a/test/terraform_aws_logs_alb_test.go b/test/terraform_aws_logs_alb_test.go index ea29004..ed98d72 100644 --- a/test/terraform_aws_logs_alb_test.go +++ b/test/terraform_aws_logs_alb_test.go @@ -13,18 +13,16 @@ import ( func TestTerraformAwsLogsAlb(t *testing.T) { t.Parallel() - expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-alb-%s", strings.ToLower(random.UniqueId())) - vpcName := fmt.Sprintf("terratest-vpc-alb-%s", strings.ToLower(random.UniqueId())) + testName := fmt.Sprintf("terratest-aws-logs-%s", strings.ToLower(random.UniqueId())) awsRegion := "us-west-2" vpcAzs := aws.GetAvailabilityZones(t, awsRegion)[:3] terraformOptions := &terraform.Options{ TerraformDir: "../examples/alb/", Vars: map[string]interface{}{ - "region": awsRegion, - "vpc_azs": vpcAzs, - "logs_bucket": expectedLogsBucket, - "vpc_name": vpcName, + "region": awsRegion, + "vpc_azs": vpcAzs, + "test_name": testName, }, EnvVars: map[string]string{ "AWS_DEFAULT_REGION": awsRegion, @@ -33,6 +31,6 @@ func TestTerraformAwsLogsAlb(t *testing.T) { defer terraform.Destroy(t, terraformOptions) // Empty logs_bucket before terraform destroy - defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + defer aws.EmptyS3Bucket(t, awsRegion, testName) terraform.InitAndApply(t, terraformOptions) } diff --git a/test/terraform_aws_logs_cloudtrail_test.go b/test/terraform_aws_logs_cloudtrail_test.go index fa1f706..cb30c99 100644 --- a/test/terraform_aws_logs_cloudtrail_test.go +++ b/test/terraform_aws_logs_cloudtrail_test.go @@ -15,14 +15,14 @@ func TestTerraformAwsLogsCloudtrail(t *testing.T) { // Running this test in parallel with other tests in the module // often causes issues when attempting to empty and delete the bucket. - expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-cloudtrail-%s", strings.ToLower(random.UniqueId())) + testName := fmt.Sprintf("terratest-aws-logs-%s", strings.ToLower(random.UniqueId())) awsRegion := "us-west-2" terraformOptions := &terraform.Options{ TerraformDir: "../examples/cloudtrail/", Vars: map[string]interface{}{ - "region": awsRegion, - "logs_bucket": expectedLogsBucket, + "region": awsRegion, + "test_name": testName, }, EnvVars: map[string]string{ "AWS_DEFAULT_REGION": awsRegion, @@ -30,7 +30,8 @@ func TestTerraformAwsLogsCloudtrail(t *testing.T) { } defer terraform.Destroy(t, terraformOptions) - // Empty logs_bucket before terraform destroy - defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + // Empty and delete logs_bucket before terraform destroy + defer aws.DeleteS3Bucket(t, awsRegion, testName) + defer aws.EmptyS3Bucket(t, awsRegion, testName) terraform.InitAndApply(t, terraformOptions) } diff --git a/test/terraform_aws_logs_combined_test.go b/test/terraform_aws_logs_combined_test.go index 173e2cd..06b7a60 100644 --- a/test/terraform_aws_logs_combined_test.go +++ b/test/terraform_aws_logs_combined_test.go @@ -13,9 +13,10 @@ import ( func TestTerraformAwsLogsCombined(t *testing.T) { // Note: do not run this test in t.Parallel() mode. - expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-combined-%s", strings.ToLower(random.UniqueId())) - vpcName := fmt.Sprintf("terratest-vpc-combined-%s", strings.ToLower(random.UniqueId())) - awsRegion := "us-west-2" + testName := fmt.Sprintf("terratest-aws-logs-%s", strings.ToLower(random.UniqueId())) + // AWS only supports one configuration recorder per region. + // Each test using aws-config will need to specify a different region. + awsRegion := "us-east-2" vpcAzs := aws.GetAvailabilityZones(t, awsRegion)[:3] testRedshift := !testing.Short() @@ -24,8 +25,7 @@ func TestTerraformAwsLogsCombined(t *testing.T) { Vars: map[string]interface{}{ "region": awsRegion, "vpc_azs": vpcAzs, - "logs_bucket": expectedLogsBucket, - "vpc_name": vpcName, + "test_name": testName, "test_redshift": testRedshift, }, EnvVars: map[string]string{ @@ -35,7 +35,7 @@ func TestTerraformAwsLogsCombined(t *testing.T) { defer terraform.Destroy(t, terraformOptions) // Empty and delete logs_bucket before terraform destroy - defer aws.DeleteS3Bucket(t, awsRegion, expectedLogsBucket) - defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + defer aws.DeleteS3Bucket(t, awsRegion, testName) + defer aws.EmptyS3Bucket(t, awsRegion, testName) terraform.InitAndApply(t, terraformOptions) } diff --git a/test/terraform_aws_logs_config_test.go b/test/terraform_aws_logs_config_test.go index 6c79c8a..4181dfe 100644 --- a/test/terraform_aws_logs_config_test.go +++ b/test/terraform_aws_logs_config_test.go @@ -13,14 +13,16 @@ import ( func TestTerraformAwsLogsConfig(t *testing.T) { t.Parallel() - expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-config-%s", strings.ToLower(random.UniqueId())) - awsRegion := "us-west-2" + testName := fmt.Sprintf("terratest-aws-logs-%s", strings.ToLower(random.UniqueId())) + // AWS only supports one configuration recorder per region. + // Each test using aws-config will need to specify a different region. + awsRegion := "us-east-2" terraformOptions := &terraform.Options{ TerraformDir: "../examples/config/", Vars: map[string]interface{}{ - "region": awsRegion, - "logs_bucket": expectedLogsBucket, + "region": awsRegion, + "test_name": testName, }, EnvVars: map[string]string{ "AWS_DEFAULT_REGION": awsRegion, @@ -29,7 +31,7 @@ func TestTerraformAwsLogsConfig(t *testing.T) { defer terraform.Destroy(t, terraformOptions) // Empty and delete logs_bucket before terraform destroy - defer aws.DeleteS3Bucket(t, awsRegion, expectedLogsBucket) - defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + defer aws.DeleteS3Bucket(t, awsRegion, testName) + defer aws.EmptyS3Bucket(t, awsRegion, testName) terraform.InitAndApply(t, terraformOptions) } diff --git a/test/terraform_aws_logs_elb_test.go b/test/terraform_aws_logs_elb_test.go index 34da8ed..63206cd 100644 --- a/test/terraform_aws_logs_elb_test.go +++ b/test/terraform_aws_logs_elb_test.go @@ -13,18 +13,16 @@ import ( func TestTerraformAwsLogsElb(t *testing.T) { t.Parallel() - expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-elb-%s", strings.ToLower(random.UniqueId())) - vpcName := fmt.Sprintf("terratest-vpc-elb-%s", strings.ToLower(random.UniqueId())) + testName := fmt.Sprintf("terratest-aws-logs-%s", strings.ToLower(random.UniqueId())) awsRegion := "us-west-2" vpcAzs := aws.GetAvailabilityZones(t, awsRegion)[:3] terraformOptions := &terraform.Options{ TerraformDir: "../examples/elb/", Vars: map[string]interface{}{ - "region": awsRegion, - "vpc_azs": vpcAzs, - "logs_bucket": expectedLogsBucket, - "vpc_name": vpcName, + "region": awsRegion, + "vpc_azs": vpcAzs, + "test_name": testName, }, EnvVars: map[string]string{ "AWS_DEFAULT_REGION": awsRegion, @@ -33,6 +31,6 @@ func TestTerraformAwsLogsElb(t *testing.T) { defer terraform.Destroy(t, terraformOptions) // Empty logs_bucket before terraform destroy - defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + defer aws.EmptyS3Bucket(t, awsRegion, testName) terraform.InitAndApply(t, terraformOptions) } diff --git a/test/terraform_aws_logs_nlb_test.go b/test/terraform_aws_logs_nlb_test.go index f624b94..48f1d3a 100644 --- a/test/terraform_aws_logs_nlb_test.go +++ b/test/terraform_aws_logs_nlb_test.go @@ -13,18 +13,16 @@ import ( func TestTerraformAwsLogsNlb(t *testing.T) { t.Parallel() - expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-nlb-%s", strings.ToLower(random.UniqueId())) - vpcName := fmt.Sprintf("terratest-vpc-nlb-%s", strings.ToLower(random.UniqueId())) + testName := fmt.Sprintf("terratest-aws-logs-%s", strings.ToLower(random.UniqueId())) awsRegion := "us-west-2" vpcAzs := aws.GetAvailabilityZones(t, awsRegion)[:3] terraformOptions := &terraform.Options{ TerraformDir: "../examples/nlb/", Vars: map[string]interface{}{ - "region": awsRegion, - "vpc_azs": vpcAzs, - "logs_bucket": expectedLogsBucket, - "vpc_name": vpcName, + "region": awsRegion, + "vpc_azs": vpcAzs, + "test_name": testName, }, EnvVars: map[string]string{ "AWS_DEFAULT_REGION": awsRegion, @@ -33,6 +31,6 @@ func TestTerraformAwsLogsNlb(t *testing.T) { defer terraform.Destroy(t, terraformOptions) // Empty logs_bucket before terraform destroy - defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + defer aws.EmptyS3Bucket(t, awsRegion, testName) terraform.InitAndApply(t, terraformOptions) } diff --git a/test/terraform_aws_logs_redshift_test.go b/test/terraform_aws_logs_redshift_test.go index 5f5e1e2..0c90f36 100644 --- a/test/terraform_aws_logs_redshift_test.go +++ b/test/terraform_aws_logs_redshift_test.go @@ -17,14 +17,14 @@ func TestTerraformAwsLogsRedshift(t *testing.T) { t.Parallel() - expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-redshift-%s", strings.ToLower(random.UniqueId())) + testName := fmt.Sprintf("terratest-aws-logs-%s", strings.ToLower(random.UniqueId())) awsRegion := "us-west-2" terraformOptions := &terraform.Options{ TerraformDir: "../examples/redshift/", Vars: map[string]interface{}{ - "region": awsRegion, - "logs_bucket": expectedLogsBucket, + "region": awsRegion, + "test_name": testName, }, EnvVars: map[string]string{ "AWS_DEFAULT_REGION": awsRegion, @@ -33,6 +33,6 @@ func TestTerraformAwsLogsRedshift(t *testing.T) { defer terraform.Destroy(t, terraformOptions) // Empty logs_bucket before terraform destroy - defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + defer aws.EmptyS3Bucket(t, awsRegion, testName) terraform.InitAndApply(t, terraformOptions) } diff --git a/test/terraform_aws_logs_s3_test.go b/test/terraform_aws_logs_s3_test.go index 50b1cb5..75886dc 100644 --- a/test/terraform_aws_logs_s3_test.go +++ b/test/terraform_aws_logs_s3_test.go @@ -13,14 +13,14 @@ import ( func TestTerraformAwsLogsS3(t *testing.T) { t.Parallel() - expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-s3-%s", strings.ToLower(random.UniqueId())) + testName := fmt.Sprintf("terratest-aws-logs-%s", strings.ToLower(random.UniqueId())) awsRegion := "us-west-2" terraformOptions := &terraform.Options{ TerraformDir: "../examples/s3/", Vars: map[string]interface{}{ - "region": awsRegion, - "logs_bucket": expectedLogsBucket, + "region": awsRegion, + "test_name": testName, }, EnvVars: map[string]string{ "AWS_DEFAULT_REGION": awsRegion, @@ -29,6 +29,6 @@ func TestTerraformAwsLogsS3(t *testing.T) { defer terraform.Destroy(t, terraformOptions) // Empty logs_bucket before terraform destroy - defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + defer aws.EmptyS3Bucket(t, awsRegion, testName) terraform.InitAndApply(t, terraformOptions) } diff --git a/test/terraform_aws_logs_test.go b/test/terraform_aws_logs_test.go index cd2d643..ec4705d 100644 --- a/test/terraform_aws_logs_test.go +++ b/test/terraform_aws_logs_test.go @@ -13,14 +13,14 @@ import ( func TestTerraformAwsLogs(t *testing.T) { t.Parallel() - expectedLogsBucket := fmt.Sprintf("terratest-aws-logs-%s", strings.ToLower(random.UniqueId())) + testName := fmt.Sprintf("terratest-aws-logs-%s", strings.ToLower(random.UniqueId())) awsRegion := "us-west-2" terraformOptions := &terraform.Options{ TerraformDir: "../examples/simple/", Vars: map[string]interface{}{ - "region": awsRegion, - "logs_bucket": expectedLogsBucket, + "region": awsRegion, + "test_name": testName, }, EnvVars: map[string]string{ "AWS_DEFAULT_REGION": awsRegion, @@ -29,6 +29,6 @@ func TestTerraformAwsLogs(t *testing.T) { defer terraform.Destroy(t, terraformOptions) // Empty logs_bucket before terraform destroy - defer aws.EmptyS3Bucket(t, awsRegion, expectedLogsBucket) + defer aws.EmptyS3Bucket(t, awsRegion, testName) terraform.InitAndApply(t, terraformOptions) }