Debian Codegraph @

auth/rbac/reconciliation/reconcile_rolebindings_test.go

raw symbols SCIP
1 /*
2 Copyright 2017 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package reconciliation
18
19 import (
20 "testing"
21
22 rbacv1 "k8s.io/api/rbac/v1"
23 apiequality "k8s.io/apimachinery/pkg/api/equality"
24 )
25
26 func binding(roleRef rbacv1.RoleRef, subjects []rbacv1.Subject) *rbacv1.ClusterRoleBinding {
27 return &rbacv1.ClusterRoleBinding{RoleRef: roleRef, Subjects: subjects}
28 }
29
30 func ref(name string) rbacv1.RoleRef {
31 return rbacv1.RoleRef{Name: name}
32 }
33
34 func subject(name string) rbacv1.Subject {
35 return rbacv1.Subject{Name: name}
36 }
37
38 func subjects(names ...string) []rbacv1.Subject {
39 r := []rbacv1.Subject{}
40 for _, name := range names {
41 r = append(r, subject(name))
42 }
43 return r
44 }
45
46 func TestDiffObjectReferenceLists(t *testing.T) {
47 tests := map[string]struct {
48 A []rbacv1.Subject
49 B []rbacv1.Subject
50 ExpectedOnlyA []rbacv1.Subject
51 ExpectedOnlyB []rbacv1.Subject
52 }{
53 "empty": {},
54
55 "matching, order-independent": {
56 A: subjects("foo", "bar"),
57 B: subjects("bar", "foo"),
58 },
59
60 "partial match": {
61 A: subjects("foo", "bar"),
62 B: subjects("foo", "baz"),
63 ExpectedOnlyA: subjects("bar"),
64 ExpectedOnlyB: subjects("baz"),
65 },
66
67 "missing": {
68 A: subjects("foo"),
69 B: subjects("bar"),
70 ExpectedOnlyA: subjects("foo"),
71 ExpectedOnlyB: subjects("bar"),
72 },
73
74 "remove duplicates": {
75 A: subjects("foo", "foo"),
76 B: subjects("bar", "bar"),
77 ExpectedOnlyA: subjects("foo"),
78 ExpectedOnlyB: subjects("bar"),
79 },
80 }
81
82 for k, tc := range tests {
83 onlyA, onlyB := diffSubjectLists(tc.A, tc.B)
84 if !apiequality.Semantic.DeepEqual(onlyA, tc.ExpectedOnlyA) {
85 t.Errorf("%s: Expected %#v, got %#v", k, tc.ExpectedOnlyA, onlyA)
86 }
87 if !apiequality.Semantic.DeepEqual(onlyB, tc.ExpectedOnlyB) {
88 t.Errorf("%s: Expected %#v, got %#v", k, tc.ExpectedOnlyB, onlyB)
89 }
90 }
91 }
92
93 func TestComputeUpdate(t *testing.T) {
94 tests := map[string]struct {
95 ExpectedBinding *rbacv1.ClusterRoleBinding
96 ActualBinding *rbacv1.ClusterRoleBinding
97 RemoveExtraSubjects bool
98
99 ExpectedUpdatedBinding *rbacv1.ClusterRoleBinding
100 ExpectedUpdateNeeded bool
101 }{
102 "match without union": {
103 ExpectedBinding: binding(ref("role"), subjects("a")),
104 ActualBinding: binding(ref("role"), subjects("a")),
105 RemoveExtraSubjects: true,
106
107 ExpectedUpdatedBinding: nil,
108 ExpectedUpdateNeeded: false,
109 },
110 "match with union": {
111 ExpectedBinding: binding(ref("role"), subjects("a")),
112 ActualBinding: binding(ref("role"), subjects("a")),
113 RemoveExtraSubjects: false,
114
115 ExpectedUpdatedBinding: nil,
116 ExpectedUpdateNeeded: false,
117 },
118
119 "different roleref with identical subjects": {
120 ExpectedBinding: binding(ref("role"), subjects("a")),
121 ActualBinding: binding(ref("differentRole"), subjects("a")),
122 RemoveExtraSubjects: false,
123
124 ExpectedUpdatedBinding: binding(ref("role"), subjects("a")),
125 ExpectedUpdateNeeded: true,
126 },
127
128 "extra subjects without union": {
129 ExpectedBinding: binding(ref("role"), subjects("a")),
130 ActualBinding: binding(ref("role"), subjects("a", "b")),
131 RemoveExtraSubjects: true,
132
133 ExpectedUpdatedBinding: binding(ref("role"), subjects("a")),
134 ExpectedUpdateNeeded: true,
135 },
136 "extra subjects with union": {
137 ExpectedBinding: binding(ref("role"), subjects("a")),
138 ActualBinding: binding(ref("role"), subjects("a", "b")),
139 RemoveExtraSubjects: false,
140
141 ExpectedUpdatedBinding: nil,
142 ExpectedUpdateNeeded: false,
143 },
144
145 "missing subjects without union": {
146 ExpectedBinding: binding(ref("role"), subjects("a", "c")),
147 ActualBinding: binding(ref("role"), subjects("a", "b")),
148 RemoveExtraSubjects: true,
149
150 ExpectedUpdatedBinding: binding(ref("role"), subjects("a", "c")),
151 ExpectedUpdateNeeded: true,
152 },
153 "missing subjects with union": {
154 ExpectedBinding: binding(ref("role"), subjects("a", "c")),
155 ActualBinding: binding(ref("role"), subjects("a", "b")),
156 RemoveExtraSubjects: false,
157
158 ExpectedUpdatedBinding: binding(ref("role"), subjects("a", "b", "c")),
159 ExpectedUpdateNeeded: true,
160 },
161 }
162
163 for k, tc := range tests {
164 actualRoleBinding := ClusterRoleBindingAdapter{ClusterRoleBinding: tc.ActualBinding}
165 expectedRoleBinding := ClusterRoleBindingAdapter{ClusterRoleBinding: tc.ExpectedBinding}
166 result, err := computeReconciledRoleBinding(actualRoleBinding, expectedRoleBinding, tc.RemoveExtraSubjects)
167 if err != nil {
168 t.Errorf("%s: %v", k, err)
169 continue
170 }
171 updateNeeded := result.Operation != ReconcileNone
172 updatedBinding := result.RoleBinding.(ClusterRoleBindingAdapter).ClusterRoleBinding
173 if updateNeeded != tc.ExpectedUpdateNeeded {
174 t.Errorf("%s: Expected\n\t%v\ngot\n\t%v (%v)", k, tc.ExpectedUpdateNeeded, updateNeeded, result.Operation)
175 continue
176 }
177 if updateNeeded && !apiequality.Semantic.DeepEqual(updatedBinding, tc.ExpectedUpdatedBinding) {
178 t.Errorf("%s: Expected\n\t%v %v\ngot\n\t%v %v", k, tc.ExpectedUpdatedBinding.RoleRef, tc.ExpectedUpdatedBinding.Subjects, updatedBinding.RoleRef, updatedBinding.Subjects)
179 }
180 }
181 }