Skip to content

Commit

Permalink
use awserror syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
ec2-bot authored and haugenj committed Apr 20, 2021
1 parent 94fc65d commit 692afe1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
6 changes: 2 additions & 4 deletions pkg/monitor/sqsevent/sqs-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"

"github.com/aws/aws-node-termination-handler/pkg/monitor"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
"github.com/aws/aws-sdk-go/service/ec2"
Expand Down Expand Up @@ -190,8 +189,7 @@ func (m SQSMonitor) retrieveNodeName(instanceID string) (string, error) {
},
})
if err != nil {
// the ec2 client doesn't use aws error codes
if strings.Contains(err.Error(), "InvalidInstanceID.NotFound") {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "InvalidInstanceID.NotFound" {
log.Info().Msgf("No instance found with instance-id %s", instanceID)
return "", ErrNodeStateNotRunning
}
Expand Down
37 changes: 36 additions & 1 deletion pkg/monitor/sqsevent/sqs-monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,42 @@ func TestMonitor_EC2NoInstances(t *testing.T) {
}
ec2Mock := h.MockedEC2{
DescribeInstancesResp: ec2.DescribeInstancesOutput{},
DescribeInstancesErr: fmt.Errorf("InvalidInstanceID.NotFound: The instance ID 'i-0d6bd3ce2bf8a6751' does not exist\n\tstatus code: 400, request id: 6a5c30e2-922d-464c-946c-a1ec76e5920b"),
}
drainChan := make(chan monitor.InterruptionEvent, 1)

sqsMonitor := sqsevent.SQSMonitor{
SQS: sqsMock,
EC2: ec2Mock,
QueueURL: "https://test-queue",
InterruptionChan: drainChan,
}

err = sqsMonitor.Monitor()
h.Ok(t, err)

select {
case <-drainChan:
h.Ok(t, fmt.Errorf("Expected no events"))
default:
h.Ok(t, nil)
}
}
}

func TestMonitor_DescribeInstancesError(t *testing.T) {
for _, event := range []sqsevent.EventBridgeEvent{spotItnEvent, asgLifecycleEvent} {
msg, err := getSQSMessageFromEvent(event)
h.Ok(t, err)
messages := []*sqs.Message{
&msg,
}
sqsMock := h.MockedSQS{
ReceiveMessageResp: sqs.ReceiveMessageOutput{Messages: messages},
ReceiveMessageErr: nil,
}
ec2Mock := h.MockedEC2{
DescribeInstancesResp: ec2.DescribeInstancesOutput{},
DescribeInstancesErr: awserr.New("InvalidInstanceID.NotFound", "The instance ID 'i-0d6bd3ce2bf8a6751' does not exist\n\tstatus code: 400, request id: 6a5c30e2-922d-464c-946c-a1ec76e5920b", fmt.Errorf("original error")),
}
drainChan := make(chan monitor.InterruptionEvent, 1)

Expand Down

0 comments on commit 692afe1

Please sign in to comment.