@@ -17,9 +17,11 @@ import (
17
17
appsv1 "k8s.io/api/apps/v1"
18
18
corev1 "k8s.io/api/core/v1"
19
19
rbacv1 "k8s.io/api/rbac/v1"
20
+ apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
20
21
"k8s.io/apimachinery/pkg/api/errors"
21
22
apimeta "k8s.io/apimachinery/pkg/api/meta"
22
23
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24
+ "k8s.io/apimachinery/pkg/labels"
23
25
"k8s.io/apimachinery/pkg/types"
24
26
"k8s.io/apimachinery/pkg/util/rand"
25
27
kubeclient "k8s.io/client-go/kubernetes"
@@ -38,6 +40,20 @@ const (
38
40
var pollDuration = time .Minute
39
41
var pollInterval = time .Second
40
42
43
+ func createNamespace (ctx context.Context ) (* corev1.Namespace , error ) {
44
+ namespaceName := fmt .Sprintf ("namespace-%s" , rand .String (8 ))
45
+ ns := & corev1.Namespace {
46
+ ObjectMeta : metav1.ObjectMeta {
47
+ Name : namespaceName ,
48
+ },
49
+ }
50
+ err := c .Create (ctx , ns )
51
+ if err != nil {
52
+ return nil , err
53
+ }
54
+ return ns , nil
55
+ }
56
+
41
57
func createServiceAccount (ctx context.Context , name types.NamespacedName , clusterExtensionName string ) (* corev1.ServiceAccount , error ) {
42
58
sa := & corev1.ServiceAccount {
43
59
ObjectMeta : metav1.ObjectMeta {
@@ -177,8 +193,12 @@ func createClusterRoleAndBindingForSA(ctx context.Context, name string, sa *core
177
193
return nil
178
194
}
179
195
180
- func testInit (t * testing.T ) (* ocv1alpha1.ClusterExtension , * catalogd.ClusterCatalog , * corev1.ServiceAccount ) {
196
+ func testInit (t * testing.T ) (* ocv1alpha1.ClusterExtension , * catalogd.ClusterCatalog , * corev1.ServiceAccount , * corev1. Namespace ) {
181
197
var err error
198
+
199
+ ns , err := createNamespace (context .Background ())
200
+ require .NoError (t , err )
201
+
182
202
extensionCatalog , err := createTestCatalog (context .Background (), testCatalogName , os .Getenv (testCatalogRefEnvVar ))
183
203
require .NoError (t , err )
184
204
@@ -191,30 +211,72 @@ func testInit(t *testing.T) (*ocv1alpha1.ClusterExtension, *catalogd.ClusterCata
191
211
192
212
defaultNamespace := types.NamespacedName {
193
213
Name : clusterExtensionName ,
194
- Namespace : "default" ,
214
+ Namespace : ns . GetName () ,
195
215
}
196
216
197
217
sa , err := createServiceAccount (context .Background (), defaultNamespace , clusterExtensionName )
198
218
require .NoError (t , err )
199
- return clusterExtension , extensionCatalog , sa
219
+ return clusterExtension , extensionCatalog , sa , ns
200
220
}
201
221
202
- func testCleanup (t * testing.T , cat * catalogd.ClusterCatalog , clusterExtension * ocv1alpha1.ClusterExtension , sa * corev1.ServiceAccount ) {
222
+ func ensureNoExtensionResources (t * testing.T , clusterExtensionName string ) {
223
+ ls := labels.Set {"olm.operatorframework.io/owner-name" : clusterExtensionName }
224
+
225
+ t .Logf ("By waiting for CustomResourceDefinitions of %q to be deleted" , clusterExtensionName )
226
+ require .EventuallyWithT (t , func (ct * assert.CollectT ) {
227
+ list := & apiextensionsv1.CustomResourceDefinitionList {}
228
+ err := c .List (context .Background (), list , client.MatchingLabelsSelector {Selector : ls .AsSelector ()})
229
+ assert .NoError (ct , err )
230
+ assert .Len (ct , list .Items , 0 )
231
+ }, 2 * pollDuration , pollInterval )
232
+
233
+ t .Logf ("By waiting for ClusterRoleBindings of %q to be deleted" , clusterExtensionName )
234
+ require .EventuallyWithT (t , func (ct * assert.CollectT ) {
235
+ list := & rbacv1.ClusterRoleBindingList {}
236
+ err := c .List (context .Background (), list , client.MatchingLabelsSelector {Selector : ls .AsSelector ()})
237
+ assert .NoError (ct , err )
238
+ assert .Len (ct , list .Items , 0 )
239
+ }, 2 * pollDuration , pollInterval )
240
+
241
+ t .Logf ("By waiting for ClusterRoles of %q to be deleted" , clusterExtensionName )
242
+ require .EventuallyWithT (t , func (ct * assert.CollectT ) {
243
+ list := & rbacv1.ClusterRoleList {}
244
+ err := c .List (context .Background (), list , client.MatchingLabelsSelector {Selector : ls .AsSelector ()})
245
+ assert .NoError (ct , err )
246
+ assert .Len (ct , list .Items , 0 )
247
+ }, 2 * pollDuration , pollInterval )
248
+ }
249
+
250
+ func testCleanup (t * testing.T , cat * catalogd.ClusterCatalog , clusterExtension * ocv1alpha1.ClusterExtension , sa * corev1.ServiceAccount , ns * corev1.Namespace ) {
251
+ t .Logf ("By deleting ClusterCatalog %q" , cat .Name )
203
252
require .NoError (t , c .Delete (context .Background (), cat ))
204
253
require .Eventually (t , func () bool {
205
254
err := c .Get (context .Background (), types.NamespacedName {Name : cat .Name }, & catalogd.ClusterCatalog {})
206
255
return errors .IsNotFound (err )
207
256
}, pollDuration , pollInterval )
257
+
258
+ t .Logf ("By deleting ClusterExtension %q" , clusterExtension .Name )
208
259
require .NoError (t , c .Delete (context .Background (), clusterExtension ))
209
260
require .Eventually (t , func () bool {
210
261
err := c .Get (context .Background (), types.NamespacedName {Name : clusterExtension .Name }, & ocv1alpha1.ClusterExtension {})
211
262
return errors .IsNotFound (err )
212
263
}, pollDuration , pollInterval )
264
+
265
+ t .Logf ("By deleting ServiceAccount %q" , sa .Name )
213
266
require .NoError (t , c .Delete (context .Background (), sa ))
214
267
require .Eventually (t , func () bool {
215
268
err := c .Get (context .Background (), types.NamespacedName {Name : sa .Name , Namespace : sa .Namespace }, & corev1.ServiceAccount {})
216
269
return errors .IsNotFound (err )
217
270
}, pollDuration , pollInterval )
271
+
272
+ t .Logf ("By deleting Namespace %q" , ns .Name )
273
+ require .NoError (t , c .Delete (context .Background (), ns ))
274
+ require .Eventually (t , func () bool {
275
+ err := c .Get (context .Background (), types.NamespacedName {Name : ns .Name }, & corev1.Namespace {})
276
+ return errors .IsNotFound (err )
277
+ }, pollDuration , pollInterval )
278
+
279
+ ensureNoExtensionResources (t , clusterExtension .Name )
218
280
}
219
281
220
282
func TestClusterExtensionInstallRegistry (t * testing.T ) {
@@ -240,8 +302,8 @@ func TestClusterExtensionInstallRegistry(t *testing.T) {
240
302
t .Log ("When a cluster extension is installed from a catalog" )
241
303
t .Log ("When the extension bundle format is registry+v1" )
242
304
243
- clusterExtension , extensionCatalog , sa := testInit (t )
244
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
305
+ clusterExtension , extensionCatalog , sa , ns := testInit (t )
306
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
245
307
defer getArtifactsOutput (t )
246
308
247
309
clusterExtension .Spec = ocv1alpha1.ClusterExtensionSpec {
@@ -255,7 +317,7 @@ func TestClusterExtensionInstallRegistry(t *testing.T) {
255
317
},
256
318
},
257
319
Install : ocv1alpha1.ClusterExtensionInstallConfig {
258
- Namespace : "default" ,
320
+ Namespace : ns . Name ,
259
321
ServiceAccount : ocv1alpha1.ServiceAccountReference {
260
322
Name : sa .Name ,
261
323
},
@@ -298,8 +360,8 @@ func TestClusterExtensionInstallRegistry(t *testing.T) {
298
360
func TestClusterExtensionInstallRegistryMultipleBundles (t * testing.T ) {
299
361
t .Log ("When a cluster extension is installed from a catalog" )
300
362
301
- clusterExtension , extensionCatalog , sa := testInit (t )
302
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
363
+ clusterExtension , extensionCatalog , sa , ns := testInit (t )
364
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
303
365
defer getArtifactsOutput (t )
304
366
305
367
clusterExtension .Spec = ocv1alpha1.ClusterExtensionSpec {
@@ -310,7 +372,7 @@ func TestClusterExtensionInstallRegistryMultipleBundles(t *testing.T) {
310
372
},
311
373
},
312
374
Install : ocv1alpha1.ClusterExtensionInstallConfig {
313
- Namespace : "default" ,
375
+ Namespace : ns . Name ,
314
376
ServiceAccount : ocv1alpha1.ServiceAccountReference {
315
377
Name : sa .Name ,
316
378
},
@@ -341,8 +403,8 @@ func TestClusterExtensionBlockInstallNonSuccessorVersion(t *testing.T) {
341
403
t .Log ("When a cluster extension is installed from a catalog" )
342
404
t .Log ("When resolving upgrade edges" )
343
405
344
- clusterExtension , extensionCatalog , sa := testInit (t )
345
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
406
+ clusterExtension , extensionCatalog , sa , ns := testInit (t )
407
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
346
408
defer getArtifactsOutput (t )
347
409
348
410
t .Log ("By creating an ClusterExtension at a specified version" )
@@ -356,7 +418,7 @@ func TestClusterExtensionBlockInstallNonSuccessorVersion(t *testing.T) {
356
418
},
357
419
},
358
420
Install : ocv1alpha1.ClusterExtensionInstallConfig {
359
- Namespace : "default" ,
421
+ Namespace : ns . Name ,
360
422
ServiceAccount : ocv1alpha1.ServiceAccountReference {
361
423
Name : sa .Name ,
362
424
},
@@ -406,8 +468,8 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
406
468
t .Log ("When a cluster extension is installed from a catalog" )
407
469
t .Log ("When resolving upgrade edges" )
408
470
409
- clusterExtension , extensionCatalog , sa := testInit (t )
410
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
471
+ clusterExtension , extensionCatalog , sa , ns := testInit (t )
472
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
411
473
defer getArtifactsOutput (t )
412
474
413
475
t .Log ("By creating an ClusterExtension at a specified version" )
@@ -420,7 +482,7 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
420
482
},
421
483
},
422
484
Install : ocv1alpha1.ClusterExtensionInstallConfig {
423
- Namespace : "default" ,
485
+ Namespace : ns . Name ,
424
486
ServiceAccount : ocv1alpha1.ServiceAccountReference {
425
487
Name : sa .Name ,
426
488
},
@@ -457,8 +519,8 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
457
519
func TestClusterExtensionInstallSuccessorVersion (t * testing.T ) {
458
520
t .Log ("When a cluster extension is installed from a catalog" )
459
521
t .Log ("When resolving upgrade edges" )
460
- clusterExtension , extensionCatalog , sa := testInit (t )
461
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
522
+ clusterExtension , extensionCatalog , sa , ns := testInit (t )
523
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
462
524
defer getArtifactsOutput (t )
463
525
464
526
t .Log ("By creating an ClusterExtension at a specified version" )
@@ -471,7 +533,7 @@ func TestClusterExtensionInstallSuccessorVersion(t *testing.T) {
471
533
},
472
534
},
473
535
Install : ocv1alpha1.ClusterExtensionInstallConfig {
474
- Namespace : "default" ,
536
+ Namespace : ns . Name ,
475
537
ServiceAccount : ocv1alpha1.ServiceAccountReference {
476
538
Name : sa .Name ,
477
539
},
@@ -507,8 +569,8 @@ func TestClusterExtensionInstallSuccessorVersion(t *testing.T) {
507
569
func TestClusterExtensionInstallReResolvesWhenCatalogIsPatched (t * testing.T ) {
508
570
t .Log ("When a cluster extension is installed from a catalog" )
509
571
t .Log ("It resolves again when a catalog is patched with new ImageRef" )
510
- clusterExtension , extensionCatalog , sa := testInit (t )
511
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
572
+ clusterExtension , extensionCatalog , sa , ns := testInit (t )
573
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
512
574
defer getArtifactsOutput (t )
513
575
514
576
clusterExtension .Spec = ocv1alpha1.ClusterExtensionSpec {
@@ -528,7 +590,7 @@ func TestClusterExtensionInstallReResolvesWhenCatalogIsPatched(t *testing.T) {
528
590
},
529
591
},
530
592
Install : ocv1alpha1.ClusterExtensionInstallConfig {
531
- Namespace : "default" ,
593
+ Namespace : ns . Name ,
532
594
ServiceAccount : ocv1alpha1.ServiceAccountReference {
533
595
Name : sa .Name ,
534
596
},
@@ -593,9 +655,11 @@ func TestClusterExtensionInstallReResolvesWhenNewCatalog(t *testing.T) {
593
655
Name : clusterExtensionName ,
594
656
},
595
657
}
596
- sa , err := createServiceAccount (context .Background (), types. NamespacedName { Name : clusterExtensionName , Namespace : "default" }, clusterExtensionName )
658
+ ns , err := createNamespace (context .Background ())
597
659
require .NoError (t , err )
598
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
660
+ sa , err := createServiceAccount (context .Background (), types.NamespacedName {Name : clusterExtensionName , Namespace : ns .Name }, clusterExtensionName )
661
+ require .NoError (t , err )
662
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
599
663
defer getArtifactsOutput (t )
600
664
601
665
clusterExtension .Spec = ocv1alpha1.ClusterExtensionSpec {
@@ -609,7 +673,7 @@ func TestClusterExtensionInstallReResolvesWhenNewCatalog(t *testing.T) {
609
673
},
610
674
},
611
675
Install : ocv1alpha1.ClusterExtensionInstallConfig {
612
- Namespace : "default" ,
676
+ Namespace : ns . Name ,
613
677
ServiceAccount : ocv1alpha1.ServiceAccountReference {
614
678
Name : sa .Name ,
615
679
},
@@ -657,8 +721,8 @@ func TestClusterExtensionInstallReResolvesWhenNewCatalog(t *testing.T) {
657
721
func TestClusterExtensionInstallReResolvesWhenManagedContentChanged (t * testing.T ) {
658
722
t .Log ("When a cluster extension is installed from a catalog" )
659
723
t .Log ("It resolves again when managed content is changed" )
660
- clusterExtension , extensionCatalog , sa := testInit (t )
661
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
724
+ clusterExtension , extensionCatalog , sa , ns := testInit (t )
725
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
662
726
defer getArtifactsOutput (t )
663
727
664
728
clusterExtension .Spec = ocv1alpha1.ClusterExtensionSpec {
@@ -672,7 +736,7 @@ func TestClusterExtensionInstallReResolvesWhenManagedContentChanged(t *testing.T
672
736
},
673
737
},
674
738
Install : ocv1alpha1.ClusterExtensionInstallConfig {
675
- Namespace : "default" ,
739
+ Namespace : ns . Name ,
676
740
ServiceAccount : ocv1alpha1.ServiceAccountReference {
677
741
Name : sa .Name ,
678
742
},
@@ -712,18 +776,18 @@ func TestClusterExtensionRecoversFromInitialInstallFailedWhenFailureFixed(t *tes
712
776
t .Log ("When a cluster extension is installed from a catalog" )
713
777
t .Log ("When the extension bundle format is registry+v1" )
714
778
715
- clusterExtension , extensionCatalog , _ := testInit (t )
779
+ clusterExtension , extensionCatalog , _ , ns := testInit (t )
780
+
716
781
name := rand .String (10 )
717
782
sa := & corev1.ServiceAccount {
718
783
ObjectMeta : metav1.ObjectMeta {
719
784
Name : name ,
720
- Namespace : "default" ,
785
+ Namespace : ns . Name ,
721
786
},
722
787
}
723
788
err := c .Create (context .Background (), sa )
724
789
require .NoError (t , err )
725
-
726
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
790
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
727
791
defer getArtifactsOutput (t )
728
792
729
793
clusterExtension .Spec = ocv1alpha1.ClusterExtensionSpec {
@@ -737,7 +801,7 @@ func TestClusterExtensionRecoversFromInitialInstallFailedWhenFailureFixed(t *tes
737
801
},
738
802
},
739
803
Install : ocv1alpha1.ClusterExtensionInstallConfig {
740
- Namespace : "default" ,
804
+ Namespace : ns . Name ,
741
805
ServiceAccount : ocv1alpha1.ServiceAccountReference {
742
806
Name : sa .Name ,
743
807
},
0 commit comments