Skip to content

Commit 581237e

Browse files
authored
Added tests (#84)
1 parent d8a88e8 commit 581237e

File tree

17 files changed

+812
-16
lines changed

17 files changed

+812
-16
lines changed

src/main.tf

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ locals {
2727

2828
policy = local.enabled ? jsondecode(data.aws_iam_policy_document.default[0].json) : null
2929

30-
# default datadog_logs_archive query.
30+
# default datadog_logs_archive query.
3131
default_query = join(" OR ", concat([join(":", ["env", var.stage]), join(":", ["account", local.aws_account_id])], var.additional_query_tags))
3232
query = var.query_override == null ? local.default_query : var.query_override
3333
}
@@ -65,7 +65,7 @@ data "aws_iam_policy_document" "default" {
6565
]
6666

6767
resources = [
68-
"arn:${local.aws_partition}:s3:::${module.cloudtrail_label.id}",
68+
"arn:${local.aws_partition}:s3:::${module.cloudtrail_bucket_label.id}",
6969
]
7070
}
7171

@@ -84,7 +84,7 @@ data "aws_iam_policy_document" "default" {
8484
]
8585

8686
resources = [
87-
"arn:${local.aws_partition}:s3:::${module.cloudtrail_label.id}/*",
87+
"arn:${local.aws_partition}:s3:::${module.cloudtrail_bucket_label.id}/*",
8888
]
8989

