@@ -17,26 +17,41 @@ 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
+ lastObservedGeneration : 3 ,
35
+ desiredLogLevel : "Trace" ,
36
+ },
37
+ expectedInternalConfig : configuration {
38
+ lastObservedGeneration : 3 , // TODO: Default to 0
39
+ desiredLogLevel : "Trace" , // TODO: Default to Normal
40
+ },
41
+ // TODO: Apply the log level when the defaulting is implemented
42
+ handlerFunctionCalled : false ,
43
+ },
29
44
{
30
45
name : "first sync run correctly updates the status" ,
31
- config : operatorv1alpha1.ClusterVersionOperator {
46
+ config : & operatorv1alpha1.ClusterVersionOperator {
32
47
ObjectMeta : metav1.ObjectMeta {
33
48
Generation : 1 ,
34
49
},
35
50
Spec : operatorv1alpha1.ClusterVersionOperatorSpec {
36
51
OperatorLogLevel : operatorv1 .Normal ,
37
52
},
38
53
},
39
- expectedConfig : operatorv1alpha1.ClusterVersionOperator {
54
+ expectedConfig : & operatorv1alpha1.ClusterVersionOperator {
40
55
ObjectMeta : metav1.ObjectMeta {
41
56
Generation : 1 ,
42
57
},
@@ -59,7 +74,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
59
74
},
60
75
{
61
76
name : "sync updates observed generation correctly" ,
62
- config : operatorv1alpha1.ClusterVersionOperator {
77
+ config : & operatorv1alpha1.ClusterVersionOperator {
63
78
ObjectMeta : metav1.ObjectMeta {
64
79
Generation : 3 ,
65
80
},
@@ -70,7 +85,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
70
85
ObservedGeneration : 2 ,
71
86
},
72
87
},
73
- expectedConfig : operatorv1alpha1.ClusterVersionOperator {
88
+ expectedConfig : & operatorv1alpha1.ClusterVersionOperator {
74
89
ObjectMeta : metav1.ObjectMeta {
75
90
Generation : 3 ,
76
91
},
@@ -93,7 +108,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
93
108
},
94
109
{
95
110
name : "sync updates desired log level correctly" ,
96
- config : operatorv1alpha1.ClusterVersionOperator {
111
+ config : & operatorv1alpha1.ClusterVersionOperator {
97
112
ObjectMeta : metav1.ObjectMeta {
98
113
Generation : 4 ,
99
114
},
@@ -104,7 +119,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
104
119
ObservedGeneration : 3 ,
105
120
},
106
121
},
107
- expectedConfig : operatorv1alpha1.ClusterVersionOperator {
122
+ expectedConfig : & operatorv1alpha1.ClusterVersionOperator {
108
123
ObjectMeta : metav1.ObjectMeta {
109
124
Generation : 4 ,
110
125
},
@@ -127,7 +142,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
127
142
},
128
143
{
129
144
name : "number of not observed generations does not impact sync" ,
130
- config : operatorv1alpha1.ClusterVersionOperator {
145
+ config : & operatorv1alpha1.ClusterVersionOperator {
131
146
ObjectMeta : metav1.ObjectMeta {
132
147
Generation : 40 ,
133
148
},
@@ -138,7 +153,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
138
153
ObservedGeneration : 3 ,
139
154
},
140
155
},
141
- expectedConfig : operatorv1alpha1.ClusterVersionOperator {
156
+ expectedConfig : & operatorv1alpha1.ClusterVersionOperator {
142
157
ObjectMeta : metav1.ObjectMeta {
143
158
Generation : 40 ,
144
159
},
@@ -163,12 +178,13 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
163
178
for _ , tt := range tests {
164
179
t .Run (tt .name , func (t * testing.T ) {
165
180
// Initialize testing logic
166
- tt .config .Name = ClusterVersionOperatorConfigurationName
167
- tt .expectedConfig .Name = ClusterVersionOperatorConfigurationName
168
-
169
- client := operatorclientsetfake .NewClientset (& tt .config )
181
+ client := operatorclientsetfake .NewClientset ()
182
+ if tt .config != nil {
183
+ tt .config .Name = ClusterVersionOperatorConfigurationName
184
+ tt .expectedConfig .Name = ClusterVersionOperatorConfigurationName
185
+ client = operatorclientsetfake .NewClientset (tt .config )
186
+ }
170
187
factory := operatorexternalversions .NewSharedInformerFactoryWithOptions (client , time .Minute )
171
-
172
188
configController := NewClusterVersionOperatorConfiguration (client , factory )
173
189
174
190
called := false
@@ -198,10 +214,15 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
198
214
}
199
215
200
216
config , err := client .OperatorV1alpha1 ().ClusterVersionOperators ().Get (ctx , ClusterVersionOperatorConfigurationName , metav1.GetOptions {})
201
- if err != nil {
217
+ if err != nil && ! apierrors . IsNotFound ( err ) {
202
218
t .Errorf ("unexpected error %v" , err )
203
219
}
204
- if diff := cmp .Diff (tt .expectedConfig , * config , cmpopts .IgnoreFields (metav1.ObjectMeta {}, "ManagedFields" )); diff != "" {
220
+
221
+ // Set nil to differentiate between nonexisting configurations
222
+ if apierrors .IsNotFound (err ) {
223
+ config = nil
224
+ }
225
+ if diff := cmp .Diff (tt .expectedConfig , config , cmpopts .IgnoreFields (metav1.ObjectMeta {}, "ManagedFields" )); diff != "" {
205
226
t .Errorf ("unexpected config (-want, +got) = %v" , diff )
206
227
}
207
228
@@ -214,88 +235,3 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
214
235
})
215
236
}
216
237
}
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 (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