Skip to content

Commit

Permalink
Add Snapshot output test
Browse files Browse the repository at this point in the history
  • Loading branch information
hubeadmin committed Mar 13, 2020
1 parent 110c59a commit 566d22a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
65 changes: 65 additions & 0 deletions pkg/aws/elasticache_snapshot_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi"
"github.com/integr8ly/cluster-service/pkg/clusterservice"
"github.com/pkg/errors"
Expand Down Expand Up @@ -69,6 +70,68 @@ func TestElasticacheSnapshotEngine_DeleteResourcesForCluster(t *testing.T) {
dryRun: true,
},
wantErr: "failed to describe cache clusters: ",
}, {
name: "error when getting cacheCluster output",
fields: fields{
elasticacheClient: func() *elasticacheClientMock {
fakeClient, err := fakeElasticacheClient(func(c *elasticacheClientMock) error {
c.DescribeCacheClustersFunc = func(in1 *elasticache.DescribeCacheClustersInput) (output *elasticache.DescribeCacheClustersOutput, e error) {
return nil, errors.New("")
}
return nil
})
if err != nil {
t.Fatal(err)
}
return fakeClient
},
taggingClient: func() *taggingClientMock {
fakeTaggingClient, err := fakeResourcetaggingClient(func(c *taggingClientMock) error {
return nil
})
if err != nil {
t.Fatal(err)
}
return fakeTaggingClient
},
logger: fakeLogger,
},
args: args{
clusterId: fakeClusterId,
dryRun: true,
},
wantErr: "cannot get cacheCluster output: ",
}, {
name: "error when describe snapshot fails",
fields: fields{
elasticacheClient: func() *elasticacheClientMock {
fakeClient, err := fakeElasticacheClient(func(c *elasticacheClientMock) error {
c.DescribeSnapshotsFunc = func(in1 *elasticache.DescribeSnapshotsInput) (output *elasticache.DescribeSnapshotsOutput, e error) {
return nil, errors.New("")
}
return nil
})
if err != nil {
t.Fatal(err)
}
return fakeClient
},
taggingClient: func() *taggingClientMock {
fakeTaggingClient, err := fakeResourcetaggingClient(func(c *taggingClientMock) error {
return nil
})
if err != nil {
t.Fatal(err)
}
return fakeTaggingClient
},
logger: fakeLogger,
},
args: args{
clusterId: fakeClusterId,
dryRun: true,
},
wantErr: "cannot describe snapshots: ",
},
}

