Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ output "additional_schemas" {
output "additional_grants" {
value = keys(module.additional_grants)
description = "Additional grants"
}
}
2 changes: 1 addition & 1 deletion src/remote-state.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module "aurora_postgres" {
source = "cloudposse/stack-config/yaml//modules/remote-state"
version = "1.5.0"
version = "1.8.0"

component = var.aurora_postgres_component_name

Expand Down
6 changes: 6 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
state/
.cache
test/test-suite.json
.atmos
test_suite.yaml

235 changes: 235 additions & 0 deletions test/component_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
package test

import (
"fmt"
"strings"
"testing"

"github.com/cloudposse/test-helpers/pkg/atmos"
helper "github.com/cloudposse/test-helpers/pkg/atmos/component-helper"
awshelper "github.com/cloudposse/test-helpers/pkg/aws"
"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/stretchr/testify/assert"
)

type ComponentSuite struct {
helper.TestSuite
}

func (s *ComponentSuite) TestDatabase() {
const component = "aurora-postgres-resources/database"
const stack = "default-test"
const awsRegion = "us-east-2"

databaseName := strings.ToLower(random.UniqueId())
inputs := map[string]interface{}{
"additional_databases": []string{databaseName},
}

defer s.DestroyAtmosComponent(s.T(), component, stack, &inputs)
options, _ := s.DeployAtmosComponent(s.T(), component, stack, &inputs)
assert.NotNil(s.T(), options)

clusterComponent := s.GetAtmosOptions("aurora-postgres", "default-test", nil)
configMap := map[string]interface{}{}
atmos.OutputStruct(s.T(), clusterComponent, "config_map", &configMap)

passwordSSMKey, ok := configMap["password_ssm_key"].(string)
assert.True(s.T(), ok, "password_ssm_key should be a string")

adminUsername, ok := configMap["username"].(string)
assert.True(s.T(), ok, "username should be an string")

adminUserPassword := aws.GetParameter(s.T(), awsRegion, passwordSSMKey)

dbUrl, ok := configMap["hostname"].(string)
assert.True(s.T(), ok, "hostname should be a string")

dbPort, ok := configMap["port"].(float64)
assert.True(s.T(), ok, "database_port should be an int")

schemaExistsInRdsInstance := awshelper.AssertPotgresqlDatabaseExists(s.T(), dbUrl, int32(dbPort), adminUsername, adminUserPassword, databaseName)
assert.True(s.T(), schemaExistsInRdsInstance)

s.DriftTest(component, stack, &inputs)
}

func (s *ComponentSuite) TestSchema() {
const component = "aurora-postgres-resources/schema"
const stack = "default-test"
const awsRegion = "us-east-2"

schemaName := strings.ToLower(random.UniqueId())
inputs := map[string]interface{}{
"additional_schemas": map[string]interface{}{
schemaName: map[string]interface{}{
"database": "postgres",
},
},
}

defer s.DestroyAtmosComponent(s.T(), component, stack, &inputs)
options, _ := s.DeployAtmosComponent(s.T(), component, stack, &inputs)
assert.NotNil(s.T(), options)

clusterComponent := s.GetAtmosOptions("aurora-postgres", "default-test", nil)
configMap := map[string]interface{}{}
atmos.OutputStruct(s.T(), clusterComponent, "config_map", &configMap)

passwordSSMKey, ok := configMap["password_ssm_key"].(string)
assert.True(s.T(), ok, "password_ssm_key should be a string")

adminUsername, ok := configMap["username"].(string)
assert.True(s.T(), ok, "username should be an string")

adminUserPassword := aws.GetParameter(s.T(), awsRegion, passwordSSMKey)

dbUrl, ok := configMap["hostname"].(string)
assert.True(s.T(), ok, "hostname should be a string")

dbPort, ok := configMap["port"].(float64)
assert.True(s.T(), ok, "database_port should be an int")

schemaExistsInRdsInstance := awshelper.AssertPotgresqlSchemaExists(s.T(), dbUrl, int32(dbPort), adminUsername, adminUserPassword, "postgres", schemaName)
assert.True(s.T(), schemaExistsInRdsInstance)

s.DriftTest(component, stack, &inputs)
}

func (s *ComponentSuite) TestUser() {
const component = "aurora-postgres-resources/user"
const stack = "default-test"
const awsRegion = "us-east-2"

userName := strings.ToLower(random.UniqueId())
serviceName := strings.ToLower(random.UniqueId())
inputs := map[string]interface{}{
"additional_users": map[string]interface{}{
serviceName: map[string]interface{}{
"db_user": userName,
"db_password": "",
"grants": []map[string]interface{}{
{
"grant": []string{"ALL"},
"db": "postgres",
"object_type": "database",
"schema": "",
},
},
},
},
}

defer s.DestroyAtmosComponent(s.T(), component, stack, &inputs)
options, _ := s.DeployAtmosComponent(s.T(), component, stack, &inputs)
assert.NotNil(s.T(), options)

clusterComponent := s.GetAtmosOptions("aurora-postgres", "default-test", nil)
configMap := map[string]interface{}{}
atmos.OutputStruct(s.T(), clusterComponent, "config_map", &configMap)

clusterIdenitfier := atmos.Output(s.T(), clusterComponent, "cluster_identifier")

passwordSSMKey := fmt.Sprintf("/aurora-postgres/%s/%s/passwords/%s", clusterIdenitfier, serviceName, userName)
userPassword := aws.GetParameter(s.T(), awsRegion, passwordSSMKey)

dbUrl, ok := configMap["hostname"].(string)
assert.True(s.T(), ok, "hostname should be a string")

dbPort, ok := configMap["port"].(float64)
assert.True(s.T(), ok, "database_port should be an int")

schemaExistsInRdsInstance := awshelper.AssertPotgresqlDatabaseExists(s.T(), dbUrl, int32(dbPort), userName, userPassword, "postgres")
assert.True(s.T(), schemaExistsInRdsInstance)

s.DriftTest(component, stack, &inputs)
}

func (s *ComponentSuite) TestGrant() {
s.T().Skip("Additional grants not working. Read more https://github.com/cloudposse-terraform-components/aws-aurora-postgres-resources/issues/17")
const component = "aurora-postgres-resources/grant"
const stack = "default-test"
const awsRegion = "us-east-2"

userName := strings.ToLower(random.UniqueId())
serviceName := strings.ToLower(random.UniqueId())
inputs := map[string]interface{}{
"additional_users": map[string]interface{}{
serviceName: map[string]interface{}{
"db_user": userName,
"db_password": "",
"grants": []map[string]interface{}{},
},
},
}

defer s.DestroyAtmosComponent(s.T(), component, stack, &inputs)
options, _ := s.DeployAtmosComponent(s.T(), component, stack, &inputs)
assert.NotNil(s.T(), options)

clusterComponent := s.GetAtmosOptions("aurora-postgres", "default-test", nil)
configMap := map[string]interface{}{}
atmos.OutputStruct(s.T(), clusterComponent, "config_map", &configMap)

clusterIdenitfier := atmos.Output(s.T(), clusterComponent, "cluster_identifier")

passwordSSMKey := fmt.Sprintf("/aurora-postgres/%s/%s/passwords/%s", clusterIdenitfier, serviceName, userName)
userPassword := aws.GetParameter(s.T(), awsRegion, passwordSSMKey)

dbUrl, ok := configMap["hostname"].(string)
assert.True(s.T(), ok, "hostname should be a string")

dbPort, ok := configMap["port"].(float64)
assert.True(s.T(), ok, "database_port should be an int")

inputs["additional_grants"] = map[string]interface{}{
userName: []map[string]interface{}{
{
"grant": []string{"ALL"},
"db": "postgres",
},
},
}

options, _ = s.DeployAtmosComponent(s.T(), component, stack, &inputs)
assert.NotNil(s.T(), options)

grantsExistsInRdsInstance := awshelper.AssertPotgresqlGrantsExists(s.T(), dbUrl, int32(dbPort), userName, userPassword, "postgres", "public")
assert.True(s.T(), grantsExistsInRdsInstance)

s.DriftTest(component, stack, &inputs)
}

func (s *ComponentSuite) TestDisabled() {
const component = "aurora-postgres-resources/disabled"
const stack = "default-test"
const awsRegion = "us-east-2"

s.VerifyEnabledFlag(component, stack, nil)
}

func TestRunSuite(t *testing.T) {
suite := new(ComponentSuite)

suite.AddDependency(t, "vpc", "default-test", nil)

subdomain := strings.ToLower(random.UniqueId())
dnsDelegatedInputs := map[string]interface{}{
"zone_config": []map[string]interface{}{
{
"subdomain": subdomain,
"zone_name": "components.cptest.test-automation.app",
},
},
}
suite.AddDependency(t, "dns-delegated", "default-test", &dnsDelegatedInputs)

clusterName := strings.ToLower(random.UniqueId())
auroraPostgresInputs := map[string]interface{}{
"cluster_name": clusterName,
}
suite.AddDependency(t, "aurora-postgres", "default-test", &auroraPostgresInputs)
helper.Run(t, suite)
}

77 changes: 77 additions & 0 deletions test/fixtures/atmos.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# CLI config is loaded from the following locations (from lowest to highest priority):
# system dir (`/usr/local/etc/atmos` on Linux, `%LOCALAPPDATA%/atmos` on Windows)
# home dir (~/.atmos)
# current directory
# ENV vars
# Command-line arguments
#
# It supports POSIX-style Globs for file names/paths (double-star `**` is supported)
# https://en.wikipedia.org/wiki/Glob_(programming)

# Base path for components, stacks and workflows configurations.
# Can also be set using `ATMOS_BASE_PATH` ENV var, or `--base-path` command-line argument.
# Supports both absolute and relative paths.
# If not provided or is an empty string, `components.terraform.base_path`, `components.helmfile.base_path`, `stacks.base_path` and `workflows.base_path`
# are independent settings (supporting both absolute and relative paths).
# If `base_path` is provided, `components.terraform.base_path`, `components.helmfile.base_path`, `stacks.base_path` and `workflows.base_path`
# are considered paths relative to `base_path`.
base_path: ""

components:
terraform:
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_BASE_PATH` ENV var, or `--terraform-dir` command-line argument
# Supports both absolute and relative paths
base_path: "components/terraform"
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_APPLY_AUTO_APPROVE` ENV var
apply_auto_approve: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_DEPLOY_RUN_INIT` ENV var, or `--deploy-run-init` command-line argument
deploy_run_init: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE` ENV var, or `--init-run-reconfigure` command-line argument
init_run_reconfigure: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_AUTO_GENERATE_BACKEND_FILE` ENV var, or `--auto-generate-backend-file` command-line argument
auto_generate_backend_file: true

stacks:
# Can also be set using `ATMOS_STACKS_BASE_PATH` ENV var, or `--config-dir` and `--stacks-dir` command-line arguments
# Supports both absolute and relative paths
base_path: "stacks"
# Can also be set using `ATMOS_STACKS_INCLUDED_PATHS` ENV var (comma-separated values string)
# Since we are distinguishing stacks based on namespace, and namespace is not part
# of the stack name, we have to set `included_paths` via the ENV var in the Dockerfile
included_paths:
- "orgs/**/*"

# Can also be set using `ATMOS_STACKS_EXCLUDED_PATHS` ENV var (comma-separated values string)
excluded_paths:
- "**/_defaults.yaml"

# Can also be set using `ATMOS_STACKS_NAME_PATTERN` ENV var
name_pattern: "{tenant}-{stage}"

workflows:
# Can also be set using `ATMOS_WORKFLOWS_BASE_PATH` ENV var, or `--workflows-dir` command-line arguments
# Supports both absolute and relative paths
base_path: "stacks/workflows"

# https://github.com/cloudposse/atmos/releases/tag/v1.33.0
logs:
file: "/dev/stdout"
# Supported log levels: Trace, Debug, Info, Warning, Off
level: Info

settings:
# Can also be set using 'ATMOS_SETTINGS_LIST_MERGE_STRATEGY' environment variable, or '--settings-list-merge-strategy' command-line argument
list_merge_strategy: replace

# `Go` templates in Atmos manifests
# https://atmos.tools/core-concepts/stacks/templating
# https://pkg.go.dev/text/template
templates:
settings:
enabled: true
# https://masterminds.github.io/sprig
sprig:
enabled: true
# https://docs.gomplate.ca
gomplate:
enabled: true
46 changes: 46 additions & 0 deletions test/fixtures/stacks/catalog/account-map.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
components:
terraform:
account-map:
metadata:
terraform_workspace: core-gbl-root
vars:
tenant: core
environment: gbl
stage: root

# This remote state is only for Cloud Posse internal use.
# It references the Cloud Posse test organizations actual infrastructure.
# remote_state_backend:
# s3:
# bucket: cptest-core-ue2-root-tfstate-core
# dynamodb_table: cptest-core-ue2-root-tfstate-core-lock
# role_arn: arn:aws:iam::822777368227:role/cptest-core-gbl-root-tfstate-core-ro
# encrypt: true
# key: terraform.tfstate
# acl: bucket-owner-full-control
# region: us-east-2

remote_state_backend_type: static
remote_state_backend:
# This static backend is used for tests that only need to use the account map iam-roles module
# to find the role to assume for Terraform operations. It is configured to use whatever
# the current user's role is, but the environment variable `TEST_ACCOUNT_ID` must be set to
# the account ID of the account that the user is currently assuming a role in.
#
# For some components, this backend is missing important data, and those components
# will need that data added to the backend configuration in order to work properly.
static:
account_info_map: {}
all_accounts: []
aws_partition: aws
full_account_map: {}
iam_role_arn_templates: {}
non_eks_accounts: []
profiles_enabled: false
root_account_aws_name: root
terraform_access_map: {}
terraform_dynamic_role_enabled: false
terraform_role_name_map:
apply: terraform
plan: planner
terraform_roles: {}
Loading