Skip to content

Commit 68b38ec

Browse files
authored
Added tests (#19)
* Added tests * Refactor tests * Fix naming conflict * Fix naming conflict * Fix naming conflict * Fix naming conflict * Pin version
1 parent 9cc2c3a commit 68b38ec

File tree

13 files changed

+818
-4
lines changed

13 files changed

+818
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
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
@@ -18,7 +19,7 @@ aws-assumed-role/
1819
**/nohup.out
1920
**/*.tfstate
2021
**/*.tfstate.*
21-
**/planfile
22+
**/planfilecd
2223
**/*.planfile
2324
**/*.kubeconfig
2425
**/.terraform.lock.hcl

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: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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/gruntwork-io/terratest/modules/random"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
type LifecyclePolicyRuleSelection struct {
17+
TagStatus string `json:"tagStatus"`
18+
TagPrefixList []string `json:"tagPrefixList"`
19+
CountType string `json:"countType"`
20+
CountNumber int `json:"countNumber"`
21+
}
22+
23+
type LifecyclePolicyRule struct {
24+
RulePriority int `json:"rulePriority"`
25+
Description string `json:"description"`
26+
Selection LifecyclePolicyRuleSelection `json:"selection"`
27+
Action map[string]string `json:"action"`
28+
}
29+
30+
type LifecyclePolicy struct {
31+
Rules []LifecyclePolicyRule `json:"rules"`
32+
}
33+
34+
type ComponentSuite struct {
35+
helper.TestSuite
36+
}
37+
38+
func (s *ComponentSuite) TestBasic() {
39+
const component = "ecr/basic"
40+
const stack = "default-test"
41+
const awsRegion = "us-east-2"
42+
43+
suffix := strings.ToLower(random.UniqueId())
44+
45+
inputs := map[string]interface{}{
46+
"images" : []string{
47+
fmt.Sprintf("infrastructure-%s", suffix),
48+
fmt.Sprintf("microservice-a-%s", suffix),
49+
fmt.Sprintf("microservice-b-%s", suffix),
50+
fmt.Sprintf("microservice-c-%s", suffix),
51+
},
52+
}
53+
54+
defer s.DestroyAtmosComponent(s.T(), component, stack, &inputs)
55+
options, _ := s.DeployAtmosComponent(s.T(), component, stack, &inputs)
56+
assert.NotNil(s.T(), options)
57+
58+
awsAccountId := aws.GetAccountId(s.T())
59+
60+
repositoryHost := atmos.Output(s.T(), options, "repository_host")
61+
assert.Equal(s.T(), fmt.Sprintf("%s.dkr.ecr.%s.amazonaws.com", awsAccountId, awsRegion), repositoryHost)
62+
63+
assert.Empty(s.T(), atmos.Output(s.T(), options, "ecr_user_name"))
64+
assert.Empty(s.T(), atmos.Output(s.T(), options, "ecr_user_arn"))
65+
assert.Empty(s.T(), atmos.Output(s.T(), options, "ecr_user_unique_id"))
66+
67+
arnMaps := map[string]string{}
68+
atmos.OutputStruct(s.T(), options, "ecr_repo_arn_map", &arnMaps)
69+
70+
urlMaps := map[string]string{}
71+
atmos.OutputStruct(s.T(), options, "ecr_repo_url_map", &urlMaps)
72+
73+
for name, arn := range arnMaps {
74+
repository := aws.GetECRRepo(s.T(), awsRegion, name)
75+
assert.Equal(s.T(), name, *repository.RepositoryName)
76+
assert.Equal(s.T(), arn, *repository.RepositoryArn)
77+
assert.Equal(s.T(), urlMaps[name], *repository.RepositoryUri)
78+
assert.EqualValues(s.T(), "IMMUTABLE", repository.ImageTagMutability)
79+
assert.True(s.T(), repository.ImageScanningConfiguration.ScanOnPush)
80+
assert.EqualValues(s.T(), "AES256", repository.EncryptionConfiguration.EncryptionType)
81+
82+
lifecyclePolicyString := aws.GetECRRepoLifecyclePolicy(s.T(), awsRegion, repository)
83+
lifecyclePolicy := LifecyclePolicy{}
84+
json.Unmarshal([]byte(lifecyclePolicyString), &lifecyclePolicy)
85+
86+
expectedLifecyclePolicy := LifecyclePolicy{
87+
Rules: []LifecyclePolicyRule{
88+
{
89+
RulePriority: 1,
90+
Description: "Protects images tagged with prod",
91+
Selection: LifecyclePolicyRuleSelection{
92+
TagStatus: "tagged",
93+
TagPrefixList: []string{"prod"},
94+
CountType: "imageCountMoreThan",
95+
CountNumber: 999999,
96+
},
97+
Action: map[string]string{
98+
"type": "expire",
99+
},
100+
},
101+
{
102+
RulePriority: 2,
103+
Description: "Remove untagged images",
104+
Selection: LifecyclePolicyRuleSelection{
105+
TagStatus: "untagged",
106+
CountType: "imageCountMoreThan",
107+
CountNumber: 1,
108+
},
109+
Action: map[string]string{
110+
"type": "expire",
111+
},
112+
},
113+
{
114+
RulePriority: 3,
115+
Description: "Rotate images when reach 500 images stored",
116+
Selection: LifecyclePolicyRuleSelection{
117+
TagStatus: "any",
118+
CountType: "imageCountMoreThan",
119+
CountNumber: 500,
120+
},
121+
Action: map[string]string{
122+
"type": "expire",
123+
},
124+
},
125+
},
126+
}
127+
assert.EqualValues(s.T(), expectedLifecyclePolicy, lifecyclePolicy)
128+
}
129+
130+
s.DriftTest(component, stack, &inputs)
131+
}
132+
133+
func (s *ComponentSuite) TestEnabledFlag() {
134+
const component = "ecr/disabled"
135+
const stack = "default-test"
136+
const awsRegion = "us-east-2"
137+
138+
s.VerifyEnabledFlag(component, stack, nil)
139+
}
140+
141+
func TestRunSuite(t *testing.T) {
142+
suite := new(ComponentSuite)
143+
helper.Run(t, suite)
144+
}

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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
components:
2+
terraform:
3+
ecr/basic:
4+
metadata:
5+
component: target
6+
vars:
7+
# Set `ecr_user_enabled: true` when this issue would be resolved
8+
# https://github.com/cloudposse-terraform-components/aws-ecr/issues/18
9+
ecr_user_enabled: false
10+
enable_lifecycle_policy: true
11+
max_image_count: 500
12+
scan_images_on_push: true
13+
protected_tags:
14+
- prod
15+
image_tag_mutability: IMMUTABLE
16+
images:
17+
- infrastructure
18+
- microservice-a
19+
- microservice-b
20+
- microservice-c
21+
read_write_account_role_map: {}
22+
# identity:
23+
# - admin
24+
# - cicd
25+
# automation:
26+
# - admin
27+
read_only_account_role_map: {}
28+
# corp: ["*"]
29+
# dev: ["*"]
30+
# prod: ["*"]
31+
# stage: ["*"]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
components:
2+
terraform:
3+
ecr/disabled:
4+
metadata:
5+
component: target
6+
vars:
7+
enabled: false
8+
# Set `ecr_user_enabled: true` when this issue would be resolved
9+
# https://github.com/cloudposse-terraform-components/aws-ecr/issues/18
10+
ecr_user_enabled: false
11+
enable_lifecycle_policy: true
12+
max_image_count: 500
13+
scan_images_on_push: true
14+
protected_tags:
15+
- prod
16+
image_tag_mutability: IMMUTABLE
17+
images:
18+
- infrastructure
19+
- microservice-a
20+
- microservice-b
21+
- microservice-c
22+
read_write_account_role_map: {}
23+
# identity:
24+
# - admin
25+
# - cicd
26+
# automation:
27+
# - admin
28+
read_only_account_role_map: {}
29+
# corp: ["*"]
30+
# dev: ["*"]
31+
# prod: ["*"]
32+
# stage: ["*"]

0 commit comments

Comments
 (0)