Skip to content

Commit

Permalink
Add first test and structure code
Browse files Browse the repository at this point in the history
  • Loading branch information
hubeadmin committed Mar 13, 2020
1 parent 7d5c3b3 commit 110c59a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
4 changes: 1 addition & 3 deletions cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package main

import (
"fmt"
"os"

"github.com/sirupsen/logrus"

"github.com/spf13/cobra"
"os"
)

const (
Expand Down
99 changes: 99 additions & 0 deletions pkg/aws/elasticache_snapshot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package aws

import (
"github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi"
"github.com/integr8ly/cluster-service/pkg/clusterservice"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

//"reflect"
"testing"
)

func TestElasticacheSnapshotEngine_DeleteResourcesForCluster(t *testing.T) {
fakeClusterId := fakeClusterID
fakeLogger, err := fakeLogger(func(l *logrus.Entry) error {
return nil
})
if err != nil {
t.Fatal(err)
}

type fields struct {
elasticacheClient func() *elasticacheClientMock
taggingClient func() *taggingClientMock
logger *logrus.Entry
}
type args struct {
clusterId string
tags map[string]string
dryRun bool
}
tests := []struct {
name string
fields fields
args args
want []*clusterservice.ReportItem
wantFn func(mock *elasticacheClientMock) error
wantErr string
}{
{
name: "error when describing clusters fail",
fields: fields{
elasticacheClient: func() *elasticacheClientMock {
fakeClient, err := fakeElasticacheClient(func(c *elasticacheClientMock) error {
return nil
})
if err != nil {
t.Fatal(err)
}
return fakeClient
},
taggingClient: func() *taggingClientMock {
fakeTaggingClient, err := fakeResourcetaggingClient(func(c *taggingClientMock) error {
c.GetResourcesFunc = func(in1 *resourcegroupstaggingapi.GetResourcesInput) (output *resourcegroupstaggingapi.GetResourcesOutput, e error) {
return nil, errors.New("")
}
return nil
})
if err != nil {
t.Fatal(err)
}

return fakeTaggingClient
},
logger: fakeLogger,
},
args: args{
clusterId: fakeClusterId,
dryRun: true,
},
wantErr: "failed to describe cache clusters: ",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fakeClient := tt.fields.elasticacheClient()
r := &ElasticacheEngine{
elasticacheClient: fakeClient,
taggingClient: tt.fields.taggingClient(),
logger: tt.fields.logger,
}
got, err := r.DeleteResourcesForCluster(tt.args.clusterId, nil, tt.args.dryRun)
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)
}
}
})
}
}

0 comments on commit 110c59a

Please sign in to comment.