|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +//go:build !windows |
| 5 | + |
| 6 | +package otlp |
| 7 | + |
| 8 | +import ( |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/suite" |
| 14 | + |
| 15 | + "github.com/aws/amazon-cloudwatch-agent-test/environment" |
| 16 | + "github.com/aws/amazon-cloudwatch-agent-test/test/otel_collect/otlpvalidation" |
| 17 | + "github.com/aws/amazon-cloudwatch-agent-test/test/status" |
| 18 | + "github.com/aws/amazon-cloudwatch-agent-test/test/test_runner" |
| 19 | +) |
| 20 | + |
| 21 | +func init() { |
| 22 | + environment.RegisterEnvironmentMetaDataFlags() |
| 23 | +} |
| 24 | + |
| 25 | +// clusterNamePrefix matches the ECS cluster naming in terraform/ecs_ec2/daemon. |
| 26 | +const clusterNamePrefix = "cwagent-integ-test-cluster-" |
| 27 | + |
| 28 | +// ECSOtlpTestRunner validates that a workload (sidecar) running on the ECS daemon |
| 29 | +// can publish OTLP metrics to the agent's OTLP receiver and have them reach CloudWatch. |
| 30 | +type ECSOtlpTestRunner struct { |
| 31 | + test_runner.BaseTestRunner |
| 32 | +} |
| 33 | + |
| 34 | +var _ test_runner.ITestRunner = (*ECSOtlpTestRunner)(nil) |
| 35 | + |
| 36 | +func (t *ECSOtlpTestRunner) GetTestName() string { return "ecs_otlp" } |
| 37 | + |
| 38 | +// GetAgentConfigFileName returns "" — config is pre-loaded by Terraform via SSM, no restart needed. |
| 39 | +func (t *ECSOtlpTestRunner) GetAgentConfigFileName() string { return "" } |
| 40 | + |
| 41 | +func (t *ECSOtlpTestRunner) GetMeasuredMetrics() []string { |
| 42 | + return []string{"otlp_test_counter", "otlp_test_gauge"} |
| 43 | +} |
| 44 | + |
| 45 | +func (t *ECSOtlpTestRunner) Validate() status.TestGroupResult { |
| 46 | + env := environment.GetEnvironmentMetaData() |
| 47 | + // Give the sidecar time to push and the agent to export before querying. |
| 48 | + time.Sleep(3 * time.Minute) |
| 49 | + // Isolate by the per-run testing_id, which the sidecar stamps as the TestId |
| 50 | + // attribute and is embedded in the ECS cluster ARN. |
| 51 | + labels := map[string]string{ |
| 52 | + "TestId": testIDFromClusterArn(env.EcsClusterArn), |
| 53 | + } |
| 54 | + return otlpvalidation.ValidateOtlpMetricsWithLabels(t.GetTestName(), env.Region, t.GetMeasuredMetrics(), labels) |
| 55 | +} |
| 56 | + |
| 57 | +// testIDFromClusterArn extracts the testing_id suffix from the ECS cluster ARN. |
| 58 | +func testIDFromClusterArn(clusterArn string) string { |
| 59 | + if i := strings.LastIndex(clusterArn, clusterNamePrefix); i != -1 { |
| 60 | + return clusterArn[i+len(clusterNamePrefix):] |
| 61 | + } |
| 62 | + return clusterArn |
| 63 | +} |
| 64 | + |
| 65 | +func TestECSOtlpSuite(t *testing.T) { |
| 66 | + suite.Run(t, new(ECSOtlpTestSuite)) |
| 67 | +} |
| 68 | + |
| 69 | +type ECSOtlpTestSuite struct { |
| 70 | + suite.Suite |
| 71 | + test_runner.TestSuite |
| 72 | +} |
| 73 | + |
| 74 | +func (suite *ECSOtlpTestSuite) GetSuiteName() string { |
| 75 | + return "ECSOtlp" |
| 76 | +} |
| 77 | + |
| 78 | +func (suite *ECSOtlpTestSuite) TestAllInSuite() { |
| 79 | + env := environment.GetEnvironmentMetaData() |
| 80 | + ecsTestRunner := &test_runner.ECSTestRunner{ |
| 81 | + Runner: &ECSOtlpTestRunner{}, |
| 82 | + RunStrategy: &test_runner.ECSAgentRunStrategy{}, |
| 83 | + Env: *env, |
| 84 | + } |
| 85 | + ecsTestRunner.Run(suite, env) |
| 86 | + suite.Assert().Equal(status.SUCCESSFUL, suite.Result.GetStatus(), "ECS OTLP Test Suite Failed") |
| 87 | +} |
0 commit comments