@@ -2,11 +2,13 @@ package convert
22
33import (
44 "fmt"
5+ "strings"
56 "testing"
67
78 . "github.com/onsi/ginkgo/v2"
89 . "github.com/onsi/gomega"
910
11+ appsv1 "k8s.io/api/apps/v1"
1012 corev1 "k8s.io/api/core/v1"
1113 rbacv1 "k8s.io/api/rbac/v1"
1214 schedulingv1 "k8s.io/api/scheduling/v1"
@@ -73,7 +75,7 @@ var _ = Describe("RegistryV1 Suite", func() {
7375 Expect (plainBundle .Objects ).To (HaveLen (1 ))
7476
7577 By ("verifying if ns has been set correctly" )
76- resObj := containsObject ( unstructuredSvc , plainBundle .Objects )
78+ resObj := findObjectByName ( svc . Name , plainBundle .Objects )
7779 Expect (resObj ).NotTo (BeNil ())
7880 Expect (resObj .GetNamespace ()).To (BeEquivalentTo (installNamespace ))
7981 })
@@ -99,7 +101,7 @@ var _ = Describe("RegistryV1 Suite", func() {
99101 Expect (plainBundle .Objects ).To (HaveLen (1 ))
100102
101103 By ("verifying if ns has been set correctly" )
102- resObj := containsObject ( unstructuredSvc , plainBundle .Objects )
104+ resObj := findObjectByName ( svc . Name , plainBundle .Objects )
103105 Expect (resObj ).NotTo (BeNil ())
104106 Expect (resObj .GetNamespace ()).To (BeEquivalentTo (installNamespace ))
105107 })
@@ -157,7 +159,7 @@ var _ = Describe("RegistryV1 Suite", func() {
157159 Expect (plainBundle .Objects ).To (HaveLen (1 ))
158160
159161 By ("verifying if ns has been set correctly" )
160- resObj := containsObject ( unstructuredpriorityclass , plainBundle .Objects )
162+ resObj := findObjectByName ( pc . Name , plainBundle .Objects )
161163 Expect (resObj ).NotTo (BeNil ())
162164 Expect (resObj .GetNamespace ()).To (BeEmpty ())
163165 })
@@ -167,22 +169,39 @@ var _ = Describe("RegistryV1 Suite", func() {
167169 Context ("Should generate objects successfully based on target namespaces" , func () {
168170 var (
169171 svc corev1.Service
170- csv v1alpha1.ClusterServiceVersion
172+ baseCSV v1alpha1.ClusterServiceVersion
171173 watchNamespaces []string
172174 )
173175
174176 BeforeEach (func () {
175- csv = v1alpha1.ClusterServiceVersion {
177+ // base CSV definition that each test case will deep copy and modify
178+ baseCSV = v1alpha1.ClusterServiceVersion {
176179 ObjectMeta : metav1.ObjectMeta {
177180 Name : "testCSV" ,
178181 Annotations : map [string ]string {
179182 "olm.properties" : fmt .Sprintf ("[{\" type\" : %s, \" value\" : \" %s\" }]" , property .TypeConstraint , "value" ),
180183 },
181184 },
182185 Spec : v1alpha1.ClusterServiceVersionSpec {
183- InstallModes : []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeMultiNamespace , Supported : true }},
184186 InstallStrategy : v1alpha1.NamedInstallStrategy {
185187 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+ },
186205 Permissions : []v1alpha1.StrategyDeploymentPermissions {
187206 {
188207 ServiceAccountName : "testServiceAccount" ,
@@ -199,6 +218,7 @@ var _ = Describe("RegistryV1 Suite", func() {
199218 },
200219 },
201220 }
221+
202222 svc = corev1.Service {
203223 ObjectMeta : metav1.ObjectMeta {
204224 Name : "testService" ,
@@ -208,13 +228,16 @@ var _ = Describe("RegistryV1 Suite", func() {
208228 installNamespace = "testInstallNamespace"
209229 })
210230
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+
212235 By ("creating a registry v1 bundle" )
213- watchNamespaces = []string {"testWatchNs1" , "testWatchNs2 " }
236+ watchNamespaces = []string {"" }
214237 unstructuredSvc := convertToUnstructured (svc )
215238 registryv1Bundle = RegistryV1 {
216239 PackageName : "testPkg" ,
217- CSV : csv ,
240+ CSV : * csv ,
218241 Others : []unstructured.Unstructured {unstructuredSvc },
219242 }
220243
@@ -224,41 +247,51 @@ var _ = Describe("RegistryV1 Suite", func() {
224247
225248 By ("verifying if plain bundle has required objects" )
226249 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 , "," )))
228256 })
229257
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 },
254269 }
255270
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+
256289 By ("creating a registry v1 bundle" )
257290 watchNamespaces = []string {"testWatchNs1" }
258291 unstructuredSvc := convertToUnstructured (svc )
259292 registryv1Bundle = RegistryV1 {
260293 PackageName : "testPkg" ,
261- CSV : csv ,
294+ CSV : * csv ,
262295 Others : []unstructured.Unstructured {unstructuredSvc },
263296 }
264297
@@ -268,41 +301,24 @@ var _ = Describe("RegistryV1 Suite", func() {
268301
269302 By ("verifying if plain bundle has required objects" )
270303 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 , "," )))
272310 })
273311
274312 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 }}
299315
300316 By ("creating a registry v1 bundle" )
301317 watchNamespaces = []string {installNamespace }
302318 unstructuredSvc := convertToUnstructured (svc )
303319 registryv1Bundle = RegistryV1 {
304320 PackageName : "testPkg" ,
305- CSV : csv ,
321+ CSV : * csv ,
306322 Others : []unstructured.Unstructured {unstructuredSvc },
307323 }
308324
@@ -312,16 +328,24 @@ var _ = Describe("RegistryV1 Suite", func() {
312328
313329 By ("verifying if plain bundle has required objects" )
314330 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 , "," )))
316337 })
317338
318339 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+
319343 By ("creating a registry v1 bundle" )
320344 watchNamespaces = []string {"testWatchNs1" , "" }
321345 unstructuredSvc := convertToUnstructured (svc )
322346 registryv1Bundle = RegistryV1 {
323347 PackageName : "testPkg" ,
324- CSV : csv ,
348+ CSV : * csv ,
325349 Others : []unstructured.Unstructured {unstructuredSvc },
326350 }
327351
@@ -332,21 +356,15 @@ var _ = Describe("RegistryV1 Suite", func() {
332356 })
333357
334358 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 }}
343361
344362 By ("creating a registry v1 bundle" )
345363 watchNamespaces = []string {"testWatchNs1" , "testWatchNs2" }
346364 unstructuredSvc := convertToUnstructured (svc )
347365 registryv1Bundle = RegistryV1 {
348366 PackageName : "testPkg" ,
349- CSV : csv ,
367+ CSV : * csv ,
350368 Others : []unstructured.Unstructured {unstructuredSvc },
351369 }
352370
@@ -357,26 +375,20 @@ var _ = Describe("RegistryV1 Suite", func() {
357375 })
358376
359377 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 },
372384 }
373385
374386 By ("creating a registry v1 bundle" )
375387 watchNamespaces = []string {"" }
376388 unstructuredSvc := convertToUnstructured (svc )
377389 registryv1Bundle = RegistryV1 {
378390 PackageName : "testPkg" ,
379- CSV : csv ,
391+ CSV : * csv ,
380392 Others : []unstructured.Unstructured {unstructuredSvc },
381393 }
382394
@@ -387,12 +399,15 @@ var _ = Describe("RegistryV1 Suite", func() {
387399 })
388400
389401 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+
390405 By ("creating a registry v1 bundle" )
391406 watchNamespaces = []string {"testWatchNs1" , "testWatchNs2" }
392407 unstructuredSvc := convertToUnstructured (svc )
393408 registryv1Bundle = RegistryV1 {
394409 PackageName : "testPkg" ,
395- CSV : csv ,
410+ CSV : * csv ,
396411 Others : []unstructured.Unstructured {unstructuredSvc },
397412 }
398413
@@ -462,11 +477,11 @@ func convertToUnstructured(obj interface{}) unstructured.Unstructured {
462477 return unstructured.Unstructured {Object : unstructuredObj }
463478}
464479
465- func containsObject ( obj unstructured. Unstructured , result []client.Object ) client.Object {
480+ func findObjectByName ( name string , result []client.Object ) client.Object {
466481 for _ , o := range result {
467482 // Since this is a controlled env, comparing only the names is sufficient for now.
468483 // In future, compare GVKs too by ensuring its set on the unstructuredObj.
469- if o .GetName () == obj . GetName () {
484+ if o .GetName () == name {
470485 return o
471486 }
472487 }
0 commit comments