Skip to content

Commit

Permalink
test(elasticache): adding tests for subnet groups and clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Apr 23, 2024
1 parent 5353a67 commit 40ff152
Show file tree
Hide file tree
Showing 6 changed files with 4,841 additions and 6 deletions.
4,546 changes: 4,546 additions & 0 deletions mocks/mock_elasticacheiface/mock.go

Large diffs are not rendered by default.

39 changes: 36 additions & 3 deletions resources/elasticache-memcacheclusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package resources

import (
"context"
"github.com/aws/aws-sdk-go/service/elasticache/elasticacheiface"
"github.com/ekristen/libnuke/pkg/types"
"github.com/sirupsen/logrus"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticache"
Expand All @@ -27,12 +30,19 @@ func init() {
})
}

type ElasticacheCacheClusterLister struct{}
type ElasticacheCacheClusterLister struct {
mockSvc elasticacheiface.ElastiCacheAPI
}

func (l *ElasticacheCacheClusterLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)

svc := elasticache.New(opts.Session)
var svc elasticacheiface.ElastiCacheAPI
if l.mockSvc != nil {
svc = l.mockSvc
} else {
svc = elasticache.New(opts.Session)
}

params := &elasticache.DescribeCacheClustersInput{MaxRecords: aws.Int64(100)}
resp, err := svc.DescribeCacheClusters(params)
Expand All @@ -42,20 +52,43 @@ func (l *ElasticacheCacheClusterLister) List(_ context.Context, o interface{}) (

var resources []resource.Resource
for _, cacheCluster := range resp.CacheClusters {
tags, err := svc.ListTagsForResource(&elasticache.ListTagsForResourceInput{
ResourceName: cacheCluster.CacheClusterId,
})
if err != nil {
logrus.WithError(err).Error("unable to retrieve tags")
continue
}

resources = append(resources, &ElasticacheCacheCluster{
svc: svc,
clusterID: cacheCluster.CacheClusterId,
status: cacheCluster.CacheClusterStatus,
Tags: tags.TagList,
})
}

return resources, nil
}

type ElasticacheCacheCluster struct {
svc *elasticache.ElastiCache
svc elasticacheiface.ElastiCacheAPI
clusterID *string
status *string
Tags []*elasticache.Tag
}

func (i *ElasticacheCacheCluster) Properties() types.Properties {
properties := types.NewProperties()

properties.Set("ClusterID", i.clusterID)
properties.Set("Status", i.status)

for _, tag := range i.Tags {
properties.SetTag(tag.Key, tag.Value)
}

return properties
}

func (i *ElasticacheCacheCluster) Remove(_ context.Context) error {
Expand Down
109 changes: 109 additions & 0 deletions resources/elasticache-memcacheclusters_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package resources

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/ekristen/aws-nuke/mocks/mock_elasticacheiface"
"github.com/ekristen/aws-nuke/pkg/nuke"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"testing"
)

func Test_Mock_ElastiCache_CacheCluster_Remove(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockElastiCache := mock_elasticacheiface.NewMockElastiCacheAPI(ctrl)

cacheCluster := ElasticacheCacheCluster{
svc: mockElastiCache,
clusterID: aws.String("foobar"),
}

mockElastiCache.EXPECT().DeleteCacheCluster(&elasticache.DeleteCacheClusterInput{
CacheClusterId: aws.String("foobar"),
}).Return(&elasticache.DeleteCacheClusterOutput{}, nil)

err := cacheCluster.Remove(nil)
a.Nil(err)
}

