@@ -3,8 +3,10 @@ package controllers
3
3
import (
4
4
"testing"
5
5
6
+ semver "github.com/blang/semver/v4"
6
7
configv1 "github.com/openshift/api/config/v1"
7
8
"github.com/openshift/operator-framework-olm/pkg/manifests"
9
+ "github.com/operator-framework/api/pkg/lib/version"
8
10
olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
9
11
"github.com/stretchr/testify/require"
10
12
appsv1 "k8s.io/api/apps/v1"
@@ -72,10 +74,39 @@ func intOrStr(val int) *intstr.IntOrString {
72
74
return & tmp
73
75
}
74
76
77
+ type testCSVOption func (* olmv1alpha1.ClusterServiceVersion )
78
+
79
+ func withVersion (v semver.Version ) func (* olmv1alpha1.ClusterServiceVersion ) {
80
+ return func (csv * olmv1alpha1.ClusterServiceVersion ) {
81
+ csv .Spec .Version = version.OperatorVersion {v }
82
+ }
83
+ }
84
+
85
+ func withDescription (description string ) func (* olmv1alpha1.ClusterServiceVersion ) {
86
+ return func (csv * olmv1alpha1.ClusterServiceVersion ) {
87
+ csv .Spec .Description = description
88
+ }
89
+ }
90
+
91
+ func withAffinity (affinity * corev1.Affinity ) func (* olmv1alpha1.ClusterServiceVersion ) {
92
+ return func (csv * olmv1alpha1.ClusterServiceVersion ) {
93
+ csv .Spec .InstallStrategy .StrategySpec .DeploymentSpecs [0 ].Spec .Template .Spec .Affinity = affinity
94
+ }
95
+ }
96
+ func withRollingUpdateStrategy (strategy * appsv1.RollingUpdateDeployment ) func (* olmv1alpha1.ClusterServiceVersion ) {
97
+ return func (csv * olmv1alpha1.ClusterServiceVersion ) {
98
+ csv .Spec .InstallStrategy .StrategySpec .DeploymentSpecs [0 ].Spec .Strategy .RollingUpdate = strategy
99
+ }
100
+ }
101
+
102
+ func withReplicas (replicas * int32 ) func (* olmv1alpha1.ClusterServiceVersion ) {
103
+ return func (csv * olmv1alpha1.ClusterServiceVersion ) {
104
+ csv .Spec .InstallStrategy .StrategySpec .DeploymentSpecs [0 ].Spec .Replicas = replicas
105
+ }
106
+ }
107
+
75
108
func newTestCSV (
76
- replicas * int32 ,
77
- strategy * appsv1.RollingUpdateDeployment ,
78
- affinity * corev1.Affinity ,
109
+ options ... testCSVOption ,
79
110
) * olmv1alpha1.ClusterServiceVersion {
80
111
csv , err := manifests .NewPackageServerCSV (
81
112
manifests .WithName (name ),
@@ -84,11 +115,10 @@ func newTestCSV(
84
115
if err != nil {
85
116
return nil
86
117
}
87
- deployment := csv .Spec .InstallStrategy .StrategySpec .DeploymentSpecs [0 ].Spec
88
- deployment .Template .Spec .Affinity = affinity
89
- deployment .Replicas = replicas
90
- deployment .Strategy .RollingUpdate = strategy
91
- csv .Spec .InstallStrategy .StrategySpec .DeploymentSpecs [0 ].Spec = deployment
118
+
119
+ for _ , o := range options {
120
+ o (csv )
121
+ }
92
122
93
123
return csv
94
124
}
@@ -133,83 +163,104 @@ func TestEnsureCSV(t *testing.T) {
133
163
singleReplicas := pointer .Int32 (singleReplicaCount )
134
164
image := getImageFromManifest ()
135
165
166
+ type wanted struct {
167
+ expectedBool bool
168
+ expectedErr error
169
+ }
170
+
136
171
tt := []struct {
137
172
name string
138
173
inputCSV * olmv1alpha1.ClusterServiceVersion
139
174
expectedCSV * olmv1alpha1.ClusterServiceVersion
140
175
highlyAvailable bool
141
- want bool
176
+ want wanted
142
177
}{
143
178
{
144
179
name : "Modified/HighlyAvailable/CorrectReplicasIncorrectRolling" ,
145
- want : true ,
180
+ want : wanted { true , nil } ,
146
181
highlyAvailable : true ,
147
- inputCSV : newTestCSV (defaultReplicas , emptyRollout , defaultAffinity ),
148
- expectedCSV : newTestCSV (defaultReplicas , defaultRollout , defaultAffinity ),
182
+ inputCSV : newTestCSV (withReplicas ( defaultReplicas ), withRollingUpdateStrategy ( emptyRollout ), withAffinity ( defaultAffinity ) ),
183
+ expectedCSV : newTestCSV (withReplicas ( defaultReplicas ), withRollingUpdateStrategy ( defaultRollout ), withAffinity ( defaultAffinity ) ),
149
184
},
150
185
{
151
186
name : "Modified/HighlyAvailable/IncorrectReplicasCorrectRolling" ,
152
- want : true ,
187
+ want : wanted { true , nil } ,
153
188
highlyAvailable : true ,
154
- inputCSV : newTestCSV (singleReplicas , defaultRollout , defaultAffinity ),
155
- expectedCSV : newTestCSV (defaultReplicas , defaultRollout , defaultAffinity ),
189
+ inputCSV : newTestCSV (withReplicas ( singleReplicas ), withRollingUpdateStrategy ( defaultRollout ), withAffinity ( defaultAffinity ) ),
190
+ expectedCSV : newTestCSV (withReplicas ( defaultReplicas ), withRollingUpdateStrategy ( defaultRollout ), withAffinity ( defaultAffinity ) ),
156
191
},
157
192
{
158
193
name : "Modified/HighlyAvailable/IncorrectPodAntiAffinity" ,
159
- want : true ,
194
+ want : wanted { true , nil } ,
160
195
highlyAvailable : true ,
161
- inputCSV : newTestCSV (singleReplicas , defaultRollout , newPodAffinity (& corev1.PodAntiAffinity {
196
+ inputCSV : newTestCSV (withReplicas ( singleReplicas ), withRollingUpdateStrategy ( defaultRollout ), withAffinity ( newPodAffinity (& corev1.PodAntiAffinity {
162
197
PreferredDuringSchedulingIgnoredDuringExecution : []corev1.WeightedPodAffinityTerm {
163
198
{
164
199
Weight : 1 ,
165
200
},
166
201
},
167
- })),
168
- expectedCSV : newTestCSV (defaultReplicas , defaultRollout , defaultAffinity ),
202
+ }))) ,
203
+ expectedCSV : newTestCSV (withReplicas ( defaultReplicas ), withRollingUpdateStrategy ( defaultRollout ), withAffinity ( defaultAffinity ) ),
169
204
},
170
205
{
171
206
name : "NotModified/HighlyAvailable" ,
172
- want : false ,
207
+ want : wanted { false , nil } ,
173
208
highlyAvailable : true ,
174
- inputCSV : newTestCSV (defaultReplicas , defaultRollout , defaultAffinity ),
175
- expectedCSV : newTestCSV (defaultReplicas , defaultRollout , defaultAffinity ),
209
+ inputCSV : newTestCSV (withReplicas ( defaultReplicas ), withRollingUpdateStrategy ( defaultRollout ), withAffinity ( defaultAffinity ) ),
210
+ expectedCSV : newTestCSV (withReplicas ( defaultReplicas ), withRollingUpdateStrategy ( defaultRollout ), withAffinity ( defaultAffinity ) ),
176
211
},
212
+
177
213
{
178
214
name : "Modified/SingleReplica/CorrectReplicasIncorrectRolling" ,
179
- want : true ,
215
+ want : wanted { true , nil } ,
180
216
highlyAvailable : false ,
181
- inputCSV : newTestCSV (singleReplicas , defaultRollout , & corev1.Affinity {}),
182
- expectedCSV : newTestCSV (singleReplicas , emptyRollout , & corev1.Affinity {}),
217
+ inputCSV : newTestCSV (withReplicas ( singleReplicas ), withRollingUpdateStrategy ( defaultRollout ), withAffinity ( & corev1.Affinity {}) ),
218
+ expectedCSV : newTestCSV (withReplicas ( singleReplicas ), withRollingUpdateStrategy ( emptyRollout ), withAffinity ( & corev1.Affinity {}) ),
183
219
},
184
220
{
185
221
name : "Modified/SingleReplica/IncorrectReplicasCorrectRolling" ,
186
- want : true ,
222
+ want : wanted { true , nil } ,
187
223
highlyAvailable : false ,
188
- inputCSV : newTestCSV (defaultReplicas , emptyRollout , & corev1.Affinity {}),
189
- expectedCSV : newTestCSV (singleReplicas , emptyRollout , & corev1.Affinity {}),
224
+ inputCSV : newTestCSV (withReplicas ( defaultReplicas ), withRollingUpdateStrategy ( emptyRollout ), withAffinity ( & corev1.Affinity {}) ),
225
+ expectedCSV : newTestCSV (withReplicas ( singleReplicas ), withRollingUpdateStrategy ( emptyRollout ), withAffinity ( & corev1.Affinity {}) ),
190
226
},
191
227
{
192
228
name : "Modified/SingleReplica/IncorrectPodAntiAffinity" ,
193
- want : true ,
229
+ want : wanted {true , nil },
230
+ highlyAvailable : false ,
231
+ inputCSV : newTestCSV (withReplicas (singleReplicas ), withRollingUpdateStrategy (emptyRollout ), withAffinity (defaultAffinity )),
232
+ expectedCSV : newTestCSV (withReplicas (singleReplicas ), withRollingUpdateStrategy (emptyRollout ), withAffinity (& corev1.Affinity {})),
233
+ },
234
+ {
235
+ name : "Modified/SingleReplica/IncorrectVersion" ,
236
+ want : wanted {true , nil },
237
+ highlyAvailable : false ,
238
+ inputCSV : newTestCSV (withReplicas (singleReplicas ), withRollingUpdateStrategy (emptyRollout ), withAffinity (& corev1.Affinity {}), withVersion (semver.Version {Major : 0 , Minor : 0 , Patch : 0 })),
239
+ expectedCSV : newTestCSV (withReplicas (singleReplicas ), withRollingUpdateStrategy (emptyRollout ), withAffinity (& corev1.Affinity {})),
240
+ },
241
+ {
242
+ name : "Modified/SingleReplica/IncorrectDescription" ,
243
+ want : wanted {true , nil },
194
244
highlyAvailable : false ,
195
- inputCSV : newTestCSV (singleReplicas , emptyRollout , defaultAffinity ),
196
- expectedCSV : newTestCSV (singleReplicas , emptyRollout , & corev1.Affinity {}),
245
+ inputCSV : newTestCSV (withReplicas ( singleReplicas ), withRollingUpdateStrategy ( emptyRollout ), withAffinity ( & corev1. Affinity {}), withDescription ( "foo" ) ),
246
+ expectedCSV : newTestCSV (withReplicas ( singleReplicas ), withRollingUpdateStrategy ( emptyRollout ), withAffinity ( & corev1.Affinity {}) ),
197
247
},
198
248
{
199
249
name : "NotModified/SingleReplica" ,
200
- want : false ,
250
+ want : wanted { false , nil } ,
201
251
highlyAvailable : false ,
202
- inputCSV : newTestCSV (singleReplicas , emptyRollout , & corev1.Affinity {}),
203
- expectedCSV : newTestCSV (singleReplicas , emptyRollout , & corev1.Affinity {}),
252
+ inputCSV : newTestCSV (withReplicas ( singleReplicas ), withRollingUpdateStrategy ( emptyRollout ), withAffinity ( & corev1.Affinity {}) ),
253
+ expectedCSV : newTestCSV (withReplicas ( singleReplicas ), withRollingUpdateStrategy ( emptyRollout ), withAffinity ( & corev1.Affinity {}) ),
204
254
},
205
255
}
206
256
207
257
for _ , tc := range tt {
208
258
tc := tc
209
259
210
260
t .Run (tc .name , func (t * testing.T ) {
211
- got := ensureCSV (logger , image , tc .inputCSV , tc .highlyAvailable )
212
- require .EqualValues (t , tc .want , got )
261
+ gotBool , gotErr := ensureCSV (logger , image , tc .inputCSV , tc .highlyAvailable )
262
+ require .EqualValues (t , tc .want .expectedBool , gotBool )
263
+ require .EqualValues (t , tc .want .expectedErr , gotErr )
213
264
require .EqualValues (t , tc .inputCSV .Spec , tc .expectedCSV .Spec )
214
265
})
215
266
}
0 commit comments