Skip to content

Commit

Permalink
Tech-Debt: Increase coverage
Browse files Browse the repository at this point in the history
- Coverage for ShardController_Get

Signed-off-by: Adil Fulara <adil_fulara@intuit.com>
  • Loading branch information
Adil Fulara committed Jan 2, 2025
1 parent a52f567 commit 640ebff
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions admiral/pkg/controller/admiral/shard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"errors"
"fmt"
admiralapiv1 "github.com/istio-ecosystem/admiral-api/pkg/apis/admiral/v1"
fake2 "github.com/istio-ecosystem/admiral-api/pkg/client/clientset/versioned/typed/admiral/v1/fake"
"github.com/istio-ecosystem/admiral/admiral/pkg/client/loader"
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/common"
"github.com/istio-ecosystem/admiral/admiral/pkg/test"
"github.com/stretchr/testify/assert"
coreV1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
k8stesting "k8s.io/client-go/testing"
"testing"
)

Expand Down Expand Up @@ -103,6 +106,85 @@ func TestShardController_Deleted(t *testing.T) {
}
}

func TestShardController_Get(t *testing.T) {

type args struct {
obj interface{}
retry bool
cacheItems map[string]*admiralapiv1.Shard
fakeClientResponse *admiralapiv1.Shard
}

type want struct {
obj interface{}
err error
}
testCases := []struct {
name string
args args
want want
}{
{
name: "K8s client not initialized returns error",
args: args{
obj: nil,
},
want: want{
err: fmt.Errorf("kubernetes client is not initialized, txId=test"),
},
},
{
name: "lookup with retry",
args: args{
obj: &admiralapiv1.Shard{ObjectMeta: v1.ObjectMeta{Name: "test"}},
retry: true,
cacheItems: map[string]*admiralapiv1.Shard{
"test": {ObjectMeta: v1.ObjectMeta{Name: "test"}},
},
},
want: want{
obj: &admiralapiv1.Shard{ObjectMeta: v1.ObjectMeta{Name: "test"}},
},
},
{
name: "lookup without retry",
args: args{
obj: &admiralapiv1.Shard{ObjectMeta: v1.ObjectMeta{Name: "test"}},
cacheItems: map[string]*admiralapiv1.Shard{
"test": {ObjectMeta: v1.ObjectMeta{Name: "test"}},
},
fakeClientResponse: &admiralapiv1.Shard{ObjectMeta: v1.ObjectMeta{Name: "test"}},
},
want: want{
obj: &admiralapiv1.Shard{ObjectMeta: v1.ObjectMeta{Name: "test"}},
},
},
}
for _, c := range testCases {
t.Run(c.name, func(t *testing.T) {
sCtrl, _ := getNewMockShardController()
if c.args.fakeClientResponse != nil {
sCtrl.CrdClient.AdmiralV1().(*fake2.FakeAdmiralV1).PrependReactor("get", "*", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
return true, c.args.fakeClientResponse, nil
})
}
for _, shard := range c.args.cacheItems {
sCtrl.Cache.UpdateShardToClusterCache("test", shard)
}
ctx := context.WithValue(context.Background(), "txId", "test")
r, err := sCtrl.Get(ctx, c.args.retry, c.args.obj)
if c.want.err == nil {
assert.Nil(t, err)
}
if c.want.err != nil && assert.Error(t, err) {
assert.Equal(t, c.want.err, err)
}
assert.Equal(t, c.want.obj, r)
})
}

}

func getNewMockShardController() (*ShardController, error) {
shardHandler := test.MockShardHandler{}
shardController, err := NewShardController(context.Background().Done(), &shardHandler, "../../test/resources/admins@fake-cluster.k8s.local", "test-ns", 0, loader.GetFakeClientLoader())
Expand Down

0 comments on commit 640ebff

Please sign in to comment.