func Test_Mock_ElastiCache_CacheCluster_List_NoTags(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockElastiCache := mock_elasticacheiface.NewMockElastiCacheAPI(ctrl)

cacheClusterLister := ElasticacheCacheClusterLister{
mockSvc: mockElastiCache,
}

mockElastiCache.EXPECT().DescribeCacheClusters(gomock.Any()).Return(&elasticache.DescribeCacheClustersOutput{
CacheClusters: []*elasticache.CacheCluster{
{
CacheClusterId: aws.String("foobar"),
CacheClusterStatus: aws.String("available"),
},
},
}, nil)

mockElastiCache.EXPECT().ListTagsForResource(&elasticache.ListTagsForResourceInput{
ResourceName: aws.String("foobar"),
}).Return(&elasticache.TagListMessage{}, nil)

resources, err := cacheClusterLister.List(nil, &nuke.ListerOpts{})
a.Nil(err)
a.Len(resources, 1)

resource := resources[0].(*ElasticacheCacheCluster)
a.Equal("foobar", resource.String())
}

func Test_Mock_ElastiCache_CacheCluster_List_WithTags(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockElastiCache := mock_elasticacheiface.NewMockElastiCacheAPI(ctrl)

cacheClusterLister := ElasticacheCacheClusterLister{
mockSvc: mockElastiCache,
}

mockElastiCache.EXPECT().DescribeCacheClusters(gomock.Any()).Return(&elasticache.DescribeCacheClustersOutput{
CacheClusters: []*elasticache.CacheCluster{
{
CacheClusterId: aws.String("foobar"),
},
},
}, nil)

mockElastiCache.EXPECT().ListTagsForResource(&elasticache.ListTagsForResourceInput{
ResourceName: aws.String("foobar"),
}).Return(&elasticache.TagListMessage{
TagList: []*elasticache.Tag{
{
Key: aws.String("Name"),
Value: aws.String("foobar"),
},
{
Key: aws.String("aws-nuke"),
Value: aws.String("test"),
},
},
}, nil)

resources, err := cacheClusterLister.List(nil, &nuke.ListerOpts{})
a.Nil(err)
a.Len(resources, 1)

resource := resources[0].(*ElasticacheCacheCluster)
a.Len(resource.Tags, 2)
a.Equal("foobar", resource.String())
a.Equal("foobar", resource.Properties().Get("tag:Name"))
a.Equal("test", resource.Properties().Get("tag:aws-nuke"))

}
2 changes: 2 additions & 0 deletions resources/elasticache-mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//go:generate ../mocks/generate_mocks.sh elasticache elasticacheiface
package resources
38 changes: 35 additions & 3 deletions resources/elasticache-subnetgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package resources
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/service/elasticache/elasticacheiface"
"github.com/ekristen/libnuke/pkg/types"
"github.com/sirupsen/logrus"
"strings"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -24,12 +27,19 @@ func init() {
})
}

type ElasticacheSubnetGroupLister struct{}
type ElasticacheSubnetGroupLister struct {
mockSvc elasticacheiface.ElastiCacheAPI
}

func (l *ElasticacheSubnetGroupLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)

svc := elasticache.New(opts.Session)
var svc elasticacheiface.ElastiCacheAPI
if l.mockSvc != nil {
svc = l.mockSvc
} else {
svc = elasticache.New(opts.Session)
}

