Skip to content

Commit

Permalink
Merge pull request #11 from VEVO/test_Ec2Processor_SetTags
Browse files Browse the repository at this point in the history
Add tests for Ec2Processor.SetTags
  • Loading branch information
aerostitch authored Jan 12, 2018
2 parents c958e91 + 0e6453e commit 16e290e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
7 changes: 5 additions & 2 deletions providers/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"github.com/aws/aws-sdk-go/aws"
"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"
"github.com/sirupsen/logrus"

"github.com/VEVO/awsRetagger/mapper"
)

// Ec2Processor holds the ec2-related actions
type Ec2Processor struct {
svc *ec2.EC2
svc ec2iface.EC2API
}

// NewEc2Processor creates a new instance of Ec2Processor containing an already
Expand All @@ -34,7 +35,9 @@ func (e *Ec2Processor) TagsToMap(tagsInput []*ec2.Tag) map[string]string {
func (e *Ec2Processor) SetTags(resourceID *string, tags []*mapper.TagItem) error {
newTags := []*ec2.Tag{}
for _, tag := range tags {
newTags = append(newTags, &ec2.Tag{Key: aws.String((*tag).Name), Value: aws.String((*tag).Value)})
if len((*tag).Name) > 0 {
newTags = append(newTags, &ec2.Tag{Key: aws.String((*tag).Name), Value: aws.String((*tag).Value)})
}
}

if len(newTags) == 0 {
Expand Down
57 changes: 57 additions & 0 deletions providers/ec2_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
package providers

import (
"errors"
"reflect"
"testing"

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

"github.com/VEVO/awsRetagger/mapper"
)

// mockEc2Client is ised to mock calls to the ec2 API
type mockEc2Client struct {
ec2iface.EC2API
// ResourceID is the resource that has been passed to the mocked function
ResourceIDs []*string
// ResourceTags are the tags that have been passed to the mocked function when
// setting or that is available on the mocked resource when getting
ResourceTags []*ec2.Tag
// ReturnError is the error that you want your mocked function to return
ReturnError error
}

func (m *mockEc2Client) CreateTags(input *ec2.CreateTagsInput) (*ec2.CreateTagsOutput, error) {
m.ResourceIDs = append(m.ResourceIDs, input.Resources...)
if len(input.Tags) > 0 {
m.ResourceTags = append(m.ResourceTags, input.Tags...)
}
return &ec2.CreateTagsOutput{}, m.ReturnError

}

func TestEc2TagsToMap(t *testing.T) {
testData := []struct {
inputTags []*ec2.Tag
Expand All @@ -26,3 +51,35 @@ func TestEc2TagsToMap(t *testing.T) {
}
}
}

func TestEc2SetTags(t *testing.T) {
testData := []struct {
inputResource string
outputResource []*string
inputTags []*mapper.TagItem
outputTags []*ec2.Tag
inputError, outputError error
}{
{"my resource", []*string{}, []*mapper.TagItem{{}}, []*ec2.Tag{}, nil, nil},
{"my resource", []*string{aws.String("my resource")}, []*mapper.TagItem{{Name: "foo", Value: "bar"}}, []*ec2.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}}, nil, nil},
{"my resource", []*string{aws.String("my resource")}, []*mapper.TagItem{{Name: "foo", Value: "bar"}, {Name: "Aerosmith", Value: "rocks"}}, []*ec2.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}, {Key: aws.String("Aerosmith"), Value: aws.String("rocks")}}, nil, nil},
{"my resource", []*string{aws.String("my resource")}, []*mapper.TagItem{{Name: "foo", Value: "bar"}}, []*ec2.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}}, errors.New("Badaboom"), errors.New("Badaboom")},
}
for _, d := range testData {
mockSvc := &mockEc2Client{ResourceIDs: []*string{}, ReturnError: d.inputError, ResourceTags: []*ec2.Tag{}}
p := Ec2Processor{svc: mockSvc}

err := p.SetTags(&d.inputResource, d.inputTags)
if !reflect.DeepEqual(err, d.outputError) {
t.Errorf("Expecting error: %v\nGot: %v\n", d.outputError, err)
}

if len(mockSvc.ResourceIDs) != len(d.outputResource) || (len(mockSvc.ResourceIDs) > 0 && *mockSvc.ResourceIDs[0] != *d.outputResource[0]) {
t.Errorf("Expecting to update resource: %v, got: %v\n", d.outputResource, mockSvc.ResourceIDs)
}

if !reflect.DeepEqual(mockSvc.ResourceTags, d.outputTags) {
t.Errorf("Expecting to update tag: %v\nGot: %v\n", d.outputTags, mockSvc.ResourceTags)
}
}
}

0 comments on commit 16e290e

Please sign in to comment.