Skip to content

Commit

Permalink
chore: watch error handler test (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
decleaver authored Oct 17, 2024
1 parent 010a320 commit a4fc5a2
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions pkg/api/resources/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
dynamicFake "k8s.io/client-go/dynamic/fake"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/cache"
)

func TestBindCoreResources(t *testing.T) {
Expand Down Expand Up @@ -463,3 +464,50 @@ func TestSimpleAndDynamicClient(t *testing.T) {
require.Equal(t, c.Pods.GetResources("default", mockPodName)[0].GetName(), mockPod.Name)
require.Equal(t, c.UDSPackages.GetResources("", mockUDSPackageName)[0].GetName(), mockUDSPackage.GetName())
}

func TestSetWatchErrorHandler(t *testing.T) {
// Create a ResourceList for CRDs
crdGVR := schema.GroupVersionResource{Group: "apiextensions.k8s.io", Version: "v1", Resource: "customresourcedefinitions"}
crdResource := &ResourceList{
Resources: make(map[string]*unstructured.Unstructured),
SparseResources: make(map[string]*unstructured.Unstructured),
Changes: make(chan struct{}, 1),
GVR: crdGVR,
CRDExists: true,
}

c := &Cache{
CRDs: crdResource,
}

mockInformer := &mockSharedIndexInformer{
setWatchErrorHandlerFunc: func(handler cache.WatchErrorHandler) error {
// Immediately call the handler to simulate a watch error
handler(nil, nil)
return nil
},
}

c.setWatchErrorHandler(mockInformer, crdResource)

// Confirm SetWatchErrorHandler sets CRDExists to false
require.False(t, crdResource.CRDExists)

// Check if a change was sent to the Changes channel
select {
case <-crdResource.Changes:
// This is the expected behavior
case <-time.After(time.Second):
t.Errorf("Expected a change to be sent to the Changes channel, but none was received")
}
}

// Mock SharedIndexInformer for testing
type mockSharedIndexInformer struct {
cache.SharedIndexInformer
setWatchErrorHandlerFunc func(handler cache.WatchErrorHandler) error
}

func (m *mockSharedIndexInformer) SetWatchErrorHandler(handler cache.WatchErrorHandler) error {
return m.setWatchErrorHandlerFunc(handler)
}

0 comments on commit a4fc5a2

Please sign in to comment.