Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused function and add runner tests/mocks #33

Merged
merged 1 commit into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should take a look at leveraging GoMock/mockgen moving forward


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