Skip to content

Commit

Permalink
Add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
britaniar committed Mar 11, 2024
1 parent d826166 commit 6f7e619
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/utils/informer/informermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package informer
import (
"context"
"fmt"
"k8s.io/klog/v2"

"sync"
"time"
Expand All @@ -16,7 +17,6 @@ import (
"k8s.io/client-go/dynamic"
"k8s.io/client-go/dynamic/dynamicinformer"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
)

// InformerManager manages dynamic shared informer for all resources, include Kubernetes resource and
Expand Down Expand Up @@ -126,7 +126,7 @@ func (s *informerManagerImpl) AddDynamicResources(dynResources []APIResourceMeta
panic(err)
}
s.apiResources[newRes.GroupVersionKind] = &newRes
klog.InfoS("Added an informer for a new resource", "res", newRes)
klog.InfoS("Added an informer for a new resource", "res", s.apiResources[newRes.GroupVersionKind])
}
}

Expand Down
135 changes: 135 additions & 0 deletions pkg/utils/informer/informermanager_test.go
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

View workflow job for this annotation

GitHub Actions / Lint

unusedresult: result of fmt.Errorf call not used (govet)

Check failure on line 42 in pkg/utils/informer/informermanager_test.go

View workflow job for this annotation

GitHub Actions / unit-tests

result of fmt.Errorf call not used

Check failure on line 42 in pkg/utils/informer/informermanager_test.go

View workflow job for this annotation

GitHub Actions / staticcheck

Errorf doesn't have side effects and its return value is ignored (SA4017)
}
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

View workflow job for this annotation

GitHub Actions / Lint

unusedresult: result of fmt.Errorf call not used (govet)

Check failure on line 49 in pkg/utils/informer/informermanager_test.go

View workflow job for this annotation

GitHub Actions / unit-tests

result of fmt.Errorf call not used

Check failure on line 49 in pkg/utils/informer/informermanager_test.go

View workflow job for this annotation

GitHub Actions / staticcheck

Errorf doesn't have side effects and its return value is ignored (SA4017)
}

// 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

View workflow job for this annotation

GitHub Actions / Lint

unusedresult: result of fmt.Errorf call not used (govet)

Check failure on line 57 in pkg/utils/informer/informermanager_test.go

View workflow job for this annotation

GitHub Actions / unit-tests

result of fmt.Errorf call not used

Check failure on line 57 in pkg/utils/informer/informermanager_test.go

View workflow job for this annotation

GitHub Actions / staticcheck

Errorf doesn't have side effects and its return value is ignored (SA4017)
}
})
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

View workflow job for this annotation

GitHub Actions / unit-tests

result of fmt.Errorf call not used

Check failure on line 107 in pkg/utils/informer/informermanager_test.go

View workflow job for this annotation

GitHub Actions / staticcheck

Errorf doesn't have side effects and its return value is ignored (SA4017)
}
}

// 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

View workflow job for this annotation

GitHub Actions / unit-tests

result of fmt.Errorf call not used

Check failure on line 115 in pkg/utils/informer/informermanager_test.go

View workflow job for this annotation

GitHub Actions / staticcheck

Errorf doesn't have side effects and its return value is ignored (SA4017)
}
}

// 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

View workflow job for this annotation

GitHub Actions / unit-tests

result of fmt.Errorf call not used

Check failure on line 124 in pkg/utils/informer/informermanager_test.go

View workflow job for this annotation

GitHub Actions / staticcheck

Errorf doesn't have side effects and its return value is ignored (SA4017)
}
// 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

View workflow job for this annotation

GitHub Actions / unit-tests

result of fmt.Errorf call not used

Check failure on line 131 in pkg/utils/informer/informermanager_test.go

View workflow job for this annotation

GitHub Actions / staticcheck

Errorf doesn't have side effects and its return value is ignored (SA4017)
}
})
})
})
79 changes: 79 additions & 0 deletions pkg/utils/informer/suite_test.go
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")
})

0 comments on commit 6f7e619

Please sign in to comment.