Skip to content

Commit

Permalink
Remove unused function and add runner tests/mocks (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
sendqueery committed Aug 24, 2020
1 parent adbe68c commit cd1239d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
41 changes: 41 additions & 0 deletions ssm/invocation/runner_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
package invocation

import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ssm"
mocks "github.com/disneystreaming/ssm-helpers/testing"
"github.com/stretchr/testify/assert"
)

func TestGetResult(t *testing.T) {
assert := assert.New(t)
mockSvc := &mocks.MockSSMClient{}

successCmd, badCmd, mockInstance :=
aws.String("success-id"), aws.String("bad-id"), aws.String("i-123")

oc := make(chan *ssm.GetCommandInvocationOutput)
ec := make(chan error)

t.Run("valid ID", func(t *testing.T) {
go GetResult(mockSvc, successCmd, mockInstance, oc, ec)
select {
case result := <-oc:
assert.Equal("success-id", *result.CommandId)
case err := <-ec:
assert.Empty(err)
}

})

t.Run("invalid ID", func(t *testing.T) {
go GetResult(mockSvc, badCmd, mockInstance, oc, ec)
select {
case result := <-oc:
assert.Empty(result)
case err := <-ec:
assert.Error(err)
}
})
}
37 changes: 19 additions & 18 deletions testing/ssm_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mocks

import (
"fmt"
"os"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand Down Expand Up @@ -56,39 +55,41 @@ func (m *MockSSMClient) TerminateSession(input *ssm.TerminateSessionInput) (outp
}

func (m *MockSSMClient) GetCommandInvocation(input *ssm.GetCommandInvocationInput) (output *ssm.GetCommandInvocationOutput, err error) {

if os.Getenv(fmt.Sprintf("%s-trycount", *input.InstanceId)) == "0" {
// Test waiting in cases where the invocation hasn't started yet
err = awserr.New("InvocationDoesNotExist", "The command ID and instance ID you specified did not match any invocations.\nVerify the command ID and the instance ID and try again.", err)
os.Setenv(fmt.Sprintf("%s-trycount", *input.InstanceId), "1")
return nil, err
} else if os.Getenv(fmt.Sprintf("%s-trycount", *input.InstanceId)) == "1" {
// Test waiting for the invocation to complete
output = &ssm.GetCommandInvocationOutput{
InstanceId: input.InstanceId,
CommandId: input.CommandId,
StatusDetails: aws.String("InProgress"),
var status string

switch *input.CommandId {
case "success-id":
status = "Success"
case "failed-id", "mixed-id":
status = "Failed"
case "pending-id":
status = "Pending"
case "bad-id":
return nil, awserr.New("InvalidCommandId", "InvalidCommandId", nil)
}
os.Setenv(fmt.Sprintf("%s-trycount", *input.InstanceId), "2")
return output, nil
} else {

output = &ssm.GetCommandInvocationOutput{
InstanceId: input.InstanceId,
CommandId: input.CommandId,
StatusDetails: aws.String("Success"),
StatusDetails: aws.String(status),
}

return output, nil
}
}

func (m *MockSSMClient) SendCommand(input *ssm.SendCommandInput) (output *ssm.SendCommandOutput, err error) {
if len(input.InstanceIds) > 0 && len(input.Targets) > 0 {
return nil, fmt.Errorf("Cannot specify instance IDs and SSM targets in same SendCommandInput")
}

// Mock our response from the SSM API
output = &ssm.SendCommandOutput{
Command: &ssm.Command{
CommandId: aws.String("1234561234561234561234561235456"),
DocumentName: input.DocumentName,
InstanceIds: input.InstanceIds,
Parameters: input.Parameters,
Targets: input.Targets,
},
}
return output, nil
Expand Down

0 comments on commit cd1239d

Please sign in to comment.