@@ -2,11 +2,13 @@ package convert
2
2
3
3
import (
4
4
"fmt"
5
+ "strings"
5
6
"testing"
6
7
7
8
. "github.com/onsi/ginkgo/v2"
8
9
. "github.com/onsi/gomega"
9
10
11
+ appsv1 "k8s.io/api/apps/v1"
10
12
corev1 "k8s.io/api/core/v1"
11
13
rbacv1 "k8s.io/api/rbac/v1"
12
14
schedulingv1 "k8s.io/api/scheduling/v1"
@@ -73,7 +75,7 @@ var _ = Describe("RegistryV1 Suite", func() {
73
75
Expect (plainBundle .Objects ).To (HaveLen (1 ))
74
76
75
77
By ("verifying if ns has been set correctly" )
76
- resObj := containsObject ( unstructuredSvc , plainBundle .Objects )
78
+ resObj := findObjectByName ( svc . Name , plainBundle .Objects )
77
79
Expect (resObj ).NotTo (BeNil ())
78
80
Expect (resObj .GetNamespace ()).To (BeEquivalentTo (installNamespace ))
79
81
})
@@ -99,7 +101,7 @@ var _ = Describe("RegistryV1 Suite", func() {
99
101
Expect (plainBundle .Objects ).To (HaveLen (1 ))
100
102
101
103
By ("verifying if ns has been set correctly" )
102
- resObj := containsObject ( unstructuredSvc , plainBundle .Objects )
104
+ resObj := findObjectByName ( svc . Name , plainBundle .Objects )
103
105
Expect (resObj ).NotTo (BeNil ())
104
106
Expect (resObj .GetNamespace ()).To (BeEquivalentTo (installNamespace ))
105
107
})
@@ -157,7 +159,7 @@ var _ = Describe("RegistryV1 Suite", func() {
157
159
Expect (plainBundle .Objects ).To (HaveLen (1 ))
158
160
159
161
By ("verifying if ns has been set correctly" )
160
- resObj := containsObject ( unstructuredpriorityclass , plainBundle .Objects )
162
+ resObj := findObjectByName ( pc . Name , plainBundle .Objects )
161
163
Expect (resObj ).NotTo (BeNil ())
162
164
Expect (resObj .GetNamespace ()).To (BeEmpty ())
163
165
})
@@ -167,22 +169,39 @@ var _ = Describe("RegistryV1 Suite", func() {
167
169
Context ("Should generate objects successfully based on target namespaces" , func () {
168
170
var (
169
171
svc corev1.Service
170
- csv v1alpha1.ClusterServiceVersion
172
+ baseCSV v1alpha1.ClusterServiceVersion
171
173
watchNamespaces []string
172
174
)
173
175
174
176
BeforeEach (func () {
175
- csv = v1alpha1.ClusterServiceVersion {
177
+ // base CSV definition that each test case will deep copy and modify
178
+ baseCSV = v1alpha1.ClusterServiceVersion {
176
179
ObjectMeta : metav1.ObjectMeta {
177
180
Name : "testCSV" ,
178
181
Annotations : map [string ]string {
179
182
"olm.properties" : fmt .Sprintf ("[{\" type\" : %s, \" value\" : \" %s\" }]" , property .TypeConstraint , "value" ),
180
183
},
181
184
},
182
185
Spec : v1alpha1.ClusterServiceVersionSpec {
183
- InstallModes : []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeMultiNamespace , Supported : true }},
184
186
InstallStrategy : v1alpha1.NamedInstallStrategy {
185
187
StrategySpec : v1alpha1.StrategyDetailsDeployment {
188
+ DeploymentSpecs : []v1alpha1.StrategyDeploymentSpec {
189
+ {
190
+ Name : "testDeployment" ,
191
+ Spec : appsv1.DeploymentSpec {
192
+ Template : corev1.PodTemplateSpec {
193
+ Spec : corev1.PodSpec {
194
+ Containers : []corev1.Container {
195
+ {
196
+ Name : "testContainer" ,
197
+ Image : "testImage" ,
198
+ },
199
+ },
200
+ },
201
+ },
202
+ },
203
+ },
204
+ },
186
205
Permissions : []v1alpha1.StrategyDeploymentPermissions {
187
206
{
188
207
ServiceAccountName : "testServiceAccount" ,
@@ -199,6 +218,7 @@ var _ = Describe("RegistryV1 Suite", func() {
199
218
},
200
219
},
201
220
}
221
+
202
222
svc = corev1.Service {
203
223
ObjectMeta : metav1.ObjectMeta {
204
224
Name : "testService" ,
@@ -208,13 +228,16 @@ var _ = Describe("RegistryV1 Suite", func() {
208
228
installNamespace = "testInstallNamespace"
209
229
})
210
230
211
- It ("should convert into plain manifests successfully" , func () {
231
+ It ("should convert into plain manifests successfully with AllNamespaces" , func () {
232
+ csv := baseCSV .DeepCopy ()
233
+ csv .Spec .InstallModes = []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeAllNamespaces , Supported : true }}
234
+
212
235
By ("creating a registry v1 bundle" )
213
- watchNamespaces = []string {"testWatchNs1" , "testWatchNs2 " }
236
+ watchNamespaces = []string {"" }
214
237
unstructuredSvc := convertToUnstructured (svc )
215
238
registryv1Bundle = RegistryV1 {
216
239
PackageName : "testPkg" ,
217
- CSV : csv ,
240
+ CSV : * csv ,
218
241
Others : []unstructured.Unstructured {unstructuredSvc },
219
242
}
220
243
@@ -224,41 +247,51 @@ var _ = Describe("RegistryV1 Suite", func() {
224
247
225
248
By ("verifying if plain bundle has required objects" )
226
249
Expect (plainBundle ).ShouldNot (BeNil ())
227
- Expect (plainBundle .Objects ).To (HaveLen (6 ))
250
+ Expect (plainBundle .Objects ).To (HaveLen (5 ))
251
+
252
+ By ("verifying olm.targetNamespaces annotation in the deployment's pod template" )
253
+ dep := findObjectByName ("testDeployment" , plainBundle .Objects )
254
+ Expect (dep ).NotTo (BeNil ())
255
+ Expect (dep .(* appsv1.Deployment ).Spec .Template .Annotations ).To (HaveKeyWithValue ("olm.targetNamespaces" , strings .Join (watchNamespaces , "," )))
228
256
})
229
257
230
- It ("should convert into plain manifests successfully with single namespace" , func () {
231
- csv = v1alpha1.ClusterServiceVersion {
232
- ObjectMeta : metav1.ObjectMeta {
233
- Name : "testCSV" ,
234
- },
235
- Spec : v1alpha1.ClusterServiceVersionSpec {
236
- InstallModes : []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeSingleNamespace , Supported : true }},
237
- InstallStrategy : v1alpha1.NamedInstallStrategy {
238
- StrategySpec : v1alpha1.StrategyDetailsDeployment {
239
- Permissions : []v1alpha1.StrategyDeploymentPermissions {
240
- {
241
- ServiceAccountName : "testServiceAccount" ,
242
- Rules : []rbacv1.PolicyRule {
243
- {
244
- APIGroups : []string {"test" },
245
- Resources : []string {"pods" },
246
- Verbs : []string {"*" },
247
- },
248
- },
249
- },
250
- },
251
- },
252
- },
253
- },
258
+ It ("should convert into plain manifests successfully with MultiNamespace" , func () {
259
+ csv := baseCSV .DeepCopy ()
260
+ csv .Spec .InstallModes = []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeMultiNamespace , Supported : true }}
261
+
262
+ By ("creating a registry v1 bundle" )
263
+ watchNamespaces = []string {"testWatchNs1" , "testWatchNs2" }
264
+ unstructuredSvc := convertToUnstructured (svc )
265
+ registryv1Bundle = RegistryV1 {
266
+ PackageName : "testPkg" ,
267
+ CSV : * csv ,
268
+ Others : []unstructured.Unstructured {unstructuredSvc },
254
269
}
255
270
271
+ By ("converting to plain" )
272
+ plainBundle , err := Convert (registryv1Bundle , installNamespace , watchNamespaces )
273
+ Expect (err ).NotTo (HaveOccurred ())
274
+
275
+ By ("verifying if plain bundle has required objects" )
276
+ Expect (plainBundle ).ShouldNot (BeNil ())
277
+ Expect (plainBundle .Objects ).To (HaveLen (7 ))
278
+
279
+ By ("verifying olm.targetNamespaces annotation in the deployment's pod template" )
280
+ dep := findObjectByName ("testDeployment" , plainBundle .Objects )
281
+ Expect (dep ).NotTo (BeNil ())
282
+ Expect (dep .(* appsv1.Deployment ).Spec .Template .Annotations ).To (HaveKeyWithValue ("olm.targetNamespaces" , strings .Join (watchNamespaces , "," )))
283
+ })
284
+
285
+ It ("should convert into plain manifests successfully with SingleNamespace" , func () {
286
+ csv := baseCSV .DeepCopy ()
287
+ csv .Spec .InstallModes = []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeSingleNamespace , Supported : true }}
288
+
256
289
By ("creating a registry v1 bundle" )
257
290
watchNamespaces = []string {"testWatchNs1" }
258
291
unstructuredSvc := convertToUnstructured (svc )
259
292
registryv1Bundle = RegistryV1 {
260
293
PackageName : "testPkg" ,
261
- CSV : csv ,
294
+ CSV : * csv ,
262
295
Others : []unstructured.Unstructured {unstructuredSvc },
263
296
}
264
297
@@ -268,41 +301,24 @@ var _ = Describe("RegistryV1 Suite", func() {
268
301
269
302
By ("verifying if plain bundle has required objects" )
270
303
Expect (plainBundle ).ShouldNot (BeNil ())
271
- Expect (plainBundle .Objects ).To (HaveLen (4 ))
304
+ Expect (plainBundle .Objects ).To (HaveLen (5 ))
305
+
306
+ By ("verifying olm.targetNamespaces annotation in the deployment's pod template" )
307
+ dep := findObjectByName ("testDeployment" , plainBundle .Objects )
308
+ Expect (dep ).NotTo (BeNil ())
309
+ Expect (dep .(* appsv1.Deployment ).Spec .Template .Annotations ).To (HaveKeyWithValue ("olm.targetNamespaces" , strings .Join (watchNamespaces , "," )))
272
310
})
273
311
274
312
It ("should convert into plain manifests successfully with own namespace" , func () {
275
- csv = v1alpha1.ClusterServiceVersion {
276
- ObjectMeta : metav1.ObjectMeta {
277
- Name : "testCSV" ,
278
- },
279
- Spec : v1alpha1.ClusterServiceVersionSpec {
280
- InstallModes : []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeOwnNamespace , Supported : true }},
281
- InstallStrategy : v1alpha1.NamedInstallStrategy {
282
- StrategySpec : v1alpha1.StrategyDetailsDeployment {
283
- Permissions : []v1alpha1.StrategyDeploymentPermissions {
284
- {
285
- ServiceAccountName : "testServiceAccount" ,
286
- Rules : []rbacv1.PolicyRule {
287
- {
288
- APIGroups : []string {"test" },
289
- Resources : []string {"pods" },
290
- Verbs : []string {"*" },
291
- },
292
- },
293
- },
294
- },
295
- },
296
- },
297
- },
298
- }
313
+ csv := baseCSV .DeepCopy ()
314
+ csv .Spec .InstallModes = []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeOwnNamespace , Supported : true }}
299
315
300
316
By ("creating a registry v1 bundle" )
301
317
watchNamespaces = []string {installNamespace }
302
318
unstructuredSvc := convertToUnstructured (svc )
303
319
registryv1Bundle = RegistryV1 {
304
320
PackageName : "testPkg" ,
305
- CSV : csv ,
321
+ CSV : * csv ,
306
322
Others : []unstructured.Unstructured {unstructuredSvc },
307
323
}
308
324
@@ -312,16 +328,24 @@ var _ = Describe("RegistryV1 Suite", func() {
312
328
313
329
By ("verifying if plain bundle has required objects" )
314
330
Expect (plainBundle ).ShouldNot (BeNil ())
315
- Expect (plainBundle .Objects ).To (HaveLen (4 ))
331
+ Expect (plainBundle .Objects ).To (HaveLen (5 ))
332
+
333
+ By ("verifying olm.targetNamespaces annotation in the deployment's pod template" )
334
+ dep := findObjectByName ("testDeployment" , plainBundle .Objects )
335
+ Expect (dep ).NotTo (BeNil ())
336
+ Expect (dep .(* appsv1.Deployment ).Spec .Template .Annotations ).To (HaveKeyWithValue ("olm.targetNamespaces" , strings .Join (watchNamespaces , "," )))
316
337
})
317
338
318
339
It ("should error when multinamespace mode is supported with an empty string in target namespaces" , func () {
340
+ csv := baseCSV .DeepCopy ()
341
+ csv .Spec .InstallModes = []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeMultiNamespace , Supported : true }}
342
+
319
343
By ("creating a registry v1 bundle" )
320
344
watchNamespaces = []string {"testWatchNs1" , "" }
321
345
unstructuredSvc := convertToUnstructured (svc )
322
346
registryv1Bundle = RegistryV1 {
323
347
PackageName : "testPkg" ,
324
- CSV : csv ,
348
+ CSV : * csv ,
325
349
Others : []unstructured.Unstructured {unstructuredSvc },
326
350
}
327
351
@@ -332,21 +356,15 @@ var _ = Describe("RegistryV1 Suite", func() {
332
356
})
333
357
334
358
It ("should error when single namespace mode is disabled with more than one target namespaces" , func () {
335
- csv = v1alpha1.ClusterServiceVersion {
336
- ObjectMeta : metav1.ObjectMeta {
337
- Name : "testCSV" ,
338
- },
339
- Spec : v1alpha1.ClusterServiceVersionSpec {
340
- InstallModes : []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeSingleNamespace , Supported : false }},
341
- },
342
- }
359
+ csv := baseCSV .DeepCopy ()
360
+ csv .Spec .InstallModes = []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeSingleNamespace , Supported : false }}
343
361
344
362
By ("creating a registry v1 bundle" )
345
363
watchNamespaces = []string {"testWatchNs1" , "testWatchNs2" }
346
364
unstructuredSvc := convertToUnstructured (svc )
347
365
registryv1Bundle = RegistryV1 {
348
366
PackageName : "testPkg" ,
349
- CSV : csv ,
367
+ CSV : * csv ,
350
368
Others : []unstructured.Unstructured {unstructuredSvc },
351
369
}
352
370
@@ -357,26 +375,20 @@ var _ = Describe("RegistryV1 Suite", func() {
357
375
})
358
376
359
377
It ("should error when all namespace mode is disabled with target namespace containing an empty string" , func () {
360
- csv = v1alpha1.ClusterServiceVersion {
361
- ObjectMeta : metav1.ObjectMeta {
362
- Name : "testCSV" ,
363
- },
364
- Spec : v1alpha1.ClusterServiceVersionSpec {
365
- InstallModes : []v1alpha1.InstallMode {
366
- {Type : v1alpha1 .InstallModeTypeAllNamespaces , Supported : false },
367
- {Type : v1alpha1 .InstallModeTypeOwnNamespace , Supported : true },
368
- {Type : v1alpha1 .InstallModeTypeSingleNamespace , Supported : true },
369
- {Type : v1alpha1 .InstallModeTypeMultiNamespace , Supported : true },
370
- },
371
- },
378
+ csv := baseCSV .DeepCopy ()
379
+ csv .Spec .InstallModes = []v1alpha1.InstallMode {
380
+ {Type : v1alpha1 .InstallModeTypeAllNamespaces , Supported : false },
381
+ {Type : v1alpha1 .InstallModeTypeOwnNamespace , Supported : true },
382
+ {Type : v1alpha1 .InstallModeTypeSingleNamespace , Supported : true },
383
+ {Type : v1alpha1 .InstallModeTypeMultiNamespace , Supported : true },
372
384
}
373
385
374
386
By ("creating a registry v1 bundle" )
375
387
watchNamespaces = []string {"" }
376
388
unstructuredSvc := convertToUnstructured (svc )
377
389
registryv1Bundle = RegistryV1 {
378
390
PackageName : "testPkg" ,
379
- CSV : csv ,
391
+ CSV : * csv ,
380
392
Others : []unstructured.Unstructured {unstructuredSvc },
381
393
}
382
394
@@ -387,12 +399,15 @@ var _ = Describe("RegistryV1 Suite", func() {
387
399
})
388
400
389
401
It ("should propagate csv annotations to chart metadata annotation" , func () {
402
+ csv := baseCSV .DeepCopy ()
403
+ csv .Spec .InstallModes = []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeMultiNamespace , Supported : true }}
404
+
390
405
By ("creating a registry v1 bundle" )
391
406
watchNamespaces = []string {"testWatchNs1" , "testWatchNs2" }
392
407
unstructuredSvc := convertToUnstructured (svc )
393
408
registryv1Bundle = RegistryV1 {
394
409
PackageName : "testPkg" ,
395
- CSV : csv ,
410
+ CSV : * csv ,
396
411
Others : []unstructured.Unstructured {unstructuredSvc },
397
412
}
398
413
@@ -462,11 +477,11 @@ func convertToUnstructured(obj interface{}) unstructured.Unstructured {
462
477
return unstructured.Unstructured {Object : unstructuredObj }
463
478
}
464
479
465
- func containsObject ( obj unstructured. Unstructured , result []client.Object ) client.Object {
480
+ func findObjectByName ( name string , result []client.Object ) client.Object {
466
481
for _ , o := range result {
467
482
// Since this is a controlled env, comparing only the names is sufficient for now.
468
483
// In future, compare GVKs too by ensuring its set on the unstructuredObj.
469
- if o .GetName () == obj . GetName () {
484
+ if o .GetName () == name {
470
485
return o
471
486
}
472
487
}
0 commit comments