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

Add a few unit tests #7

Merged
merged 1 commit into from
Dec 12, 2017
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions cloudfront_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions cloudwatch_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
28 changes: 28 additions & 0 deletions ec2_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
28 changes: 28 additions & 0 deletions elasticsearch_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
3 changes: 2 additions & 1 deletion rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
116 changes: 116 additions & 0 deletions rds_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}