|
| 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 | +} |
0 commit comments