-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package informer | ||
|
||
import ( | ||
"fmt" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/tools/cache" | ||
) | ||
|
||
var _ = Describe("Informer Manager Suite", func() { | ||
Context("add then remove", Ordered, func() { | ||
It("add one dynamic resource", func() { | ||
//Create a resource event handler | ||
handler := &cache.ResourceEventHandlerFuncs{ | ||
AddFunc: func(obj interface{}) {}, | ||
UpdateFunc: func(oldObj, newObj interface{}) {}, | ||
DeleteFunc: func(obj interface{}) {}, | ||
} | ||
|
||
// Create a dynamic resource | ||
dynResource := APIResourceMeta{ | ||
GroupVersionKind: schema.GroupVersionKind{ | ||
Group: "example.com", | ||
Version: "v1", | ||
Kind: "ExampleResource", | ||
}, | ||
GroupVersionResource: schema.GroupVersionResource{ | ||
Group: "example.com", | ||
Version: "v1", | ||
Resource: "exampleresources", | ||
}, | ||
IsClusterScoped: true, | ||
isStaticResource: false, | ||
Registration: nil, | ||
} | ||
|
||
// Make sure that the add method returns with no errors | ||
//Check that the dynamic resource was added to the informer manager | ||
impl.AddDynamicResources([]APIResourceMeta{dynResource}, handler, true) | ||
if _, ok := impl.apiResources[dynResource.GroupVersionKind]; !ok { | ||
fmt.Errorf("Expected dynamic resource %v to be added to informer manager", dynResource) | ||
Check failure on line 42 in pkg/utils/informer/informermanager_test.go GitHub Actions / Lint
Check failure on line 42 in pkg/utils/informer/informermanager_test.go GitHub Actions / unit-tests
|
||
} | ||
addedResource := impl.apiResources[dynResource.GroupVersionKind] | ||
|
||
// Check that the informer was created for the dynamic resource | ||
informer := impl.informerFactory.ForResource(addedResource.GroupVersionResource).Informer() | ||
if informer == nil { | ||
fmt.Errorf("Expected informer to be created for resource %v", addedResource) | ||
Check failure on line 49 in pkg/utils/informer/informermanager_test.go GitHub Actions / Lint
Check failure on line 49 in pkg/utils/informer/informermanager_test.go GitHub Actions / unit-tests
|
||
} | ||
|
||
// Remove the dynamic resource from the informer manager | ||
impl.AddDynamicResources([]APIResourceMeta{}, handler, true) | ||
|
||
// Verify the map. Check that the dynamic resource was removed from the informer manager | ||
if _, ok := impl.apiResources[dynResource.GroupVersionKind]; ok { | ||
fmt.Errorf("Expected dynamic resource %v to be removed from informer manager", dynResource) | ||
Check failure on line 57 in pkg/utils/informer/informermanager_test.go GitHub Actions / Lint
Check failure on line 57 in pkg/utils/informer/informermanager_test.go GitHub Actions / unit-tests
|
||
} | ||
}) | ||
It("multiple dynamic resources", func() { | ||
handler := &cache.ResourceEventHandlerFuncs{ | ||
AddFunc: func(obj interface{}) {}, | ||
UpdateFunc: func(oldObj, newObj interface{}) {}, | ||
DeleteFunc: func(obj interface{}) {}, | ||
} | ||
|
||
// Create dynamic resources | ||
dynResources := []APIResourceMeta{ | ||
{ | ||
GroupVersionKind: schema.GroupVersionKind{ | ||
Group: "example.com", | ||
Version: "v1", | ||
Kind: "ExampleResource", | ||
}, | ||
GroupVersionResource: schema.GroupVersionResource{ | ||
Group: "example.com", | ||
Version: "v1", | ||
Resource: "exampleresources", | ||
}, | ||
IsClusterScoped: true, | ||
isStaticResource: false, | ||
Registration: nil, | ||
}, | ||
{ | ||
GroupVersionKind: schema.GroupVersionKind{ | ||
Group: "anotherexample.com", | ||
Version: "v1", | ||
Kind: "AnotherExampleResource", | ||
}, | ||
GroupVersionResource: schema.GroupVersionResource{ | ||
Group: "anotherexample.com", | ||
Version: "v1", | ||
Resource: "anotherexampleresources", | ||
}, | ||
IsClusterScoped: true, | ||
isStaticResource: false, | ||
Registration: nil, | ||
}, | ||
} | ||
|
||
// Make sure that the add method returns with no errors | ||
// Check that the dynamic resources were added to the informer manager | ||
impl.AddDynamicResources([]APIResourceMeta{dynResources[0]}, handler, false) | ||
impl.AddDynamicResources(dynResources, handler, true) | ||
for _, dynResource := range dynResources { | ||
if _, ok := impl.apiResources[dynResource.GroupVersionKind]; !ok { | ||
fmt.Errorf("Expected dynamic resource %v to be added to informer manager", dynResource) | ||
Check failure on line 107 in pkg/utils/informer/informermanager_test.go GitHub Actions / unit-tests
|
||
} | ||
} | ||
|
||
// Check that the informer was created for the dynamic resources | ||
for _, dynResource := range dynResources { | ||
informer := impl.informerFactory.ForResource(dynResource.GroupVersionResource).Informer() | ||
if informer == nil { | ||
fmt.Errorf("Expected informer to be created for resource %v", dynResource) | ||
Check failure on line 115 in pkg/utils/informer/informermanager_test.go GitHub Actions / unit-tests
|
||
} | ||
} | ||
|
||
// Remove the dynamic resource from the informer manager | ||
impl.AddDynamicResources([]APIResourceMeta{dynResources[0]}, handler, true) | ||
|
||
// verify the map. Check that the dynamic resource was removed from the informer manager | ||
if _, ok := impl.apiResources[dynResources[1].GroupVersionKind]; ok { | ||
fmt.Errorf("Expected dynamic resource %v to be removed from informer manager", dynResources[1]) | ||
Check failure on line 124 in pkg/utils/informer/informermanager_test.go GitHub Actions / unit-tests
|
||
} | ||
// Remove the dynamic resource from the informer manager | ||
impl.AddDynamicResources([]APIResourceMeta{}, handler, true) | ||
|
||
// verify the map. Check that the dynamic resource was removed from the informer manager | ||
if _, ok := impl.apiResources[dynResources[0].GroupVersionKind]; ok { | ||
fmt.Errorf("Expected dynamic resource %v to be removed from informer manager", dynResources[0]) | ||
Check failure on line 131 in pkg/utils/informer/informermanager_test.go GitHub Actions / unit-tests
|
||
} | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
Copyright (c) Microsoft Corporation. | ||
Licensed under the MIT license. | ||
*/ | ||
|
||
package informer | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/dynamic" | ||
"k8s.io/client-go/dynamic/dynamicinformer" | ||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
) | ||
|
||
var ( | ||
testEnv *envtest.Environment | ||
//dynamicClient dynamic.DynamicClient | ||
impl *informerManagerImpl | ||
informerMgr Manager | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
) | ||
|
||
func TestAPIs(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
|
||
RunSpecs(t, "Informer Manager Suite") | ||
} | ||
|
||
var _ = BeforeSuite(func() { | ||
klog.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) | ||
|
||
ctx, cancel = context.WithCancel(context.TODO()) | ||
|
||
By("bootstrapping test environment") | ||
|
||
// Start the test environment. | ||
testEnv = &envtest.Environment{} | ||
restCfg, err := testEnv.Start() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(restCfg).NotTo(BeNil()) | ||
|
||
// Setup dynamic client for informer manager | ||
dynamicClient, err := dynamic.NewForConfig(restCfg) | ||
Expect(dynamicClient).NotTo(BeNil()) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
impl = &informerManagerImpl{ | ||
dynamicClient: dynamicClient, | ||
ctx: ctx, | ||
cancel: cancel, | ||
informerFactory: dynamicinformer.NewDynamicSharedInformerFactory(dynamicClient, time.Minute*5), | ||
apiResources: make(map[schema.GroupVersionKind]*APIResourceMeta), | ||
} | ||
|
||
informerMgr = impl | ||
defer func() { | ||
informerMgr.Start() | ||
}() | ||
|
||
informerMgr.WaitForCacheSync() | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
defer klog.Flush() | ||
cancel() | ||
|
||
By("Tearing down the test environment") | ||
Expect(testEnv.Stop()).Should(Succeed(), "Failed to stop test environment") | ||
}) |