Expand All @@ -84,11 +147,13 @@ func TestElasticacheSnapshotEngine_DeleteResourcesForCluster(t *testing.T) {
if tt.wantErr != "" && err.Error() != tt.wantErr {
t.Errorf("DeleteResourcesForCluster() error = %v, wantErr %v", err, tt.wantErr)
return

}

if !equalReportItems(got, tt.want) {
t.Errorf("DeleteResourcesForCluster() got = %v, want %v", got, tt.want)
}

if tt.wantFn != nil {
if err := tt.wantFn(fakeClient); err != nil {
t.Errorf("DeleteResourcesForCluster() err = %v", err)
Expand Down
6 changes: 3 additions & 3 deletions pkg/aws/manager_elasticache_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ func (r *ElasticacheSnapshotManager) DeleteResourcesForCluster(clusterId string,
reportItem.ActionStatus = clusterservice.ActionStatusDryRun
continue
}
ssLogger.Debug("performing deletion of snapshotOutput cachecluster")
ssLogger.Debug("performing deletion of snapshot")
snapshotInput := &elasticache.DescribeSnapshotsInput{
CacheClusterId: aws.String(cacheClusterId),
CacheClusterId: &cacheClusterId,
}
snapshotOutput, err := r.elasticacheClient.DescribeSnapshots(snapshotInput)
if err != nil {
return nil, errors.WrapLog(err, "cannot Describe snapshotInput", logger)
return nil, errors.WrapLog(err, "cannot describe snapshots", logger)
}
if len(snapshotOutput.Snapshots) > 0 && aws.StringValue(snapshotOutput.Snapshots[0].SnapshotStatus) == statusDeleting {
ssLogger.Debugf("deletion of snapshots already in progress")
Expand Down
2 changes: 2 additions & 0 deletions pkg/aws/manager_elasticache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,13 @@ func TestElasticacheEngine_DeleteResourcesForCluster(t *testing.T) {
if tt.wantErr != "" && err.Error() != tt.wantErr {
t.Errorf("DeleteResourcesForCluster() error = %v, wantErr %v", err, tt.wantErr)
return

}

if !equalReportItems(got, tt.want) {
t.Errorf("DeleteResourcesForCluster() got = %v, want %v", got, tt.want)
}

if tt.wantFn != nil {
if err := tt.wantFn(fakeClient); err != nil {
t.Errorf("DeleteResourcesForCluster() err = %v", err)
Expand Down
23 changes: 19 additions & 4 deletions pkg/aws/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,20 @@ const (
fakeRDSClientInstanceARN = fakeARN
fakeRDSClientInstanceDeletionProtection = true

//ELasticache-specific
fakeElasticacheClientName = "elasticache Replication group"
fakeElasticacheClientRegion = "eu-west-1"
fakeElasticacheClientReplicationGroupId = "testRepGroupID"
fakeElasticacheClientDescription = "TestDescription"
fakeElasticacheClientEngine = "redis"
fakeElasticacheClientTagKey = "integreatly.org/clusterID"
fakeElasticacheClientTagValue = "test"
fakeElasticacheClientCacheNodeType = "cache.t2.micro"
fakeElasticacheClientStatusAvailable = "available"
fakeResourceTaggingClientArn = "arn:fake:testIdentifier"
fakeResourceTaggingClientTagKey = "testTag"
fakeResourceTaggingClientTagValue = "testValue"
fakeClusterID = "testClusterID"
fakeCacheClusterStatus = "available"
fakeActionEngineName = "Fake Action Engine"
fakeElasticacheSnapshotName = "testSnapshotName"
fakeElasticacheSnapshotStatus = "available"

//resource tagging-specific
fakeResourceTagMappingARN = fakeARN
Expand Down Expand Up @@ -189,6 +188,16 @@ func fakeS3BatchClient(modifyFn func(c *s3BatchDeleteClientMock) error) (*s3Batc
return client, nil
}

//ELASTICACHE
func fakeElasticacheSnapshot() *elasticache.Snapshot {
return &elasticache.Snapshot{
CacheClusterId: aws.String(fakeClusterID),
CacheNodeType: aws.String(fakeElasticacheClientCacheNodeType),
Engine: aws.String(fakeElasticacheClientEngine),
SnapshotName: aws.String(fakeElasticacheSnapshotName),
SnapshotStatus: aws.String(fakeElasticacheSnapshotStatus),
}
}
func fakeReportItemReplicationGroupDeleting() *clusterservice.ReportItem {
return &clusterservice.ReportItem{
ID: fakeElasticacheClientReplicationGroupId,
Expand Down Expand Up @@ -235,6 +244,12 @@ func fakeElasticacheClient(modifyFn func(c *elasticacheClientMock) error) (*elas
fakeElasticacheReplicationGroup(),
}}, nil
},
DescribeSnapshotsFunc: func(in1 *elasticache.DescribeSnapshotsInput) (output *elasticache.DescribeSnapshotsOutput, e error) {
return &elasticache.DescribeSnapshotsOutput{
Snapshots: []*elasticache.Snapshot{
fakeElasticacheSnapshot(),
}}, nil
},
DescribeCacheClustersFunc: func(in1 *elasticache.DescribeCacheClustersInput) (output *elasticache.DescribeCacheClustersOutput, e error) {
return &elasticache.DescribeCacheClustersOutput{
CacheClusters: []*elasticache.CacheCluster{
Expand Down

0 comments on commit 566d22a

Please sign in to comment.