@@ -32,6 +32,99 @@ import (
32
32
"k8s.io/client-go/tools/cache"
33
33
)
34
34
35
+ type triggerFunc func (gvr schema.GroupVersionResource , ns string , fakeClient * fake.FakeDynamicClient , testObject * unstructured.Unstructured ) * unstructured.Unstructured
36
+
37
+ func triggerFactory (t * testing.T ) triggerFunc {
38
+ return func (gvr schema.GroupVersionResource , ns string , fakeClient * fake.FakeDynamicClient , _ * unstructured.Unstructured ) * unstructured.Unstructured {
39
+ testObject := newUnstructured ("apps/v1" , "Deployment" , "ns-foo" , "name-foo" )
40
+ createdObj , err := fakeClient .Resource (gvr ).Namespace (ns ).Create (context .TODO (), testObject , metav1.CreateOptions {})
41
+ if err != nil {
42
+ t .Error (err )
43
+ }
44
+ return createdObj
45
+ }
46
+ }
47
+
48
+ func handler (rcvCh chan <- * unstructured.Unstructured ) * cache.ResourceEventHandlerFuncs {
49
+ return & cache.ResourceEventHandlerFuncs {
50
+ AddFunc : func (obj interface {}) {
51
+ rcvCh <- obj .(* unstructured.Unstructured )
52
+ },
53
+ }
54
+ }
55
+
56
+ func TestFilteredDynamicSharedInformerFactory (t * testing.T ) {
57
+ scenarios := []struct {
58
+ name string
59
+ existingObj * unstructured.Unstructured
60
+ gvr schema.GroupVersionResource
61
+ informNS string
62
+ ns string
63
+ trigger func (gvr schema.GroupVersionResource , ns string , fakeClient * fake.FakeDynamicClient , testObject * unstructured.Unstructured ) * unstructured.Unstructured
64
+ handler func (rcvCh chan <- * unstructured.Unstructured ) * cache.ResourceEventHandlerFuncs
65
+ }{
66
+ // scenario 1
67
+ {
68
+ name : "scenario 1: test adding an object in different namespace should not trigger AddFunc" ,
69
+ informNS : "ns-bar" ,
70
+ ns : "ns-foo" ,
71
+ gvr : schema.GroupVersionResource {Group : "apps" , Version : "v1" , Resource : "deployments" },
72
+ trigger : triggerFactory (t ),
73
+ handler : handler ,
74
+ },
75
+ // scenario 2
76
+ {
77
+ name : "scenario 2: test adding an object should trigger AddFunc" ,
78
+ informNS : "ns-foo" ,
79
+ ns : "ns-foo" ,
80
+ gvr : schema.GroupVersionResource {Group : "apps" , Version : "v1" , Resource : "deployments" },
81
+ trigger : triggerFactory (t ),
82
+ handler : handler ,
83
+ },
84
+ }
85
+
86
+ for _ , ts := range scenarios {
87
+ t .Run (ts .name , func (t * testing.T ) {
88
+ // test data
89
+ timeout := time .Duration (3 * time .Second )
90
+ ctx , cancel := context .WithTimeout (context .Background (), timeout )
91
+ defer cancel ()
92
+ scheme := runtime .NewScheme ()
93
+ informerReciveObjectCh := make (chan * unstructured.Unstructured , 1 )
94
+ objs := []runtime.Object {}
95
+ if ts .existingObj != nil {
96
+ objs = append (objs , ts .existingObj )
97
+ }
98
+ fakeClient := fake .NewSimpleDynamicClient (scheme , objs ... )
99
+ target := dynamicinformer .NewFilteredDynamicSharedInformerFactory (fakeClient , 0 , ts .informNS , nil )
100
+
101
+ // act
102
+ informerListerForGvr := target .ForResource (ts .gvr )
103
+ informerListerForGvr .Informer ().AddEventHandler (ts .handler (informerReciveObjectCh ))
104
+ target .Start (ctx .Done ())
105
+ if synced := target .WaitForCacheSync (ctx .Done ()); ! synced [ts .gvr ] {
106
+ t .Errorf ("informer for %s hasn't synced" , ts .gvr )
107
+ }
108
+
109
+ testObject := ts .trigger (ts .gvr , ts .ns , fakeClient , ts .existingObj )
110
+ select {
111
+ case objFromInformer := <- informerReciveObjectCh :
112
+ if ts .ns != ts .informNS {
113
+ t .Errorf ("informer received an object for namespace %s when watching namespace %s" , ts .ns , ts .informNS )
114
+ }
115
+ if ! equality .Semantic .DeepEqual (testObject , objFromInformer ) {
116
+ t .Fatalf ("%v" , diff .ObjectDiff (testObject , objFromInformer ))
117
+ }
118
+ case <- ctx .Done ():
119
+ if ts .ns == ts .informNS {
120
+ t .Errorf ("tested informer haven't received an object, waited %v" , timeout )
121
+ }
122
+ }
123
+ })
124
+ }
125
+
126
+ }
127
+
35
128
func TestDynamicSharedInformerFactory (t * testing.T ) {
36
129
scenarios := []struct {
37
130
name string
0 commit comments