1
+ //go:build envtest
2
+ // +build envtest
3
+
1
4
package upgradecheck
2
5
3
6
/*
@@ -18,146 +21,32 @@ package upgradecheck
18
21
import (
19
22
"context"
20
23
"encoding/json"
21
- "fmt"
22
24
"net/http"
23
- "net/http/httptest"
24
- "os"
25
25
"path/filepath"
26
- "strings"
27
26
"testing"
28
27
29
- "github.com/wojas/genericr"
30
28
"gotest.tools/v3/assert"
31
29
corev1 "k8s.io/api/core/v1"
32
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33
- "k8s.io/apimachinery/pkg/types"
34
30
"k8s.io/apimachinery/pkg/util/uuid"
35
- "k8s.io/apimachinery/pkg/version"
36
31
"k8s.io/client-go/discovery"
37
32
38
33
// Google Kubernetes Engine / Google Cloud Platform authentication provider
39
34
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
40
35
"k8s.io/client-go/rest"
41
36
crclient "sigs.k8s.io/controller-runtime/pkg/client"
42
- "sigs.k8s.io/controller-runtime/pkg/client/fake"
43
37
"sigs.k8s.io/controller-runtime/pkg/envtest"
44
38
45
39
"github.com/crunchydata/postgres-operator/internal/controller/postgrescluster"
46
40
"github.com/crunchydata/postgres-operator/internal/controller/runtime"
47
- "github.com/crunchydata/postgres-operator/internal/logging"
48
41
"github.com/crunchydata/postgres-operator/internal/naming"
49
42
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
50
43
)
51
44
52
- type fakeClientWithError struct {
53
- crclient.Client
54
- errorType string
55
- }
56
-
57
- func (f * fakeClientWithError ) Get (ctx context.Context , key types.NamespacedName , obj crclient.Object ) error {
58
- switch f .errorType {
59
- case "get error" :
60
- return fmt .Errorf ("get error" )
61
- default :
62
- return f .Client .Get (ctx , key , obj )
63
- }
64
- }
65
-
66
- // TODO: PatchType is not supported currently by fake
67
- // - https://github.com/kubernetes/client-go/issues/970
68
- // Once that gets fixed, we can test without envtest
69
- func (f * fakeClientWithError ) Patch (ctx context.Context , obj crclient.Object ,
70
- patch crclient.Patch , opts ... crclient.PatchOption ) error {
71
- switch {
72
- case f .errorType == "patch error" :
73
- return fmt .Errorf ("patch error" )
74
- default :
75
- return f .Client .Patch (ctx , obj , patch , opts ... )
76
- }
77
- }
78
-
79
- func (f * fakeClientWithError ) List (ctx context.Context , objList crclient.ObjectList ,
80
- opts ... crclient.ListOption ) error {
81
- switch f .errorType {
82
- case "list error" :
83
- return fmt .Errorf ("list error" )
84
- default :
85
- return f .Client .List (ctx , objList , opts ... )
86
- }
87
- }
88
-
89
- func setupDeploymentID (t * testing.T ) string {
90
- t .Helper ()
91
- deploymentID = string (uuid .NewUUID ())
92
- return deploymentID
93
- }
94
-
95
- func setupFakeClientWithPGOScheme (t * testing.T , includeCluster bool ) crclient.Client {
96
- t .Helper ()
97
- pgoScheme , err := runtime .CreatePostgresOperatorScheme ()
98
- if err != nil {
99
- t .Fatal (err )
100
- }
101
- if includeCluster {
102
- pc := & v1beta1.PostgresClusterList {
103
- Items : []v1beta1.PostgresCluster {
104
- {
105
- ObjectMeta : metav1.ObjectMeta {
106
- Name : "hippo" ,
107
- },
108
- },
109
- {
110
- ObjectMeta : metav1.ObjectMeta {
111
- Name : "elephant" ,
112
- },
113
- },
114
- },
115
- }
116
- return fake .NewClientBuilder ().WithScheme (pgoScheme ).WithLists (pc ).Build ()
117
- }
118
- return fake .NewClientBuilder ().WithScheme (pgoScheme ).Build ()
119
- }
120
-
121
- func setupVersionServer (t * testing.T , works bool ) (version.Info , * httptest.Server ) {
122
- t .Helper ()
123
- expect := version.Info {
124
- Major : "1" ,
125
- Minor : "22" ,
126
- GitCommit : "v1.22.2" ,
127
- }
128
- server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter ,
129
- req * http.Request ) {
130
- if works {
131
- output , _ := json .Marshal (expect )
132
- w .Header ().Set ("Content-Type" , "application/json" )
133
- w .WriteHeader (http .StatusOK )
134
- // We don't need to check the error output from this
135
- _ , _ = w .Write (output )
136
- } else {
137
- w .WriteHeader (http .StatusBadRequest )
138
- }
139
- }))
140
- t .Cleanup (server .Close )
141
-
142
- return expect , server
143
- }
144
-
145
- func setupLogCapture (ctx context.Context ) (context.Context , * []string ) {
146
- calls := []string {}
147
- testlog := genericr .New (func (input genericr.Entry ) {
148
- calls = append (calls , input .Message )
149
- })
150
- return logging .NewContext (ctx , testlog ), & calls
151
- }
152
-
153
45
func TestGenerateHeader (t * testing.T ) {
154
- if ! strings .EqualFold (os .Getenv ("USE_EXISTING_CLUSTER" ), "true" ) {
155
- t .Skip ("Server-Side Apply required" )
156
- }
157
46
setupDeploymentID (t )
158
47
ctx := context .Background ()
159
48
env := & envtest.Environment {
160
- CRDDirectoryPaths : []string {filepath .Join (".." , ".." , ".." , " config" , "crd" , "bases" )},
49
+ CRDDirectoryPaths : []string {filepath .Join (".." , ".." , "config" , "crd" , "bases" )},
161
50
}
162
51
cfg , err := env .Start ()
163
52
assert .NilError (t , err )
@@ -168,6 +57,8 @@ func TestGenerateHeader(t *testing.T) {
168
57
cc , err := crclient .New (cfg , crclient.Options {Scheme : pgoScheme })
169
58
assert .NilError (t , err )
170
59
60
+ setupNamespace (t , cc )
61
+
171
62
dc , err := discovery .NewDiscoveryClientForConfig (cfg )
172
63
assert .NilError (t , err )
173
64
server , err := dc .ServerVersion ()
@@ -248,9 +139,6 @@ func TestGenerateHeader(t *testing.T) {
248
139
}
249
140
250
141
func TestEnsureID (t * testing.T ) {
251
- if ! strings .EqualFold (os .Getenv ("USE_EXISTING_CLUSTER" ), "true" ) {
252
- t .Skip ("Server-Side Apply required" )
253
- }
254
142
ctx := context .Background ()
255
143
env := & envtest.Environment {}
256
144
config , err := env .Start ()
@@ -260,6 +148,8 @@ func TestEnsureID(t *testing.T) {
260
148
cc , err := crclient .New (config , crclient.Options {})
261
149
assert .NilError (t , err )
262
150
151
+ setupNamespace (t , cc )
152
+
263
153
t .Run ("success, no id set in mem or configmap" , func (t * testing.T ) {
264
154
deploymentID = ""
265
155
oldID := deploymentID
@@ -392,9 +282,6 @@ func TestEnsureID(t *testing.T) {
392
282
}
393
283
394
284
func TestManageUpgradeCheckConfigMap (t * testing.T ) {
395
- if ! strings .EqualFold (os .Getenv ("USE_EXISTING_CLUSTER" ), "true" ) {
396
- t .Skip ("Server-Side Apply required" )
397
- }
398
285
ctx := context .Background ()
399
286
env := & envtest.Environment {}
400
287
config , err := env .Start ()
@@ -404,6 +291,8 @@ func TestManageUpgradeCheckConfigMap(t *testing.T) {
404
291
cc , err := crclient .New (config , crclient.Options {})
405
292
assert .NilError (t , err )
406
293
294
+ setupNamespace (t , cc )
295
+
407
296
t .Run ("no namespace given" , func (t * testing.T ) {
408
297
ctx , calls := setupLogCapture (ctx )
409
298
t .Setenv ("PGO_NAMESPACE" , "" )
@@ -526,9 +415,6 @@ func TestManageUpgradeCheckConfigMap(t *testing.T) {
526
415
}
527
416
528
417
func TestApplyConfigMap (t * testing.T ) {
529
- if ! strings .EqualFold (os .Getenv ("USE_EXISTING_CLUSTER" ), "true" ) {
530
- t .Skip ("Server-Side Apply required" )
531
- }
532
418
ctx := context .Background ()
533
419
env := & envtest.Environment {}
534
420
config , err := env .Start ()
@@ -538,6 +424,8 @@ func TestApplyConfigMap(t *testing.T) {
538
424
cc , err := crclient .New (config , crclient.Options {})
539
425
assert .NilError (t , err )
540
426
427
+ setupNamespace (t , cc )
428
+
541
429
t .Run ("successful create" , func (t * testing.T ) {
542
430
cmRetrieved := & corev1.ConfigMap {}
543
431
err := cc .Get (ctx , naming .AsObjectKey (naming .UpgradeCheckConfigMap ()), cmRetrieved )
0 commit comments