params := &elasticache.DescribeCacheSubnetGroupsInput{MaxRecords: aws.Int64(100)}
resp, err := svc.DescribeCacheSubnetGroups(params)
Expand All @@ -39,18 +49,28 @@ func (l *ElasticacheSubnetGroupLister) List(_ context.Context, o interface{}) ([

var resources []resource.Resource
for _, subnetGroup := range resp.CacheSubnetGroups {
tags, err := svc.ListTagsForResource(&elasticache.ListTagsForResourceInput{
ResourceName: subnetGroup.CacheSubnetGroupName,
})
if err != nil {
logrus.WithError(err).Error("unable to retrieve tags")
continue
}

resources = append(resources, &ElasticacheSubnetGroup{
svc: svc,
name: subnetGroup.CacheSubnetGroupName,
Tags: tags.TagList,
})
}

return resources, nil
}

type ElasticacheSubnetGroup struct {
svc *elasticache.ElastiCache
svc elasticacheiface.ElastiCacheAPI
name *string
Tags []*elasticache.Tag
}

func (i *ElasticacheSubnetGroup) Filter() error {
Expand All @@ -60,6 +80,18 @@ func (i *ElasticacheSubnetGroup) Filter() error {
return nil
}

func (i *ElasticacheSubnetGroup) Properties() types.Properties {
properties := types.NewProperties()

properties.Set("Name", i.name)

for _, tag := range i.Tags {
properties.SetTag(tag.Key, tag.Value)
}

return properties
}

func (i *ElasticacheSubnetGroup) Remove(_ context.Context) error {
params := &elasticache.DeleteCacheSubnetGroupInput{
CacheSubnetGroupName: i.name,
Expand Down
113 changes: 113 additions & 0 deletions resources/elasticache-subnetgroups_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package resources

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticache"

"github.com/ekristen/aws-nuke/mocks/mock_elasticacheiface"
"github.com/ekristen/aws-nuke/pkg/nuke"
)

func Test_Mock_ElastiCache_SubnetGroup_Remove(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockElastiCache := mock_elasticacheiface.NewMockElastiCacheAPI(ctrl)

subnetGroup := ElasticacheSubnetGroup{
svc: mockElastiCache,
name: aws.String("foobar"),
}

mockElastiCache.EXPECT().DeleteCacheSubnetGroup(gomock.Any()).Return(&elasticache.DeleteCacheSubnetGroupOutput{}, nil)

err := subnetGroup.Remove(nil)
a.Nil(err)
a.Equal("foobar", *subnetGroup.name)
}

func Test_Mock_ElastiCache_SubnetGroup_List_NoTags(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockElastiCache := mock_elasticacheiface.NewMockElastiCacheAPI(ctrl)

subnetGroupsLister := ElasticacheSubnetGroupLister{
mockSvc: mockElastiCache,
}

mockElastiCache.EXPECT().DescribeCacheSubnetGroups(gomock.Any()).Return(&elasticache.DescribeCacheSubnetGroupsOutput{
CacheSubnetGroups: []*elasticache.CacheSubnetGroup{
{
CacheSubnetGroupName: aws.String("foobar"),
},
},
}, nil)

mockElastiCache.EXPECT().ListTagsForResource(&elasticache.ListTagsForResourceInput{
ResourceName: aws.String("foobar"),
}).Return(&elasticache.TagListMessage{}, nil)

resources, err := subnetGroupsLister.List(nil, &nuke.ListerOpts{})
a.Nil(err)
a.Len(resources, 1)

resource := resources[0].(*ElasticacheSubnetGroup)
a.Len(resource.Tags, 0)

a.Equal("foobar", resource.String())
}

func Test_Mock_ElastiCache_SubnetGroup_List_WithTags(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockElastiCache := mock_elasticacheiface.NewMockElastiCacheAPI(ctrl)

subnetGroupsLister := ElasticacheSubnetGroupLister{
mockSvc: mockElastiCache,
}

mockElastiCache.EXPECT().DescribeCacheSubnetGroups(gomock.Any()).Return(&elasticache.DescribeCacheSubnetGroupsOutput{
CacheSubnetGroups: []*elasticache.CacheSubnetGroup{
{
CacheSubnetGroupName: aws.String("foobar"),
},
},
}, nil)

mockElastiCache.EXPECT().ListTagsForResource(&elasticache.ListTagsForResourceInput{
ResourceName: aws.String("foobar"),
}).Return(&elasticache.TagListMessage{
TagList: []*elasticache.Tag{
{
Key: aws.String("Name"),
Value: aws.String("foobar"),
},
{
Key: aws.String("aws-nuke"),
Value: aws.String("test"),
},
},
}, nil)

resources, err := subnetGroupsLister.List(nil, &nuke.ListerOpts{})
a.Nil(err)

a.Len(resources, 1)

resource := resources[0].(*ElasticacheSubnetGroup)
a.Len(resource.Tags, 2)

a.Equal("foobar", resource.String())
a.Equal("foobar", resource.Properties().Get("tag:Name"))
a.Equal("test", resource.Properties().Get("tag:aws-nuke"))
}

0 comments on commit 40ff152

Please sign in to comment.