From 10a6ac538e12d7ebed2ad70bfb1b9cde02a67c70 Mon Sep 17 00:00:00 2001 From: Joseph Herlant Date: Tue, 5 Dec 2017 21:59:59 -0800 Subject: [PATCH] Add a few unit tests --- CHANGELOG.md | 2 + cloudfront_test.go | 5 +- cloudwatch_test.go | 27 ++++++++++ ec2_test.go | 28 ++++++++++ elasticsearch_test.go | 28 ++++++++++ rds.go | 3 +- rds_test.go | 116 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 206 insertions(+), 3 deletions(-) create mode 100644 cloudwatch_test.go create mode 100644 ec2_test.go create mode 100644 elasticsearch_test.go create mode 100644 rds_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 443877f..d9dac3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added +- Add more unit tests ## [0.1.0] - 2017-11-22 diff --git a/cloudfront_test.go b/cloudfront_test.go index 67a3c50..a696ff2 100644 --- a/cloudfront_test.go +++ b/cloudfront_test.go @@ -2,11 +2,12 @@ package main import ( "errors" + "reflect" + "testing" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/cloudfront" "github.com/aws/aws-sdk-go/service/cloudfront/cloudfrontiface" - "reflect" - "testing" ) type mockCloudFrontClient struct { diff --git a/cloudwatch_test.go b/cloudwatch_test.go new file mode 100644 index 0000000..a0c49e8 --- /dev/null +++ b/cloudwatch_test.go @@ -0,0 +1,27 @@ +package main + +import ( + "reflect" + "testing" + + "github.com/aws/aws-sdk-go/aws" +) + +func TestCwTagsToMap(t *testing.T) { + testData := []struct { + inputTags map[string]*string + outputTags map[string]string + }{ + {map[string]*string{}, map[string]string{}}, + {map[string]*string{"foo": aws.String("bar")}, map[string]string{"foo": "bar"}}, + {map[string]*string{"foo": aws.String("bar"), "Aerosmith": aws.String("rocks")}, map[string]string{"foo": "bar", "Aerosmith": "rocks"}}, + } + for _, d := range testData { + p := CwProcessor{} + + res := p.TagsToMap(d.inputTags) + if !reflect.DeepEqual(res, d.outputTags) { + t.Errorf("Expecting to get tags: %v\nGot: %v\n", d.outputTags, res) + } + } +} diff --git a/ec2_test.go b/ec2_test.go new file mode 100644 index 0000000..aa9393e --- /dev/null +++ b/ec2_test.go @@ -0,0 +1,28 @@ +package main + +import ( + "reflect" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/ec2" +) + +func TestEc2TagsToMap(t *testing.T) { + testData := []struct { + inputTags []*ec2.Tag + outputTags map[string]string + }{ + {[]*ec2.Tag{}, map[string]string{}}, + {[]*ec2.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}}, map[string]string{"foo": "bar"}}, + {[]*ec2.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}, {Key: aws.String("Aerosmith"), Value: aws.String("rocks")}}, map[string]string{"foo": "bar", "Aerosmith": "rocks"}}, + } + for _, d := range testData { + p := Ec2Processor{} + + res := p.TagsToMap(d.inputTags) + if !reflect.DeepEqual(res, d.outputTags) { + t.Errorf("Expecting to get tags: %v\nGot: %v\n", d.outputTags, res) + } + } +} diff --git a/elasticsearch_test.go b/elasticsearch_test.go new file mode 100644 index 0000000..a8b831f --- /dev/null +++ b/elasticsearch_test.go @@ -0,0 +1,28 @@ +package main + +import ( + "reflect" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/elasticsearchservice" +) + +func TestElkTagsToMap(t *testing.T) { + testData := []struct { + inputTags []*elasticsearchservice.Tag + outputTags map[string]string + }{ + {[]*elasticsearchservice.Tag{}, map[string]string{}}, + {[]*elasticsearchservice.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}}, map[string]string{"foo": "bar"}}, + {[]*elasticsearchservice.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}, {Key: aws.String("Aerosmith"), Value: aws.String("rocks")}}, map[string]string{"foo": "bar", "Aerosmith": "rocks"}}, + } + for _, d := range testData { + p := ElkProcessor{} + + res := p.TagsToMap(d.inputTags) + if !reflect.DeepEqual(res, d.outputTags) { + t.Errorf("Expecting to get tags: %v\nGot: %v\n", d.outputTags, res) + } + } +} diff --git a/rds.go b/rds.go index 17f39e3..260dc5f 100644 --- a/rds.go +++ b/rds.go @@ -4,12 +4,13 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/rds" + "github.com/aws/aws-sdk-go/service/rds/rdsiface" "github.com/sirupsen/logrus" ) // RdsProcessor holds the rds-related actions type RdsProcessor struct { - svc *rds.RDS + svc rdsiface.RDSAPI } // NewRdsProcessor creates a new instance of RdsProcessor containing an already diff --git a/rds_test.go b/rds_test.go new file mode 100644 index 0000000..2316659 --- /dev/null +++ b/rds_test.go @@ -0,0 +1,116 @@ +package main + +import ( + "errors" + "reflect" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/rds" + "github.com/aws/aws-sdk-go/service/rds/rdsiface" +) + +type mockRdsClient struct { + rdsiface.RDSAPI + // ResourceID is the resource that has been passed to the mocked function + ResourceID *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 []*rds.Tag + // ReturnError is the error that you want your mocked function to return + ReturnError error +} + +func (m *mockRdsClient) AddTagsToResource(input *rds.AddTagsToResourceInput) (*rds.AddTagsToResourceOutput, error) { + m.ResourceID = input.ResourceName + if input.Tags != nil { + m.ResourceTags = append(m.ResourceTags, input.Tags...) + } + return &rds.AddTagsToResourceOutput{}, m.ReturnError +} + +func (m *mockRdsClient) ListTagsForResource(input *rds.ListTagsForResourceInput) (*rds.ListTagsForResourceOutput, error) { + m.ResourceID = input.ResourceName + return &rds.ListTagsForResourceOutput{TagList: m.ResourceTags}, m.ReturnError +} + +func TestRdsTagsToMap(t *testing.T) { + testData := []struct { + inputTags []*rds.Tag + outputTags map[string]string + }{ + {[]*rds.Tag{}, map[string]string{}}, + {[]*rds.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}}, map[string]string{"foo": "bar"}}, + {[]*rds.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}, {Key: aws.String("Aerosmith"), Value: aws.String("rocks")}}, map[string]string{"foo": "bar", "Aerosmith": "rocks"}}, + } + for _, d := range testData { + p := RdsProcessor{} + + res := p.TagsToMap(d.inputTags) + if !reflect.DeepEqual(res, d.outputTags) { + t.Errorf("Expecting to get tags: %v\nGot: %v\n", d.outputTags, res) + } + } +} + +func TestRdsSetTags(t *testing.T) { + testData := []struct { + inputResource, outputResource string + inputTags []*TagItem + outputTags []*rds.Tag + inputError, outputError error + }{ + {"my resource", "my resource", []*TagItem{{}}, []*rds.Tag{{Key: aws.String(""), Value: aws.String("")}}, nil, nil}, + {"my resource", "my resource", []*TagItem{{Name: "foo", Value: "bar"}}, []*rds.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}}, nil, nil}, + {"my resource", "my resource", []*TagItem{{Name: "foo", Value: "bar"}, {Name: "Aerosmith", Value: "rocks"}}, []*rds.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}, {Key: aws.String("Aerosmith"), Value: aws.String("rocks")}}, nil, nil}, + {"my resource", "my resource", []*TagItem{{Name: "foo", Value: "bar"}}, []*rds.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}}, errors.New("Badaboom"), errors.New("Badaboom")}, + } + for _, d := range testData { + mockSvc := &mockRdsClient{ReturnError: d.inputError, ResourceTags: []*rds.Tag{}} + p := RdsProcessor{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 *mockSvc.ResourceID != d.outputResource { + t.Errorf("Expecting to update resource: %s, got: %s\n", d.outputResource, *mockSvc.ResourceID) + } + + if !reflect.DeepEqual(mockSvc.ResourceTags, d.outputTags) { + t.Errorf("Expecting to update tag: %v\nGot: %v\n", d.outputTags, mockSvc.ResourceTags) + } + } +} + +func TestRdsGetTags(t *testing.T) { + testData := []struct { + inputResource, outputResource string + inputTags []*rds.Tag + outputTags []*rds.Tag + inputError, outputError error + }{ + {"my resource", "my resource", []*rds.Tag{}, []*rds.Tag{}, nil, nil}, + {"my resource", "my resource", []*rds.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}}, []*rds.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}}, nil, nil}, + {"my resource", "my resource", []*rds.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}, {Key: aws.String("Aerosmith"), Value: aws.String("rocks")}}, []*rds.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}, {Key: aws.String("Aerosmith"), Value: aws.String("rocks")}}, nil, nil}, + {"my resource", "my resource", []*rds.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}}, []*rds.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}}, errors.New("Badaboom"), errors.New("Badaboom")}, + } + for _, d := range testData { + mockSvc := &mockRdsClient{ReturnError: d.inputError, ResourceTags: d.inputTags} + p := RdsProcessor{svc: mockSvc} + + res, err := p.GetTags(&d.inputResource) + if !reflect.DeepEqual(err, d.outputError) { + t.Errorf("Expecting error: %v\nGot: %v\n", d.outputError, err) + } + + if *mockSvc.ResourceID != d.outputResource { + t.Errorf("Expecting resource: %s, got: %s\n", d.outputResource, *mockSvc.ResourceID) + } + + if !reflect.DeepEqual(res, d.outputTags) { + t.Errorf("Expecting to get tags: %v\nGot: %v\n", d.outputTags, res) + } + } +}