Skip to content

Commit

Permalink
update deco, dont return terminated instances (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
fishnix authored Sep 9, 2021
1 parent d543ac6 commit 29c1c9e
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ RUN chmod 555 /app/api
RUN apk add --no-cache bash ca-certificates

# Install Deco
ARG DECO_VERSION=1.0.2
ARG DECO_VERSION=1.3.0
ARG DECO_OS=linux
ARG DECO_ARCH=amd64
ADD https://github.com/YaleUniversity/deco/releases/download/v${DECO_VERSION}/deco-v${DECO_VERSION}-${DECO_OS}-${DECO_ARCH}.tar.gz /usr/local/bin/deco.tar.gz
ADD https://github.com/YaleUniversity/deco/releases/download/v${DECO_VERSION}/deco_${DECO_VERSION}_${DECO_OS}_${DECO_ARCH}.tar.gz /usr/local/bin/deco.tar.gz
RUN cd /usr/local/bin && tar -zxvf deco.tar.gz && rm -f deco.tar.gz && chmod 555 deco && /usr/local/bin/deco version

COPY docker/*_config.sh /app
Expand Down
9 changes: 8 additions & 1 deletion ec2/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func (e *Ec2) GetInstance(ctx context.Context, id string) (*ec2.Instance, error)

out, err := e.Service.DescribeInstancesWithContext(ctx, &ec2.DescribeInstancesInput{
InstanceIds: aws.StringSlice([]string{id}),
Filters: []*ec2.Filter{
notTerminated(),
},
})

if err != nil {
Expand All @@ -75,8 +78,12 @@ func (e *Ec2) GetInstance(ctx context.Context, id string) (*ec2.Instance, error)

log.Debugf("got output for instance %s: %+v", id, out)

if len(out.Reservations) == 0 || len(out.Reservations[0].Instances) == 0 {
return nil, apierror.New(apierror.ErrNotFound, "Resource not found", nil)
}

if len(out.Reservations) != 1 || len(out.Reservations[0].Instances) != 1 {
return nil, apierror.New(apierror.ErrBadRequest, "unexpected count", nil)
return nil, apierror.New(apierror.ErrBadRequest, "Unexpected resource count", nil)
}

return out.Reservations[0].Instances[0], nil
Expand Down
81 changes: 81 additions & 0 deletions ec2/instances_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package ec2

import (
"context"
"reflect"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)

func (m *mockEC2Client) DescribeInstancesWithContext(ctx context.Context, input *ec2.DescribeInstancesInput, opts ...request.Option) (*ec2.DescribeInstancesOutput, error) {
if m.err != nil {
return nil, m.err
}

if len(input.InstanceIds) != 0 && aws.StringValue(input.InstanceIds[0]) == "i-notfound" {
return &ec2.DescribeInstancesOutput{}, nil
} else if len(input.InstanceIds) != 0 && aws.StringValue(input.InstanceIds[0]) == "i-multiple" {
return &ec2.DescribeInstancesOutput{
Reservations: []*ec2.Reservation{},
}, nil
}

return nil, nil
}

func TestEc2_GetInstance(t *testing.T) {
type fields struct {
session *session.Session
Service ec2iface.EC2API
DefaultKMSKeyId string
DefaultSgs []string
DefaultSubnets []string
org string
}
type args struct {
ctx context.Context
id string
}
tests := []struct {
name string
fields fields
args args
want *ec2.Instance
wantErr bool
}{
{
name: "empty id",
fields: fields{Service: newmockEC2Client(t, nil)},
args: args{
ctx: context.TODO(),
id: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &Ec2{
session: tt.fields.session,
Service: tt.fields.Service,
DefaultKMSKeyId: tt.fields.DefaultKMSKeyId,
DefaultSgs: tt.fields.DefaultSgs,
DefaultSubnets: tt.fields.DefaultSubnets,
org: tt.fields.org,
}
got, err := e.GetInstance(tt.args.ctx, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("Ec2.GetInstance() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Ec2.GetInstance() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 29c1c9e

Please sign in to comment.