Skip to content

Commit

Permalink
Tech-Debt: Increase coverage
Browse files Browse the repository at this point in the history
- 100% coverage for ClientDiscoveryHandler

Signed-off-by: Adil Fulara <[email protected]>
  • Loading branch information
Adil Fulara committed Dec 25, 2024
1 parent 5ecf834 commit 786e3dc
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 19 deletions.
86 changes: 70 additions & 16 deletions admiral/pkg/clusters/clientdiscovery_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestClientDiscoveryHandler_Added(t *testing.T) {
readOnly bool
disableCache bool
admiralReadyOnly bool
depCtrlAddErr error
}

type want struct {
Expand All @@ -61,8 +62,7 @@ func TestClientDiscoveryHandler_Added(t *testing.T) {
{
name: "No global identifier on obj results in no errors",
args: args{
obj: &common.K8sObject{Annotations: map[string]string{}, Labels: map[string]string{}},
options: []Option{argoEnabled(true), swAwareNsCache(true), dependencyProcessing(true)},
obj: &common.K8sObject{Annotations: map[string]string{}, Labels: map[string]string{}},
},
want: want{},
},
Expand All @@ -77,7 +77,6 @@ func TestClientDiscoveryHandler_Added(t *testing.T) {
"admiral.io/identityPartition": "test",
},
},
options: []Option{argoEnabled(true), swAwareNsCache(true), dependencyProcessing(true)},
disableCache: true,
},
want: want{
Expand All @@ -95,14 +94,36 @@ func TestClientDiscoveryHandler_Added(t *testing.T) {
"admiral.io/identityPartition": "test",
},
},
options: []Option{argoEnabled(true), swAwareNsCache(true), dependencyProcessing(true)},
options: []Option{argoEnabled(true), swAwareNsCache(true)},
readOnly: true,
},
want: want{
identityClusterNSCacheLen: 1,
partitionIdentityCacheLen: 1,
},
},
{
name: "Admiral during cache warmup returns early",
args: args{
obj: &common.K8sObject{
Namespace: namespaceForRollout,
Name: "obj-rollout-namespace",
Annotations: map[string]string{"identity": identity1},
Labels: map[string]string{
"admiral.io/identityPartition": "test",
},
},
options: []Option{
argoEnabled(true),
swAwareNsCache(true),
cacheWarmupTime(1 * time.Minute),
},
},
want: want{
identityClusterNSCacheLen: 1,
partitionIdentityCacheLen: 1,
},
},
{
name: "Rollout with same identifier present in the namespace should " +
"return without processing and no errors",
Expand Down Expand Up @@ -144,7 +165,8 @@ func TestClientDiscoveryHandler_Added(t *testing.T) {
identityClusterNSCacheLen: 1,
partitionIdentityCacheLen: 1,
},
}, {
},
{
name: "Valid client with no dependency record returns without processing",
args: args{
obj: &common.K8sObject{
Expand All @@ -155,14 +177,17 @@ func TestClientDiscoveryHandler_Added(t *testing.T) {
"admiral.io/identityPartition": "test",
},
},
options: []Option{argoEnabled(true), swAwareNsCache(true), dependencyProcessing(true)},
options: []Option{
argoEnabled(true),
swAwareNsCache(true)},
},
want: want{
identityClusterNSCacheLen: 1,
partitionIdentityCacheLen: 1,
},
}, {
name: "Valid client with valid dependency record results in writing all dependencies endpoints to this cluster",
},
{
name: "Valid client with valid dependency record results in calling depHandler Added()",
args: args{
obj: &common.K8sObject{
Namespace: namespace,
Expand All @@ -172,7 +197,9 @@ func TestClientDiscoveryHandler_Added(t *testing.T) {
"admiral.io/identityPartition": "test",
},
},
options: []Option{argoEnabled(true), swAwareNsCache(true), dependencyProcessing(true)},
options: []Option{
argoEnabled(true),
},
dep: &v1alpha1.Dependency{
Spec: model.Dependency{
Source: identity1,
Expand All @@ -181,9 +208,34 @@ func TestClientDiscoveryHandler_Added(t *testing.T) {
},
},
want: want{
identityClusterNSCacheLen: 1,
partitionIdentityCacheLen: 1,
depCtrlAddCalled: 1,
depCtrlAddCalled: 1,
},
},
{
name: "Valid client with valid dependency record with error in calling depHandler Added() returns error",
args: args{
obj: &common.K8sObject{
Namespace: namespace,
Name: "obj",
Annotations: map[string]string{"identity": identity1},
Labels: map[string]string{
"admiral.io/identityPartition": "test",
},
},
options: []Option{
argoEnabled(true),
},
dep: &v1alpha1.Dependency{
Spec: model.Dependency{
Source: identity1,
Destinations: []string{identity2},
},
},
depCtrlAddErr: errors.New("oops"),
},
want: want{
depCtrlAddCalled: 1,
err: errors.New("oops"),
},
},
}
Expand All @@ -196,7 +248,7 @@ func TestClientDiscoveryHandler_Added(t *testing.T) {
ctx := context.Background()
InitAdmiralForTesting(c.args.options...)

mockDepCtrl := &test.MockDependencyHandler{}
mockDepCtrl := test.NewMockDependencyHandler(c.args.depCtrlAddErr, nil, nil)
depController, err := admiral.NewDependencyController(stop, mockDepCtrl, loader.FakeKubeconfigPath, "", time.Second*time.Duration(300), loader.GetFakeClientLoader())
if err != nil {
t.Fatalf("failed to initialize dependency controller, err: %v", err)
Expand Down Expand Up @@ -246,9 +298,7 @@ func TestClientDiscoveryHandler_Added(t *testing.T) {
PartitionIdentityCache: common.NewMap(),
}

if c.args.readOnly {
commonUtil.CurrentAdmiralState.ReadOnly = true
}
commonUtil.CurrentAdmiralState.ReadOnly = c.args.readOnly
if c.args.disableCache {
rr.AdmiralCache = nil
}
Expand All @@ -270,6 +320,10 @@ func TestClientDiscoveryHandler_Added(t *testing.T) {
t.Fatalf("Expected error '%v' got 'nil'", c.want.err)
}
}

if c.want.depCtrlAddCalled > 0 {
assert.Equal(t, c.want.depCtrlAddCalled, mockDepCtrl.AddedCnt)
}
})
}
}
6 changes: 6 additions & 0 deletions admiral/pkg/clusters/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func dependencyProcessing(enabled bool) Option {
}
}

