Skip to content

Commit 9bbdb9f

Browse files
authored
Implement integ tests for the new AmazonCloudWatch-ManageAgent SSM document (#607)
1 parent 038ce97 commit 9bbdb9f

File tree

15 files changed

+889
-6
lines changed

15 files changed

+889
-6
lines changed

generator/test_case_generator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ var testTypeToTestConfig = map[string][]testConfig{
157157
testDir: "./test/dualstack_endpoint",
158158
targets: map[string]map[string]struct{}{"os": {"al2": {}}, "arc": {"amd64": {}}},
159159
},
160+
{
161+
testDir: "./test/ssm_document",
162+
targets: map[string]map[string]struct{}{"os": {"al2": {}}},
163+
},
160164
},
161165
testTypeKeyEc2SELinux: {
162166
{testDir: "./test/ca_bundle"},
@@ -224,6 +228,7 @@ var testTypeToTestConfig = map[string][]testConfig{
224228
*/
225229
"ec2_mac": {
226230
{testDir: "../../../test/feature/mac"},
231+
{testDir: "../../../test/ssm_document"},
227232
},
228233
"ec2_windows": {
229234
{testDir: "../../../test/feature/windows"},
@@ -242,6 +247,7 @@ var testTypeToTestConfig = map[string][]testConfig{
242247
testDir: "../../../test/feature/windows/custom_start/ssm_start",
243248
targets: map[string]map[string]struct{}{"os": {"win-2019": {}}},
244249
},
250+
{testDir: "../../../test/ssm_document"},
245251
// assume role test doesn't add much value, and it already being tested with linux
246252
//{testDir: "../../../test/assume_role"},
247253
},

terraform/ec2/win/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ resource "null_resource" "integration_test_run" {
187187
provisioner "remote-exec" {
188188
inline = [
189189
"set AWS_REGION=${var.region}",
190-
"C:\\validator.exe --test-name=${var.test_dir}"
190+
"C:\\validator.exe --test-name=${var.test_dir} --computeType=EC2 --instanceId=${aws_instance.cwagent.id}"
191191
]
192192
}
193193
}

test/nvidia_gpu/nvidia_gpu_windows.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"log"
1111
"time"
1212

13-
"github.com/aws/amazon-cloudwatch-agent-test/environment"
1413
"github.com/aws/amazon-cloudwatch-agent-test/filesystem"
1514
"github.com/aws/amazon-cloudwatch-agent-test/util/awsservice"
1615
"github.com/aws/amazon-cloudwatch-agent-test/util/common"
@@ -30,10 +29,6 @@ var (
3029
expectedNvidiaGPUWindowsMetrics = []string{"Memory % Committed Bytes In Use", "nvidia_smi utilization_gpu", "nvidia_smi utilization_memory", "nvidia_smi power_draw", "nvidia_smi temperature_gpu"}
3130
)
3231

33-
func init() {
34-
environment.RegisterEnvironmentMetaDataFlags()
35-
}
36-
3732
func Validate() error {
3833
err := common.CopyFile(configWindowsJSON, configWindowsOutputPath)
3934
if err != nil {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"agent": {
3+
"region": "us-west-2"
4+
},
5+
"metrics": {
6+
"append_dimensions": {
7+
"InstanceId": "${aws:InstanceId}"
8+
},
9+
"metrics_collected": {
10+
"mem": {
11+
"measurement": [
12+
"mem_used_percent"
13+
]
14+
},
15+
"disk": {
16+
"measurement": [
17+
"used_percent"
18+
],
19+
"resources": [
20+
"*"
21+
]
22+
}
23+
}
24+
}
25+
}

test/ssm_document/constants.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package ssm_document
5+
6+
const (
7+
testManageAgentDocument = "Test-AmazonCloudWatch-ManageAgent-"
8+
9+
// Actions
10+
actionStart = "start"
11+
actionStop = "stop"
12+
actionConfigure = "configure"
13+
actionConfigureAppend = "configure (append)"
14+
actionConfigureRemove = "configure (remove)"
15+
16+
// Parameters
17+
paramAction = "action"
18+
paramOptionalConfigurationSource = "optionalConfigurationSource"
19+
paramOptionalConfigurationLocation = "optionalConfigurationLocation"
20+
paramOptionalRestart = "optionalRestart"
21+
22+
// Parameter Values
23+
configSourceSSM = "ssm"
24+
configSourceAll = "all"
25+
restartNo = "no"
26+
27+
// Agent Status
28+
agentStatusRunning = "running"
29+
agentStatusStopped = "stopped"
30+
31+
// Config Status
32+
configStatusConfigured = "configured"
33+
configStatusNotConfigured = "not configured"
34+
35+
// SSM ParametersStore Configs
36+
agentConfigFile1 = "agentConfig1"
37+
agentConfigFile2 = "agentConfig2"
38+
)
39+
40+
type agentStatus struct {
41+
Status string `json:"status"`
42+
ConfigStatus string `json:"configstatus"`
43+
Version string `json:"version"`
44+
StartTime string `json:"starttime"`
45+
}
46+
47+
type testCase struct {
48+
parameters map[string][]string
49+
actionName string
50+
expectedAgentStatus string
51+
expectedConfigStatus string
52+
}

test/ssm_document/helper.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package ssm_document
5+
6+
import (
7+
"encoding/json"
8+
"fmt"
9+
"log"
10+
11+
"github.com/aws/aws-sdk-go-v2/service/ssm"
12+
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
13+
14+
"github.com/aws/amazon-cloudwatch-agent-test/util/awsservice"
15+
)
16+
17+
func RunAndVerifySSMAction(documentName string, instanceIds []string, tc testCase) error {
18+
log.Printf("Testing %s action", tc.actionName)
19+
20+
out, err := awsservice.RunSSMDocument(documentName, instanceIds, tc.parameters)
21+
if err != nil {
22+
return fmt.Errorf("%s action failed: %v", tc.actionName, err)
23+
}
24+
25+
if err := VerifyAgentAction(out, instanceIds[0], documentName, tc); err != nil {
26+
return fmt.Errorf("%s verification failed: %v", tc.actionName, err)
27+
}
28+
29+
log.Printf("%s action completed successfully", tc.actionName)
30+
return nil
31+
}
32+
33+
func VerifyAgentAction(out *ssm.SendCommandOutput, instanceId, documentName string, tc testCase) error {
34+
var status agentStatus
35+
36+
//Wait for command completion
37+
_, err := awsservice.WaitForCommandCompletion(*out.Command.CommandId, instanceId)
38+
if err != nil {
39+
return fmt.Errorf("failed to get command result: %v", err)
40+
}
41+
42+
// Verify agent status
43+
statusParams := map[string][]string{"action": {"status"}}
44+
statusOut, err := awsservice.RunSSMDocument(documentName, []string{instanceId}, statusParams)
45+
if err != nil {
46+
return fmt.Errorf("failed to check agent status: %v", err)
47+
}
48+
49+
statusResult, err := awsservice.WaitForCommandCompletion(*statusOut.Command.CommandId, instanceId)
50+
if err != nil {
51+
return fmt.Errorf("failed to get status result: %v", err)
52+
}
53+
54+
for _, plugin := range statusResult.CommandInvocations[0].CommandPlugins {
55+
if plugin.Status == types.CommandPluginStatusFailed {
56+
return fmt.Errorf("command plugin failed: %s", *plugin.Name)
57+
}
58+
if plugin.Status == types.CommandPluginStatusTimedOut {
59+
return fmt.Errorf("command plugin timed out: %s", *plugin.Name)
60+
}
61+
outputAsByte := []byte(*plugin.Output)
62+
if json.Valid(outputAsByte) {
63+
err := json.Unmarshal([]byte(*plugin.Output), &status)
64+
if err != nil {
65+
return fmt.Errorf("failed to unmarshal status output: %v", err)
66+
}
67+
}
68+
}
69+
70+
if status.Status != tc.expectedAgentStatus {
71+
return fmt.Errorf("agent status verification failed. Expected: %s, Output: %s", tc.expectedAgentStatus, status.Status)
72+
}
73+
if status.ConfigStatus != tc.expectedConfigStatus {
74+
return fmt.Errorf("config status verification failed. Expected: %s, Output: %s", tc.expectedConfigStatus, status.ConfigStatus)
75+
}
76+
77+
return nil
78+
}

test/ssm_document/parameters.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Receivers that agent needs to tests
2+
receivers: ["system"]
3+
4+
#Test case name
5+
test_case: "win_restart"
6+
validate_type: "feature"
7+
data_type: "logs"
8+
number_monitored_logs: 1
9+
values_per_minute: "2"
10+
agent_collection_period: 60
11+
cloudwatch_agent_config: "<cloudwatch_agent_config>"
12+
metric_namespace: "CloudWatchAgentWinFeature"
13+
metric_validation:
14+
log_validation:
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"agent": {
3+
"region": "us-west-2"
4+
},
5+
"metrics": {
6+
"append_dimensions": {
7+
"InstanceId": "${aws:InstanceId}"
8+
},
9+
"metrics_collected": {
10+
"mem": {
11+
"measurement": [
12+
"mem_used_percent"
13+
]
14+
},
15+
"disk": {
16+
"measurement": [
17+
"used_percent"
18+
],
19+
"resources": [
20+
"*"
21+
]
22+
}
23+
}
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"agent": {
3+
"region": "us-west-2"
4+
},
5+
"metrics": {
6+
"append_dimensions": {
7+
"InstanceId": "${aws:InstanceId}"
8+
},
9+
"metrics_collected": {
10+
"cpu": {
11+
"measurement": [
12+
"cpu_usage_idle",
13+
"cpu_usage_iowait"
14+
]
15+
},
16+
"netstat": {
17+
"measurement": [
18+
"tcp_established",
19+
"tcp_time_wait"
20+
]
21+
}
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)