Skip to content

Commit 3a4d53b

Browse files
jamoroso-caylentrosemarywanghbarros-caylent
authored
CA-18 RDS Postgres Initial Tests (#24)
* CI-159 Update Makefile * adding first test * adding first test * simple test * adding helper function for connectivity test * CA-18 adding initial tests * standard for test_examples folder name * make lint, terraform/docs and fmt * go mod tidy * fix comments * big refactor - test_structure + testCases * updating versions * fixes module name * CA-71 - update tests/ to instantiate examples/ * using expectApplyError * 1st version of cloudwatch logs integration * adding upgrade logs and updating local.tfvars * adding parameter log_statement=all * creates param group log parameters from module variables * fix: adding require lib * adding comments * adding test README * smallfix * update to README * removing local.tfvars * reverting change on output * Updated documentation * Changed indivual outputs to module output. Co-authored-by: Rosemary Wang <66625360+rosemarywang@users.noreply.github.com> Co-authored-by: Hugo Barros <hugo.barros@caylent.com>
1 parent 00ca2fa commit 3a4d53b

File tree

14 files changed

+1037
-15
lines changed

14 files changed

+1037
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Local .terraform directories
22
**/.terraform/*
33

4+
*.terraform.lock.hcl
45
# .tfstate files
56
*.tfstate
67
*.tfstate.*

examples/minimal/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ No provider.
1313
|------|-------------|------|---------|:--------:|
1414
| ingress\_cidr\_blocks | CIDR blocks to attach to security groups for ingress | `list(string)` | n/a | yes |
1515
| name\_prefix | A string to prepend to names of resources created by this example | `any` | n/a | yes |
16-
| security\_group\_ids | List of security group IDs to allow ingress from (i.e. Spark cluster SG IDs, Tamr VM SG ID) | `list(string)` | n/a | yes |
1716
| subnet\_ids | List of at least 2 subnets in different AZs for DB subnet group | `list(string)` | n/a | yes |
1817
| vpc\_id | VPC ID of network. | `string` | n/a | yes |
1918
| egress\_cidr\_blocks | CIDR blocks to attach to security groups for egress | `list(string)` | <pre>[<br> "0.0.0.0/0"<br>]</pre> | no |
@@ -23,7 +22,6 @@ No provider.
2322

2423
| Name | Description |
2524
|------|-------------|
26-
| ingress\_ports | List of ingress ports |
2725
| rds | n/a |
2826

2927
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/minimal/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ module "rds_postgres" {
22
# source = "git::https://github.com/Datatamer/terraform-aws-rds-postgres.git?ref=3.0.0"
33
source = "../.."
44

5-
identifier_prefix = "example-rds-pg-"
5+
identifier_prefix = "${var.name_prefix}-example-rds-pg-"
66
postgres_name = "example0"
7-
parameter_group_name = "example-rds-postgres-pg"
7+
parameter_group_name = "${var.name_prefix}-example-rds-postgres-pg"
88
username = "exampleUsername"
99
password = "examplePassword" #tfsec:ignore:GEN003
1010

1111
vpc_id = var.vpc_id
12-
subnet_group_name = "example_subnet_group"
12+
subnet_group_name = "${var.name_prefix}_example_subnet_group"
1313
# Network requirement: DB subnet group needs a subnet in at least two Availability Zones
1414
rds_subnet_ids = var.subnet_ids
1515
security_group_ids = module.rds-postgres-sg.security_group_ids

examples/minimal/outputs.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
output "ingress_ports" {
2-
value = module.sg-ports
3-
description = "List of ingress ports"
4-
}
5-
61
output "rds" {
72
value = module.rds_postgres
83
}

examples/minimal/variables.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ variable "subnet_ids" {
88
description = "List of at least 2 subnets in different AZs for DB subnet group"
99
}
1010

11-
variable "security_group_ids" {
12-
description = "List of security group IDs to allow ingress from (i.e. Spark cluster SG IDs, Tamr VM SG ID)"
13-
type = list(string)
14-
}
15-
1611
variable "name_prefix" {
1712
description = "A string to prepend to names of resources created by this example"
1813
}

test/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Tests
2+
3+
This folder contains automated tests for this Module. All of the tests are written in [Go](https://golang.org/).
4+
Most of these are "integration tests" that deploy real infrastructure using Terraform and verify that infrastructure works as expected using a helper library called [Terratest](https://github.com/gruntwork-io/terratest).
5+
6+
7+
8+
## WARNING WARNING WARNING
9+
10+
**Note #1**: Many of these tests create real resources in an AWS account and then try to clean those resources up at the end of a test run. That means these tests may cost you money to run! When adding tests, please be considerate of the resources you create and take extra care to clean everything up when you're done!
11+
12+
**Note #2**: Never forcefully shut the tests down (e.g. by hitting `CTRL + C`) or the cleanup tasks won't run!
13+
14+
**Note #3**: We need to set `-timeout 60m` on all tests not because they necessarily take that long, but because Go has a default test timeout of 10 minutes, after which it forcefully kills the tests with a `SIGQUIT`, preventing the cleanup tasks from running. Therefore, we set an overlying long timeout to make sure all tests have enough time to finish and clean up.
15+
16+
17+
18+
## Running the tests
19+
20+
### Prerequisites
21+
22+
- Install the latest version of [Go](https://golang.org/).
23+
- Install [Terraform](https://www.terraform.io/downloads.html).
24+
- Configure your AWS credentials using one of the [options supported by the AWS SDK](http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html). Usually, the easiest option is to set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables.
25+
26+
27+
### Run all the tests
28+
29+
```bash
30+
go test -v -timeout 60m
31+
```
32+
33+
34+
### Run a specific test
35+
36+
To run a specific test called `TestFoo`:
37+
38+
```bash
39+
go test -v -timeout 60m -run TestFoo
40+
```
41+
42+
When using `t.Run("test_name",...)` inside a test function, you may run a specific test with:
43+
```bash
44+
go test -v -timeout 60m -run TestFoo/test_name
45+
```

test/create_db_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package test
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/gruntwork-io/terratest/modules/aws"
8+
"github.com/gruntwork-io/terratest/modules/random"
9+
"github.com/gruntwork-io/terratest/modules/terraform"
10+
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
// These const are declared according to what is found at "../../examples/minimal/main.tf"
15+
const (
16+
expectedPw = "examplePassword"
17+
expectedUsername = "exampleUsername"
18+
expectedDBName = "example0"
19+
)
20+
21+
// initTestCases initializes a list of RdsTestCase
22+
func initTestCases() []RdsTestCase {
23+
return []RdsTestCase{
24+
{
25+
testName: "minimal",
26+
expectApplyError: false,
27+
vars: map[string]interface{}{
28+
"vpc_cidr": "172.18.0.0/18",
29+
"database_subnets": []string{"172.18.0.0/24", "172.18.1.0/24"},
30+
"egress_cidr_blocks": []string{"0.0.0.0/0"},
31+
"ingress_cidr_blocks": []string{"0.0.0.0/0"},
32+
"name_prefix": "",
33+
},
34+
},
35+
}
36+
}
37+
38+
// TestTerraformCreateRDS runs all test cases
39+
func TestTerraformCreateRDS(t *testing.T) {
40+
41+
testCases := initTestCases()
42+
43+
for _, testCase := range testCases {
44+
testCase := testCase
45+
46+
t.Run(testCase.testName, func(t *testing.T) {
47+
t.Parallel()
48+
49+
// These will create a tempTestFolder for each bucketTestCase.
50+
tempTestFolder := test_structure.CopyTerraformFolderToTemp(t, "..", "test_examples/minimal")
51+
52+
// this stage will generate a random `awsRegion` and a `uniqueId` to be used in tests.
53+
test_structure.RunTestStage(t, "pick_new_randoms", func() {
54+
usRegions := []string{"us-east-1", "us-east-2", "us-west-1", "us-west-2"}
55+
// This function will first check for the Env Var TERRATEST_REGION and return its value if != ""
56+
awsRegion := aws.GetRandomStableRegion(t, usRegions, nil)
57+
58+
test_structure.SaveString(t, tempTestFolder, "region", awsRegion)
59+
test_structure.SaveString(t, tempTestFolder, "unique_id", strings.ToLower(random.UniqueId()))
60+
})
61+
62+
defer test_structure.RunTestStage(t, "teardown", func() {
63+
teraformOptions := test_structure.LoadTerraformOptions(t, tempTestFolder)
64+
terraform.Destroy(t, teraformOptions)
65+
})
66+
67+
test_structure.RunTestStage(t, "setup_options", func() {
68+
awsRegion := test_structure.LoadString(t, tempTestFolder, "region")
69+
uniqueID := test_structure.LoadString(t, tempTestFolder, "unique_id")
70+
71+
testCase.vars["name_prefix"] = uniqueID
72+
73+
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
74+
TerraformDir: tempTestFolder,
75+
Vars: testCase.vars,
76+
EnvVars: map[string]string{
77+
"AWS_REGION": awsRegion,
78+
},
79+
})
80+
81+
test_structure.SaveTerraformOptions(t, tempTestFolder, terraformOptions)
82+
})
83+
84+
test_structure.RunTestStage(t, "create_rds", func() {
85+
terraformOptions := test_structure.LoadTerraformOptions(t, tempTestFolder)
86+
_, err := terraform.InitAndApplyE(t, terraformOptions)
87+
88+
if testCase.expectApplyError {
89+
require.Error(t, err)
90+
// If it failed as expected, we should skip the rest (validate function).
91+
t.SkipNow()
92+
}
93+
})
94+
95+
test_structure.RunTestStage(t, "validate", func() {
96+
awsRegion := test_structure.LoadString(t, tempTestFolder, "region")
97+
terraformOptions := test_structure.LoadTerraformOptions(t, tempTestFolder)
98+
validateModuleOutputs(t,
99+
terraformOptions,
100+
awsRegion,
101+
int64(5432),
102+
expectedUsername,
103+
expectedDBName,
104+
)
105+
})
106+
})
107+
}
108+
}

test/go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/Datatamer/terraform-aws-rds-postgres
2+
3+
go 1.16
4+
5+
require (
6+
github.com/aws/aws-sdk-go v1.40.40
7+
github.com/gruntwork-io/terratest v0.37.8
8+
github.com/stretchr/testify v1.7.0
9+
)

0 commit comments

Comments
 (0)