@@ -8,20 +8,21 @@ import (
8
8
"testing"
9
9
10
10
"github.com/stretchr/testify/require"
11
- "go.uber.org/zap"
12
11
"go.uber.org/zap/zaptest"
12
+ corev1 "k8s.io/api/core/v1"
13
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
14
"k8s.io/apimachinery/pkg/runtime"
15
15
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
16
16
"k8s.io/client-go/kubernetes/fake"
17
17
"k8s.io/client-go/kubernetes/scheme"
18
18
"k8s.io/client-go/rest"
19
+ client_go_testing "k8s.io/client-go/testing"
19
20
"k8s.io/client-go/tools/record"
20
21
ctrl "sigs.k8s.io/controller-runtime"
21
22
"sigs.k8s.io/controller-runtime/pkg/builder"
22
23
"sigs.k8s.io/controller-runtime/pkg/cache"
23
24
"sigs.k8s.io/controller-runtime/pkg/client"
24
- crFake "sigs.k8s.io/controller-runtime/pkg/client/fake"
25
+ client_fake "sigs.k8s.io/controller-runtime/pkg/client/fake"
25
26
"sigs.k8s.io/controller-runtime/pkg/cluster"
26
27
"sigs.k8s.io/controller-runtime/pkg/reconcile"
27
28
@@ -113,7 +114,7 @@ func TestManagerStart(t *testing.T) {
113
114
waitForCacheSyncResult : tc .waitForCacheSyncResult ,
114
115
}
115
116
eventsGetter := fake .NewClientset ().CoreV1 ()
116
- m , err := NewManager (& mckCluster , eventsGetter , zaptest .NewLogger (t ))
117
+ m , err := NewManager (& mckCluster , eventsGetter , zaptest .NewLogger (t ), nil )
117
118
require .NoError (t , err )
118
119
119
120
gotErr := ""
@@ -225,7 +226,7 @@ func TestDryRunReportError(t *testing.T) {
225
226
} {
226
227
t .Run (tc .name , func (t * testing.T ) {
227
228
eventsGetter := fake .NewClientset ().CoreV1 ()
228
- m , err := NewManager (& mockCluster {}, eventsGetter , zaptest .NewLogger (t ))
229
+ m , err := NewManager (& mockCluster {}, eventsGetter , zaptest .NewLogger (t ), nil )
229
230
require .NoError (t , err )
230
231
m .reportError (context .Background (), obj , tc .err )
231
232
@@ -246,26 +247,25 @@ func TestDryRunReportError(t *testing.T) {
246
247
247
248
type mockReconciler struct {
248
249
reconcile.Reconciler
250
+ Resource client.Object
249
251
ErrFail error
250
- Resource akov2.AtlasCustomResource
251
252
}
252
253
253
254
func (m * mockReconciler ) Reconcile (_ context.Context , _ ctrl.Request ) (ctrl.Result , error ) {
254
255
return ctrl.Result {}, m .ErrFail
255
256
}
256
257
257
258
func (m * mockReconciler ) For () (client.Object , builder.Predicates ) {
258
- return m .Resource .(client. Object ) , builder.Predicates {}
259
+ return m .Resource , builder.Predicates {}
259
260
}
260
261
261
262
func TestManager_dryRunReconcilers (t * testing.T ) {
262
263
tests := []struct {
263
264
name string
264
265
reconcilers []reconciler
265
- logger * zap.Logger
266
- instanceUID string
267
- ctx context.Context
268
- wantErr bool
266
+ objects []client.Object
267
+ wantEvents []* corev1.Event
268
+ namespaces []string
269
269
}{
270
270
{
271
271
name : "Should run dry run without errors for AtlasProject resource" ,
@@ -275,56 +275,150 @@ func TestManager_dryRunReconcilers(t *testing.T) {
275
275
ErrFail : nil ,
276
276
},
277
277
},
278
- wantErr : false ,
279
- ctx : context .Background (),
280
- logger : zap .L (),
278
+ objects : []client.Object {
279
+ & akov2.AtlasProject {
280
+ TypeMeta : metav1.TypeMeta {
281
+ Kind : "AtlasProject" ,
282
+ APIVersion : "atlas.mongodb.com/v1" ,
283
+ },
284
+ ObjectMeta : metav1.ObjectMeta {
285
+ Name : "test" ,
286
+ Namespace : "test" ,
287
+ },
288
+ Spec : akov2.AtlasProjectSpec {},
289
+ Status : status.AtlasProjectStatus {},
290
+ },
291
+ },
292
+ wantEvents : []* corev1.Event {
293
+ {
294
+ InvolvedObject : corev1.ObjectReference {Kind : "AtlasProject" , APIVersion : "atlas.mongodb.com/v1" , Namespace : "test" , Name : "test" },
295
+ Message : "done" ,
296
+ },
297
+ },
281
298
},
282
299
{
283
- name : "Should not fail when a reconciler fails" ,
300
+ name : "Should emit an error when a reconciler fails" ,
284
301
reconcilers : []reconciler {
285
302
& mockReconciler {
286
303
Resource : & akov2.AtlasProject {},
287
304
ErrFail : fmt .Errorf ("failed to reconcile" ),
288
305
},
289
306
},
290
- wantErr : false ,
291
- ctx : context .Background (),
292
- logger : zap .L (),
307
+ objects : []client.Object {
308
+ & akov2.AtlasProject {
309
+ TypeMeta : metav1.TypeMeta {
310
+ Kind : "AtlasProject" ,
311
+ APIVersion : "atlas.mongodb.com/v1" ,
312
+ },
313
+ ObjectMeta : metav1.ObjectMeta {
314
+ Name : "test" ,
315
+ Namespace : "test" ,
316
+ },
317
+ Spec : akov2.AtlasProjectSpec {},
318
+ Status : status.AtlasProjectStatus {},
319
+ },
320
+ },
321
+ wantEvents : []* corev1.Event {
322
+ {
323
+ InvolvedObject : corev1.ObjectReference {Kind : "AtlasProject" , APIVersion : "atlas.mongodb.com/v1" , Namespace : "test" , Name : "test" },
324
+ Message : "failed to reconcile" ,
325
+ },
326
+ {
327
+ InvolvedObject : corev1.ObjectReference {Kind : "AtlasProject" , APIVersion : "atlas.mongodb.com/v1" , Namespace : "test" , Name : "test" },
328
+ Message : "done" ,
329
+ },
330
+ },
331
+ },
332
+ {
333
+ name : "Should ignore objects from a different namespace" ,
334
+ reconcilers : []reconciler {
335
+ & mockReconciler {
336
+ Resource : & akov2.AtlasProject {},
337
+ },
338
+ },
339
+ objects : []client.Object {
340
+ & akov2.AtlasProject {
341
+ TypeMeta : metav1.TypeMeta {
342
+ Kind : "AtlasProject" ,
343
+ APIVersion : "atlas.mongodb.com/v1" ,
344
+ },
345
+ ObjectMeta : metav1.ObjectMeta {
346
+ Name : "test" ,
347
+ Namespace : "test" ,
348
+ },
349
+ Spec : akov2.AtlasProjectSpec {},
350
+ Status : status.AtlasProjectStatus {},
351
+ },
352
+ & akov2.AtlasProject {
353
+ TypeMeta : metav1.TypeMeta {
354
+ Kind : "AtlasProject" ,
355
+ APIVersion : "atlas.mongodb.com/v1" ,
356
+ },
357
+ ObjectMeta : metav1.ObjectMeta {
358
+ Name : "test2" ,
359
+ Namespace : "ignored" ,
360
+ },
361
+ Spec : akov2.AtlasProjectSpec {},
362
+ Status : status.AtlasProjectStatus {},
363
+ },
364
+ },
365
+ namespaces : []string {"test" },
366
+ wantEvents : []* corev1.Event {
367
+ {
368
+ InvolvedObject : corev1.ObjectReference {Kind : "AtlasProject" , APIVersion : "atlas.mongodb.com/v1" , Namespace : "test" , Name : "test" },
369
+ Message : "done" ,
370
+ },
371
+ },
293
372
},
294
373
}
374
+
295
375
for _ , tt := range tests {
296
376
t .Run (tt .name , func (t * testing.T ) {
297
- eventsGetter := fake .NewClientset ().CoreV1 ()
298
377
schm := scheme .Scheme
299
378
require .NoError (t , akov2 .AddToScheme (schm ))
300
379
301
380
clstr := & mockCluster {
302
381
startErr : nil ,
303
382
waitForCacheSyncResult : true ,
304
- client : crFake .NewClientBuilder ().WithScheme (schm ).WithObjects (
305
- & akov2.AtlasProject {
306
- TypeMeta : metav1.TypeMeta {
307
- Kind : "AtlasProject" ,
308
- APIVersion : "atlas.mongodb.com/v1" ,
309
- },
310
- ObjectMeta : metav1.ObjectMeta {
311
- Name : "test" ,
312
- Namespace : "test" ,
313
- },
314
- Spec : akov2.AtlasProjectSpec {},
315
- Status : status.AtlasProjectStatus {},
316
- }).Build (),
383
+ client : client_fake .NewClientBuilder ().WithScheme (schm ).WithObjects (tt .objects ... ).Build (),
384
+ }
385
+
386
+ eventsClient := fake .NewClientset ()
387
+ logger := zaptest .NewLogger (t )
388
+ m , err := NewManager (clstr , eventsClient .CoreV1 (), logger , tt .namespaces )
389
+ if err != nil {
390
+ t .Fatal (err )
391
+ }
392
+
393
+ for _ , r := range tt .reconcilers {
394
+ m .SetupReconciler (r )
317
395
}
318
- m := & Manager {
319
- Cluster : clstr ,
320
- reconcilers : tt .reconcilers ,
321
- logger : tt .logger ,
322
- instanceUID : tt .instanceUID ,
323
- eventsClient : eventsGetter ,
396
+
397
+ if err := m .dryRunReconcilers (context .Background ()); err != nil {
398
+ t .Error (err )
399
+ return
324
400
}
325
- if err := m .dryRunReconcilers (tt .ctx ); (err != nil ) != tt .wantErr {
326
- t .Errorf ("dryRunReconcilers() error = %v, wantErr %v" , err , tt .wantErr )
401
+
402
+ gotEvents := []* corev1.Event {}
403
+ for _ , action := range eventsClient .Actions () {
404
+ createAction , ok := action .(client_go_testing.CreateAction )
405
+ if ! ok {
406
+ t .Errorf ("Unexpected action: %v" , action )
407
+ continue
408
+ }
409
+ event , ok := createAction .GetObject ().(* corev1.Event )
410
+ if ! ok {
411
+ t .Errorf ("Unexpected event: %v" , event )
412
+ continue
413
+ }
414
+ prunedEvent := & corev1.Event {
415
+ InvolvedObject : event .InvolvedObject ,
416
+ Message : event .Message ,
417
+ }
418
+ prunedEvent .InvolvedObject .ResourceVersion = ""
419
+ gotEvents = append (gotEvents , prunedEvent )
327
420
}
421
+ require .Equal (t , tt .wantEvents , gotEvents )
328
422
})
329
423
}
330
424
}
0 commit comments