Skip to content

Commit

Permalink
Added tests (#15)
Browse files Browse the repository at this point in the history
* Added tests

* Refactor tests

* Pin version
  • Loading branch information
goruha authored Feb 20, 2025
1 parent bf11d8c commit 1d1c0ed
Show file tree
Hide file tree
Showing 12 changed files with 735 additions and 3 deletions.
5 changes: 5 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
state/
.cache
test/test-suite.json
.atmos
test_suite.yaml
102 changes: 102 additions & 0 deletions test/component_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package test

import (
"encoding/json"
"fmt"
"strings"
"testing"

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

type BucketPolicy struct {
Version string `json:"Version"`
Statement []struct {
Sid string `json:"Sid,omitempty"`
Principal struct {
Service string `json:"Service"`
} `json:"Principal,omitempty"`
Effect string `json:"Effect"`
Action string `json:"Action"`
Resource interface{} `json:"Resource"`
Condition struct {
StringEquals map[string]string `json:"StringEquals,omitempty"`
StringNotEquals map[string][]string `json:"StringNotEquals,omitempty"`
Null map[string]string `json:"Null,omitempty"`
Bool map[string]bool `json:"Bool,omitempty"`
ArnLike map[string][]string `json:"ArnLike,omitempty"`
} `json:"Condition"`
} `json:"Statement"`
}

type ComponentSuite struct {
helper.TestSuite
}

func (s *ComponentSuite) TestBasic() {
const component = "vpc-flow-logs-bucket/basic"
const stack = "default-test"
const awsRegion = "us-east-2"

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

bucketID := atmos.Output(s.T(), options, "vpc_flow_logs_bucket_id")
assert.NotEmpty(s.T(), bucketID)

bucketArn := atmos.Output(s.T(), options, "vpc_flow_logs_bucket_arn")
assert.True(s.T(), strings.HasSuffix(bucketArn, bucketID))

actualStatus := aws.GetS3BucketVersioning(s.T(), awsRegion, bucketID)
expectedStatus := "Suspended"
assert.Equal(s.T(), expectedStatus, actualStatus)

policyString := aws.GetS3BucketPolicy(s.T(), awsRegion, bucketID)

var policy BucketPolicy
json.Unmarshal([]byte(policyString), &policy)

statement := policy.Statement[0]
assert.Equal(s.T(), "ForceSSLOnlyAccess", statement.Sid)
assert.Equal(s.T(), "s3:*", statement.Action)
assert.Equal(s.T(), "Deny", statement.Effect)
assert.ElementsMatch(s.T(), []string{
fmt.Sprintf("arn:aws:s3:::%s/*", bucketID),
fmt.Sprintf("arn:aws:s3:::%s", bucketID),
}, statement.Resource)
assert.Equal(s.T(), false, statement.Condition.Bool["aws:SecureTransport"])

statement = policy.Statement[1]
assert.Equal(s.T(), "AWSLogDeliveryWrite", statement.Sid)
assert.Equal(s.T(), "Allow", statement.Effect)
assert.Equal(s.T(), "delivery.logs.amazonaws.com", statement.Principal.Service)
assert.Equal(s.T(), "s3:PutObject", statement.Action)
assert.Equal(s.T(), fmt.Sprintf("arn:aws:s3:::%s/*", bucketID), statement.Resource)
assert.Equal(s.T(), "bucket-owner-full-control", statement.Condition.StringEquals["s3:x-amz-acl"])

statement = policy.Statement[2]
assert.Equal(s.T(), "AWSLogDeliveryAclCheck", statement.Sid)
assert.Equal(s.T(), "Allow", statement.Effect)
assert.Equal(s.T(), "delivery.logs.amazonaws.com", statement.Principal.Service)
assert.Equal(s.T(), "s3:GetBucketAcl", statement.Action)
assert.Equal(s.T(), fmt.Sprintf("arn:aws:s3:::%s", bucketID), statement.Resource)

s.DriftTest(component, stack, nil)
}

func (s *ComponentSuite) TestEnabledFlag() {
const component = "vpc-flow-logs-bucket/disabled"
const stack = "default-test"
const awsRegion = "us-east-2"

s.VerifyEnabledFlag(component, stack, nil)
}

func TestRunSuite(t *testing.T) {
suite := new(ComponentSuite)
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: {}
12 changes: 12 additions & 0 deletions test/fixtures/stacks/catalog/usecase/basic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
components:
terraform:
vpc-flow-logs-bucket/basic:
metadata:
component: target
vars:
enabled: true
noncurrent_version_expiration_days: 180
noncurrent_version_transition_days: 30
standard_transition_days: 60
glacier_transition_days: 180
expiration_days: 365
12 changes: 12 additions & 0 deletions test/fixtures/stacks/catalog/usecase/disabled.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
components:
terraform:
vpc-flow-logs-bucket/disabled:
metadata:
component: target
vars:
enabled: false
noncurrent_version_expiration_days: 180
noncurrent_version_transition_days: 30
standard_transition_days: 60
glacier_transition_days: 180
expiration_days: 365
66 changes: 66 additions & 0 deletions test/fixtures/stacks/orgs/default/test/_defaults.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import:
- catalog/account-map

terraform:
backend_type: local
backend:
local:
path: '{{ getenv "COMPONENT_HELPER_STATE_DIR" | default "../../../state" }}/{{ .component }}/terraform.tfstate'
workspace_dir: '{{ getenv "COMPONENT_HELPER_STATE_DIR" | default "../../../state" }}/{{ .component }}/'
vars:
namespace: eg
tenant: default
environment: ue2
region: us-east-2
stage: test
label_order:
- namespace
- tenant
- environment
- stage
- name
- attributes
descriptor_formats:
account_name:
format: "%v-%v"
labels:
- tenant
- stage
stack:
format: "%v-%v-%v"
labels:
- tenant
- environment
- stage

components:
terraform:
account-map:
remote_state_backend:
static:
account_info_map:
default-test:
account_email_format: aws+cptest-%[email protected]
eks: true
id: '{{ getenv "TEST_ACCOUNT_ID" | default "<TEST_ACCOUNT_ID>" }}'
ou: default
parent_ou: none
stage: test
tags:
eks: false
tenant: default
all_accounts:
- default-test
artifacts_account_account_name: default-test
audit_account_account_name: default-test
dns_account_account_name: default-test
eks_accounts:
- default-test
full_account_map:
default-test: '{{ getenv "TEST_ACCOUNT_ID" | default "<TEST_ACCOUNT_ID>" }}'
iam_role_arn_templates:
default-test: 'arn:aws:iam::{{ getenv "TEST_ACCOUNT_ID" | default "<TEST_ACCOUNT_ID>" }}:role/tester-%s'
identity_account_account_name: default-test
root_account_account_name: default-test
terraform_roles:
default-test: ''
4 changes: 4 additions & 0 deletions test/fixtures/stacks/orgs/default/test/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import:
- orgs/default/test/_defaults
- catalog/usecase/basic
- catalog/usecase/disabled
18 changes: 18 additions & 0 deletions test/fixtures/vendor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: atmos/v1
kind: AtmosVendorConfig
metadata:
name: fixtures
description: Atmos vendoring manifest
spec:
sources:
- component: "account-map"
source: github.com/cloudposse/terraform-aws-components.git//modules/account-map?ref={{.Version}}
version: 1.520.0
targets:
- "components/terraform/account-map"
included_paths:
- "**/*.tf"
- "**/*.md"
- "**/*.tftmpl"
- "**/modules/**"
excluded_paths: []
Loading

0 comments on commit 1d1c0ed

Please sign in to comment.