Skip to content

Commit 1d1c0ed

Browse files
authored
Added tests (#15)
* Added tests * Refactor tests * Pin version
1 parent bf11d8c commit 1d1c0ed

File tree

12 files changed

+735
-3
lines changed

12 files changed

+735
-3
lines changed

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: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package test
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"strings"
7+
"testing"
8+
9+
"github.com/cloudposse/test-helpers/pkg/atmos"
10+
helper "github.com/cloudposse/test-helpers/pkg/atmos/component-helper"
11+
"github.com/gruntwork-io/terratest/modules/aws"
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
type BucketPolicy struct {
16+
Version string `json:"Version"`
17+
Statement []struct {
18+
Sid string `json:"Sid,omitempty"`
19+
Principal struct {
20+
Service string `json:"Service"`
21+
} `json:"Principal,omitempty"`
22+
Effect string `json:"Effect"`
23+
Action string `json:"Action"`
24+
Resource interface{} `json:"Resource"`
25+
Condition struct {
26+
StringEquals map[string]string `json:"StringEquals,omitempty"`
27+
StringNotEquals map[string][]string `json:"StringNotEquals,omitempty"`
28+
Null map[string]string `json:"Null,omitempty"`
29+
Bool map[string]bool `json:"Bool,omitempty"`
30+
ArnLike map[string][]string `json:"ArnLike,omitempty"`
31+
} `json:"Condition"`
32+
} `json:"Statement"`
33+
}
34+
35+
type ComponentSuite struct {
36+
helper.TestSuite
37+
}
38+
39+
func (s *ComponentSuite) TestBasic() {
40+
const component = "vpc-flow-logs-bucket/basic"
41+
const stack = "default-test"
42+
const awsRegion = "us-east-2"
43+
44+
defer s.DestroyAtmosComponent(s.T(), component, stack, nil)
45+
options, _ := s.DeployAtmosComponent(s.T(), component, stack, nil)
46+
assert.NotNil(s.T(), options)
47+
48+
bucketID := atmos.Output(s.T(), options, "vpc_flow_logs_bucket_id")
49+
assert.NotEmpty(s.T(), bucketID)
50+
51+
bucketArn := atmos.Output(s.T(), options, "vpc_flow_logs_bucket_arn")
52+
assert.True(s.T(), strings.HasSuffix(bucketArn, bucketID))
53+
54+
actualStatus := aws.GetS3BucketVersioning(s.T(), awsRegion, bucketID)
55+
expectedStatus := "Suspended"
56+
assert.Equal(s.T(), expectedStatus, actualStatus)
57+
58+
policyString := aws.GetS3BucketPolicy(s.T(), awsRegion, bucketID)
59+
60+
var policy BucketPolicy
61+
json.Unmarshal([]byte(policyString), &policy)
62+
63+
statement := policy.Statement[0]
64+
assert.Equal(s.T(), "ForceSSLOnlyAccess", statement.Sid)
65+
assert.Equal(s.T(), "s3:*", statement.Action)
66+
assert.Equal(s.T(), "Deny", statement.Effect)
67+
assert.ElementsMatch(s.T(), []string{
68+
fmt.Sprintf("arn:aws:s3:::%s/*", bucketID),
69+
fmt.Sprintf("arn:aws:s3:::%s", bucketID),
70+
}, statement.Resource)
71+
assert.Equal(s.T(), false, statement.Condition.Bool["aws:SecureTransport"])
72+
73+
statement = policy.Statement[1]
74+
assert.Equal(s.T(), "AWSLogDeliveryWrite", statement.Sid)
75+
assert.Equal(s.T(), "Allow", statement.Effect)
76+
assert.Equal(s.T(), "delivery.logs.amazonaws.com", statement.Principal.Service)
77+
assert.Equal(s.T(), "s3:PutObject", statement.Action)
78+
assert.Equal(s.T(), fmt.Sprintf("arn:aws:s3:::%s/*", bucketID), statement.Resource)
79+
assert.Equal(s.T(), "bucket-owner-full-control", statement.Condition.StringEquals["s3:x-amz-acl"])
80+
81+
statement = policy.Statement[2]
82+
assert.Equal(s.T(), "AWSLogDeliveryAclCheck", statement.Sid)
83+
assert.Equal(s.T(), "Allow", statement.Effect)
84+
assert.Equal(s.T(), "delivery.logs.amazonaws.com", statement.Principal.Service)
85+
assert.Equal(s.T(), "s3:GetBucketAcl", statement.Action)
86+
assert.Equal(s.T(), fmt.Sprintf("arn:aws:s3:::%s", bucketID), statement.Resource)
87+
88+
s.DriftTest(component, stack, nil)
89+
}
90+
91+
func (s *ComponentSuite) TestEnabledFlag() {
92+
const component = "vpc-flow-logs-bucket/disabled"
93+
const stack = "default-test"
94+
const awsRegion = "us-east-2"
95+
96+
s.VerifyEnabledFlag(component, stack, nil)
97+
}
98+
99+
func TestRunSuite(t *testing.T) {
100+
suite := new(ComponentSuite)
101+
helper.Run(t, suite)
102+
}

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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
components:
2+
terraform:
3+
vpc-flow-logs-bucket/basic:
4+
metadata:
5+
component: target
6+
vars:
7+
enabled: true
8+
noncurrent_version_expiration_days: 180
9+
noncurrent_version_transition_days: 30
10+
standard_transition_days: 60
11+
glacier_transition_days: 180
12+
expiration_days: 365
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
components:
2+
terraform:
3+
vpc-flow-logs-bucket/disabled:
4+
metadata:
5+
component: target
6+
vars:
7+
enabled: false
8+
noncurrent_version_expiration_days: 180
9+
noncurrent_version_transition_days: 30
10+
standard_transition_days: 60
11+
glacier_transition_days: 180
12+
expiration_days: 365
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import:
2+
- catalog/account-map
3+
4+
terraform:
5+
backend_type: local
6+
backend:
7+
local:
8+
path: '{{ getenv "COMPONENT_HELPER_STATE_DIR" | default "../../../state" }}/{{ .component }}/terraform.tfstate'
9+
workspace_dir: '{{ getenv "COMPONENT_HELPER_STATE_DIR" | default "../../../state" }}/{{ .component }}/'
10+
vars:
11+
namespace: eg
12+
tenant: default
13+
environment: ue2
14+
region: us-east-2
15+
stage: test
16+
label_order:
17+
- namespace
18+
- tenant
19+
- environment
20+
- stage
21+
- name
22+
- attributes
23+
descriptor_formats:
24+
account_name:
25+
format: "%v-%v"
26+
labels:
27+
- tenant
28+
- stage
29+
stack:
30+
format: "%v-%v-%v"
31+
labels:
32+
- tenant
33+
- environment
34+
- stage
35+
36+
components:
37+
terraform:
38+
account-map:
39+
remote_state_backend:
40+
static:
41+
account_info_map:
42+
default-test:
43+
account_email_format: aws+cptest-%[email protected]
44+
eks: true
45+
id: '{{ getenv "TEST_ACCOUNT_ID" | default "<TEST_ACCOUNT_ID>" }}'
46+
ou: default
47+
parent_ou: none
48+
stage: test
49+
tags:
50+
eks: false
51+
tenant: default
52+
all_accounts:
53+
- default-test
54+
artifacts_account_account_name: default-test
55+
audit_account_account_name: default-test
56+
dns_account_account_name: default-test
57+
eks_accounts:
58+
- default-test
59+
full_account_map:
60+
default-test: '{{ getenv "TEST_ACCOUNT_ID" | default "<TEST_ACCOUNT_ID>" }}'
61+
iam_role_arn_templates:
62+
default-test: 'arn:aws:iam::{{ getenv "TEST_ACCOUNT_ID" | default "<TEST_ACCOUNT_ID>" }}:role/tester-%s'
63+
identity_account_account_name: default-test
64+
root_account_account_name: default-test
65+
terraform_roles:
66+
default-test: ''
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import:
2+
- orgs/default/test/_defaults
3+
- catalog/usecase/basic
4+
- catalog/usecase/disabled

test/fixtures/vendor.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: atmos/v1
2+
kind: AtmosVendorConfig
3+
metadata:
4+
name: fixtures
5+
description: Atmos vendoring manifest
6+
spec:
7+
sources:
8+
- component: "account-map"
9+
source: github.com/cloudposse/terraform-aws-components.git//modules/account-map?ref={{.Version}}
10+
version: 1.520.0
11+
targets:
12+
- "components/terraform/account-map"
13+
included_paths:
14+
- "**/*.tf"
15+
- "**/*.md"
16+
- "**/*.tftmpl"
17+
- "**/modules/**"
18+
excluded_paths: []

0 commit comments

Comments
 (0)