...
1
16
17 package bootstrappolicy
18
19 import (
20 "reflect"
21 "testing"
22
23 "k8s.io/apimachinery/pkg/api/meta"
24 "k8s.io/apimachinery/pkg/util/sets"
25 )
26
27
28
29
30 var rolesWithAllowStar = sets.NewString(
31 saRolePrefix+"namespace-controller",
32 saRolePrefix+"generic-garbage-collector",
33 saRolePrefix+"resourcequota-controller",
34 saRolePrefix+"horizontal-pod-autoscaler",
35 saRolePrefix+"clusterrole-aggregation-controller",
36 saRolePrefix+"disruption-controller",
37 )
38
39
40
41
42
43 func TestNoStarsForControllers(t *testing.T) {
44 for _, role := range ControllerRoles() {
45 if rolesWithAllowStar.Has(role.Name) {
46 continue
47 }
48
49 for i, rule := range role.Rules {
50 for j, verb := range rule.Verbs {
51 if verb == "*" {
52 t.Errorf("%s.Rule[%d].Verbs[%d] is star", role.Name, i, j)
53 }
54 }
55 for j, group := range rule.APIGroups {
56 if group == "*" {
57 t.Errorf("%s.Rule[%d].APIGroups[%d] is star", role.Name, i, j)
58 }
59 }
60 for j, resource := range rule.Resources {
61 if resource == "*" {
62 t.Errorf("%s.Rule[%d].Resources[%d] is star", role.Name, i, j)
63 }
64 }
65 }
66 }
67 }
68
69 func TestControllerRoleLabel(t *testing.T) {
70 roles := ControllerRoles()
71 for i := range roles {
72 role := roles[i]
73 accessor, err := meta.Accessor(&role)
74 if err != nil {
75 t.Fatalf("unexpected error: %v", err)
76 }
77 if got, want := accessor.GetLabels(), map[string]string{"kubernetes.io/bootstrapping": "rbac-defaults"}; !reflect.DeepEqual(got, want) {
78 t.Errorf("ClusterRole: %s GetLabels() = %s, want %s", accessor.GetName(), got, want)
79 }
80 }
81
82 rolebindings := ControllerRoleBindings()
83 for i := range rolebindings {
84 rolebinding := rolebindings[i]
85 accessor, err := meta.Accessor(&rolebinding)
86 if err != nil {
87 t.Fatalf("unexpected error: %v", err)
88 }
89 if got, want := accessor.GetLabels(), map[string]string{"kubernetes.io/bootstrapping": "rbac-defaults"}; !reflect.DeepEqual(got, want) {
90 t.Errorf("ClusterRoleBinding: %s GetLabels() = %s, want %s", accessor.GetName(), got, want)
91 }
92 }
93 }
94
View as plain text