9090
condition {
@@ -98,7 +98,7 @@ data "aws_iam_policy_document" "default" {
9898
test = "StringLike"
9999
variable = "aws:SourceArn"
100100
values = [
101-
"arn:${local.aws_partition}:cloudtrail:*:${local.aws_account_id}:trail/*datadog-logs-archive",
101+
"arn:${local.aws_partition}:cloudtrail:*:${local.aws_account_id}:trail/${module.this.id}",
102102
]
103103
}
104104

@@ -119,7 +119,7 @@ data "aws_iam_policy_document" "default" {
119119
]
120120

121121
resources = [
122-
"arn:${local.aws_partition}:s3:::${module.cloudtrail_label.id}/*",
122+
"arn:${local.aws_partition}:s3:::${module.cloudtrail_bucket_label.id}/*",
123123
]
124124

125125
condition {
@@ -133,7 +133,7 @@ data "aws_iam_policy_document" "default" {
133133
test = "StringLike"
134134
variable = "aws:SourceArn"
135135
values = [
136-
"arn:${local.aws_partition}:cloudtrail:*:${local.aws_account_id}:trail/*datadog-logs-archive",
136+
"arn:${local.aws_partition}:cloudtrail:*:${local.aws_account_id}:trail/${module.this.id}",
137137
]
138138
}
139139

@@ -211,7 +211,7 @@ module "archive_bucket" {
211211
user_enabled = false
212212
versioning_enabled = true
213213

214-
object_lock_configuration = {
214+
object_lock_configuration = var.object_lock_days_archive == 0 ? null : {
215215
mode = var.object_lock_mode_archive
216216
days = var.object_lock_days_archive
217217
years = null
@@ -220,7 +220,7 @@ module "archive_bucket" {
220220
context = module.this.context
221221
}
222222

223-
module "cloudtrail_label" {
223+
module "cloudtrail_bucket_label" {
224224
source = "cloudposse/label/null"
225225
version = "0.25.0" # requires Terraform >= 0.13.0
226226

@@ -278,7 +278,7 @@ module "cloudtrail_s3_bucket" {
278278
label_key_case = "lower"
279279
label_value_case = "lower"
280280

281-
object_lock_configuration = {
281+
object_lock_configuration = var.object_lock_days_cloudtrail == 0 ? null : {
282282
mode = var.object_lock_mode_cloudtrail
283283
days = var.object_lock_days_cloudtrail
284284
years = null
@@ -294,7 +294,7 @@ module "cloudtrail_s3_bucket" {
294294
# https://github.com/hashicorp/terraform/issues/5613
295295
allow_ssl_requests_only = false
296296

297-
context = module.cloudtrail_label.context
297+
context = module.cloudtrail_bucket_label.context
298298
}
299299

300300
module "cloudtrail" {

src/variables.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ variable "query_override" {
77
type = string
88
nullable = true
99
description = "Override query for datadog archive. If null would be used query 'env:{stage} OR account:{aws account id} OR {additional_query_tags}'"
10+
default = null
1011
}
1112

1213
variable "additional_query_tags" {

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/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/component_test.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package test
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
"testing"
8+
9+
helper "github.com/cloudposse/test-helpers/pkg/atmos/component-helper"
10+
awsTerratest "github.com/gruntwork-io/terratest/modules/aws"
11+
"github.com/cloudposse/test-helpers/pkg/atmos"
12+
"github.com/gruntwork-io/terratest/modules/aws"
13+
"github.com/gruntwork-io/terratest/modules/random"
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
type ComponentSuite struct {
18+
helper.TestSuite
19+
20+
datadogAPIKey string // Datadog API key
21+
datadogAppKey string // Datadog App key
22+
datadogHost string // Datadog host
23+
randomID string
24+
awsRegion string
25+
}
26+
27+
func (s *ComponentSuite) TestBasic() {
28+
const component = "datadog-logs-archive/basic"
29+
const stack = "default-test"
30+
const awsRegion = "us-east-2"
31+
32+
randomID := strings.ToLower(random.UniqueId())
33+
34+
// Store the Datadog API key in SSM for the duration of the test.
35+
apiKeyPath := fmt.Sprintf("/datadog/%s/datadog_api_key", randomID)
36+
awsTerratest.PutParameter(s.T(), s.awsRegion, apiKeyPath, "Datadog API Key", s.datadogAPIKey)
37+
38+
// Store the Datadog App key in SSM for the duration of the test.
39+
appKeyPath := fmt.Sprintf("/datadog/%s/datadog_app_key", randomID)
40+
awsTerratest.PutParameter(s.T(), s.awsRegion, appKeyPath, "Datadog App Key", s.datadogAppKey)
41+
42+
defer func() {
43+
if !s.Config.SkipDestroyComponent {
44+
awsTerratest.DeleteParameter(s.T(), awsRegion, apiKeyPath)
45+
awsTerratest.DeleteParameter(s.T(), awsRegion, appKeyPath)
46+
}
47+
}()
48+
49+
defer s.DestroyAtmosComponent(s.T(), component, stack, nil)
50+
options, _ := s.DeployAtmosComponent(s.T(), component, stack, nil)
51+
assert.NotNil(s.T(), options)
52+
53+
cloudtrailBucketName := atmos.Output(s.T(), options, "cloudtrail_bucket_id")
54+
55+
defer func() {
56+
if !s.Config.SkipDestroyComponent {
57+
atmos.DestroyE(s.T(), options)
58+
aws.EmptyS3Bucket(s.T(), awsRegion, cloudtrailBucketName)
59+
}
60+
}()
61+
62+
s.DriftTest(component, stack, nil)
63+
}
64+
65+
func (s *ComponentSuite) TestEnabledFlag() {
66+
const component = "datadog-logs-archive/disabled"
67+
const stack = "default-test"
68+
const awsRegion = "us-east-2"
69+
70+
randomID := strings.ToLower(random.UniqueId())
71+
72+
// Store the Datadog API key in SSM for the duration of the test.
73+
apiKeyPath := fmt.Sprintf("/datadog/%s/datadog_api_key", randomID)
74+
awsTerratest.PutParameter(s.T(), s.awsRegion, apiKeyPath, "Datadog API Key", s.datadogAPIKey)
75+
76+
// Store the Datadog App key in SSM for the duration of the test.
77+
appKeyPath := fmt.Sprintf("/datadog/%s/datadog_app_key", randomID)
78+
awsTerratest.PutParameter(s.T(), s.awsRegion, appKeyPath, "Datadog App Key", s.datadogAppKey)
79+
80+
defer func() {
81+
awsTerratest.DeleteParameter(s.T(), awsRegion, apiKeyPath)
82+
awsTerratest.DeleteParameter(s.T(), awsRegion, appKeyPath)
83+
}()
84+
85+
s.VerifyEnabledFlag(component, stack, nil)
86+
}
87+
88+
func (s *ComponentSuite) SetupSuite() {
89+
s.InitConfig()
90+
s.Config.ComponentDestDir = "components/terraform/datadog-logs-archive"
91+
92+
// Store the Datadog API key in SSM for the duration of the test.
93+
// Add the key to /datadog/<RANDOMID>/datadog_api_key to avoid
94+
// conflicts during parallel tests and remove the key after the test.
95+
s.datadogAPIKey = os.Getenv("DD_API_KEY")
96+
if s.datadogAPIKey == "" {
97+
s.T().Fatal("DD_API_KEY environment variable must be set")
98+
}
99+
100+
// Store the Datadog App key in SSM for the duration of the test.
101+
// Add the key to /datadog/<RANDOMID>/datadog_app_key to avoid
102+
// conflicts during parallel tests and remove the key after the test.
103+
s.datadogAppKey = os.Getenv("DD_APP_KEY")
104+
if s.datadogAppKey == "" {
105+
s.T().Fatal("DD_APP_KEY environment variable must be set")
106+
}
107+
108+
s.randomID = strings.ToLower(random.UniqueId())
109+
s.awsRegion = "us-east-2"
110+
s.datadogHost = "us5.datadoghq.com"
111+
112+
if !s.Config.SkipDeployDependencies {
113+
apiKeyPath := fmt.Sprintf("/datadog/%s/datadog_api_key", s.randomID)
114+
awsTerratest.PutParameter(s.T(), s.awsRegion, apiKeyPath, "Datadog API Key", s.datadogAPIKey)
115+
116+
appKeyPath := fmt.Sprintf("/datadog/%s/datadog_app_key", s.randomID)
117+
awsTerratest.PutParameter(s.T(), s.awsRegion, appKeyPath, "Datadog App Key", s.datadogAppKey)
118+
119+
inputs := map[string]any{
120+
"datadog_site_url": s.datadogHost,
121+
"datadog_secrets_source_store_account_region": s.awsRegion,
122+
"datadog_secrets_source_store_account_stage": "test",
123+
"datadog_secrets_source_store_account_tenant": "default",
124+
"datadog_api_secret_key": s.randomID,
125+
"datadog_app_secret_key": s.randomID,
126+
}
127+
s.AddDependency(s.T(), "datadog-configuration", "default-test", &inputs)
128+
s.AddDependency(s.T(), "datadog-integration", "default-test", &map[string]any{})
129+
}
130+
131+
s.TestSuite.SetupSuite()
132+
}
133+
134+
func (s *ComponentSuite) TearDownSuite() {
135+
s.TestSuite.TearDownSuite()
136+
if !s.Config.SkipDestroyDependencies {
137+
apiKeyPath := fmt.Sprintf("/datadog/%s/datadog_api_key", s.randomID)
138+
awsTerratest.DeleteParameter(s.T(), s.awsRegion, apiKeyPath)
139+
140+
appKeyPath := fmt.Sprintf("/datadog/%s/datadog_app_key", s.randomID)
141+
awsTerratest.DeleteParameter(s.T(), s.awsRegion, appKeyPath)
142+
}
143+
}
144+
145+
func TestRunSuite(t *testing.T) {
146+
suite := new(ComponentSuite)
147+
148+
helper.Run(t, suite)
149+
}

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+
datadog-configuration:
4+
vars:
5+
enabled: true
6+
name: datadog-configuration
7+
datadog_secrets_store_type: SSM
8+
datadog_site_url: us5.datadoghq.com
9+
datadog_secrets_source_store_account_stage: auto
10+
datadog_secrets_source_store_account_region: "us-east-2"
11+
12+

0 commit comments

Comments
 (0)