@@ -140,8 +140,45 @@ func TestSkipWebSocket(t *testing.T) {
140140}
141141
142142func TestRunInformerToWatch (t * testing.T ) { //nolint: funlen
143+ grvlist := []schema.GroupVersionResource {
144+ {Group : "" , Version : "v1" , Resource : "pods" },
145+ {Group : "apps" , Version : "v1" , Resource : "deployments" },
146+ }
147+ clientMap := map [schema.GroupVersionResource ]string {
148+ {Group : "" , Version : "v1" , Resource : "pods" }: "PodList" ,
149+ {Group : "apps" , Version : "v1" , Resource : "deployments" }: "DeploymentList" ,
150+ }
151+ mockPod := & unstructured.Unstructured {
152+ Object : map [string ]interface {}{
153+ "apiVersion" : "v1" ,
154+ "kind" : "Pod" ,
155+ "metadata" : map [string ]interface {}{
156+ "name" : "test-pod" ,
157+ "namespace" : "default" ,
158+ "creationTimestamp" : time .Now ().UTC ().Format (time .RFC3339 ),
159+ },
160+ },
161+ }
162+ beforeCache := & MockCache {
163+ store : map [string ]string {
164+ "+pods+default+test-context-2" : "pod-data" ,
165+ "apps+deployments+default+test-context-2" : "deployment-data" ,
166+ "+nodes+default+test-context-2" : "node-data" ,
167+ "apps+replicaset+default+test-context-2" : "replicaset-data" ,
168+ },
169+ }
170+
171+ afterCache := & MockCache {
172+ store : map [string ]string {
173+ "apps+deployments+default+test-context-2" : "deployment-data" ,
174+ "+nodes+default+test-context-2" : "node-data" ,
175+ "apps+replicaset+default+test-context-2" : "replicaset-data" ,
176+ },
177+ }
178+
143179 tests := []struct {
144180 name string
181+ eventType string
145182 contextKey string
146183 gvrList []schema.GroupVersionResource
147184 clientMap map [schema.GroupVersionResource ]string
@@ -150,50 +187,42 @@ func TestRunInformerToWatch(t *testing.T) { //nolint: funlen
150187 afterCache * MockCache
151188 }{
152189 {
153- name : "testing run watcher informer" ,
154- contextKey : "test-context-2" ,
155- gvrList : []schema.GroupVersionResource {
156- {Group : "" , Version : "v1" , Resource : "pods" },
157- {Group : "apps" , Version : "v1" , Resource : "deployments" },
158- },
159- clientMap : map [schema.GroupVersionResource ]string {
160- {Group : "" , Version : "v1" , Resource : "pods" }: "PodList" ,
161- {Group : "apps" , Version : "v1" , Resource : "deployments" }: "DeploymentList" ,
162- },
163- mockPod : & unstructured.Unstructured {
164- Object : map [string ]interface {}{
165- "apiVersion" : "v1" ,
166- "kind" : "Pod" ,
167- "metadata" : map [string ]interface {}{
168- "name" : "test-pod" ,
169- "namespace" : "default" ,
170- "creationTimestamp" : time .Now ().UTC ().Format (time .RFC3339 ),
171- },
172- },
173- },
174- beforeCache : & MockCache {
175- store : map [string ]string {
176- "+pods+default+test-context-2" : "pod-data" ,
177- "apps+deployments+default+test-context-2" : "deployment-data" ,
178- "+nodes+default+test-context-2" : "node-data" ,
179- "apps+replicaset+default+test-context-2" : "replicaset-data" ,
180- },
181- },
182- afterCache : & MockCache {
183- store : map [string ]string {
184- "apps+deployments+default+test-context-2" : "deployment-data" ,
185- "+nodes+default+test-context-2" : "node-data" ,
186- "apps+replicaset+default+test-context-2" : "replicaset-data" ,
187- },
188- },
190+ name : "testing run watcher informer" ,
191+ eventType : "add" ,
192+ contextKey : "test-context-2" ,
193+ gvrList : grvlist ,
194+ clientMap : clientMap ,
195+ mockPod : mockPod ,
196+ beforeCache : beforeCache ,
197+ afterCache : afterCache ,
198+ },
199+ {
200+ name : "testing run watcher informer for update event" ,
201+ eventType : "update" ,
202+ contextKey : "test-context-2" ,
203+ gvrList : grvlist ,
204+ clientMap : clientMap ,
205+ mockPod : mockPod ,
206+ beforeCache : beforeCache ,
207+ afterCache : afterCache ,
208+ },
209+ {
210+ name : "testing run watcher informer for delete event" ,
211+ eventType : "delete" ,
212+ contextKey : "test-context-2" ,
213+ gvrList : grvlist ,
214+ clientMap : clientMap ,
215+ mockPod : mockPod ,
216+ beforeCache : beforeCache ,
217+ afterCache : afterCache ,
189218 },
190219 }
191220
192221 for _ , tc := range tests {
193222 t .Run (tc .name , func (t * testing.T ) {
194- schema := runtime .NewScheme ()
223+ scheme := runtime .NewScheme ()
195224
196- client := dynamicfake .NewSimpleDynamicClientWithCustomListKinds (schema , tc .clientMap )
225+ client := dynamicfake .NewSimpleDynamicClientWithCustomListKinds (scheme , tc .clientMap )
197226 factory := dynamicinformer .NewFilteredDynamicSharedInformerFactory (client , 0 , "" , nil )
198227
199228 mockCache := tc .beforeCache
@@ -203,8 +232,30 @@ func TestRunInformerToWatch(t *testing.T) { //nolint: funlen
203232 factory .Start (stopCh )
204233 factory .WaitForCacheSync (stopCh )
205234
206- err := client .Tracker ().Add (tc .mockPod )
207- assert .NoError (t , err )
235+ switch tc .eventType {
236+ case "add" :
237+ err := client .Tracker ().Add (tc .mockPod )
238+ assert .NoError (t , err )
239+
240+ case "update" :
241+ err := client .Tracker ().Add (tc .mockPod )
242+ assert .NoError (t , err )
243+
244+ updatedPod := tc .mockPod .DeepCopy ()
245+ updatedPod .Object ["metadata" ].(map [string ]interface {})["labels" ] = map [string ]interface {}{"app" : "updated" }
246+
247+ gvr := schema.GroupVersionResource {Group : "" , Version : "v1" , Resource : "pods" }
248+ err = client .Tracker ().Update (gvr , updatedPod , "default" )
249+ assert .NoError (t , err )
250+
251+ case "delete" :
252+ err := client .Tracker ().Add (tc .mockPod )
253+ assert .NoError (t , err )
254+
255+ gvr := schema.GroupVersionResource {Group : "" , Version : "v1" , Resource : "pods" }
256+ err = client .Tracker ().Delete (gvr , tc .mockPod .GetNamespace (), tc .mockPod .GetName ())
257+ assert .NoError (t , err )
258+ }
208259
209260 time .Sleep (100 * time .Millisecond )
210261
0 commit comments