func cacheWarmupTime(duration time.Duration) Option {
return func(params *common.AdmiralParams) {
params.CacheReconcileDuration = duration
}
}

func InitAdmiralForTesting(options ...Option) {
common.ResetSync()
params := common.AdmiralParams{
Expand Down
23 changes: 20 additions & 3 deletions admiral/pkg/test/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,35 @@ func (m *MockNodeHandler) Deleted(obj *k8sCoreV1.Node) {
}

type MockDependencyHandler struct {
AddedCnt int
UpdatedCnt int
DeletedCnt int
addedErr error
updatedErr error
deletedErr error
}

func NewMockDependencyHandler(addErr error, updateErr error, deleteError error) *MockDependencyHandler {
return &MockDependencyHandler{
addedErr: addErr,
updatedErr: updateErr,
deletedErr: deleteError,
}
}

func (m *MockDependencyHandler) Added(ctx context.Context, obj *admiralV1.Dependency) error {
return nil
m.AddedCnt++
return m.addedErr
}

func (m *MockDependencyHandler) Updated(ctx context.Context, obj *admiralV1.Dependency) error {
return nil
m.UpdatedCnt++
return m.updatedErr
}

func (m *MockDependencyHandler) Deleted(ctx context.Context, obj *admiralV1.Dependency) error {
return nil
m.DeletedCnt++
return m.deletedErr
}

type MockGlobalTrafficHandler struct {
Expand Down

0 comments on commit 786e3dc

Please sign in to comment.