Skip to content

Commit 439176c

Browse files
committed
config: test: Merge test functions together
Due to recent changes, the two test functions can now be safely merged together. The second function verified the overall synchronization of the configuration, which is now also being exercised in the first function. The tests cases were migrated over to the first function or were already present.
1 parent 012816b commit 439176c

File tree

1 file changed

+39
-103
lines changed

1 file changed

+39
-103
lines changed

pkg/cvo/configuration/configuration_test.go

Lines changed: 39 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,41 @@ import (
1717
operatorexternalversions "github.com/openshift/client-go/operator/informers/externalversions"
1818
)
1919

20-
func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
20+
func TestClusterVersionOperatorConfiguration_APIServerSync(t *testing.T) {
2121
tests := []struct {
2222
name string
23-
config operatorv1alpha1.ClusterVersionOperator
24-
expectedConfig operatorv1alpha1.ClusterVersionOperator
23+
config *operatorv1alpha1.ClusterVersionOperator
24+
expectedConfig *operatorv1alpha1.ClusterVersionOperator
2525
internalConfig configuration
2626
expectedInternalConfig configuration
2727
handlerFunctionCalled bool
2828
}{
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+
},
2944
{
3045
name: "first sync run correctly updates the status",
31-
config: operatorv1alpha1.ClusterVersionOperator{
46+
config: &operatorv1alpha1.ClusterVersionOperator{
3247
ObjectMeta: metav1.ObjectMeta{
3348
Generation: 1,
3449
},
3550
Spec: operatorv1alpha1.ClusterVersionOperatorSpec{
3651
OperatorLogLevel: operatorv1.Normal,
3752
},
3853
},
39-
expectedConfig: operatorv1alpha1.ClusterVersionOperator{
54+
expectedConfig: &operatorv1alpha1.ClusterVersionOperator{
4055
ObjectMeta: metav1.ObjectMeta{
4156
Generation: 1,
4257
},
@@ -59,7 +74,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
5974
},
6075
{
6176
name: "sync updates observed generation correctly",
62-
config: operatorv1alpha1.ClusterVersionOperator{
77+
config: &operatorv1alpha1.ClusterVersionOperator{
6378
ObjectMeta: metav1.ObjectMeta{
6479
Generation: 3,
6580
},
@@ -70,7 +85,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
7085
ObservedGeneration: 2,
7186
},
7287
},
73-
expectedConfig: operatorv1alpha1.ClusterVersionOperator{
88+
expectedConfig: &operatorv1alpha1.ClusterVersionOperator{
7489
ObjectMeta: metav1.ObjectMeta{
7590
Generation: 3,
7691
},
@@ -93,7 +108,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
93108
},
94109
{
95110
name: "sync updates desired log level correctly",
96-
config: operatorv1alpha1.ClusterVersionOperator{
111+
config: &operatorv1alpha1.ClusterVersionOperator{
97112
ObjectMeta: metav1.ObjectMeta{
98113
Generation: 4,
99114
},
@@ -104,7 +119,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
104119
ObservedGeneration: 3,
105120
},
106121
},
107-
expectedConfig: operatorv1alpha1.ClusterVersionOperator{
122+
expectedConfig: &operatorv1alpha1.ClusterVersionOperator{
108123
ObjectMeta: metav1.ObjectMeta{
109124
Generation: 4,
110125
},
@@ -127,7 +142,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
127142
},
128143
{
129144
name: "number of not observed generations does not impact sync",
130-
config: operatorv1alpha1.ClusterVersionOperator{
145+
config: &operatorv1alpha1.ClusterVersionOperator{
131146
ObjectMeta: metav1.ObjectMeta{
132147
Generation: 40,
133148
},
@@ -138,7 +153,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
138153
ObservedGeneration: 3,
139154
},
140155
},
141-
expectedConfig: operatorv1alpha1.ClusterVersionOperator{
156+
expectedConfig: &operatorv1alpha1.ClusterVersionOperator{
142157
ObjectMeta: metav1.ObjectMeta{
143158
Generation: 40,
144159
},
@@ -163,12 +178,13 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
163178
for _, tt := range tests {
164179
t.Run(tt.name, func(t *testing.T) {
165180
// 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+
}
170187
factory := operatorexternalversions.NewSharedInformerFactoryWithOptions(client, time.Minute)
171-
172188
configController := NewClusterVersionOperatorConfiguration(client, factory)
173189

174190
called := false
@@ -198,10 +214,15 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
198214
}
199215

200216
config, err := client.OperatorV1alpha1().ClusterVersionOperators().Get(ctx, ClusterVersionOperatorConfigurationName, metav1.GetOptions{})
201-
if err != nil {
217+
if err != nil && !apierrors.IsNotFound(err) {
202218
t.Errorf("unexpected error %v", err)
203219
}
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 != "" {
205226
t.Errorf("unexpected config (-want, +got) = %v", diff)
206227
}
207228

@@ -214,88 +235,3 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
214235
})
215236
}
216237
}
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

Comments
 (0)