@@ -26,7 +26,6 @@ import (
26
26
"strings"
27
27
"testing"
28
28
29
- "github.com/go-logr/logr"
30
29
"github.com/wojas/genericr"
31
30
"gotest.tools/v3/assert"
32
31
corev1 "k8s.io/api/core/v1"
@@ -45,6 +44,7 @@ import (
45
44
46
45
"github.com/crunchydata/postgres-operator/internal/controller/postgrescluster"
47
46
"github.com/crunchydata/postgres-operator/internal/controller/runtime"
47
+ "github.com/crunchydata/postgres-operator/internal/logging"
48
48
"github.com/crunchydata/postgres-operator/internal/naming"
49
49
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
50
50
)
@@ -125,7 +125,7 @@ func setupVersionServer(t *testing.T, works bool) (version.Info, *httptest.Serve
125
125
Minor : "22" ,
126
126
GitCommit : "v1.22.2" ,
127
127
}
128
- return expect , httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter ,
128
+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter ,
129
129
req * http.Request ) {
130
130
if works {
131
131
output , _ := json .Marshal (expect )
@@ -137,15 +137,17 @@ func setupVersionServer(t *testing.T, works bool) (version.Info, *httptest.Serve
137
137
w .WriteHeader (http .StatusBadRequest )
138
138
}
139
139
}))
140
+ t .Cleanup (server .Close )
141
+
142
+ return expect , server
140
143
}
141
144
142
- func setupLogCapture (t * testing.T ) (* []string , logr.Logger ) {
143
- t .Helper ()
145
+ func setupLogCapture (ctx context.Context ) (context.Context , * []string ) {
144
146
calls := []string {}
145
147
testlog := genericr .New (func (input genericr.Entry ) {
146
148
calls = append (calls , input .Message )
147
149
})
148
- return & calls , testlog
150
+ return logging . NewContext ( ctx , testlog ), & calls
149
151
}
150
152
151
153
func TestGenerateHeader (t * testing.T ) {
@@ -177,10 +179,10 @@ func TestGenerateHeader(t *testing.T) {
177
179
fakeClientWithOptionalError := & fakeClientWithError {
178
180
cc , "patch error" ,
179
181
}
180
- calls , testlog := setupLogCapture (t )
182
+ ctx , calls := setupLogCapture (ctx )
181
183
182
184
res := generateHeader (ctx , cfg , fakeClientWithOptionalError ,
183
- testlog , "1.2.3" , reconciler .IsOpenShift )
185
+ "1.2.3" , reconciler .IsOpenShift )
184
186
assert .Equal (t , len (* calls ), 1 )
185
187
assert .Equal (t , (* calls )[0 ], `upgrade check issue: could not apply configmap` )
186
188
assert .Equal (t , res .IsOpenShift , reconciler .IsOpenShift )
@@ -197,10 +199,10 @@ func TestGenerateHeader(t *testing.T) {
197
199
fakeClientWithOptionalError := & fakeClientWithError {
198
200
cc , "list error" ,
199
201
}
200
- calls , testlog := setupLogCapture (t )
202
+ ctx , calls := setupLogCapture (ctx )
201
203
202
204
res := generateHeader (ctx , cfg , fakeClientWithOptionalError ,
203
- testlog , "1.2.3" , reconciler .IsOpenShift )
205
+ "1.2.3" , reconciler .IsOpenShift )
204
206
assert .Equal (t , len (* calls ), 1 )
205
207
assert .Equal (t , (* calls )[0 ], `upgrade check issue: could not count postgres clusters` )
206
208
assert .Equal (t , res .IsOpenShift , reconciler .IsOpenShift )
@@ -211,11 +213,11 @@ func TestGenerateHeader(t *testing.T) {
211
213
})
212
214
213
215
t .Run ("error getting server version info" , func (t * testing.T ) {
214
- calls , testlog := setupLogCapture (t )
216
+ ctx , calls := setupLogCapture (ctx )
215
217
badcfg := & rest.Config {}
216
218
217
219
res := generateHeader (ctx , badcfg , cc ,
218
- testlog , "1.2.3" , reconciler .IsOpenShift )
220
+ "1.2.3" , reconciler .IsOpenShift )
219
221
assert .Equal (t , len (* calls ), 1 )
220
222
assert .Equal (t , (* calls )[0 ], `upgrade check issue: could not retrieve server version` )
221
223
assert .Equal (t , res .IsOpenShift , reconciler .IsOpenShift )
@@ -229,10 +231,10 @@ func TestGenerateHeader(t *testing.T) {
229
231
})
230
232
231
233
t .Run ("success" , func (t * testing.T ) {
232
- calls , testlog := setupLogCapture (t )
234
+ ctx , calls := setupLogCapture (ctx )
233
235
234
236
res := generateHeader (ctx , cfg , cc ,
235
- testlog , "1.2.3" , reconciler .IsOpenShift )
237
+ "1.2.3" , reconciler .IsOpenShift )
236
238
assert .Equal (t , len (* calls ), 0 )
237
239
assert .Equal (t , res .IsOpenShift , reconciler .IsOpenShift )
238
240
assert .Equal (t , deploymentID , res .DeploymentID )
@@ -261,9 +263,9 @@ func TestEnsureID(t *testing.T) {
261
263
t .Run ("success, no id set in mem or configmap" , func (t * testing.T ) {
262
264
deploymentID = ""
263
265
oldID := deploymentID
264
- calls , testlog := setupLogCapture (t )
266
+ ctx , calls := setupLogCapture (ctx )
265
267
266
- newID := ensureDeploymentID (ctx , cc , testlog )
268
+ newID := ensureDeploymentID (ctx , cc )
267
269
assert .Equal (t , len (* calls ), 0 )
268
270
assert .Assert (t , newID != oldID )
269
271
assert .Assert (t , newID == deploymentID )
@@ -283,9 +285,9 @@ func TestEnsureID(t *testing.T) {
283
285
err := cc .Get (ctx , naming .AsObjectKey (
284
286
naming .UpgradeCheckConfigMap ()), cm )
285
287
assert .Error (t , err , `configmaps "pgo-upgrade-check" not found` )
286
- calls , testlog := setupLogCapture (t )
288
+ ctx , calls := setupLogCapture (ctx )
287
289
288
- newID := ensureDeploymentID (ctx , cc , testlog )
290
+ newID := ensureDeploymentID (ctx , cc )
289
291
assert .Equal (t , len (* calls ), 0 )
290
292
assert .Assert (t , newID == oldID )
291
293
assert .Assert (t , newID == deploymentID )
@@ -315,8 +317,8 @@ func TestEnsureID(t *testing.T) {
315
317
assert .NilError (t , err )
316
318
317
319
oldID := setupDeploymentID (t )
318
- calls , testlog := setupLogCapture (t )
319
- newID := ensureDeploymentID (ctx , cc , testlog )
320
+ ctx , calls := setupLogCapture (ctx )
321
+ newID := ensureDeploymentID (ctx , cc )
320
322
assert .Equal (t , len (* calls ), 0 )
321
323
assert .Assert (t , newID != oldID )
322
324
assert .Assert (t , newID == deploymentID )
@@ -342,13 +344,10 @@ func TestEnsureID(t *testing.T) {
342
344
assert .NilError (t , err )
343
345
344
346
oldID := setupDeploymentID (t )
345
- calls , testlog := setupLogCapture (t )
346
- oldEnvVar := os .Getenv ("PGO_NAMESPACE" )
347
- os .Setenv ("PGO_NAMESPACE" , "" )
347
+ ctx , calls := setupLogCapture (ctx )
348
+ t .Setenv ("PGO_NAMESPACE" , "" )
348
349
349
- newID := ensureDeploymentID (ctx , cc , testlog )
350
- // reset the var before testing so that errors here do not interfere with subsequent tests
351
- os .Setenv ("PGO_NAMESPACE" , oldEnvVar )
350
+ newID := ensureDeploymentID (ctx , cc )
352
351
assert .Equal (t , len (* calls ), 1 )
353
352
assert .Equal (t , (* calls )[0 ], `upgrade check issue: namespace not set` )
354
353
assert .Assert (t , newID == oldID )
@@ -363,9 +362,9 @@ func TestEnsureID(t *testing.T) {
363
362
cc , "get error" ,
364
363
}
365
364
oldID := setupDeploymentID (t )
366
- calls , testlog := setupLogCapture (t )
365
+ ctx , calls := setupLogCapture (ctx )
367
366
368
- newID := ensureDeploymentID (ctx , fakeClientWithOptionalError , testlog )
367
+ newID := ensureDeploymentID (ctx , fakeClientWithOptionalError )
369
368
assert .Equal (t , len (* calls ), 1 )
370
369
assert .Equal (t , (* calls )[0 ], `upgrade check issue: error retrieving configmap` )
371
370
assert .Assert (t , newID == oldID )
@@ -383,8 +382,8 @@ func TestEnsureID(t *testing.T) {
383
382
}
384
383
oldID := setupDeploymentID (t )
385
384
386
- calls , testlog := setupLogCapture (t )
387
- newID := ensureDeploymentID (ctx , fakeClientWithOptionalError , testlog )
385
+ ctx , calls := setupLogCapture (ctx )
386
+ newID := ensureDeploymentID (ctx , fakeClientWithOptionalError )
388
387
assert .Equal (t , len (* calls ), 1 )
389
388
assert .Equal (t , (* calls )[0 ], `upgrade check issue: could not apply configmap` )
390
389
assert .Assert (t , newID == oldID )
@@ -406,13 +405,10 @@ func TestManageUpgradeCheckConfigMap(t *testing.T) {
406
405
assert .NilError (t , err )
407
406
408
407
t .Run ("no namespace given" , func (t * testing.T ) {
409
- calls , testlog := setupLogCapture (t )
410
- oldEnvVar := os .Getenv ("PGO_NAMESPACE" )
411
- os .Setenv ("PGO_NAMESPACE" , "" )
408
+ ctx , calls := setupLogCapture (ctx )
409
+ t .Setenv ("PGO_NAMESPACE" , "" )
412
410
413
- returnedCM := manageUpgradeCheckConfigMap (ctx , cc , testlog , "current-id" )
414
- // reset the var before testing so that errors here do not interfere with subsequent tests
415
- os .Setenv ("PGO_NAMESPACE" , oldEnvVar )
411
+ returnedCM := manageUpgradeCheckConfigMap (ctx , cc , "current-id" )
416
412
assert .Equal (t , len (* calls ), 1 )
417
413
assert .Equal (t , (* calls )[0 ], `upgrade check issue: namespace not set` )
418
414
assert .Assert (t , returnedCM .Data ["deployment_id" ] == "current-id" )
@@ -424,8 +420,8 @@ func TestManageUpgradeCheckConfigMap(t *testing.T) {
424
420
naming .UpgradeCheckConfigMap ()), cmRetrieved )
425
421
assert .Error (t , err , `configmaps "pgo-upgrade-check" not found` )
426
422
427
- calls , testlog := setupLogCapture (t )
428
- returnedCM := manageUpgradeCheckConfigMap (ctx , cc , testlog , "current-id" )
423
+ ctx , calls := setupLogCapture (ctx )
424
+ returnedCM := manageUpgradeCheckConfigMap (ctx , cc , "current-id" )
429
425
430
426
assert .Equal (t , len (* calls ), 0 )
431
427
assert .Assert (t , returnedCM .Data ["deployment_id" ] == "current-id" )
@@ -437,10 +433,10 @@ func TestManageUpgradeCheckConfigMap(t *testing.T) {
437
433
fakeClientWithOptionalError := & fakeClientWithError {
438
434
cc , "get error" ,
439
435
}
440
- calls , testlog := setupLogCapture (t )
436
+ ctx , calls := setupLogCapture (ctx )
441
437
442
438
returnedCM := manageUpgradeCheckConfigMap (ctx , fakeClientWithOptionalError ,
443
- testlog , "current-id" )
439
+ "current-id" )
444
440
assert .Equal (t , len (* calls ), 1 )
445
441
assert .Equal (t , (* calls )[0 ], `upgrade check issue: error retrieving configmap` )
446
442
assert .Assert (t , returnedCM .Data ["deployment_id" ] == "current-id" )
@@ -461,8 +457,8 @@ func TestManageUpgradeCheckConfigMap(t *testing.T) {
461
457
naming .UpgradeCheckConfigMap ()), cmRetrieved )
462
458
assert .NilError (t , err )
463
459
464
- calls , testlog := setupLogCapture (t )
465
- returnedCM := manageUpgradeCheckConfigMap (ctx , cc , testlog , "current-id" )
460
+ ctx , calls := setupLogCapture (ctx )
461
+ returnedCM := manageUpgradeCheckConfigMap (ctx , cc , "current-id" )
466
462
assert .Equal (t , len (* calls ), 0 )
467
463
assert .Assert (t , returnedCM .Data ["deployment_id" ] == "current-id" )
468
464
err = cc .Delete (ctx , cm )
@@ -484,8 +480,8 @@ func TestManageUpgradeCheckConfigMap(t *testing.T) {
484
480
naming .UpgradeCheckConfigMap ()), cmRetrieved )
485
481
assert .NilError (t , err )
486
482
487
- calls , testlog := setupLogCapture (t )
488
- returnedCM := manageUpgradeCheckConfigMap (ctx , cc , testlog , "current-id" )
483
+ ctx , calls := setupLogCapture (ctx )
484
+ returnedCM := manageUpgradeCheckConfigMap (ctx , cc , "current-id" )
489
485
assert .Equal (t , len (* calls ), 0 )
490
486
assert .Assert (t , returnedCM .Data ["deployment_id" ] == "current-id" )
491
487
err = cc .Delete (ctx , cm )
@@ -507,8 +503,8 @@ func TestManageUpgradeCheckConfigMap(t *testing.T) {
507
503
naming .UpgradeCheckConfigMap ()), cmRetrieved )
508
504
assert .NilError (t , err )
509
505
510
- calls , testlog := setupLogCapture (t )
511
- returnedCM := manageUpgradeCheckConfigMap (ctx , cc , testlog , "current-id" )
506
+ ctx , calls := setupLogCapture (ctx )
507
+ returnedCM := manageUpgradeCheckConfigMap (ctx , cc , "current-id" )
512
508
assert .Equal (t , len (* calls ), 0 )
513
509
assert .Assert (t , returnedCM .Data ["deployment-id" ] != "current-id" )
514
510
err = cc .Delete (ctx , cm )
@@ -520,9 +516,9 @@ func TestManageUpgradeCheckConfigMap(t *testing.T) {
520
516
cc , "patch error" ,
521
517
}
522
518
523
- calls , testlog := setupLogCapture (t )
519
+ ctx , calls := setupLogCapture (ctx )
524
520
returnedCM := manageUpgradeCheckConfigMap (ctx , fakeClientWithOptionalError ,
525
- testlog , "current-id" )
521
+ "current-id" )
526
522
assert .Equal (t , len (* calls ), 1 )
527
523
assert .Equal (t , (* calls )[0 ], `upgrade check issue: could not apply configmap` )
528
524
assert .Assert (t , returnedCM .Data ["deployment_id" ] == "current-id" )
@@ -652,8 +648,8 @@ func TestGetManagedClusters(t *testing.T) {
652
648
653
649
t .Run ("success" , func (t * testing.T ) {
654
650
fakeClient := setupFakeClientWithPGOScheme (t , true )
655
- calls , testlog := setupLogCapture (t )
656
- count := getManagedClusters (ctx , fakeClient , testlog )
651
+ ctx , calls := setupLogCapture (ctx )
652
+ count := getManagedClusters (ctx , fakeClient )
657
653
assert .Equal (t , len (* calls ), 0 )
658
654
assert .Assert (t , count == 2 )
659
655
})
@@ -662,8 +658,8 @@ func TestGetManagedClusters(t *testing.T) {
662
658
fakeClientWithOptionalError := & fakeClientWithError {
663
659
setupFakeClientWithPGOScheme (t , true ), "list error" ,
664
660
}
665
- calls , testlog := setupLogCapture (t )
666
- count := getManagedClusters (ctx , fakeClientWithOptionalError , testlog )
661
+ ctx , calls := setupLogCapture (ctx )
662
+ count := getManagedClusters (ctx , fakeClientWithOptionalError )
667
663
assert .Equal (t , len (* calls ), 1 )
668
664
assert .Equal (t , (* calls )[0 ], `upgrade check issue: could not count postgres clusters` )
669
665
assert .Assert (t , count == 0 )
@@ -673,23 +669,22 @@ func TestGetManagedClusters(t *testing.T) {
673
669
func TestGetServerVersion (t * testing.T ) {
674
670
t .Run ("success" , func (t * testing.T ) {
675
671
expect , server := setupVersionServer (t , true )
676
- defer server . Close ( )
677
- calls , testlog := setupLogCapture ( t )
678
- got := getServerVersion (& rest.Config {
672
+ ctx , calls := setupLogCapture ( context . Background () )
673
+
674
+ got := getServerVersion (ctx , & rest.Config {
679
675
Host : server .URL ,
680
- }, testlog )
676
+ })
681
677
assert .Equal (t , len (* calls ), 0 )
682
678
assert .Equal (t , got , expect .String ())
683
679
})
684
680
685
681
t .Run ("failure" , func (t * testing.T ) {
686
682
_ , server := setupVersionServer (t , false )
687
- defer server . Close ( )
683
+ ctx , calls := setupLogCapture ( context . Background () )
688
684
689
- calls , testlog := setupLogCapture (t )
690
- got := getServerVersion (& rest.Config {
685
+ got := getServerVersion (ctx , & rest.Config {
691
686
Host : server .URL ,
692
- }, testlog )
687
+ })
693
688
assert .Equal (t , len (* calls ), 1 )
694
689
assert .Equal (t , (* calls )[0 ], `upgrade check issue: could not retrieve server version` )
695
690
assert .Equal (t , got , "" )
0 commit comments