Skip to content

Commit cc31c78

Browse files
committed
Added tests
1 parent 5ddfd10 commit cc31c78

File tree

19 files changed

+1525
-6
lines changed

19 files changed

+1525
-6
lines changed

src/provider-helm.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,8 @@ locals {
133133
"--profile", var.kube_exec_auth_aws_profile
134134
] : []
135135

136-
kube_exec_auth_role_arn = coalesce(var.kube_exec_auth_role_arn, module.iam_roles.terraform_role_arn)
137136
exec_role = local.kube_exec_auth_enabled && var.kube_exec_auth_role_arn_enabled ? [
138-
"--role-arn", local.kube_exec_auth_role_arn
137+
"--role-arn", coalesce(var.kube_exec_auth_role_arn, module.iam_roles.terraform_role_arn)
139138
] : []
140139

141140
# Provide dummy configuration for the case where the EKS cluster is not available.

src/remote-state.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module "eks" {
22
source = "cloudposse/stack-config/yaml//modules/remote-state"
3-
version = "1.5.0"
3+
version = "1.8.0"
44

55
component = var.eks_component_name
66

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: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
package test
2+
3+
import (
4+
"context"
5+
"testing"
6+
"fmt"
7+
"strings"
8+
"os"
9+
// "time"
10+
"github.com/cloudposse/test-helpers/pkg/atmos"
11+
"github.com/cloudposse/test-helpers/pkg/helm"
12+
awsHelper "github.com/cloudposse/test-helpers/pkg/aws"
13+
helper "github.com/cloudposse/test-helpers/pkg/atmos/component-helper"
14+
awsTerratest "github.com/gruntwork-io/terratest/modules/aws"
15+
"github.com/gruntwork-io/terratest/modules/random"
16+
"github.com/google/go-github/v70/github"
17+
"github.com/stretchr/testify/assert"
18+
19+
// metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
// corev1 "k8s.io/api/core/v1"
21+
// "k8s.io/apimachinery/pkg/runtime/schema"
22+
// "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
23+
24+
// "k8s.io/client-go/dynamic"
25+
// "k8s.io/client-go/dynamic/dynamicinformer"
26+
// "k8s.io/client-go/tools/cache"
27+
)
28+
29+
type ComponentSuite struct {
30+
helper.TestSuite
31+
}
32+
33+
func (s *ComponentSuite) TestBasic() {
34+
const component = "eks/actions-runner-controller/basic"
35+
const stack = "default-test"
36+
const awsRegion = "us-east-2"
37+
38+
clusterOptions := s.GetAtmosOptions("eks/cluster", stack, nil)
39+
clusrerId := atmos.Output(s.T(), clusterOptions, "eks_cluster_id")
40+
cluster := awsHelper.GetEksCluster(s.T(), context.Background(), awsRegion, clusrerId)
41+
clientset, err := awsHelper.NewK8SClientset(cluster)
42+
assert.NoError(s.T(), err)
43+
assert.NotNil(s.T(), clientset)
44+
45+
githubOrg := "cloudposse-tests"
46+
47+
token := os.Getenv("GITHUB_TOKEN")
48+
49+
randomID := strings.ToLower(random.UniqueId())
50+
51+
namespace := fmt.Sprintf("external-secrets-%s", randomID)
52+
secretPathPrefix := fmt.Sprintf("test-%s", randomID)
53+
secretGithubPATPath := fmt.Sprintf("/%s/token", secretPathPrefix)
54+
secretWebhookPath := fmt.Sprintf("/%s/webhook", secretPathPrefix)
55+
secretDockerConfigPath := fmt.Sprintf("/%s/docker", secretPathPrefix)
56+
57+
defer func() {
58+
awsTerratest.DeleteParameter(s.T(), awsRegion, secretGithubPATPath)
59+
awsTerratest.DeleteParameter(s.T(), awsRegion, secretWebhookPath)
60+
awsTerratest.DeleteParameter(s.T(), awsRegion, secretDockerConfigPath)
61+
}()
62+
awsTerratest.PutParameter(s.T(), awsRegion, secretGithubPATPath, "Test value", token)
63+
awsTerratest.PutParameter(s.T(), awsRegion, secretWebhookPath, "Test value", randomID)
64+
awsTerratest.PutParameter(s.T(), awsRegion, secretDockerConfigPath, "Test value", randomID)
65+
66+
inputs := map[string]interface{}{
67+
"kubernetes_namespace": namespace,
68+
"ssm_github_secret_path": secretGithubPATPath,
69+
"ssm_github_webhook_secret_token_path": secretWebhookPath,
70+
"ssm_docker_config_json_path": secretDockerConfigPath,
71+
"runners": map[string]interface{}{
72+
"infra-runner": map[string]interface{}{
73+
"node_selector": map[string]interface{}{
74+
"kubernetes.io/os": "linux",
75+
"kubernetes.io/arch": "amd64",
76+
},
77+
"type": "organization",
78+
"dind_enabled": true,
79+
"image": "summerwind/actions-runner-dind",
80+
"scope": githubOrg,
81+
"min_replicas": 1,
82+
"max_replicas": 1,
83+
"scale_down_delay_seconds": 100,
84+
"scheduled_overrides": []map[string]interface{}{},
85+
"resources": map[string]interface{}{
86+
"limits": map[string]interface{}{
87+
"cpu": "200m",
88+
"memory": "512Mi",
89+
},
90+
"requests": map[string]interface{}{
91+
"cpu": "100m",
92+
"memory": "128Mi",
93+
},
94+
},
95+
"webhook_driven_scaling_enabled": false,
96+
"max_duration": "90m",
97+
"pull_driven_scaling_enabled": true,
98+
"labels": []string{
99+
randomID,
100+
},
101+
},
102+
},
103+
}
104+
105+
defer s.DestroyAtmosComponent(s.T(), component, stack, &inputs)
106+
options, _ := s.DeployAtmosComponent(s.T(), component, stack, &inputs)
107+
assert.NotNil(s.T(), options)
108+
109+
110+
metadataArray := []helm.Metadata{}
111+
112+
atmos.OutputStruct(s.T(), options, "metadata", &metadataArray)
113+
114+
metadata := metadataArray[0]
115+
116+
assert.Equal(s.T(), metadata.AppVersion, "0.27.6")
117+
assert.Equal(s.T(), metadata.Chart, "actions-runner-controller")
118+
assert.NotNil(s.T(), metadata.FirstDeployed)
119+
assert.NotNil(s.T(), metadata.LastDeployed)
120+
assert.Equal(s.T(), metadata.Name, "actions-runner-controller")
121+
assert.Equal(s.T(), metadata.Namespace, namespace)
122+
assert.NotNil(s.T(), metadata.Values)
123+
assert.Equal(s.T(), metadata.Version, "0.23.7")
124+
125+
126+
metadataRunners := map[string][]helm.Metadata{}
127+
128+
atmos.OutputStruct(s.T(), options, "metadata_action_runner_releases", &metadataRunners)
129+
130+
assert.Equal(s.T(), len(metadataRunners), 1)
131+
132+
runnerMetadata := metadataRunners["infra-runner"][0]
133+
134+
assert.Equal(s.T(), runnerMetadata.AppVersion, "v1alpha1")
135+
assert.Equal(s.T(), runnerMetadata.Chart, "actions-runner")
136+
assert.NotNil(s.T(), runnerMetadata.FirstDeployed)
137+
assert.NotNil(s.T(), runnerMetadata.LastDeployed)
138+
assert.Equal(s.T(), runnerMetadata.Name, "infra-runner")
139+
assert.Equal(s.T(), runnerMetadata.Namespace, namespace)
140+
assert.NotNil(s.T(), runnerMetadata.Values)
141+
assert.Equal(s.T(), runnerMetadata.Version, "0.3.2")
142+
143+
144+
client := github.NewClient(nil).WithAuthToken(token)
145+
146+
runners, _, err := client.Actions.ListOrganizationRunners(context.Background(), githubOrg, nil)
147+
assert.Nil(s.T(), err)
148+
assert.NotNil(s.T(), runners)
149+
assert.True(s.T(), len(runners.Runners) > 0, "Expected at least one self-hosted runner")
150+
151+
found := false
152+
for _, runner := range runners.Runners {
153+
for _, label := range runner.Labels {
154+
if label.GetName() == randomID {
155+
found = true
156+
break
157+
}
158+
}
159+
if found {
160+
break
161+
}
162+
}
163+
assert.True(s.T(), found, "Expected to find a self-hosted runner with the label")
164+
165+
s.DriftTest(component, stack, &inputs)
166+
}
167+
168+
func (s *ComponentSuite) TestEnabledFlag() {
169+
const component = "eks/actions-runner-controller/disabled"
170+
const stack = "default-test"
171+
s.VerifyEnabledFlag(component, stack, nil)
172+
}
173+
174+
func (s *ComponentSuite) SetupSuite() {
175+
s.TestSuite.InitConfig()
176+
s.TestSuite.Config.ComponentDestDir = "components/terraform/eks/actions-runner-controller"
177+
s.TestSuite.SetupSuite()
178+
}
179+
180+
func TestRunSuite(t *testing.T) {
181+
suite := new(ComponentSuite)
182+
suite.AddDependency(t, "vpc", "default-test", nil)
183+
184+
subdomain := strings.ToLower(random.UniqueId())
185+
inputs := map[string]interface{}{
186+
"zone_config": []map[string]interface{}{
187+
{
188+
"subdomain": subdomain,
189+
"zone_name": "components.cptest.test-automation.app",
190+
},
191+
},
192+
}
193+
suite.AddDependency(t, "dns-delegated", "default-test", &inputs)
194+
195+
suite.AddDependency(t, "eks/cluster", "default-test", nil)
196+
suite.AddDependency(t, "eks/cert-manager", "default-test", nil)
197+
helper.Run(t, suite)
198+
}

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: true
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: []

0 commit comments

Comments
 (0)