|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +//go:build !windows |
| 5 | + |
| 6 | +package collectd_otlp |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + "log" |
| 13 | + "net/http" |
| 14 | + "os/exec" |
| 15 | + "path/filepath" |
| 16 | + "testing" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/stretchr/testify/suite" |
| 20 | + |
| 21 | + "github.com/aws/amazon-cloudwatch-agent-test/environment" |
| 22 | + "github.com/aws/amazon-cloudwatch-agent-test/test/otlp_export/otlpvalidation" |
| 23 | + "github.com/aws/amazon-cloudwatch-agent-test/test/status" |
| 24 | + "github.com/aws/amazon-cloudwatch-agent-test/test/test_runner" |
| 25 | + "github.com/aws/amazon-cloudwatch-agent-test/util/common" |
| 26 | +) |
| 27 | + |
| 28 | +func init() { |
| 29 | + environment.RegisterEnvironmentMetaDataFlags() |
| 30 | +} |
| 31 | + |
| 32 | +type CollectdOtlpTestSuite struct { |
| 33 | + suite.Suite |
| 34 | + test_runner.TestSuite |
| 35 | +} |
| 36 | + |
| 37 | +func (s *CollectdOtlpTestSuite) SetupSuite() { |
| 38 | + log.Println(">>>> Starting CollectdOtlpTestSuite") |
| 39 | +} |
| 40 | + |
| 41 | +func (s *CollectdOtlpTestSuite) TearDownSuite() { |
| 42 | + s.Result.Print() |
| 43 | + log.Println(">>>> Finished CollectdOtlpTestSuite") |
| 44 | +} |
| 45 | + |
| 46 | +func (s *CollectdOtlpTestSuite) TestAllInSuite() { |
| 47 | + runner := &CollectdOtlpTestRunner{} |
| 48 | + s.AddToSuiteResult(runner.run()) |
| 49 | + s.Assert().Equal(status.SUCCESSFUL, s.Result.GetStatus(), "CollectdOtlp Test Suite Failed") |
| 50 | +} |
| 51 | + |
| 52 | +func TestCollectdOtlpSuite(t *testing.T) { |
| 53 | + suite.Run(t, new(CollectdOtlpTestSuite)) |
| 54 | +} |
| 55 | + |
| 56 | +type CollectdOtlpTestRunner struct { |
| 57 | + test_runner.BaseTestRunner |
| 58 | +} |
| 59 | + |
| 60 | +var _ test_runner.ITestRunner = (*CollectdOtlpTestRunner)(nil) |
| 61 | + |
| 62 | +const yamlConfigPath = "/tmp/config.yaml" |
| 63 | +const yamlStartCommand = "sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -s -c " |
| 64 | + |
| 65 | +func (t *CollectdOtlpTestRunner) run() status.TestGroupResult { |
| 66 | + if err := exec.Command("sudo", "/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl", "-a", "remove-config").Run(); err != nil { |
| 67 | + log.Printf("remove-config failed: %v", err) |
| 68 | + } |
| 69 | + common.CopyFile(filepath.Join("agent_configs", t.GetAgentConfigFileName()), yamlConfigPath) |
| 70 | + if err := common.StartAgentWithCommand(yamlConfigPath, false, false, yamlStartCommand); err != nil { |
| 71 | + return status.TestGroupResult{ |
| 72 | + Name: t.GetTestName(), |
| 73 | + TestResults: []status.TestResult{{Name: "Starting Agent", Status: status.FAILED, Reason: err}}, |
| 74 | + } |
| 75 | + } |
| 76 | + for i := 0; i < 15; i++ { |
| 77 | + body := bytes.NewBufferString("[]") |
| 78 | + resp, err := http.Post("http://127.0.0.1:25826/", "application/json", body) |
| 79 | + if err == nil { |
| 80 | + resp.Body.Close() |
| 81 | + break |
| 82 | + } |
| 83 | + time.Sleep(2 * time.Second) |
| 84 | + } |
| 85 | + if err := t.SetupAfterAgentRun(); err != nil { |
| 86 | + return status.TestGroupResult{ |
| 87 | + Name: t.GetTestName(), |
| 88 | + TestResults: []status.TestResult{{Name: "Setup After Agent Run", Status: status.FAILED, Reason: err}}, |
| 89 | + } |
| 90 | + } |
| 91 | + time.Sleep(30 * time.Second) |
| 92 | + common.StopAgent() |
| 93 | + return t.Validate() |
| 94 | +} |
| 95 | + |
| 96 | +func (t *CollectdOtlpTestRunner) GetTestName() string { return "CollectdOtlp" } |
| 97 | +func (t *CollectdOtlpTestRunner) GetAgentConfigFileName() string { return "collectd_otlp.yaml" } |
| 98 | +func (t *CollectdOtlpTestRunner) GetAgentRunDuration() time.Duration { |
| 99 | + return 4 * time.Minute |
| 100 | +} |
| 101 | +func (t *CollectdOtlpTestRunner) GetMeasuredMetrics() []string { |
| 102 | + return []string{"gauge.gauge_1"} |
| 103 | +} |
| 104 | + |
| 105 | +func (t *CollectdOtlpTestRunner) SetupAfterAgentRun() error { |
| 106 | + return sendCollectdHTTPMetrics(t.GetAgentRunDuration()) |
| 107 | +} |
| 108 | + |
| 109 | +func (t *CollectdOtlpTestRunner) Validate() status.TestGroupResult { |
| 110 | + return otlpvalidation.ValidateOtlpMetrics(t.GetTestName(), "us-west-2", t.GetMeasuredMetrics()) |
| 111 | +} |
| 112 | + |
| 113 | +func sendCollectdHTTPMetrics(duration time.Duration) error { |
| 114 | + metrics := []map[string]interface{}{ |
| 115 | + {"values": []int{1}, "dstypes": []string{"gauge"}, "dsnames": []string{"value"}, "time": 0, "interval": 10, "host": "testhost", "plugin": "gauge_1", "plugin_instance": "", "type": "gauge", "type_instance": "gauge_1"}, |
| 116 | + {"values": []int{1}, "dstypes": []string{"counter"}, "dsnames": []string{"value"}, "time": 0, "interval": 10, "host": "testhost", "plugin": "counter_1", "plugin_instance": "", "type": "counter", "type_instance": "counter_1"}, |
| 117 | + } |
| 118 | + end := time.Now().Add(duration) |
| 119 | + for time.Now().Before(end) { |
| 120 | + for j := range metrics { |
| 121 | + metrics[j]["time"] = time.Now().Unix() |
| 122 | + } |
| 123 | + body, err := json.Marshal(metrics) |
| 124 | + if err != nil { |
| 125 | + return fmt.Errorf("marshal error: %w", err) |
| 126 | + } |
| 127 | + resp, err := http.Post("http://127.0.0.1:25826/", "application/json", bytes.NewReader(body)) |
| 128 | + if err != nil { |
| 129 | + log.Printf("collectd post error: %v", err) |
| 130 | + } else { |
| 131 | + resp.Body.Close() |
| 132 | + } |
| 133 | + time.Sleep(time.Second) |
| 134 | + } |
| 135 | + return nil |
| 136 | +} |
0 commit comments