Skip to content

Commit 3d16f37

Browse files
authored
Add component tests (#16)
* Added tests * Added github app * Fix tests * Added asserts
1 parent 9100bfa commit 3d16f37

File tree

14 files changed

+954
-10
lines changed

14 files changed

+954
-10
lines changed

src/main.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ locals {
1414

1515
manifest_kubernetes_namespace = var.manifest_kubernetes_namespace
1616

17-
team_slugs = toset(compact([
17+
team_slugs = local.enabled ? toset(compact([
1818
for permission in var.permissions : lookup(permission, "team_slug", null)
19-
]))
19+
])) : []
2020

2121
team_ids = [
2222
for team in data.github_team.default : team.id

src/outputs.tf

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ output "repository" {
1515

1616
output "repository_description" {
1717
description = "Repository description"
18-
value = local.github_repository.description
18+
value = local.enabled ? local.github_repository.description : null
1919
}
2020

2121
output "repository_default_branch" {
2222
description = "Repository default branch"
23-
value = local.github_repository.default_branch
23+
value = local.enabled ? local.github_repository.default_branch : null
2424
}
2525

2626
output "repository_url" {
2727
description = "Repository URL"
28-
value = local.github_repository.html_url
28+
value = local.enabled ? local.github_repository.html_url : null
2929
}
3030

3131
output "repository_git_clone_url" {
3232
description = "Repository git clone URL"
33-
value = local.github_repository.git_clone_url
33+
value = local.enabled ? local.github_repository.git_clone_url : null
3434
}
3535

3636
output "repository_ssh_clone_url" {
3737
description = "Repository SSH clone URL"
38-
value = local.github_repository.ssh_clone_url
38+
value = local.enabled ? local.github_repository.ssh_clone_url : null
3939
}

test/.gitignore

+5
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

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
// "strconv"
8+
"testing"
9+
"strings"
10+
11+
helper "github.com/cloudposse/test-helpers/pkg/atmos/component-helper"
12+
awsTerratest "github.com/gruntwork-io/terratest/modules/aws"
13+
"github.com/gruntwork-io/terratest/modules/random"
14+
"github.com/google/go-github/v70/github"
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 = "argocd-github-repo/basic"
24+
const stack = "default-test"
25+
const awsRegion = "us-east-2"
26+
const githubOrg = "cloudposse-tests"
27+
28+
token := os.Getenv("GITHUB_TOKEN")
29+
30+
randomID := strings.ToLower(random.UniqueId())
31+
32+
secretPath := fmt.Sprintf("/argocd/%s/github/api_key", randomID)
33+
deployKeyPath := fmt.Sprintf("/argocd/deploy_keys/%s/%s", randomID, "%s")
34+
repoName := fmt.Sprintf("argocd-github-repo-%s", randomID)
35+
36+
defer func() {
37+
awsTerratest.DeleteParameter(s.T(), awsRegion, secretPath)
38+
}()
39+
awsTerratest.PutParameter(s.T(), awsRegion, secretPath, "Github API Key", token)
40+
41+
inputs := map[string]interface{}{
42+
"ssm_github_deploy_key_format": deployKeyPath,
43+
"ssm_github_api_key": secretPath,
44+
"name": repoName,
45+
"github_organization": githubOrg,
46+
}
47+
48+
defer s.DestroyAtmosComponent(s.T(), component, stack, &inputs)
49+
options, _ := s.DeployAtmosComponent(s.T(), component, stack, &inputs)
50+
assert.NotNil(s.T(), options)
51+
52+
client := github.NewClient(nil).WithAuthToken(token)
53+
54+
// Check if the repository exists
55+
repo, _, err := client.Repositories.Get(context.Background(), githubOrg, repoName)
56+
assert.Nil(s.T(), err)
57+
assert.NotNil(s.T(), repo)
58+
59+
filePath := "mgmt/uw2-sandbox/argocd/applicationset.yaml"
60+
// Use the GitHub API to check if the file exists in the repository
61+
_, _, _, err = client.Repositories.GetContents(context.Background(), githubOrg, repoName, filePath, nil)
62+
assert.Nil(s.T(), err)
63+
s.DriftTest(component, stack, &inputs)
64+
}
65+
66+
func (s *ComponentSuite) TestEnabledFlag() {
67+
const component = "argocd-github-repo/disabled"
68+
const stack = "default-test"
69+
const awsRegion = "us-east-2"
70+
71+
s.VerifyEnabledFlag(component, stack, nil)
72+
}
73+
74+
func TestRunSuite(t *testing.T) {
75+
suite := new(ComponentSuite)
76+
helper.Run(t, suite)
77+
}

test/fixtures/atmos.yaml

+77
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
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: {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
components:
2+
terraform:
3+
argocd-github-repo/basic:
4+
metadata:
5+
component: target
6+
vars:
7+
enabled: true
8+
github_user: goruha
9+
github_user_email: ci@acme.com
10+
github_organization: cloudposse-tests
11+
github_codeowner_teams:
12+
- "@cloudposse-tests/admin"
13+
permissions:
14+
- team_slug: admin
15+
permission: admin
16+
name: argocd-deploy-non-prod
17+
description: "ArgoCD desired state repository (Non-production) for ACME applications"
18+
environments:
19+
- tenant: mgmt
20+
environment: uw2
21+
stage: sandbox
22+
auto-sync: false
23+
gitignore_entries: []
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
components:
2+
terraform:
3+
argocd-github-repo/disabled:
4+
metadata:
5+
component: target
6+
vars:
7+
enabled: false
8+
github_user: ci-acme
9+
github_user_email: ci@acme.com
10+
github_organization: cloudposse-tests
11+
github_codeowner_teams:
12+
- "@cloudposse-tests/admin"
13+
permissions:
14+
- team_slug: admins
15+
permission: admin
16+
name: argocd-deploy-non-prod
17+
description: "ArgoCD desired state repository (Non-production) for ACME applications"
18+
environments:
19+
- tenant: mgmt
20+
environment: uw2
21+
stage: sandbox
22+
auto-sync: false
23+
gitignore_entries: []
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-%s@example.com
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: ''
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

+18
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)