@@ -14,7 +14,10 @@ package dynamiccontroller
14
14
15
15
import (
16
16
"context"
17
+ "fmt"
17
18
"io"
19
+ "maps"
20
+ "slices"
18
21
"testing"
19
22
"time"
20
23
@@ -152,3 +155,59 @@ func TestEnqueueObject(t *testing.T) {
152
155
153
156
assert .Equal (t , 1 , dc .queue .Len ())
154
157
}
158
+
159
+ func TestInstanceUpdatePolicy (t * testing.T ) {
160
+ logger := noopLogger ()
161
+
162
+ scheme := runtime .NewScheme ()
163
+ gvr := schema.GroupVersionResource {Group : "test" , Version : "v1" , Resource : "tests" }
164
+ gvk := schema.GroupVersionKind {Group : "test" , Version : "v1" , Kind : "Test" }
165
+
166
+ objs := make (map [string ]runtime.Object )
167
+
168
+ obj1 := & unstructured.Unstructured {}
169
+ obj1 .SetGroupVersionKind (gvk )
170
+ obj1 .SetNamespace ("default" )
171
+ obj1 .SetName ("test-object-1" )
172
+ objs [obj1 .GetNamespace ()+ "/" + obj1 .GetName ()] = obj1
173
+
174
+ obj2 := & unstructured.Unstructured {}
175
+ obj2 .SetGroupVersionKind (gvk )
176
+ obj2 .SetNamespace ("test-namespace" )
177
+ obj2 .SetName ("test-object-2" )
178
+ objs [obj2 .GetNamespace ()+ "/" + obj2 .GetName ()] = obj2
179
+
180
+ client := fake .NewSimpleDynamicClientWithCustomListKinds (scheme , map [schema.GroupVersionResource ]string {
181
+ gvr : "TestList" ,
182
+ }, slices .Collect (maps .Values (objs ))... )
183
+
184
+ dc := NewDynamicController (logger , Config {}, client )
185
+
186
+ handlerFunc := Handler (func (ctx context.Context , req controllerruntime.Request ) error {
187
+ fmt .Println ("reconciling instance" , req )
188
+ return nil
189
+ })
190
+
191
+ // simulate initial creation of the resource graph
192
+ err := dc .StartServingGVK (context .Background (), gvr , handlerFunc )
193
+ assert .NoError (t , err )
194
+
195
+ // simulate reconciling the instances
196
+ for dc .queue .Len () > 0 {
197
+ item , _ := dc .queue .Get ()
198
+ dc .queue .Done (item )
199
+ dc .queue .Forget (item )
200
+ }
201
+
202
+ // simulate updating the resource graph
203
+ err = dc .StartServingGVK (context .Background (), gvr , handlerFunc )
204
+ assert .NoError (t , err )
205
+
206
+ // check if the expected objects are queued
207
+ assert .Equal (t , dc .queue .Len (), 2 )
208
+ for dc .queue .Len () > 0 {
209
+ name , _ := dc .queue .Get ()
210
+ _ , ok := objs [name .NamespacedKey ]
211
+ assert .True (t , ok )
212
+ }
213
+ }
0 commit comments