Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
452767e
CI-159 Update Makefile
rosemarywang Jul 6, 2020
293fac6
adding first test
hbarros-caylent Jul 1, 2021
1e25f16
adding first test
hbarros-caylent Jul 1, 2021
cade63e
simple test
hbarros-caylent Jul 1, 2021
5391968
adding helper function for connectivity test
hbarros-caylent Jul 1, 2021
89b29a2
Merge tag '2.0.0' into CA-18
hbarros-caylent Jul 9, 2021
23ff23f
CA-18 adding initial tests
hbarros-caylent Jul 15, 2021
620fa76
standard for test_examples folder name
hbarros-caylent Jul 15, 2021
8463d01
make lint, terraform/docs and fmt
hbarros-caylent Jul 15, 2021
057d812
go mod tidy
hbarros-caylent Jul 15, 2021
7df5db6
fix comments
hbarros-caylent Jul 15, 2021
7195926
Merge remote-tracking branch 'upstream/master' into CA-18
hbarros-caylent Sep 10, 2021
d030414
big refactor - test_structure + testCases
hbarros-caylent Sep 15, 2021
8b97937
updating versions
hbarros-caylent Sep 15, 2021
515375f
fixes module name
hbarros-caylent Sep 16, 2021
4762b16
CA-71 - update tests/ to instantiate examples/
hbarros-caylent Oct 22, 2021
97f5ba6
using expectApplyError
hbarros-caylent Nov 2, 2021
bd89420
1st version of cloudwatch logs integration
hbarros-caylent Nov 3, 2021
3dfcd41
adding upgrade logs and updating local.tfvars
hbarros-caylent Nov 3, 2021
4eb6a4a
adding parameter log_statement=all
hbarros-caylent Nov 3, 2021
14a6138
creates param group log parameters from module variables
hbarros-caylent Nov 3, 2021
7302b65
Merge tag '3.1.0' into CA-18
hbarros-caylent Nov 16, 2021
2c4bc48
fix: adding require lib
hbarros-caylent Nov 17, 2021
1b10888
adding comments
hbarros-caylent Nov 19, 2021
049ecc6
adding test README
hbarros-caylent Jan 20, 2022
ac2069f
Merge remote-tracking branch 'upstream/master' into CA-18
hbarros-caylent Feb 23, 2022
ecc9d93
smallfix
hbarros-caylent Feb 23, 2022
f420039
update to README
hbarros-caylent Feb 23, 2022
fea9c3f
removing local.tfvars
hbarros-caylent Feb 23, 2022
11ba9a2
reverting change on output
hbarros-caylent Feb 24, 2022
b1e2f02
Updated documentation
joseamoroso Mar 3, 2022
cfa8749
Changed indivual outputs to module output.
joseamoroso Mar 4, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Local .terraform directories
**/.terraform/*

*.terraform.lock.hcl
# .tfstate files
*.tfstate
*.tfstate.*
Expand Down
2 changes: 0 additions & 2 deletions examples/minimal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ No provider.
|------|-------------|------|---------|:--------:|
| ingress\_cidr\_blocks | CIDR blocks to attach to security groups for ingress | `list(string)` | n/a | yes |
| name\_prefix | A string to prepend to names of resources created by this example | `any` | n/a | yes |
| 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 |
| subnet\_ids | List of at least 2 subnets in different AZs for DB subnet group | `list(string)` | n/a | yes |
| vpc\_id | VPC ID of network. | `string` | n/a | yes |
| egress\_cidr\_blocks | CIDR blocks to attach to security groups for egress | `list(string)` | <pre>[<br> "0.0.0.0/0"<br>]</pre> | no |
Expand All @@ -23,7 +22,6 @@ No provider.

| Name | Description |
|------|-------------|
| ingress\_ports | List of ingress ports |
| rds | n/a |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
6 changes: 3 additions & 3 deletions examples/minimal/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ module "rds_postgres" {
# source = "git::https://github.com/Datatamer/terraform-aws-rds-postgres.git?ref=3.0.0"
source = "../.."

identifier_prefix = "example-rds-pg-"
identifier_prefix = "${var.name_prefix}-example-rds-pg-"
postgres_name = "example0"
parameter_group_name = "example-rds-postgres-pg"
parameter_group_name = "${var.name_prefix}-example-rds-postgres-pg"
username = "exampleUsername"
password = "examplePassword" #tfsec:ignore:GEN003

vpc_id = var.vpc_id
subnet_group_name = "example_subnet_group"
subnet_group_name = "${var.name_prefix}_example_subnet_group"
# Network requirement: DB subnet group needs a subnet in at least two Availability Zones
rds_subnet_ids = var.subnet_ids
security_group_ids = module.rds-postgres-sg.security_group_ids
Expand Down
5 changes: 0 additions & 5 deletions examples/minimal/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
output "ingress_ports" {
value = module.sg-ports
description = "List of ingress ports"
}

output "rds" {
value = module.rds_postgres
}
5 changes: 0 additions & 5 deletions examples/minimal/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ variable "subnet_ids" {
description = "List of at least 2 subnets in different AZs for DB subnet group"
}

variable "security_group_ids" {
description = "List of security group IDs to allow ingress from (i.e. Spark cluster SG IDs, Tamr VM SG ID)"
type = list(string)
}

variable "name_prefix" {
description = "A string to prepend to names of resources created by this example"
}
Expand Down
45 changes: 45 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Tests

This folder contains automated tests for this Module. All of the tests are written in [Go](https://golang.org/).
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).



## WARNING WARNING WARNING

**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!

**Note #2**: Never forcefully shut the tests down (e.g. by hitting `CTRL + C`) or the cleanup tasks won't run!

**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.



## Running the tests

### Prerequisites

- Install the latest version of [Go](https://golang.org/).
- Install [Terraform](https://www.terraform.io/downloads.html).
- 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.


### Run all the tests

```bash
go test -v -timeout 60m
```


### Run a specific test

To run a specific test called `TestFoo`:

```bash
go test -v -timeout 60m -run TestFoo
```

When using `t.Run("test_name",...)` inside a test function, you may run a specific test with:
```bash
go test -v -timeout 60m -run TestFoo/test_name
```
108 changes: 108 additions & 0 deletions test/create_db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package test

import (
"strings"
"testing"

"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
"github.com/stretchr/testify/require"
)

// These const are declared according to what is found at "../../examples/minimal/main.tf"
const (
expectedPw = "examplePassword"
expectedUsername = "exampleUsername"
expectedDBName = "example0"
)

// initTestCases initializes a list of RdsTestCase
func initTestCases() []RdsTestCase {
return []RdsTestCase{
{
testName: "minimal",
expectApplyError: false,
vars: map[string]interface{}{
"vpc_cidr": "172.18.0.0/18",
"database_subnets": []string{"172.18.0.0/24", "172.18.1.0/24"},
"egress_cidr_blocks": []string{"0.0.0.0/0"},
"ingress_cidr_blocks": []string{"0.0.0.0/0"},
"name_prefix": "",
},
},
}
}

// TestTerraformCreateRDS runs all test cases
func TestTerraformCreateRDS(t *testing.T) {

testCases := initTestCases()

for _, testCase := range testCases {
testCase := testCase

t.Run(testCase.testName, func(t *testing.T) {
t.Parallel()

// These will create a tempTestFolder for each bucketTestCase.
tempTestFolder := test_structure.CopyTerraformFolderToTemp(t, "..", "test_examples/minimal")

// this stage will generate a random `awsRegion` and a `uniqueId` to be used in tests.
test_structure.RunTestStage(t, "pick_new_randoms", func() {
usRegions := []string{"us-east-1", "us-east-2", "us-west-1", "us-west-2"}
// This function will first check for the Env Var TERRATEST_REGION and return its value if != ""
awsRegion := aws.GetRandomStableRegion(t, usRegions, nil)

test_structure.SaveString(t, tempTestFolder, "region", awsRegion)
test_structure.SaveString(t, tempTestFolder, "unique_id", strings.ToLower(random.UniqueId()))
})

defer test_structure.RunTestStage(t, "teardown", func() {
teraformOptions := test_structure.LoadTerraformOptions(t, tempTestFolder)
terraform.Destroy(t, teraformOptions)
})

test_structure.RunTestStage(t, "setup_options", func() {
awsRegion := test_structure.LoadString(t, tempTestFolder, "region")
uniqueID := test_structure.LoadString(t, tempTestFolder, "unique_id")

testCase.vars["name_prefix"] = uniqueID

terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
TerraformDir: tempTestFolder,
Vars: testCase.vars,
EnvVars: map[string]string{
"AWS_REGION": awsRegion,
},
})

test_structure.SaveTerraformOptions(t, tempTestFolder, terraformOptions)
})

test_structure.RunTestStage(t, "create_rds", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, tempTestFolder)
_, err := terraform.InitAndApplyE(t, terraformOptions)

if testCase.expectApplyError {
require.Error(t, err)
// If it failed as expected, we should skip the rest (validate function).
t.SkipNow()
}
})

test_structure.RunTestStage(t, "validate", func() {
awsRegion := test_structure.LoadString(t, tempTestFolder, "region")
terraformOptions := test_structure.LoadTerraformOptions(t, tempTestFolder)
validateModuleOutputs(t,
terraformOptions,
awsRegion,
int64(5432),
expectedUsername,
expectedDBName,
)
})
})
}
}
9 changes: 9 additions & 0 deletions test/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/Datatamer/terraform-aws-rds-postgres

go 1.16

require (
github.com/aws/aws-sdk-go v1.40.40
github.com/gruntwork-io/terratest v0.37.8
github.com/stretchr/testify v1.7.0
)
Loading