Skip to content

Commit f87fa7c

Browse files
committed
Added tests
1 parent d0354f0 commit f87fa7c

File tree

16 files changed

+848
-8
lines changed

16 files changed

+848
-8
lines changed

src/main.tf

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ module "elasticsearch" {
5757
}
5858

5959
resource "random_password" "elasticsearch_password" {
60+
count = local.create_password ? 1 : 0
6061
# character length
6162
length = 33
6263

63-
special = true
64-
upper = true
65-
lower = true
66-
number = true
64+
special = true
65+
upper = true
66+
lower = true
67+
numeric = true
6768

6869
min_special = 1
6970
min_upper = 1
@@ -101,7 +102,7 @@ resource "aws_ssm_parameter" "elasticsearch_kibana_endpoint" {
101102

102103
module "elasticsearch_log_cleanup" {
103104
source = "cloudposse/lambda-elasticsearch-cleanup/aws"
104-
version = "0.14.1"
105+
version = "0.16.1"
105106

106107
es_endpoint = module.elasticsearch.domain_endpoint
107108
es_domain_arn = module.elasticsearch.domain_arn

test/.gitignore

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

test/component_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package test
2+
3+
import (
4+
//"context"
5+
"fmt"
6+
"strings"
7+
"testing"
8+
9+
// "github.com/aws/aws-sdk-go-v2/service/docdb"
10+
"github.com/cloudposse/test-helpers/pkg/atmos"
11+
helper "github.com/cloudposse/test-helpers/pkg/atmos/component-helper"
12+
// awshelper "github.com/cloudposse/test-helpers/pkg/aws"
13+
"github.com/gruntwork-io/terratest/modules/aws"
14+
"github.com/gruntwork-io/terratest/modules/random"
15+
"github.com/stretchr/testify/assert"
16+
)
17+
18+
type ComponentSuite struct {
19+
helper.TestSuite
20+
}
21+
22+
func (s *ComponentSuite) TestBasic() {
23+
const component = "elasticsearch/basic"
24+
const stack = "default-test"
25+
const awsRegion = "us-east-2"
26+
27+
accountId := aws.GetAccountId(s.T())
28+
assert.NotNil(s.T(), accountId)
29+
30+
inputs := map[string]interface{}{}
31+
32+
defer s.DestroyAtmosComponent(s.T(), component, stack, &inputs)
33+
options, _ := s.DeployAtmosComponent(s.T(), component, stack, &inputs)
34+
assert.NotNil(s.T(), options)
35+
36+
securityGroupId := atmos.Output(s.T(), options, "security_group_id")
37+
assert.True(s.T(), strings.HasPrefix(securityGroupId, "sg-"))
38+
39+
domainArn := atmos.Output(s.T(), options, "domain_arn")
40+
assert.True(s.T(), strings.HasPrefix(domainArn, fmt.Sprintf("arn:aws:es:%s:%s:domain/eg-default-ue2-test-e-", awsRegion, accountId)))
41+
42+
domainId := atmos.Output(s.T(), options, "domain_id")
43+
assert.True(s.T(), strings.Contains(domainId, "eg-default-ue2-test-e-"))
44+
45+
domainEndpoint := atmos.Output(s.T(), options, "domain_endpoint")
46+
assert.True(s.T(), strings.HasPrefix(domainEndpoint, "vpc-eg-default-ue2-test-e-"))
47+
48+
kibanaEndpoint := atmos.Output(s.T(), options, "kibana_endpoint")
49+
assert.True(s.T(), strings.HasPrefix(kibanaEndpoint, "vpc-eg-default-ue2-test-e-"))
50+
51+
domainHostname := atmos.Output(s.T(), options, "domain_hostname")
52+
assert.True(s.T(), strings.HasPrefix(domainHostname, "eg-default-ue2-test-e-"))
53+
54+
kibanaHostname := atmos.Output(s.T(), options, "kibana_hostname")
55+
assert.True(s.T(), strings.HasSuffix(kibanaHostname, "components.cptest.test-automation.app"))
56+
57+
userIamRoleName := atmos.Output(s.T(), options, "elasticsearch_user_iam_role_name")
58+
assert.Empty(s.T(), userIamRoleName)
59+
60+
userIamRoleArn := atmos.Output(s.T(), options, "elasticsearch_user_iam_role_arn")
61+
assert.Empty(s.T(), userIamRoleArn)
62+
63+
masterPasswordSSMKey := atmos.Output(s.T(), options, "master_password_ssm_key")
64+
assert.Equal(s.T(), masterPasswordSSMKey, "/elasticsearch/e/password")
65+
66+
s.DriftTest(component, stack, &inputs)
67+
}
68+
69+
// func (s *ComponentSuite) TestEnabledFlag() {
70+
// const component = "elasticsearch/disabled"
71+
// const stack = "default-test"
72+
// const awsRegion = "us-east-2"
73+
74+
// s.VerifyEnabledFlag(component, stack, nil)
75+
// }
76+
77+
78+
func TestRunSuite(t *testing.T) {
79+
suite := new(ComponentSuite)
80+
81+
suite.AddDependency(t, "vpc", "default-test", nil)
82+
83+
subdomain := strings.ToLower(random.UniqueId())
84+
inputs := map[string]interface{}{
85+
"zone_config": []map[string]interface{}{
86+
{
87+
"subdomain": subdomain,
88+
"zone_name": "components.cptest.test-automation.app",
89+
},
90+
},
91+
}
92+
suite.AddDependency(t, "dns-delegated", "default-test", &inputs)
93+
helper.Run(t, suite)
94+
}

test/fixtures/atmos.yaml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
components:
2+
terraform:
3+
dns-delegated:
4+
metadata:
5+
component: dns-delegated
6+
vars:
7+
zone_config:
8+
- subdomain: test
9+
zone_name: example.net
10+
request_acm_certificate: false
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
components:
2+
terraform:
3+
dns-primary:
4+
metadata:
5+
component: dns-primary
6+
vars:
7+
domain_names:
8+
- example.net
9+
record_config: []
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
components:
2+
terraform:
3+
elasticsearch/basic:
4+
metadata:
5+
component: target
6+
vars:
7+
enabled: true
8+
name: "e"
9+
instance_type: "t3.medium.elasticsearch"
10+
elasticsearch_version: "7.9"
11+
encrypt_at_rest_enabled: true
12+
dedicated_master_enabled: false
13+
elasticsearch_subdomain_name: "es"
14+
kibana_subdomain_name: "kibana"
15+
ebs_volume_size: 40
16+
create_iam_service_linked_role: true
17+
kibana_hostname_enabled: true
18+
domain_hostname_enabled: true
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
components:
2+
terraform:
3+
elasticsearch/disabled:
4+
metadata:
5+
component: target
6+
vars:
7+
enabled: false
8+
instance_type: "t3.medium.elasticsearch"
9+
elasticsearch_version: "7.9"
10+
encrypt_at_rest_enabled: true
11+
dedicated_master_enabled: false
12+
elasticsearch_subdomain_name: "es"
13+
kibana_subdomain_name: "kibana"
14+
ebs_volume_size: 40
15+
create_iam_service_linked_role: true
16+
kibana_hostname_enabled: true
17+
domain_hostname_enabled: true
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
components:
2+
terraform:
3+
vpc:
4+
metadata:
5+
component: vpc
6+
vars:
7+
name: "vpc"
8+
availability_zones:
9+
- "b"
10+
- "c"
11+
public_subnets_enabled: true
12+
max_nats: 1
13+
# Private subnets do not need internet access
14+
nat_gateway_enabled: false
15+
nat_instance_enabled: false
16+
subnet_type_tag_key: "eg.cptest.co/subnet/type"
17+
max_subnet_count: 3
18+
vpc_flow_logs_enabled: false
19+
ipv4_primary_cidr_block: "172.16.0.0/16"

0 commit comments

Comments
 (0)