Skip to content

Commit

Permalink
improve countingResourceEventHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
limhawjia committed Jul 12, 2023
1 parent 446f5e5 commit cf4a9a7
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 80 deletions.
64 changes: 64 additions & 0 deletions pkg/util/informermanager/federatedinformermanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,70 @@ import (
"github.com/onsi/gomega"
)

func TestFederatedInformerManager(t *testing.T) {
_ = gomega.NewWithT(t)

t.Run("clients for existing clusters should be available eventually", func(t *testing.T) {

})

t.Run("clients for new clusters should be available eventually", func(t *testing.T) {

})

t.Run("listers for existing FTCs and clusters should be available eventually", func(t *testing.T) {

})

t.Run("listers for new FTCs should be available eventually", func(t *testing.T) {

})

t.Run("listers for new clusteres should be available eventually", func(t *testing.T) {

})

t.Run("event handlers for existing FTCs should be registed eventually", func(t *testing.T) {

})

t.Run("event handlers for new FTCs should be registered eventually", func(t *testing.T) {

})

t.Run("event handlers for new clusters should be registered eventually", func(t *testing.T) {

})

t.Run("event handler should receive correct lastApplied and latest FTCs", func(t *testing.T) {

})

t.Run("event handler should be registered on FTC update", func(t *testing.T) {

})

t.Run("event handler should be unregistered on FTC update", func(t *testing.T) {

})

t.Run("event handler should be re-registered on FTC update", func(t *testing.T) {

})

t.Run("event handler should remain unchanged on FTC update", func(t *testing.T) {

})

t.Run("event handler should be unregistered on FTC deletion", func(t *testing.T) {

})

t.Run("event handler should be unregistered on cluster deletion", func(t *testing.T) {

})
}

