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