Skip to content

Commit 38a7748

Browse files
authored
Merge pull request #99 from WilboMo/sendSuccess
Add success verification to getCommandResult
2 parents 2dc28af + fa73285 commit 38a7748

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

updater/aws.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,9 @@ func (u *updater) getCommandResult(commandID string, instanceID string) ([]byte,
463463
return nil, fmt.Errorf("failed to retrieve command invocation output: %w", err)
464464
}
465465
commandResults := []byte(aws.StringValue(resp.StandardOutputContent))
466+
if aws.StringValue(resp.Status) != ssm.CommandInvocationStatusSuccess {
467+
return nil, fmt.Errorf("command %s has not reached success status, current status %q", commandID, aws.StringValue(resp.Status))
468+
}
466469
return commandResults, nil
467470
}
468471

updater/aws_test.go

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,61 @@ import (
1515
"github.com/stretchr/testify/require"
1616
)
1717

18+
func TestGetCommandResult(t *testing.T) {
19+
cases := []struct {
20+
name string
21+
invocationOut *ssm.GetCommandInvocationOutput
22+
expectedError string
23+
expectedOut []byte
24+
invocationError error
25+
}{
26+
{
27+
name: "getCommand success",
28+
invocationOut: &ssm.GetCommandInvocationOutput{
29+
Status: aws.String("Success"),
30+
StandardOutputContent: aws.String("OutputContent"),
31+
},
32+
expectedOut: []byte(aws.StringValue(aws.String("OutputContent"))),
33+
},
34+
{
35+
name: "getCommand fail",
36+
invocationError: errors.New("failed to get command invocation"),
37+
expectedError: "failed to retrieve command invocation output: failed to get command invocation",
38+
invocationOut: nil,
39+
expectedOut: nil,
40+
},
41+
{
42+
name: "command status non-Success",
43+
invocationOut: &ssm.GetCommandInvocationOutput{
44+
Status: aws.String("TimedOut"),
45+
StandardOutputContent: nil,
46+
},
47+
expectedError: "command command-id has not reached success status, current status \"TimedOut\"",
48+
expectedOut: nil,
49+
},
50+
}
51+
for _, tc := range cases {
52+
t.Run(tc.name, func(t *testing.T) {
53+
mockSSM := MockSSM{
54+
GetCommandInvocationFn: func(input *ssm.GetCommandInvocationInput) (*ssm.GetCommandInvocationOutput, error) {
55+
assert.Equal(t, "command-id", aws.StringValue(input.CommandId))
56+
assert.Equal(t, "instance-id", aws.StringValue(input.InstanceId))
57+
return tc.invocationOut, tc.invocationError
58+
},
59+
}
60+
u := updater{ssm: mockSSM}
61+
actual, err := u.getCommandResult("command-id", "instance-id")
62+
if tc.expectedOut != nil {
63+
require.NoError(t, err)
64+
assert.EqualValues(t, tc.expectedOut, actual)
65+
} else {
66+
require.Error(t, err)
67+
assert.EqualError(t, err, tc.expectedError)
68+
}
69+
})
70+
}
71+
}
72+
1873
func TestSendCommandSuccess(t *testing.T) {
1974
instances := []string{"inst-id-1", "inst-id-2"}
2075
waitInstanceIDs := []string{}
@@ -774,24 +829,28 @@ func TestUpdateInstance(t *testing.T) {
774829
{
775830
name: "update state available",
776831
invocationOut: &ssm.GetCommandInvocationOutput{
832+
Status: aws.String("Success"),
777833
StandardOutputContent: aws.String(fmt.Sprintf(checkPattern, updateStateAvailable)),
778834
},
779835
expectedSSMCommandCallOrder: []string{"check-document", "apply-document", "reboot-document"},
780836
}, {
781837
name: "update state ready",
782838
invocationOut: &ssm.GetCommandInvocationOutput{
839+
Status: aws.String("Success"),
783840
StandardOutputContent: aws.String(fmt.Sprintf(checkPattern, updateStateReady)),
784841
},
785842
expectedSSMCommandCallOrder: []string{"check-document", "reboot-document"},
786843
}, {
787844
name: "update state idle",
788845
invocationOut: &ssm.GetCommandInvocationOutput{
846+
Status: aws.String("Success"),
789847
StandardOutputContent: aws.String(fmt.Sprintf(checkPattern, updateStateIdle)),
790848
},
791849
expectedSSMCommandCallOrder: []string{"check-document"},
792850
}, {
793851
name: "update state staged",
794852
invocationOut: &ssm.GetCommandInvocationOutput{
853+
Status: aws.String("Success"),
795854
StandardOutputContent: aws.String(fmt.Sprintf(checkPattern, updateStateStaged)),
796855
},
797856
expectedSSMCommandCallOrder: []string{"check-document"},
@@ -859,6 +918,7 @@ func TestUpdateInstanceErr(t *testing.T) {
859918
assert.Equal(t, "command-id", aws.StringValue(input.CommandId))
860919
assert.Equal(t, "instance-id", aws.StringValue(input.InstanceId))
861920
return &ssm.GetCommandInvocationOutput{
921+
Status: aws.String("Success"),
862922
StandardOutputContent: aws.String("{\"update_state\": \"Available\", \"active_partition\": { \"image\": { \"version\": \"0.0.0\"}}}"),
863923
}, nil
864924
}
@@ -1003,20 +1063,23 @@ func TestVerifyUpdate(t *testing.T) {
10031063
{
10041064
name: "verify success",
10051065
invocationOut: &ssm.GetCommandInvocationOutput{
1066+
Status: aws.String("Success"),
10061067
StandardOutputContent: aws.String(fmt.Sprintf(checkPattern, updateStateIdle, "0.0.1")),
10071068
},
10081069
expectedOk: true,
10091070
},
10101071
{
10111072
name: "version is same",
10121073
invocationOut: &ssm.GetCommandInvocationOutput{
1074+
Status: aws.String("Success"),
10131075
StandardOutputContent: aws.String(fmt.Sprintf(checkPattern, updateStateIdle, "0.0.0")),
10141076
},
10151077
expectedOk: false,
10161078
},
10171079
{
10181080
name: "another version is available",
10191081
invocationOut: &ssm.GetCommandInvocationOutput{
1082+
Status: aws.String("Success"),
10201083
StandardOutputContent: aws.String(fmt.Sprintf(checkPattern, updateStateAvailable, "0.0.1")),
10211084
},
10221085
expectedOk: true,
@@ -1076,7 +1139,9 @@ func TestVerifyUpdateErr(t *testing.T) {
10761139
mockGetCommandInvocation := func(input *ssm.GetCommandInvocationInput) (*ssm.GetCommandInvocationOutput, error) {
10771140
assert.Equal(t, "command-id", aws.StringValue(input.CommandId))
10781141
assert.Equal(t, "instance-id", aws.StringValue(input.InstanceId))
1079-
return &ssm.GetCommandInvocationOutput{}, nil
1142+
return &ssm.GetCommandInvocationOutput{
1143+
Status: aws.String("Success"),
1144+
}, nil
10801145
}
10811146
t.Run("check err", func(t *testing.T) {
10821147
ssmCheckErr := errors.New("failed to send check command")

0 commit comments

Comments
 (0)