Skip to content

Commit 4299480

Browse files
committed
Added tests
1 parent ad6ff2d commit 4299480

File tree

17 files changed

+1679
-8
lines changed

17 files changed

+1679
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ aws-assumed-role/
77
*.iml
88
.direnv
99
.envrc
10+
.cache
1011

1112
# Compiled and auto-generated files
1213
# Note that the leading "**/" appears necessary for Docker even if not for Git

src/remote-state.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ locals {
44

55
module "dns-delegated" {
66
source = "cloudposse/stack-config/yaml//modules/remote-state"
7-
version = "1.5.0"
7+
version = "1.8.0"
88

99
component = "dns-delegated"
1010
environment = "gbl"
@@ -14,7 +14,7 @@ module "dns-delegated" {
1414

1515
module "eks" {
1616
source = "cloudposse/stack-config/yaml//modules/remote-state"
17-
version = "1.5.0"
17+
version = "1.8.0"
1818

1919
for_each = var.eks_component_names
2020

@@ -25,7 +25,7 @@ module "eks" {
2525

2626
module "vpc" {
2727
source = "cloudposse/stack-config/yaml//modules/remote-state"
28-
version = "1.5.0"
28+
version = "1.8.0"
2929

3030
component = var.vpc_component_name
3131

@@ -34,7 +34,7 @@ module "vpc" {
3434

3535
module "vpc_ingress" {
3636
source = "cloudposse/stack-config/yaml//modules/remote-state"
37-
version = "1.5.0"
37+
version = "1.8.0"
3838

3939
for_each = local.accounts_with_vpc
4040

@@ -49,7 +49,7 @@ module "vpc_ingress" {
4949

5050
module "primary_cluster" {
5151
source = "cloudposse/stack-config/yaml//modules/remote-state"
52-
version = "1.5.0"
52+
version = "1.8.0"
5353

5454
count = local.remote_read_replica_enabled ? 1 : 0
5555

test/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
state/
2+
.cache
3+
test/test-suite.json
4+
.atmos

test/component_test.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
6+
awssdk "github.com/aws/aws-sdk-go/aws"
7+
"github.com/aws/aws-sdk-go/service/ec2"
8+
"github.com/cloudposse/test-helpers/pkg/atmos"
9+
helper "github.com/cloudposse/test-helpers/pkg/atmos/aws-component-helper"
10+
"github.com/gruntwork-io/terratest/modules/aws"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestComponent(t *testing.T) {
15+
awsRegion := "us-east-2"
16+
17+
fixture := helper.NewFixture(t, "../", awsRegion, "test/fixtures")
18+
19+
defer fixture.TearDown()
20+
fixture.SetUp(&atmos.Options{})
21+
22+
fixture.Suite("default", func(t *testing.T, suite *helper.Suite) {
23+
suite.AddDependency("vpc", "default-test")
24+
25+
suite.Setup(t, func(t *testing.T, atm *helper.Atmos) {
26+
randomID := suite.GetRandomIdentifier()
27+
inputs := map[string]interface{}{
28+
"zone_config": []map[string]string{
29+
{
30+
"subdomain": randomID,
31+
"zone_name": "example.net",
32+
},
33+
},
34+
}
35+
atm.GetAndDeploy("dns-delegated", "default-test", inputs)
36+
})
37+
38+
suite.TearDown(t, func(t *testing.T, atm *helper.Atmos) {
39+
atm.GetAndDestroy("dns-delegated", "default-test", map[string]interface{}{})
40+
})
41+
42+
suite.Test(t, "single-cluster", func(t *testing.T, atm *helper.Atmos) {
43+
inputs := map[string]interface{}{
44+
"name": "rds",
45+
"mysql_deletion_protection": false,
46+
"mysql_storage_encrypted": true,
47+
"aurora_mysql_engine": "aurora-mysql",
48+
"allowed_cidr_blocks": []string{},
49+
"eks_component_names": []string{},
50+
"publicly_accessible": true,
51+
"aurora_mysql_engine_version": "8.0.mysql_aurora.3.02.0",
52+
"aurora_mysql_cluster_family": "aurora-mysql8.0",
53+
"mysql_name": "shared",
54+
"mysql_cluster_size": 2,
55+
"mysql_admin_user": "",
56+
"mysql_admin_password": "",
57+
"mysql_db_name": "",
58+
"mysql_instance_type": "db.t3.medium",
59+
"mysql_skip_final_snapshot": true,
60+
}
61+
62+
defer atm.GetAndDestroy("aurora-mysql/cluster", "default-test", inputs)
63+
component := atm.GetAndDeploy("aurora-mysql/cluster", "default-test", inputs)
64+
65+
clusterARN := atm.Output(component, "aurora_mysql_cluster_arn")
66+
require.Equal(t, clusterARN, "")
67+
68+
// output "aurora_mysql_cluster_id" {
69+
// output "aurora_mysql_cluster_name" {
70+
// output "aurora_mysql_endpoint" {
71+
// output "aurora_mysql_master_hostname" {
72+
// output "aurora_mysql_master_password" {
73+
// output "aurora_mysql_master_password_ssm_key" {
74+
// output "aurora_mysql_master_username" {
75+
// output "aurora_mysql_reader_endpoint" {
76+
// output "aurora_mysql_replicas_hostname" {
77+
// output "cluster_domain" {
78+
// output "kms_key_arn" {
79+
80+
})
81+
82+
// suite.Test(t, "public-private-subnets", func(t *testing.T, atm *helper.Atmos) {
83+
// inputs := map[string]interface{}{
84+
// "name": "vpc-terraform",
85+
// "availability_zones": []string{"b", "c"},
86+
// "public_subnets_enabled": true,
87+
// "nat_gateway_enabled": true,
88+
// "nat_instance_enabled": false,
89+
// "subnet_type_tag_key": "eg.cptest.co/subnet/type",
90+
// "max_nats": 1,
91+
// "max_subnet_count": 3,
92+
// "vpc_flow_logs_enabled": false,
93+
// "ipv4_primary_cidr_block": "172.16.0.0/16",
94+
// }
95+
96+
// defer atm.GetAndDestroy("vpc/public", "default-test", inputs)
97+
// component := atm.GetAndDeploy("vpc/public", "default-test", inputs)
98+
99+
// vpcId := atm.Output(component, "vpc_id")
100+
// require.True(t, strings.HasPrefix(vpcId, "vpc-"))
101+
102+
// vpc := aws.GetVpcById(t, vpcId, awsRegion)
103+
104+
// assert.Equal(t, vpc.Name, fmt.Sprintf("eg-default-ue2-test-vpc-terraform-%s", component.RandomIdentifier))
105+
// assert.Equal(t, *vpc.CidrAssociations[0], "172.16.0.0/16")
106+
// assert.Equal(t, *vpc.CidrBlock, "172.16.0.0/16")
107+
// assert.Nil(t, vpc.Ipv6CidrAssociations)
108+
// assert.Equal(t, vpc.Tags["Environment"], "ue2")
109+
// assert.Equal(t, vpc.Tags["Namespace"], "eg")
110+
// assert.Equal(t, vpc.Tags["Stage"], "test")
111+
// assert.Equal(t, vpc.Tags["Tenant"], "default")
112+
113+
// subnets := vpc.Subnets
114+
// require.Equal(t, 4, len(subnets))
115+
116+
// public_subnet_ids := atm.OutputList(component, "public_subnet_ids")
117+
// assert.Equal(t, 2, len(public_subnet_ids))
118+
119+
// public_subnet_cidrs := atm.OutputList(component, "public_subnet_cidrs")
120+
// assert.Equal(t, 2, len(public_subnet_cidrs))
121+
122+
// private_subnet_ids := atm.OutputList(component, "private_subnet_ids")
123+
// assert.Equal(t, 2, len(private_subnet_ids))
124+
125+
// private_subnet_cidrs := atm.OutputList(component, "private_subnet_cidrs")
126+
// assert.Equal(t, 2, len(private_subnet_cidrs))
127+
128+
// assert.False(t, aws.IsPublicSubnet(t, private_subnet_ids[0], awsRegion))
129+
// assert.False(t, aws.IsPublicSubnet(t, private_subnet_ids[1], awsRegion))
130+
131+
// assert.True(t, aws.IsPublicSubnet(t, public_subnet_ids[0], awsRegion))
132+
// assert.True(t, aws.IsPublicSubnet(t, public_subnet_ids[1], awsRegion))
133+
134+
// nats, err := GetNatsByVpcIdE(t, vpcId, awsRegion)
135+
// assert.NoError(t, err)
136+
// assert.Equal(t, 1, len(nats))
137+
// })
138+
139+
})
140+
}
141+
142+
func GetNatsByVpcIdE(t *testing.T, vpcId string, awsRegion string) ([]*ec2.NatGateway, error) {
143+
client, err := aws.NewEc2ClientE(t, awsRegion)
144+
if err != nil {
145+
return nil, err
146+
}
147+
148+
filter := ec2.Filter{Name: awssdk.String("vpc-id"), Values: []*string{&vpcId}}
149+
response, err := client.DescribeNatGateways(&ec2.DescribeNatGatewaysInput{Filter: []*ec2.Filter{&filter}})
150+
if err != nil {
151+
return nil, err
152+
}
153+
return response.NatGateways, nil
154+
}

test/fixtures/atmos.yaml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# CLI config is loaded from the following locations (from lowest to highest priority):
2+
# system dir (`/usr/local/etc/atmos` on Linux, `%LOCALAPPDATA%/atmos` on Windows)
3+
# home dir (~/.atmos)
4+
# current directory
5+
# ENV vars
6+
# Command-line arguments
7+
#
8+
# It supports POSIX-style Globs for file names/paths (double-star `**` is supported)
9+
# https://en.wikipedia.org/wiki/Glob_(programming)
10+
11+
# Base path for components, stacks and workflows configurations.
12+
# Can also be set using `ATMOS_BASE_PATH` ENV var, or `--base-path` command-line argument.
13+
# Supports both absolute and relative paths.
14+
# If not provided or is an empty string, `components.terraform.base_path`, `components.helmfile.base_path`, `stacks.base_path` and `workflows.base_path`
15+
# are independent settings (supporting both absolute and relative paths).
16+
# If `base_path` is provided, `components.terraform.base_path`, `components.helmfile.base_path`, `stacks.base_path` and `workflows.base_path`
17+
# are considered paths relative to `base_path`.
18+
base_path: ""
19+
20+
components:
21+
terraform:
22+
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_BASE_PATH` ENV var, or `--terraform-dir` command-line argument
23+
# Supports both absolute and relative paths
24+
base_path: "components/terraform"
25+
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_APPLY_AUTO_APPROVE` ENV var
26+
apply_auto_approve: true
27+
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_DEPLOY_RUN_INIT` ENV var, or `--deploy-run-init` command-line argument
28+
deploy_run_init: true
29+
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE` ENV var, or `--init-run-reconfigure` command-line argument
30+
init_run_reconfigure: true
31+
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_AUTO_GENERATE_BACKEND_FILE` ENV var, or `--auto-generate-backend-file` command-line argument
32+
auto_generate_backend_file: true
33+
34+
stacks:
35+
# Can also be set using `ATMOS_STACKS_BASE_PATH` ENV var, or `--config-dir` and `--stacks-dir` command-line arguments
36+
# Supports both absolute and relative paths
37+
base_path: "stacks"
38+
# Can also be set using `ATMOS_STACKS_INCLUDED_PATHS` ENV var (comma-separated values string)
39+
# Since we are distinguishing stacks based on namespace, and namespace is not part
40+
# of the stack name, we have to set `included_paths` via the ENV var in the Dockerfile
41+
included_paths:
42+
- "orgs/**/*"
43+
44+
# Can also be set using `ATMOS_STACKS_EXCLUDED_PATHS` ENV var (comma-separated values string)
45+
excluded_paths:
46+
- "**/_defaults.yaml"
47+
48+
# Can also be set using `ATMOS_STACKS_NAME_PATTERN` ENV var
49+
name_pattern: "{tenant}-{stage}"
50+
51+
workflows:
52+
# Can also be set using `ATMOS_WORKFLOWS_BASE_PATH` ENV var, or `--workflows-dir` command-line arguments
53+
# Supports both absolute and relative paths
54+
base_path: "stacks/workflows"
55+
56+
# https://github.com/cloudposse/atmos/releases/tag/v1.33.0
57+
logs:
58+
file: "/dev/stdout"
59+
# Supported log levels: Trace, Debug, Info, Warning, Off
60+
level: Info
61+
62+
settings:
63+
# Can also be set using 'ATMOS_SETTINGS_LIST_MERGE_STRATEGY' environment variable, or '--settings-list-merge-strategy' command-line argument
64+
list_merge_strategy: replace
65+
66+
# `Go` templates in Atmos manifests
67+
# https://atmos.tools/core-concepts/stacks/templating
68+
# https://pkg.go.dev/text/template
69+
templates:
70+
settings:
71+
enabled: true
72+
# https://masterminds.github.io/sprig
73+
sprig:
74+
enabled: true
75+
# https://docs.gomplate.ca
76+
gomplate:
77+
enabled: true
78+
79+
commands:
80+
- name: "test-components"
81+
description: "List the Atmos virtual components configured for testing"
82+
steps:
83+
- >
84+
atmos describe stacks --format json --sections=component,metadata --components=component -s sandbox
85+
| jq '.[] | .components.terraform | to_entries |
86+
map(select(.value.component == "component" and (.value.metadata.type != "abstract" or .value.metadata.type == null)))
87+
| .[].key'
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
components:
2+
terraform:
3+
account-map:
4+
metadata:
5+
terraform_workspace: core-gbl-root
6+
vars:
7+
tenant: core
8+
environment: gbl
9+
stage: root
10+
11+
# This remote state is only for Cloud Posse internal use.
12+
# It references the Cloud Posse test organizations actual infrastructure.
13+
# remote_state_backend:
14+
# s3:
15+
# bucket: cptest-core-ue2-root-tfstate-core
16+
# dynamodb_table: cptest-core-ue2-root-tfstate-core-lock
17+
# role_arn: arn:aws:iam::822777368227:role/cptest-core-gbl-root-tfstate-core-ro
18+
# encrypt: true
19+
# key: terraform.tfstate
20+
# acl: bucket-owner-full-control
21+
# region: us-east-2
22+
23+
remote_state_backend_type: static
24+
remote_state_backend:
25+
# This static backend is used for tests that only need to use the account map iam-roles module
26+
# to find the role to assume for Terraform operations. It is configured to use whatever
27+
# the current user's role is, but the environment variable `TEST_ACCOUNT_ID` must be set to
28+
# the account ID of the account that the user is currently assuming a role in.
29+
#
30+
# For some components, this backend is missing important data, and those components
31+
# will need that data added to the backend configuration in order to work properly.
32+
static:
33+
account_info_map: {}
34+
all_accounts: []
35+
aws_partition: aws
36+
full_account_map: {}
37+
iam_role_arn_templates: {}
38+
non_eks_accounts: []
39+
profiles_enabled: false
40+
root_account_aws_name: root
41+
terraform_access_map: {}
42+
terraform_dynamic_role_enabled: false
43+
terraform_role_name_map:
44+
apply: terraform
45+
plan: planner
46+
terraform_roles: {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
components:
2+
terraform:
3+
dns-delegated:
4+
vars:
5+
name: "dns-delegated"
6+
zone_config: []
7+
request_acm_certificate: false
8+
dns_private_zone_enabled: false
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
components:
2+
terraform:
3+
dns-primary:
4+
vars:
5+
domain_names: []

0 commit comments

Comments
 (0)