8
8
"github.com/stretchr/testify/assert"
9
9
"github.com/stretchr/testify/require"
10
10
11
+ rbacv1 "k8s.io/api/rbac/v1"
11
12
"k8s.io/apimachinery/pkg/api/errors"
12
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
14
"k8s.io/apimachinery/pkg/labels"
@@ -16,7 +17,9 @@ import (
16
17
"github.com/operator-framework/api/pkg/operators/v1alpha1"
17
18
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned/fake"
18
19
listersv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1"
20
+ "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/cache"
19
21
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorlister/operatorlisterfakes"
22
+ "github.com/operator-framework/operator-registry/pkg/registry"
20
23
)
21
24
22
25
func TestCopyToNamespace (t * testing.T ) {
@@ -404,3 +407,65 @@ func TestCSVCopyPrototype(t *testing.T) {
404
407
},
405
408
}, dst )
406
409
}
410
+
411
+ func TestOperator_getClusterRoleAggregationRule (t * testing.T ) {
412
+ tests := []struct {
413
+ name string
414
+ apis cache.APISet
415
+ suffix string
416
+ want func (* testing.T , * rbacv1.AggregationRule )
417
+ wantErr require.ErrorAssertionFunc
418
+ }{
419
+ {
420
+ name : "no aggregation rule when no APIs" ,
421
+ apis : cache.APISet {},
422
+ suffix : "admin" ,
423
+ want : func (t * testing.T , rule * rbacv1.AggregationRule ) {
424
+ require .Nil (t , rule )
425
+ },
426
+ wantErr : require .NoError ,
427
+ },
428
+ {
429
+ name : "ordered selectors in aggregation rule" ,
430
+ apis : cache.APISet {
431
+ registry.APIKey {Group : "example.com" , Version : "v1alpha1" , Kind : "Foo" }: {},
432
+ registry.APIKey {Group : "example.com" , Version : "v1alpha2" , Kind : "Foo" }: {},
433
+ registry.APIKey {Group : "example.com" , Version : "v1alpha3" , Kind : "Foo" }: {},
434
+ registry.APIKey {Group : "example.com" , Version : "v1alpha4" , Kind : "Foo" }: {},
435
+ registry.APIKey {Group : "example.com" , Version : "v1alpha5" , Kind : "Foo" }: {},
436
+ registry.APIKey {Group : "example.com" , Version : "v1alpha1" , Kind : "Bar" }: {},
437
+ registry.APIKey {Group : "example.com" , Version : "v1alpha2" , Kind : "Bar" }: {},
438
+ registry.APIKey {Group : "example.com" , Version : "v1alpha3" , Kind : "Bar" }: {},
439
+ registry.APIKey {Group : "example.com" , Version : "v1alpha4" , Kind : "Bar" }: {},
440
+ registry.APIKey {Group : "example.com" , Version : "v1alpha5" , Kind : "Bar" }: {},
441
+ },
442
+ suffix : "admin" ,
443
+ want : func (t * testing.T , rule * rbacv1.AggregationRule ) {
444
+ getOneKey := func (t * testing.T , m map [string ]string ) string {
445
+ require .Len (t , m , 1 )
446
+ for k := range m {
447
+ return k
448
+ }
449
+ t .Fatalf ("no keys found in map" )
450
+ return ""
451
+ }
452
+
453
+ a := getOneKey (t , rule .ClusterRoleSelectors [0 ].MatchLabels )
454
+ for _ , selector := range rule .ClusterRoleSelectors [1 :] {
455
+ b := getOneKey (t , selector .MatchLabels )
456
+ require .Lessf (t , a , b , "expected selector match labels keys to be in sorted ascending order" )
457
+ a = b
458
+ }
459
+ },
460
+ wantErr : require .NoError ,
461
+ },
462
+ }
463
+ for _ , tt := range tests {
464
+ t .Run (tt .name , func (t * testing.T ) {
465
+ a := & Operator {}
466
+ got , err := a .getClusterRoleAggregationRule (tt .apis , tt .suffix )
467
+ tt .wantErr (t , err )
468
+ tt .want (t , got )
469
+ })
470
+ }
471
+ }
0 commit comments