@@ -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,19 @@ const (
38
40
var pollDuration = time .Minute
39
41
var pollInterval = time .Second
40
42
43
+ func createNamespace (ctx context.Context , name string ) (* corev1.Namespace , error ) {
44
+ ns := & corev1.Namespace {
45
+ ObjectMeta : metav1.ObjectMeta {
46
+ Name : name ,
47
+ },
48
+ }
49
+ err := c .Create (ctx , ns )
50
+ if err != nil {
51
+ return nil , err
52
+ }
53
+ return ns , nil
54
+ }
55
+
41
56
func createServiceAccount (ctx context.Context , name types.NamespacedName , clusterExtensionName string ) (* corev1.ServiceAccount , error ) {
42
57
sa := & corev1.ServiceAccount {
43
58
ObjectMeta : metav1.ObjectMeta {
@@ -177,44 +192,93 @@ func createClusterRoleAndBindingForSA(ctx context.Context, name string, sa *core
177
192
return nil
178
193
}
179
194
180
- func testInit (t * testing.T ) (* ocv1alpha1.ClusterExtension , * catalogd.ClusterCatalog , * corev1.ServiceAccount ) {
195
+ func testInit (t * testing.T ) (* ocv1alpha1.ClusterExtension , * catalogd.ClusterCatalog , * corev1.ServiceAccount , * corev1. Namespace ) {
181
196
var err error
182
- extensionCatalog , err := createTestCatalog (context .Background (), testCatalogName , os .Getenv (testCatalogRefEnvVar ))
183
- require .NoError (t , err )
184
197
185
198
clusterExtensionName := fmt .Sprintf ("clusterextension-%s" , rand .String (8 ))
199
+
200
+ ns , err := createNamespace (context .Background (), clusterExtensionName )
201
+ require .NoError (t , err )
202
+
186
203
clusterExtension := & ocv1alpha1.ClusterExtension {
187
204
ObjectMeta : metav1.ObjectMeta {
188
205
Name : clusterExtensionName ,
189
206
},
190
207
}
191
208
192
- defaultNamespace := types.NamespacedName {
209
+ extensionCatalog , err := createTestCatalog (context .Background (), testCatalogName , os .Getenv (testCatalogRefEnvVar ))
210
+ require .NoError (t , err )
211
+
212
+ name := types.NamespacedName {
193
213
Name : clusterExtensionName ,
194
- Namespace : "default" ,
214
+ Namespace : ns . GetName () ,
195
215
}
196
216
197
- sa , err := createServiceAccount (context .Background (), defaultNamespace , clusterExtensionName )
217
+ sa , err := createServiceAccount (context .Background (), name , 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
+ // CRDs may take an extra long time to be deleted, and may run into the following error:
226
+ // Condition=Terminating Status=True Reason=InstanceDeletionFailed Message="could not list instances: storage is (re)initializing"
227
+ t .Logf ("By waiting for CustomResourceDefinitions of %q to be deleted" , clusterExtensionName )
228
+ require .EventuallyWithT (t , func (ct * assert.CollectT ) {
229
+ list := & apiextensionsv1.CustomResourceDefinitionList {}
230
+ err := c .List (context .Background (), list , client.MatchingLabelsSelector {Selector : ls .AsSelector ()})
231
+ assert .NoError (ct , err )
232
+ assert .Empty (ct , list .Items )
233
+ }, 5 * pollDuration , pollInterval )
234
+
235
+ t .Logf ("By waiting for ClusterRoleBindings of %q to be deleted" , clusterExtensionName )
236
+ require .EventuallyWithT (t , func (ct * assert.CollectT ) {
237
+ list := & rbacv1.ClusterRoleBindingList {}
238
+ err := c .List (context .Background (), list , client.MatchingLabelsSelector {Selector : ls .AsSelector ()})
239
+ assert .NoError (ct , err )
240
+ assert .Empty (ct , list .Items )
241
+ }, 2 * pollDuration , pollInterval )
242
+
243
+ t .Logf ("By waiting for ClusterRoles of %q to be deleted" , clusterExtensionName )
244
+ require .EventuallyWithT (t , func (ct * assert.CollectT ) {
245
+ list := & rbacv1.ClusterRoleList {}
246
+ err := c .List (context .Background (), list , client.MatchingLabelsSelector {Selector : ls .AsSelector ()})
247
+ assert .NoError (ct , err )
248
+ assert .Empty (ct , list .Items )
249
+ }, 2 * pollDuration , pollInterval )
250
+ }
251
+
252
+ func testCleanup (t * testing.T , cat * catalogd.ClusterCatalog , clusterExtension * ocv1alpha1.ClusterExtension , sa * corev1.ServiceAccount , ns * corev1.Namespace ) {
253
+ t .Logf ("By deleting ClusterCatalog %q" , cat .Name )
203
254
require .NoError (t , c .Delete (context .Background (), cat ))
204
255
require .Eventually (t , func () bool {
205
256
err := c .Get (context .Background (), types.NamespacedName {Name : cat .Name }, & catalogd.ClusterCatalog {})
206
257
return errors .IsNotFound (err )
207
258
}, pollDuration , pollInterval )
259
+
260
+ t .Logf ("By deleting ClusterExtension %q" , clusterExtension .Name )
208
261
require .NoError (t , c .Delete (context .Background (), clusterExtension ))
209
262
require .Eventually (t , func () bool {
210
263
err := c .Get (context .Background (), types.NamespacedName {Name : clusterExtension .Name }, & ocv1alpha1.ClusterExtension {})
211
264
return errors .IsNotFound (err )
212
265
}, pollDuration , pollInterval )
266
+
267
+ t .Logf ("By deleting ServiceAccount %q" , sa .Name )
213
268
require .NoError (t , c .Delete (context .Background (), sa ))
214
269
require .Eventually (t , func () bool {
215
270
err := c .Get (context .Background (), types.NamespacedName {Name : sa .Name , Namespace : sa .Namespace }, & corev1.ServiceAccount {})
216
271
return errors .IsNotFound (err )
217
272
}, pollDuration , pollInterval )
273
+
274
+ ensureNoExtensionResources (t , clusterExtension .Name )
275
+
276
+ t .Logf ("By deleting Namespace %q" , ns .Name )
277
+ require .NoError (t , c .Delete (context .Background (), ns ))
278
+ require .Eventually (t , func () bool {
279
+ err := c .Get (context .Background (), types.NamespacedName {Name : ns .Name }, & corev1.Namespace {})
280
+ return errors .IsNotFound (err )
281
+ }, pollDuration , pollInterval )
218
282
}
219
283
220
284
func TestClusterExtensionInstallRegistry (t * testing.T ) {
@@ -240,8 +304,8 @@ func TestClusterExtensionInstallRegistry(t *testing.T) {
240
304
t .Log ("When a cluster extension is installed from a catalog" )
241
305
t .Log ("When the extension bundle format is registry+v1" )
242
306
243
- clusterExtension , extensionCatalog , sa := testInit (t )
244
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
307
+ clusterExtension , extensionCatalog , sa , ns := testInit (t )
308
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
245
309
defer getArtifactsOutput (t )
246
310
247
311
clusterExtension .Spec = ocv1alpha1.ClusterExtensionSpec {
@@ -255,7 +319,7 @@ func TestClusterExtensionInstallRegistry(t *testing.T) {
255
319
},
256
320
},
257
321
Install : ocv1alpha1.ClusterExtensionInstallConfig {
258
- Namespace : "default" ,
322
+ Namespace : ns . Name ,
259
323
ServiceAccount : ocv1alpha1.ServiceAccountReference {
260
324
Name : sa .Name ,
261
325
},
@@ -298,8 +362,8 @@ func TestClusterExtensionInstallRegistry(t *testing.T) {
298
362
func TestClusterExtensionInstallRegistryMultipleBundles (t * testing.T ) {
299
363
t .Log ("When a cluster extension is installed from a catalog" )
300
364
301
- clusterExtension , extensionCatalog , sa := testInit (t )
302
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
365
+ clusterExtension , extensionCatalog , sa , ns := testInit (t )
366
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
303
367
defer getArtifactsOutput (t )
304
368
305
369
clusterExtension .Spec = ocv1alpha1.ClusterExtensionSpec {
@@ -310,7 +374,7 @@ func TestClusterExtensionInstallRegistryMultipleBundles(t *testing.T) {
310
374
},
311
375
},
312
376
Install : ocv1alpha1.ClusterExtensionInstallConfig {
313
- Namespace : "default" ,
377
+ Namespace : ns . Name ,
314
378
ServiceAccount : ocv1alpha1.ServiceAccountReference {
315
379
Name : sa .Name ,
316
380
},
@@ -341,8 +405,8 @@ func TestClusterExtensionBlockInstallNonSuccessorVersion(t *testing.T) {
341
405
t .Log ("When a cluster extension is installed from a catalog" )
342
406
t .Log ("When resolving upgrade edges" )
343
407
344
- clusterExtension , extensionCatalog , sa := testInit (t )
345
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
408
+ clusterExtension , extensionCatalog , sa , ns := testInit (t )
409
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
346
410
defer getArtifactsOutput (t )
347
411
348
412
t .Log ("By creating an ClusterExtension at a specified version" )
@@ -356,7 +420,7 @@ func TestClusterExtensionBlockInstallNonSuccessorVersion(t *testing.T) {
356
420
},
357
421
},
358
422
Install : ocv1alpha1.ClusterExtensionInstallConfig {
359
- Namespace : "default" ,
423
+ Namespace : ns . Name ,
360
424
ServiceAccount : ocv1alpha1.ServiceAccountReference {
361
425
Name : sa .Name ,
362
426
},
@@ -406,8 +470,8 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
406
470
t .Log ("When a cluster extension is installed from a catalog" )
407
471
t .Log ("When resolving upgrade edges" )
408
472
409
- clusterExtension , extensionCatalog , sa := testInit (t )
410
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
473
+ clusterExtension , extensionCatalog , sa , ns := testInit (t )
474
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
411
475
defer getArtifactsOutput (t )
412
476
413
477
t .Log ("By creating an ClusterExtension at a specified version" )
@@ -420,7 +484,7 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
420
484
},
421
485
},
422
486
Install : ocv1alpha1.ClusterExtensionInstallConfig {
423
- Namespace : "default" ,
487
+ Namespace : ns . Name ,
424
488
ServiceAccount : ocv1alpha1.ServiceAccountReference {
425
489
Name : sa .Name ,
426
490
},
@@ -457,8 +521,8 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
457
521
func TestClusterExtensionInstallSuccessorVersion (t * testing.T ) {
458
522
t .Log ("When a cluster extension is installed from a catalog" )
459
523
t .Log ("When resolving upgrade edges" )
460
- clusterExtension , extensionCatalog , sa := testInit (t )
461
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
524
+ clusterExtension , extensionCatalog , sa , ns := testInit (t )
525
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
462
526
defer getArtifactsOutput (t )
463
527
464
528
t .Log ("By creating an ClusterExtension at a specified version" )
@@ -471,7 +535,7 @@ func TestClusterExtensionInstallSuccessorVersion(t *testing.T) {
471
535
},
472
536
},
473
537
Install : ocv1alpha1.ClusterExtensionInstallConfig {
474
- Namespace : "default" ,
538
+ Namespace : ns . Name ,
475
539
ServiceAccount : ocv1alpha1.ServiceAccountReference {
476
540
Name : sa .Name ,
477
541
},
@@ -507,8 +571,8 @@ func TestClusterExtensionInstallSuccessorVersion(t *testing.T) {
507
571
func TestClusterExtensionInstallReResolvesWhenCatalogIsPatched (t * testing.T ) {
508
572
t .Log ("When a cluster extension is installed from a catalog" )
509
573
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 )
574
+ clusterExtension , extensionCatalog , sa , ns := testInit (t )
575
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
512
576
defer getArtifactsOutput (t )
513
577
514
578
clusterExtension .Spec = ocv1alpha1.ClusterExtensionSpec {
@@ -528,7 +592,7 @@ func TestClusterExtensionInstallReResolvesWhenCatalogIsPatched(t *testing.T) {
528
592
},
529
593
},
530
594
Install : ocv1alpha1.ClusterExtensionInstallConfig {
531
- Namespace : "default" ,
595
+ Namespace : ns . Name ,
532
596
ServiceAccount : ocv1alpha1.ServiceAccountReference {
533
597
Name : sa .Name ,
534
598
},
@@ -593,9 +657,11 @@ func TestClusterExtensionInstallReResolvesWhenNewCatalog(t *testing.T) {
593
657
Name : clusterExtensionName ,
594
658
},
595
659
}
596
- sa , err := createServiceAccount (context .Background (), types.NamespacedName {Name : clusterExtensionName , Namespace : "default" }, clusterExtensionName )
660
+ ns , err := createNamespace (context .Background (), clusterExtensionName )
661
+ require .NoError (t , err )
662
+ sa , err := createServiceAccount (context .Background (), types.NamespacedName {Name : clusterExtensionName , Namespace : ns .Name }, clusterExtensionName )
597
663
require .NoError (t , err )
598
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
664
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
599
665
defer getArtifactsOutput (t )
600
666
601
667
clusterExtension .Spec = ocv1alpha1.ClusterExtensionSpec {
@@ -609,7 +675,7 @@ func TestClusterExtensionInstallReResolvesWhenNewCatalog(t *testing.T) {
609
675
},
610
676
},
611
677
Install : ocv1alpha1.ClusterExtensionInstallConfig {
612
- Namespace : "default" ,
678
+ Namespace : ns . Name ,
613
679
ServiceAccount : ocv1alpha1.ServiceAccountReference {
614
680
Name : sa .Name ,
615
681
},
@@ -657,8 +723,8 @@ func TestClusterExtensionInstallReResolvesWhenNewCatalog(t *testing.T) {
657
723
func TestClusterExtensionInstallReResolvesWhenManagedContentChanged (t * testing.T ) {
658
724
t .Log ("When a cluster extension is installed from a catalog" )
659
725
t .Log ("It resolves again when managed content is changed" )
660
- clusterExtension , extensionCatalog , sa := testInit (t )
661
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
726
+ clusterExtension , extensionCatalog , sa , ns := testInit (t )
727
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
662
728
defer getArtifactsOutput (t )
663
729
664
730
clusterExtension .Spec = ocv1alpha1.ClusterExtensionSpec {
@@ -672,7 +738,7 @@ func TestClusterExtensionInstallReResolvesWhenManagedContentChanged(t *testing.T
672
738
},
673
739
},
674
740
Install : ocv1alpha1.ClusterExtensionInstallConfig {
675
- Namespace : "default" ,
741
+ Namespace : ns . Name ,
676
742
ServiceAccount : ocv1alpha1.ServiceAccountReference {
677
743
Name : sa .Name ,
678
744
},
@@ -712,18 +778,18 @@ func TestClusterExtensionRecoversFromInitialInstallFailedWhenFailureFixed(t *tes
712
778
t .Log ("When a cluster extension is installed from a catalog" )
713
779
t .Log ("When the extension bundle format is registry+v1" )
714
780
715
- clusterExtension , extensionCatalog , _ := testInit (t )
781
+ clusterExtension , extensionCatalog , _ , ns := testInit (t )
782
+
716
783
name := rand .String (10 )
717
784
sa := & corev1.ServiceAccount {
718
785
ObjectMeta : metav1.ObjectMeta {
719
786
Name : name ,
720
- Namespace : "default" ,
787
+ Namespace : ns . Name ,
721
788
},
722
789
}
723
790
err := c .Create (context .Background (), sa )
724
791
require .NoError (t , err )
725
-
726
- defer testCleanup (t , extensionCatalog , clusterExtension , sa )
792
+ defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
727
793
defer getArtifactsOutput (t )
728
794
729
795
clusterExtension .Spec = ocv1alpha1.ClusterExtensionSpec {
@@ -737,7 +803,7 @@ func TestClusterExtensionRecoversFromInitialInstallFailedWhenFailureFixed(t *tes
737
803
},
738
804
},
739
805
Install : ocv1alpha1.ClusterExtensionInstallConfig {
740
- Namespace : "default" ,
806
+ Namespace : ns . Name ,
741
807
ServiceAccount : ocv1alpha1.ServiceAccountReference {
742
808
Name : sa .Name ,
743
809
},
0 commit comments