Skip to content

Commit 62fcff7

Browse files
committed
refactor(test): support Datadog App key and refactor secrets
- Add handling for Datadog App key in test suite, storing it in SSM alongside the API key to avoid conflicts in parallel tests. - Refactor struct field names for consistency and clarity. - Update SSM parameter paths and cleanup logic for both API and App keys. - Fix stack fixture metadata and monitor config path globbing for improved test reliability.
1 parent 9c87209 commit 62fcff7

File tree

3 files changed

+71
-23
lines changed

3 files changed

+71
-23
lines changed

test/component_test.go

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,33 @@ import (
1515
type ComponentSuite struct {
1616
helper.TestSuite
1717

18-
DDAPIKey string
19-
DDHost string
20-
RandomID string
21-
AWSRegion string
18+
datadogAPIKey string // Datadog API key
19+
datadogAppKey string // Datadog App key
20+
datadogHost string // Datadog host
21+
randomID string
22+
awsRegion string
2223
}
2324

2425
func (s *ComponentSuite) TestBasicDatadogMonitor() {
2526
const component = "datadog-monitor/basic"
2627
const stack = "default-test"
2728
const awsRegion = "us-east-2"
2829

30+
randomID := strings.ToLower(random.UniqueId())
31+
32+
// Store the Datadog API key in SSM for the duration of the test.
33+
apiKeyPath := fmt.Sprintf("/datadog/%s/datadog_api_key", randomID)
34+
awsTerratest.PutParameter(s.T(), s.awsRegion, apiKeyPath, "Datadog API Key", s.datadogAPIKey)
35+
36+
// Store the Datadog App key in SSM for the duration of the test.
37+
appKeyPath := fmt.Sprintf("/datadog/%s/datadog_app_key", randomID)
38+
awsTerratest.PutParameter(s.T(), s.awsRegion, appKeyPath, "Datadog App Key", s.datadogAppKey)
39+
40+
defer func() {
41+
awsTerratest.DeleteParameter(s.T(), awsRegion, apiKeyPath)
42+
awsTerratest.DeleteParameter(s.T(), awsRegion, appKeyPath)
43+
}()
44+
2945
defer s.DestroyAtmosComponent(s.T(), component, stack, nil)
3046
options, _ := s.DeployAtmosComponent(s.T(), component, stack, nil)
3147
assert.NotNil(s.T(), options)
@@ -36,6 +52,23 @@ func (s *ComponentSuite) TestBasicDatadogMonitor() {
3652
func (s *ComponentSuite) TestEnabledFlag() {
3753
const component = "datadog-monitor/disabled"
3854
const stack = "default-test"
55+
const awsRegion = "us-east-2"
56+
57+
randomID := strings.ToLower(random.UniqueId())
58+
59+
// Store the Datadog API key in SSM for the duration of the test.
60+
apiKeyPath := fmt.Sprintf("/datadog/%s/datadog_api_key", randomID)
61+
awsTerratest.PutParameter(s.T(), s.awsRegion, apiKeyPath, "Datadog API Key", s.datadogAPIKey)
62+
63+
// Store the Datadog App key in SSM for the duration of the test.
64+
appKeyPath := fmt.Sprintf("/datadog/%s/datadog_app_key", randomID)
65+
awsTerratest.PutParameter(s.T(), s.awsRegion, appKeyPath, "Datadog App Key", s.datadogAppKey)
66+
67+
defer func() {
68+
awsTerratest.DeleteParameter(s.T(), awsRegion, apiKeyPath)
69+
awsTerratest.DeleteParameter(s.T(), awsRegion, appKeyPath)
70+
}()
71+
3972
s.VerifyEnabledFlag(component, stack, nil)
4073
}
4174

@@ -44,27 +77,39 @@ func (s *ComponentSuite) SetupSuite() {
4477
s.Config.ComponentDestDir = "components/terraform/datadog-monitor"
4578

4679
// Store the Datadog API key in SSM for the duration of the test.
47-
// The API key is obtained from GitHub secrets as an environment variable
48-
// in the test pipelines.
49-
s.DDAPIKey = os.Getenv("DD_API_KEY")
50-
if s.DDAPIKey == "" {
80+
// Add the key to /datadog/<RANDOMID>/datadog_api_key to avoid
81+
// conflicts during parallel tests and remove the key after the test.
82+
s.datadogAPIKey = os.Getenv("DD_API_KEY")
83+
if s.datadogAPIKey == "" {
5184
s.T().Fatal("DD_API_KEY environment variable must be set")
5285
}
5386

54-
s.RandomID = strings.ToLower(random.UniqueId())
55-
s.AWSRegion = "us-east-2"
56-
s.DDHost = "us5.datadoghq.com"
87+
// Store the Datadog App key in SSM for the duration of the test.
88+
// Add the key to /datadog/<RANDOMID>/datadog_app_key to avoid
89+
// conflicts during parallel tests and remove the key after the test.
90+
s.datadogAppKey = os.Getenv("DD_APP_KEY")
91+
if s.datadogAPIKey == "" {
92+
s.T().Fatal("DD_APP_KEY environment variable must be set")
93+
}
94+
95+
s.randomID = strings.ToLower(random.UniqueId())
96+
s.awsRegion = "us-east-2"
97+
s.datadogHost = "us5.datadoghq.com"
5798

5899
if !s.Config.SkipDeployDependencies {
59-
secretPath := fmt.Sprintf("/datadog/%s/api_key", s.RandomID)
60-
awsTerratest.PutParameter(s.T(), s.AWSRegion, secretPath, "Datadog API Key", s.DDAPIKey)
100+
apiKeyPath := fmt.Sprintf("/datadog/%s/datadog_api_key", s.randomID)
101+
awsTerratest.PutParameter(s.T(), s.awsRegion, apiKeyPath, "Datadog API Key", s.datadogAPIKey)
102+
103+
appKeyPath := fmt.Sprintf("/datadog/%s/datadog_app_key", s.randomID)
104+
awsTerratest.PutParameter(s.T(), s.awsRegion, appKeyPath, "Datadog App Key", s.datadogAppKey)
61105

62106
inputs := map[string]any{
63-
"datadog_site_url": s.DDHost,
64-
"datadog_secrets_source_store_account_region": s.AWSRegion,
65-
"datadog_secrets_source_store_account_stage": "default",
66-
"datadog_secrets_source_store_account_tenant": "test",
67-
"datadog_api_secret_key": s.RandomID,
107+
"datadog_site_url": s.datadogHost,
108+
"datadog_secrets_source_store_account_region": s.awsRegion,
109+
"datadog_secrets_source_store_account_stage": "test",
110+
"datadog_secrets_source_store_account_tenant": "default",
111+
"datadog_api_secret_key": s.randomID,
112+
"datadog_app_secret_key": s.randomID,
68113
}
69114
s.AddDependency(s.T(), "datadog-configuration", "default-test", &inputs)
70115
}
@@ -75,8 +120,11 @@ func (s *ComponentSuite) SetupSuite() {
75120
func (s *ComponentSuite) TearDownSuite() {
76121
s.TestSuite.TearDownSuite()
77122
if !s.Config.SkipDestroyDependencies {
78-
secretPath := fmt.Sprintf("/datadog/%s/api_key", s.RandomID)
79-
awsTerratest.DeleteParameter(s.T(), s.AWSRegion, secretPath)
123+
apiKeyPath := fmt.Sprintf("/datadog/%s/datadog_api_key", s.randomID)
124+
awsTerratest.DeleteParameter(s.T(), s.awsRegion, apiKeyPath)
125+
126+
appKeyPath := fmt.Sprintf("/datadog/%s/datadog_app_key", s.randomID)
127+
awsTerratest.DeleteParameter(s.T(), s.awsRegion, appKeyPath)
80128
}
81129
}
82130

test/fixtures/stacks/catalog/usecase/basic.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ components:
22
terraform:
33
datadog-monitor/basic:
44
metadata:
5-
component: target
5+
component: datadog-monitor
66
vars:
77
enabled: true
88
name: datadog-monitor
99
local_datadog_monitors_config_paths:
10-
- catalog/monitors/defaults/*.yaml
10+
- "catalog/monitors/defaults/*"
1111
datadog_monitor_globals:
1212
groupby_simple_monitor: false
1313
new_group_delay: 0

test/fixtures/stacks/catalog/usecase/disabled.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ components:
22
terraform:
33
datadog-monitor/disabled:
44
metadata:
5-
component: target
5+
component: datadog-monitor
66
vars:
77
enabled: false
88
name: datadog-monitor

0 commit comments

Comments
 (0)