Skip to content

Commit 06b6212

Browse files
committed
config: test: Merge individual tests together
Due to recent changes, the two tests can now be safely merged together. The second test verified the overall synchronization, which is now also being exercised in the first test. The tests cases were copied or were already present.
1 parent f9f88f5 commit 06b6212

File tree

1 file changed

+35
-103
lines changed

1 file changed

+35
-103
lines changed

pkg/cvo/configuration/configuration_test.go

Lines changed: 35 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,37 @@ 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+
expectedInternalConfig: configuration{
35+
lastObservedGeneration: 0,
36+
desiredLogLevel: operatorv1.Normal,
37+
},
38+
handlerFunctionCalled: true,
39+
},
2940
{
3041
name: "first sync run correctly updates the status",
31-
config: operatorv1alpha1.ClusterVersionOperator{
42+
config: &operatorv1alpha1.ClusterVersionOperator{
3243
ObjectMeta: metav1.ObjectMeta{
3344
Generation: 1,
3445
},
3546
Spec: operatorv1alpha1.ClusterVersionOperatorSpec{
3647
OperatorLogLevel: operatorv1.Normal,
3748
},
3849
},
39-
expectedConfig: operatorv1alpha1.ClusterVersionOperator{
50+
expectedConfig: &operatorv1alpha1.ClusterVersionOperator{
4051
ObjectMeta: metav1.ObjectMeta{
4152
Generation: 1,
4253
},
@@ -59,7 +70,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
5970
},
6071
{
6172
name: "sync updates observed generation correctly",
62-
config: operatorv1alpha1.ClusterVersionOperator{
73+
config: &operatorv1alpha1.ClusterVersionOperator{
6374
ObjectMeta: metav1.ObjectMeta{
6475
Generation: 3,
6576
},
@@ -70,7 +81,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
7081
ObservedGeneration: 2,
7182
},
7283
},
73-
expectedConfig: operatorv1alpha1.ClusterVersionOperator{
84+
expectedConfig: &operatorv1alpha1.ClusterVersionOperator{
7485
ObjectMeta: metav1.ObjectMeta{
7586
Generation: 3,
7687
},
@@ -93,7 +104,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
93104
},
94105
{
95106
name: "sync updates desired log level correctly",
96-
config: operatorv1alpha1.ClusterVersionOperator{
107+
config: &operatorv1alpha1.ClusterVersionOperator{
97108
ObjectMeta: metav1.ObjectMeta{
98109
Generation: 4,
99110
},
@@ -104,7 +115,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
104115
ObservedGeneration: 3,
105116
},
106117
},
107-
expectedConfig: operatorv1alpha1.ClusterVersionOperator{
118+
expectedConfig: &operatorv1alpha1.ClusterVersionOperator{
108119
ObjectMeta: metav1.ObjectMeta{
109120
Generation: 4,
110121
},
@@ -127,7 +138,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
127138
},
128139
{
129140
name: "number of not observed generations does not impact sync",
130-
config: operatorv1alpha1.ClusterVersionOperator{
141+
config: &operatorv1alpha1.ClusterVersionOperator{
131142
ObjectMeta: metav1.ObjectMeta{
132143
Generation: 40,
133144
},
@@ -138,7 +149,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
138149
ObservedGeneration: 3,
139150
},
140151
},
141-
expectedConfig: operatorv1alpha1.ClusterVersionOperator{
152+
expectedConfig: &operatorv1alpha1.ClusterVersionOperator{
142153
ObjectMeta: metav1.ObjectMeta{
143154
Generation: 40,
144155
},
@@ -163,12 +174,13 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
163174
for _, tt := range tests {
164175
t.Run(tt.name, func(t *testing.T) {
165176
// 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+
}
170183
factory := operatorexternalversions.NewSharedInformerFactoryWithOptions(client, time.Minute)
171-
172184
configController := NewClusterVersionOperatorConfiguration().UsingKubeAPIServer(client, factory)
173185

174186
called := false
@@ -198,10 +210,15 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
198210
}
199211

200212
config, err := client.OperatorV1alpha1().ClusterVersionOperators().Get(ctx, ClusterVersionOperatorConfigurationName, metav1.GetOptions{})
201-
if err != nil {
213+
if err != nil && !apierrors.IsNotFound(err) {
202214
t.Errorf("unexpected error %v", err)
203215
}
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 != "" {
205222
t.Errorf("unexpected config (-want, +got) = %v", diff)
206223
}
207224

@@ -214,88 +231,3 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
214231
})
215232
}
216233
}
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

Comments
 (0)