@@ -24,6 +24,7 @@ import (
24
24
"time"
25
25
26
26
"github.com/go-logr/logr/testr"
27
+ "github.com/stretchr/testify/assert"
27
28
"github.com/stretchr/testify/require"
28
29
appsv1 "k8s.io/api/apps/v1"
29
30
corev1 "k8s.io/api/core/v1"
@@ -78,13 +79,7 @@ func TestRootSyncReconcilerDeploymentLifecycle(t *testing.T) {
78
79
errCh := startControllerManager (ctx , t , fakeClient , testReconciler )
79
80
80
81
// Wait for manager to exit before returning
81
- defer func () {
82
- cancel ()
83
- t .Log ("waiting for controller-manager to stop" )
84
- for err := range errCh {
85
- require .NoError (t , err )
86
- }
87
- }()
82
+ defer stopControllerManager (t , cancel , errCh )
88
83
89
84
watchCtx , watchCancel := context .WithTimeout (ctx , 10 * time .Second )
90
85
defer watchCancel ()
@@ -201,13 +196,7 @@ func TestReconcileInvalidRootSyncLifecycle(t *testing.T) {
201
196
errCh := startControllerManager (ctx , t , fakeClient , testReconciler )
202
197
203
198
// Wait for manager to exit before returning
204
- defer func () {
205
- cancel ()
206
- t .Log ("waiting for controller-manager to stop" )
207
- for err := range errCh {
208
- require .NoError (t , err )
209
- }
210
- }()
199
+ defer stopControllerManager (t , cancel , errCh )
211
200
212
201
t .Log ("watching for RootSync status update" )
213
202
watchCtx , watchCancel := context .WithTimeout (ctx , 10 * time .Second )
@@ -274,13 +263,7 @@ func TestReconcileRootSyncLifecycleValidToInvalid1(t *testing.T) {
274
263
errCh := startControllerManager (ctx , t , fakeClient , testReconciler )
275
264
276
265
// Wait for manager to exit before returning
277
- defer func () {
278
- cancel ()
279
- t .Log ("waiting for controller-manager to stop" )
280
- for err := range errCh {
281
- require .NoError (t , err )
282
- }
283
- }()
266
+ defer stopControllerManager (t , cancel , errCh )
284
267
285
268
reconcilerKey := core .RootReconcilerObjectKey (rs .Name )
286
269
@@ -517,13 +500,7 @@ func testDriftProtection(t *testing.T, fakeClient *syncerFake.Client, testReconc
517
500
errCh := startControllerManager (ctx , t , fakeClient , testReconciler )
518
501
519
502
// Wait for manager to exit before returning
520
- defer func () {
521
- cancel ()
522
- t .Log ("waiting for controller-manager to stop" )
523
- for err := range errCh {
524
- require .NoError (t , err )
525
- }
526
- }()
503
+ defer stopControllerManager (t , cancel , errCh )
527
504
528
505
key := objKeyFunc (client .ObjectKeyFromObject (syncObj ))
529
506
@@ -650,6 +627,16 @@ func startControllerManager(ctx context.Context, t *testing.T, fakeClient *synce
650
627
return errCh
651
628
}
652
629
630
+ func stopControllerManager (t * testing.T , cancel context.CancelFunc , errCh <- chan error ) {
631
+ cancel ()
632
+ t .Log ("waiting for controller-manager to stop" )
633
+ for err := range errCh {
634
+ // Error/Assert instead of Fatal/Require, to avoid panic when called async.
635
+ // Go tests that panic in a defer can be flakey.
636
+ assert .NoError (t , err )
637
+ }
638
+ }
639
+
653
640
func logObjectYAMLIfFailed (t * testing.T , fakeClient * syncerFake.Client , obj client.Object ) {
654
641
if t .Failed () {
655
642
err := fakeClient .Get (context .Background (), client .ObjectKeyFromObject (obj ), obj )
0 commit comments