// Verifies that clients for existing clusters are eventually available after the FederatedInformerManager is started.
func TestFederatedInformerManagerClientAvailableForExistingClusters(t *testing.T) {
g := gomega.NewGomegaWithT(t)
Expand Down
116 changes: 68 additions & 48 deletions pkg/util/informermanager/informermanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func TestInformerManager(t *testing.T) {
cm1 := getTestConfigMap("cm-1", "default")
sc1 := getTestSecret("sc-1", "default")

alwaysRegistered := &countingResourceEventHandler{}
neverRegistered := &countingResourceEventHandler{}
alwaysRegistered := newCountingResourceEventHandler()
neverRegistered := newCountingResourceEventHandler()

defaultFTCs := []*fedcorev1a1.FederatedTypeConfig{deploymentFTC, configmapFTC, secretFTC}
defaultObjs := []*unstructured.Unstructured{dp1, cm1, sc1}
Expand All @@ -137,8 +137,12 @@ func TestInformerManager(t *testing.T) {

// 2. Verify alwaysRegistered is eventually registered for all existing FTCs.

alwaysRegistered.ExpectGenerateEvents(3)
alwaysRegistered.ExpectAddEvents(3)
alwaysRegistered.ExpectGenerateEvents(deploymentFTC.Name, 1)
alwaysRegistered.ExpectGenerateEvents(configmapFTC.Name, 1)
alwaysRegistered.ExpectGenerateEvents(secretFTC.Name, 1)
alwaysRegistered.ExpectAddEvents(deploymentGVK, 1)
alwaysRegistered.ExpectAddEvents(configmapGVK, 1)
alwaysRegistered.ExpectAddEvents(secretGVK, 1)
alwaysRegistered.AssertEventually(g, time.Second*2)

// 3. Verify newly generated events are received by alwaysRegistered
Expand All @@ -147,16 +151,16 @@ func TestInformerManager(t *testing.T) {
Namespace("default").
Create(ctx, getTestSecret("sc-2", "default"), metav1.CreateOptions{})
g.Expect(err).ToNot(gomega.HaveOccurred())
alwaysRegistered.ExpectAddEvents(1)
alwaysRegistered.ExpectAddEvents(secretGVK, 1)

dp1.SetAnnotations(map[string]string{"test": "test"})
_, err = dynamicClient.Resource(common.DeploymentGVR).Namespace("default").Update(ctx, dp1, metav1.UpdateOptions{})
g.Expect(err).ToNot(gomega.HaveOccurred())
alwaysRegistered.ExpectUpdateEvents(1)
alwaysRegistered.ExpectUpdateEvents(deploymentGVK, 1)

err = dynamicClient.Resource(common.ConfigMapGVR).Namespace("default").Delete(ctx, cm1.GetName(), metav1.DeleteOptions{})
g.Expect(err).ToNot(gomega.HaveOccurred())
alwaysRegistered.ExpectDeleteEvents(1)
alwaysRegistered.ExpectDeleteEvents(configmapGVK, 1)

alwaysRegistered.AssertEventually(g, time.Second*2)

Expand Down Expand Up @@ -185,8 +189,8 @@ func TestInformerManager(t *testing.T) {
dm3 := getTestDaemonSet("dm-3", "default")
dm4 := getTestDaemonSet("dm-4", "default")

alwaysRegistered := &countingResourceEventHandler{}
neverRegistered := &countingResourceEventHandler{}
alwaysRegistered := newCountingResourceEventHandler()
neverRegistered := newCountingResourceEventHandler()

defaultFTCs := []*fedcorev1a1.FederatedTypeConfig{}
defaultObjs := []*unstructured.Unstructured{dm1, dm2, dm3, dm4}
Expand All @@ -210,20 +214,20 @@ func TestInformerManager(t *testing.T) {

// 3. Verify that alwaysRegistered is eventually registered for the new Daemonset FTC

alwaysRegistered.ExpectGenerateEvents(1)
alwaysRegistered.ExpectAddEvents(4)
alwaysRegistered.ExpectGenerateEvents(daemonsetFTC.Name, 1)
alwaysRegistered.ExpectAddEvents(daemonsetGVK, 4)
alwaysRegistered.AssertEventually(g, time.Second*2)

// 4. Verify that newly generated events are also received by alwaysRegistered

dm1.SetAnnotations(map[string]string{"test": "test"})
_, err = dynamicClient.Resource(common.DaemonSetGVR).Namespace("default").Update(ctx, dm1, metav1.UpdateOptions{})
g.Expect(err).ToNot(gomega.HaveOccurred())
alwaysRegistered.ExpectUpdateEvents(1)
alwaysRegistered.ExpectUpdateEvents(daemonsetGVK, 1)

err = dynamicClient.Resource(common.DaemonSetGVR).Namespace("default").Delete(ctx, dm4.GetName(), metav1.DeleteOptions{})
g.Expect(err).ToNot(gomega.HaveOccurred())
alwaysRegistered.ExpectDeleteEvents(1)
alwaysRegistered.ExpectDeleteEvents(daemonsetGVK, 1)

alwaysRegistered.AssertEventually(g, time.Second*2)

Expand Down Expand Up @@ -307,7 +311,7 @@ func TestInformerManager(t *testing.T) {
ftc := deploymentFTC.DeepCopy()
ftc.SetAnnotations(map[string]string{"predicate": "false", "generator": "true"})

handler := &countingResourceEventHandler{}
handler := newCountingResourceEventHandler()
generator := newAnnotationBasedGenerator(handler)

defaultFTCs := []*fedcorev1a1.FederatedTypeConfig{ftc}
Expand All @@ -328,25 +332,25 @@ func TestInformerManager(t *testing.T) {

// 4. Verify that handler is registered and additional events are received

handler.ExpectGenerateEvents(1)
handler.ExpectAddEvents(1)
handler.ExpectGenerateEvents(ftc.Name, 1)
handler.ExpectAddEvents(deploymentGVK, 1)

handler.AssertEventually(g, time.Second*2)

dp2, err := dynamicClient.Resource(common.DeploymentGVR).
Namespace("default").
Create(ctx, getTestDeployment("dp-2", "default"), metav1.CreateOptions{})
g.Expect(err).ToNot(gomega.HaveOccurred())
handler.ExpectAddEvents(1)
handler.ExpectAddEvents(deploymentGVK, 1)

dp2.SetAnnotations(map[string]string{"test-annotation": "test-value"})
dp2, err = dynamicClient.Resource(common.DeploymentGVR).Namespace("default").Update(ctx, dp2, metav1.UpdateOptions{})
g.Expect(err).ToNot(gomega.HaveOccurred())
handler.ExpectUpdateEvents(1)
handler.ExpectUpdateEvents(deploymentGVK, 1)

err = dynamicClient.Resource(common.DeploymentGVR).Namespace("default").Delete(ctx, dp2.GetName(), metav1.DeleteOptions{})
g.Expect(err).ToNot(gomega.HaveOccurred())
handler.ExpectDeleteEvents(1)
handler.ExpectDeleteEvents(deploymentGVK, 1)

handler.AssertEventually(g, time.Second*2)
})
Expand All @@ -362,7 +366,7 @@ func TestInformerManager(t *testing.T) {
ftc := deploymentFTC.DeepCopy()
ftc.SetAnnotations(map[string]string{"predicate": "true", "generator": "true"})

handler := &countingResourceEventHandler{}
handler := newCountingResourceEventHandler()
generator := newAnnotationBasedGenerator(handler)

defaultFTCs := []*fedcorev1a1.FederatedTypeConfig{ftc}
Expand All @@ -373,8 +377,8 @@ func TestInformerManager(t *testing.T) {

// 2. Verify that handler is registered initially.

handler.ExpectGenerateEvents(1)
handler.ExpectAddEvents(1)
handler.ExpectGenerateEvents(ftc.Name, 1)
handler.ExpectAddEvents(deploymentGVK, 1)
handler.AssertEventually(g, time.Second*2)

// 3. Update FTC to trigger unregistration
Expand Down Expand Up @@ -411,7 +415,7 @@ func TestInformerManager(t *testing.T) {
dp1 := getTestDeployment("dp-1", "default")
ftc := deploymentFTC.DeepCopy()

handler := &countingResourceEventHandler{}
handler := newCountingResourceEventHandler()
generator := &EventHandlerGenerator{
Predicate: alwaysRegisterPredicate,
Generator: handler.GenerateEventHandler,
Expand All @@ -425,8 +429,8 @@ func TestInformerManager(t *testing.T) {

// 2. Verify that handler is registered initially

handler.ExpectGenerateEvents(1)
handler.ExpectAddEvents(1)
handler.ExpectGenerateEvents(ftc.Name, 1)
handler.ExpectAddEvents(deploymentGVK, 1)
handler.AssertEventually(g, time.Second*2)

// 3. Trigger FTC updates and verify re-registration
Expand All @@ -435,15 +439,15 @@ func TestInformerManager(t *testing.T) {
_, err := fedClient.CoreV1alpha1().FederatedTypeConfigs().Update(ctx, ftc, metav1.UpdateOptions{})
g.Expect(err).ToNot(gomega.HaveOccurred())

handler.ExpectGenerateEvents(1)
handler.ExpectAddEvents(1)
handler.ExpectGenerateEvents(ftc.Name, 1)
handler.ExpectAddEvents(deploymentGVK, 1)
handler.AssertEventually(g, time.Second*2)

dp1.SetAnnotations(map[string]string{"test": "test"})
_, err = dynamicClient.Resource(common.DeploymentGVR).Namespace("default").Update(ctx, dp1, metav1.UpdateOptions{})
g.Expect(err).ToNot(gomega.HaveOccurred())

handler.ExpectUpdateEvents(1)
handler.ExpectUpdateEvents(deploymentGVK, 1)
handler.AssertEventually(g, time.Second*2)
})

Expand All @@ -456,7 +460,7 @@ func TestInformerManager(t *testing.T) {
dp1 := getTestDeployment("dp-1", "default")
ftc := deploymentFTC.DeepCopy()

handler := &countingResourceEventHandler{}
handler := newCountingResourceEventHandler()
generator := &EventHandlerGenerator{
Predicate: registerOncePredicate,
Generator: handler.GenerateEventHandler,
Expand All @@ -470,8 +474,8 @@ func TestInformerManager(t *testing.T) {

// 2. Verify that handler is registered initially

handler.ExpectGenerateEvents(1)
handler.ExpectAddEvents(1)
handler.ExpectGenerateEvents(ftc.Name, 1)
handler.ExpectAddEvents(deploymentGVK, 1)
handler.AssertEventually(g, time.Second*2)

// 3. Trigger FTC updates and verify no re-registration
Expand All @@ -488,7 +492,7 @@ func TestInformerManager(t *testing.T) {
_, err = dynamicClient.Resource(common.DeploymentGVR).Namespace("default").Update(ctx, dp1, metav1.UpdateOptions{})
g.Expect(err).ToNot(gomega.HaveOccurred())

handler.ExpectUpdateEvents(1)
handler.ExpectUpdateEvents(deploymentGVK, 1)
handler.AssertEventually(g, time.Second*2)
})

Expand All @@ -502,8 +506,8 @@ func TestInformerManager(t *testing.T) {
cm1 := getTestConfigMap("cm-1", "default")
sc1 := getTestSecret("sc-1", "default")

handler1 := &countingResourceEventHandler{}
handler2 := &countingResourceEventHandler{}
handler1 := newCountingResourceEventHandler()
handler2 := newCountingResourceEventHandler()
generator1 := &EventHandlerGenerator{
Predicate: registerOncePredicate,
Generator: handler1.GenerateEventHandler,
Expand All @@ -521,12 +525,20 @@ func TestInformerManager(t *testing.T) {

// 2. Verify that handler1 and handler2 is registered initially for all FTCs

handler1.ExpectGenerateEvents(3)
handler1.ExpectAddEvents(3)
handler1.ExpectGenerateEvents(deploymentFTC.Name, 1)
handler1.ExpectGenerateEvents(configmapFTC.Name, 1)
handler1.ExpectGenerateEvents(secretFTC.Name, 1)
handler1.ExpectAddEvents(deploymentGVK, 1)
handler1.ExpectAddEvents(configmapGVK, 1)
handler1.ExpectAddEvents(secretGVK, 1)
handler1.AssertEventually(g, time.Second*2)

handler2.ExpectGenerateEvents(3)
handler2.ExpectAddEvents(3)
handler2.ExpectGenerateEvents(deploymentFTC.Name, 1)
handler2.ExpectGenerateEvents(configmapFTC.Name, 1)
handler2.ExpectGenerateEvents(secretFTC.Name, 1)
handler2.ExpectAddEvents(deploymentGVK, 1)
handler2.ExpectAddEvents(configmapGVK, 1)
handler2.ExpectAddEvents(secretGVK, 1)
handler2.AssertEventually(g, time.Second*2)

// 3. Delete the deployment FTC
Expand Down Expand Up @@ -559,14 +571,14 @@ func TestInformerManager(t *testing.T) {
Namespace("default").
Create(ctx, getTestSecret("sc-2", "default"), metav1.CreateOptions{})
g.Expect(err).ToNot(gomega.HaveOccurred())
handler1.ExpectAddEvents(1)
handler2.ExpectAddEvents(1)
handler1.ExpectAddEvents(secretGVK, 1)
handler2.ExpectAddEvents(secretGVK, 1)

cm1.SetAnnotations(map[string]string{"test": "test"})
_, err = dynamicClient.Resource(common.ConfigMapGVR).Namespace("default").Update(ctx, cm1, metav1.UpdateOptions{})
g.Expect(err).ToNot(gomega.HaveOccurred())
handler1.ExpectUpdateEvents(1)
handler2.ExpectUpdateEvents(1)
handler1.ExpectUpdateEvents(configmapGVK, 1)
handler2.ExpectUpdateEvents(configmapGVK, 1)

handler1.AssertEventually(g, time.Second*2)
handler2.AssertEventually(g, time.Second*2)
Expand All @@ -582,8 +594,8 @@ func TestInformerManager(t *testing.T) {
cm1 := getTestConfigMap("cm-1", "default")
sc1 := getTestSecret("sc-1", "default")

handler1 := &countingResourceEventHandler{}
handler2 := &countingResourceEventHandler{}
handler1 := newCountingResourceEventHandler()
handler2 := newCountingResourceEventHandler()
generator1 := &EventHandlerGenerator{
Predicate: registerOncePredicate,
Generator: handler1.GenerateEventHandler,
Expand All @@ -602,12 +614,20 @@ func TestInformerManager(t *testing.T) {

// 2. Verify that handler1 and handler2 is registered initially for all FTCs

handler1.ExpectGenerateEvents(3)
handler1.ExpectAddEvents(3)
handler1.ExpectGenerateEvents(deploymentFTC.Name, 1)
handler1.ExpectGenerateEvents(configmapFTC.Name, 1)
handler1.ExpectGenerateEvents(secretFTC.Name, 1)
handler1.ExpectAddEvents(deploymentGVK, 1)
handler1.ExpectAddEvents(configmapGVK, 1)
handler1.ExpectAddEvents(secretGVK, 1)
handler1.AssertEventually(g, time.Second*2)

handler2.ExpectGenerateEvents(3)
handler2.ExpectAddEvents(3)
handler2.ExpectGenerateEvents(deploymentFTC.Name, 1)
handler2.ExpectGenerateEvents(configmapFTC.Name, 1)
handler2.ExpectGenerateEvents(secretFTC.Name, 1)
handler2.ExpectAddEvents(deploymentGVK, 1)
handler2.ExpectAddEvents(configmapGVK, 1)
handler2.ExpectAddEvents(secretGVK, 1)
handler2.AssertEventually(g, time.Second*2)

// 3. Shutdown the manager
Expand Down
Loading

0 comments on commit cf4a9a7

Please sign in to comment.