clusterroleaggregation_controller_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package clusterroleaggregation
  14. import (
  15. "testing"
  16. rbacv1 "k8s.io/api/rbac/v1"
  17. "k8s.io/apimachinery/pkg/api/equality"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apimachinery/pkg/util/diff"
  21. fakeclient "k8s.io/client-go/kubernetes/fake"
  22. rbaclisters "k8s.io/client-go/listers/rbac/v1"
  23. clienttesting "k8s.io/client-go/testing"
  24. "k8s.io/client-go/tools/cache"
  25. "k8s.io/kubernetes/pkg/controller"
  26. )
  27. func TestSyncClusterRole(t *testing.T) {
  28. hammerRules := func() []rbacv1.PolicyRule {
  29. return []rbacv1.PolicyRule{
  30. {Verbs: []string{"hammer"}, Resources: []string{"nails"}},
  31. {Verbs: []string{"hammer"}, Resources: []string{"wedges"}},
  32. }
  33. }
  34. chiselRules := func() []rbacv1.PolicyRule {
  35. return []rbacv1.PolicyRule{
  36. {Verbs: []string{"chisel"}, Resources: []string{"mortises"}},
  37. }
  38. }
  39. sawRules := func() []rbacv1.PolicyRule {
  40. return []rbacv1.PolicyRule{
  41. {Verbs: []string{"saw"}, Resources: []string{"boards"}},
  42. }
  43. }
  44. role := func(name string, labels map[string]string, rules []rbacv1.PolicyRule) *rbacv1.ClusterRole {
  45. return &rbacv1.ClusterRole{
  46. ObjectMeta: metav1.ObjectMeta{Name: name, Labels: labels},
  47. Rules: rules,
  48. }
  49. }
  50. combinedRole := func(selectors []map[string]string, rules ...[]rbacv1.PolicyRule) *rbacv1.ClusterRole {
  51. ret := &rbacv1.ClusterRole{
  52. ObjectMeta: metav1.ObjectMeta{Name: "combined"},
  53. AggregationRule: &rbacv1.AggregationRule{},
  54. }
  55. for _, selector := range selectors {
  56. ret.AggregationRule.ClusterRoleSelectors = append(ret.AggregationRule.ClusterRoleSelectors,
  57. metav1.LabelSelector{MatchLabels: selector})
  58. }
  59. for _, currRules := range rules {
  60. ret.Rules = append(ret.Rules, currRules...)
  61. }
  62. return ret
  63. }
  64. tests := []struct {
  65. name string
  66. startingClusterRoles []*rbacv1.ClusterRole
  67. clusterRoleToSync string
  68. expectedClusterRole *rbacv1.ClusterRole
  69. }{
  70. {
  71. name: "remove dead rules",
  72. startingClusterRoles: []*rbacv1.ClusterRole{
  73. role("hammer", map[string]string{"foo": "bar"}, hammerRules()),
  74. combinedRole([]map[string]string{{"foo": "bar"}}, sawRules()),
  75. },
  76. clusterRoleToSync: "combined",
  77. expectedClusterRole: combinedRole([]map[string]string{{"foo": "bar"}}, hammerRules()),
  78. },
  79. {
  80. name: "strip rules",
  81. startingClusterRoles: []*rbacv1.ClusterRole{
  82. role("hammer", map[string]string{"foo": "not-bar"}, hammerRules()),
  83. combinedRole([]map[string]string{{"foo": "bar"}}, hammerRules()),
  84. },
  85. clusterRoleToSync: "combined",
  86. expectedClusterRole: combinedRole([]map[string]string{{"foo": "bar"}}),
  87. },
  88. {
  89. name: "select properly and put in order",
  90. startingClusterRoles: []*rbacv1.ClusterRole{
  91. role("hammer", map[string]string{"foo": "bar"}, hammerRules()),
  92. role("chisel", map[string]string{"foo": "bar"}, chiselRules()),
  93. role("saw", map[string]string{"foo": "not-bar"}, sawRules()),
  94. combinedRole([]map[string]string{{"foo": "bar"}}),
  95. },
  96. clusterRoleToSync: "combined",
  97. expectedClusterRole: combinedRole([]map[string]string{{"foo": "bar"}}, chiselRules(), hammerRules()),
  98. },
  99. {
  100. name: "select properly with multiple selectors",
  101. startingClusterRoles: []*rbacv1.ClusterRole{
  102. role("hammer", map[string]string{"foo": "bar"}, hammerRules()),
  103. role("chisel", map[string]string{"foo": "bar"}, chiselRules()),
  104. role("saw", map[string]string{"foo": "not-bar"}, sawRules()),
  105. combinedRole([]map[string]string{{"foo": "bar"}, {"foo": "not-bar"}}),
  106. },
  107. clusterRoleToSync: "combined",
  108. expectedClusterRole: combinedRole([]map[string]string{{"foo": "bar"}, {"foo": "not-bar"}}, chiselRules(), hammerRules(), sawRules()),
  109. },
  110. {
  111. name: "select properly remove duplicates",
  112. startingClusterRoles: []*rbacv1.ClusterRole{
  113. role("hammer", map[string]string{"foo": "bar"}, hammerRules()),
  114. role("chisel", map[string]string{"foo": "bar"}, chiselRules()),
  115. role("saw", map[string]string{"foo": "bar"}, sawRules()),
  116. role("other-saw", map[string]string{"foo": "not-bar"}, sawRules()),
  117. combinedRole([]map[string]string{{"foo": "bar"}, {"foo": "not-bar"}}),
  118. },
  119. clusterRoleToSync: "combined",
  120. expectedClusterRole: combinedRole([]map[string]string{{"foo": "bar"}, {"foo": "not-bar"}}, chiselRules(), hammerRules(), sawRules()),
  121. },
  122. {
  123. name: "no diff skip",
  124. startingClusterRoles: []*rbacv1.ClusterRole{
  125. role("hammer", map[string]string{"foo": "bar"}, hammerRules()),
  126. combinedRole([]map[string]string{{"foo": "bar"}}, hammerRules()),
  127. },
  128. clusterRoleToSync: "combined",
  129. expectedClusterRole: nil,
  130. }}
  131. for _, test := range tests {
  132. t.Run(test.name, func(t *testing.T) {
  133. indexer := cache.NewIndexer(controller.KeyFunc, cache.Indexers{})
  134. objs := []runtime.Object{}
  135. for _, obj := range test.startingClusterRoles {
  136. objs = append(objs, obj)
  137. indexer.Add(obj)
  138. }
  139. fakeClient := fakeclient.NewSimpleClientset(objs...)
  140. c := ClusterRoleAggregationController{
  141. clusterRoleClient: fakeClient.RbacV1(),
  142. clusterRoleLister: rbaclisters.NewClusterRoleLister(indexer),
  143. }
  144. err := c.syncClusterRole(test.clusterRoleToSync)
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. if test.expectedClusterRole == nil {
  149. if len(fakeClient.Actions()) != 0 {
  150. t.Fatalf("unexpected actions %#v", fakeClient.Actions())
  151. }
  152. return
  153. }
  154. if len(fakeClient.Actions()) != 1 {
  155. t.Fatalf("unexpected actions %#v", fakeClient.Actions())
  156. }
  157. action := fakeClient.Actions()[0]
  158. if !action.Matches("update", "clusterroles") {
  159. t.Fatalf("unexpected action %#v", action)
  160. }
  161. updateAction, ok := action.(clienttesting.UpdateAction)
  162. if !ok {
  163. t.Fatalf("unexpected action %#v", action)
  164. }
  165. if !equality.Semantic.DeepEqual(updateAction.GetObject().(*rbacv1.ClusterRole), test.expectedClusterRole) {
  166. t.Fatalf("%v", diff.ObjectDiff(test.expectedClusterRole, updateAction.GetObject().(*rbacv1.ClusterRole)))
  167. }
  168. })
  169. }
  170. }