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