Skip to content

Commit 9c87209

Browse files
committed
test: initial refactoring with ssm capabilities
1 parent 770b020 commit 9c87209

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

test/component_test.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
package test
22

33
import (
4+
"fmt"
5+
"os"
6+
"strings"
47
"testing"
58

69
helper "github.com/cloudposse/test-helpers/pkg/atmos/component-helper"
10+
awsTerratest "github.com/gruntwork-io/terratest/modules/aws"
11+
"github.com/gruntwork-io/terratest/modules/random"
712
"github.com/stretchr/testify/assert"
813
)
914

1015
type ComponentSuite struct {
1116
helper.TestSuite
17+
18+
DDAPIKey string
19+
DDHost string
20+
RandomID string
21+
AWSRegion string
1222
}
1323

1424
func (s *ComponentSuite) TestBasicDatadogMonitor() {
@@ -29,10 +39,49 @@ func (s *ComponentSuite) TestEnabledFlag() {
2939
s.VerifyEnabledFlag(component, stack, nil)
3040
}
3141

42+
func (s *ComponentSuite) SetupSuite() {
43+
s.InitConfig()
44+
s.Config.ComponentDestDir = "components/terraform/datadog-monitor"
45+
46+
// 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 == "" {
51+
s.T().Fatal("DD_API_KEY environment variable must be set")
52+
}
53+
54+
s.RandomID = strings.ToLower(random.UniqueId())
55+
s.AWSRegion = "us-east-2"
56+
s.DDHost = "us5.datadoghq.com"
57+
58+
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)
61+
62+
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,
68+
}
69+
s.AddDependency(s.T(), "datadog-configuration", "default-test", &inputs)
70+
}
71+
72+
s.TestSuite.SetupSuite()
73+
}
74+
75+
func (s *ComponentSuite) TearDownSuite() {
76+
s.TestSuite.TearDownSuite()
77+
if !s.Config.SkipDestroyDependencies {
78+
secretPath := fmt.Sprintf("/datadog/%s/api_key", s.RandomID)
79+
awsTerratest.DeleteParameter(s.T(), s.AWSRegion, secretPath)
80+
}
81+
}
82+
3283
func TestRunSuite(t *testing.T) {
3384
suite := new(ComponentSuite)
3485

35-
suite.AddDependency(t, "datadog-configuration", "default-test", nil)
36-
3786
helper.Run(t, suite)
3887
}

0 commit comments

Comments
 (0)