@@ -17,26 +17,37 @@ import (
17
17
operatorexternalversions "github.com/openshift/client-go/operator/informers/externalversions"
18
18
)
19
19
20
- func TestClusterVersionOperatorConfiguration_sync (t * testing.T ) {
20
+ func TestClusterVersionOperatorConfiguration_APIServerSync (t * testing.T ) {
21
21
tests := []struct {
22
22
name string
23
- config operatorv1alpha1.ClusterVersionOperator
24
- expectedConfig operatorv1alpha1.ClusterVersionOperator
23
+ config * operatorv1alpha1.ClusterVersionOperator
24
+ expectedConfig * operatorv1alpha1.ClusterVersionOperator
25
25
internalConfig configuration
26
26
expectedInternalConfig configuration
27
27
handlerFunctionCalled bool
28
28
}{
29
+ {
30
+ name : "the configuration resource does not exist in the cluster -> default configuration" ,
31
+ config : nil ,
32
+ expectedConfig : nil ,
33
+ internalConfig : configuration {},
34
+ expectedInternalConfig : configuration {
35
+ lastObservedGeneration : 0 ,
36
+ desiredLogLevel : operatorv1 .Normal ,
37
+ },
38
+ handlerFunctionCalled : true ,
39
+ },
29
40
{
30
41
name : "first sync run correctly updates the status" ,
31
- config : operatorv1alpha1.ClusterVersionOperator {
42
+ config : & operatorv1alpha1.ClusterVersionOperator {
32
43
ObjectMeta : metav1.ObjectMeta {
33
44
Generation : 1 ,
34
45
},
35
46
Spec : operatorv1alpha1.ClusterVersionOperatorSpec {
36
47
OperatorLogLevel : operatorv1 .Normal ,
37
48
},
38
49
},
39
- expectedConfig : operatorv1alpha1.ClusterVersionOperator {
50
+ expectedConfig : & operatorv1alpha1.ClusterVersionOperator {
40
51
ObjectMeta : metav1.ObjectMeta {
41
52
Generation : 1 ,
42
53
},
@@ -59,7 +70,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
59
70
},
60
71
{
61
72
name : "sync updates observed generation correctly" ,
62
- config : operatorv1alpha1.ClusterVersionOperator {
73
+ config : & operatorv1alpha1.ClusterVersionOperator {
63
74
ObjectMeta : metav1.ObjectMeta {
64
75
Generation : 3 ,
65
76
},
@@ -70,7 +81,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
70
81
ObservedGeneration : 2 ,
71
82
},
72
83
},
73
- expectedConfig : operatorv1alpha1.ClusterVersionOperator {
84
+ expectedConfig : & operatorv1alpha1.ClusterVersionOperator {
74
85
ObjectMeta : metav1.ObjectMeta {
75
86
Generation : 3 ,
76
87
},
@@ -93,7 +104,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
93
104
},
94
105
{
95
106
name : "sync updates desired log level correctly" ,
96
- config : operatorv1alpha1.ClusterVersionOperator {
107
+ config : & operatorv1alpha1.ClusterVersionOperator {
97
108
ObjectMeta : metav1.ObjectMeta {
98
109
Generation : 4 ,
99
110
},
@@ -104,7 +115,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
104
115
ObservedGeneration : 3 ,
105
116
},
106
117
},
107
- expectedConfig : operatorv1alpha1.ClusterVersionOperator {
118
+ expectedConfig : & operatorv1alpha1.ClusterVersionOperator {
108
119
ObjectMeta : metav1.ObjectMeta {
109
120
Generation : 4 ,
110
121
},
@@ -127,7 +138,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
127
138
},
128
139
{
129
140
name : "number of not observed generations does not impact sync" ,
130
- config : operatorv1alpha1.ClusterVersionOperator {
141
+ config : & operatorv1alpha1.ClusterVersionOperator {
131
142
ObjectMeta : metav1.ObjectMeta {
132
143
Generation : 40 ,
133
144
},
@@ -138,7 +149,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
138
149
ObservedGeneration : 3 ,
139
150
},
140
151
},
141
- expectedConfig : operatorv1alpha1.ClusterVersionOperator {
152
+ expectedConfig : & operatorv1alpha1.ClusterVersionOperator {
142
153
ObjectMeta : metav1.ObjectMeta {
143
154
Generation : 40 ,
144
155
},
@@ -163,12 +174,13 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
163
174
for _ , tt := range tests {
164
175
t .Run (tt .name , func (t * testing.T ) {
165
176
// Initialize testing logic
166
- tt .config .Name = ClusterVersionOperatorConfigurationName
167
- tt .expectedConfig .Name = ClusterVersionOperatorConfigurationName
168
-
169
- client := operatorclientsetfake .NewClientset (& tt .config )
177
+ client := operatorclientsetfake .NewClientset ()
178
+ if tt .config != nil {
179
+ tt .config .Name = ClusterVersionOperatorConfigurationName
180
+ tt .expectedConfig .Name = ClusterVersionOperatorConfigurationName
181
+ client = operatorclientsetfake .NewClientset (tt .config )
182
+ }
170
183
factory := operatorexternalversions .NewSharedInformerFactoryWithOptions (client , time .Minute )
171
-
172
184
configController := NewClusterVersionOperatorConfiguration ().UsingKubeAPIServer (client , factory )
173
185
174
186
called := false
@@ -198,10 +210,15 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
198
210
}
199
211
200
212
config , err := client .OperatorV1alpha1 ().ClusterVersionOperators ().Get (ctx , ClusterVersionOperatorConfigurationName , metav1.GetOptions {})
201
- if err != nil {
213
+ if err != nil && ! apierrors . IsNotFound ( err ) {
202
214
t .Errorf ("unexpected error %v" , err )
203
215
}
204
- if diff := cmp .Diff (tt .expectedConfig , * config , cmpopts .IgnoreFields (metav1.ObjectMeta {}, "ManagedFields" )); diff != "" {
216
+
217
+ // Set nil to differentiate between nonexisting configurations
218
+ if apierrors .IsNotFound (err ) {
219
+ config = nil
220
+ }
221
+ if diff := cmp .Diff (tt .expectedConfig , config , cmpopts .IgnoreFields (metav1.ObjectMeta {}, "ManagedFields" )); diff != "" {
205
222
t .Errorf ("unexpected config (-want, +got) = %v" , diff )
206
223
}
207
224
@@ -214,88 +231,3 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
214
231
})
215
232
}
216
233
}
217
-
218
- func TestClusterVersionOperatorConfiguration_Sync (t * testing.T ) {
219
- tests := []struct {
220
- name string
221
- config * operatorv1alpha1.ClusterVersionOperator
222
- expectedConfig * operatorv1alpha1.ClusterVersionOperator
223
- }{
224
- {
225
- name : "the configuration resource does not exist in the cluster -> ignore" ,
226
- config : nil ,
227
- expectedConfig : nil ,
228
- },
229
- {
230
- name : "Sync updates the ClusterVersionOperator resource" ,
231
- config : & operatorv1alpha1.ClusterVersionOperator {
232
- ObjectMeta : metav1.ObjectMeta {
233
- Name : "cluster" ,
234
- Generation : 4 ,
235
- },
236
- Spec : operatorv1alpha1.ClusterVersionOperatorSpec {
237
- OperatorLogLevel : operatorv1 .Trace ,
238
- },
239
- Status : operatorv1alpha1.ClusterVersionOperatorStatus {
240
- ObservedGeneration : 3 ,
241
- },
242
- },
243
- expectedConfig : & operatorv1alpha1.ClusterVersionOperator {
244
- ObjectMeta : metav1.ObjectMeta {
245
- Name : "cluster" ,
246
- Generation : 4 ,
247
- },
248
- Spec : operatorv1alpha1.ClusterVersionOperatorSpec {
249
- OperatorLogLevel : operatorv1 .Trace ,
250
- },
251
- Status : operatorv1alpha1.ClusterVersionOperatorStatus {
252
- ObservedGeneration : 4 ,
253
- },
254
- },
255
- },
256
- }
257
- for _ , tt := range tests {
258
- t .Run (tt .name , func (t * testing.T ) {
259
- // Initialize testing logic
260
- var client * operatorclientsetfake.Clientset
261
- if tt .config != nil {
262
- client = operatorclientsetfake .NewClientset (tt .config )
263
- } else {
264
- client = operatorclientsetfake .NewClientset ()
265
- }
266
-
267
- factory := operatorexternalversions .NewSharedInformerFactoryWithOptions (client , time .Minute )
268
- cvoConfiguration := NewClusterVersionOperatorConfiguration ().UsingKubeAPIServer (client , factory )
269
- defer cvoConfiguration .queue .ShutDown ()
270
-
271
- ctx , cancelFunc := context .WithDeadline (context .Background (), time .Now ().Add (time .Minute ))
272
- defer cancelFunc ()
273
-
274
- err := cvoConfiguration .Start (ctx )
275
- if err != nil {
276
- t .Errorf ("unexpected error %v" , err )
277
- }
278
-
279
- // Run tested functionality
280
- err = cvoConfiguration .Sync (ctx , "ClusterVersionOperator/cluster" )
281
- if err != nil {
282
- t .Errorf ("unexpected error %v" , err )
283
- }
284
-
285
- // Verify results
286
- config , err := client .OperatorV1alpha1 ().ClusterVersionOperators ().Get (ctx , "cluster" , metav1.GetOptions {})
287
- if err != nil && ! apierrors .IsNotFound (err ) {
288
- t .Errorf ("unexpected error %v" , err )
289
- }
290
-
291
- switch {
292
- case apierrors .IsNotFound (err ) && tt .expectedConfig != nil :
293
- t .Errorf ("expected config to be '%v', got NotFound" , * tt .expectedConfig )
294
- case err == nil :
295
- if diff := cmp .Diff (* tt .expectedConfig , * config , cmpopts .IgnoreFields (metav1.ObjectMeta {}, "ManagedFields" )); diff != "" {
296
- t .Errorf ("unexpected config (-want, +got) = %v" , diff )
297
- }
298
- }
299
- })
300
- }
301
- }
0 commit comments