|
| 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 rbac_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "k8s.io/apimachinery/pkg/runtime" |
| 24 | + "k8s.io/apimachinery/pkg/util/diff" |
| 25 | + "k8s.io/kubernetes/pkg/api" |
| 26 | + "k8s.io/kubernetes/pkg/apis/rbac" |
| 27 | + "k8s.io/kubernetes/pkg/apis/rbac/v1" |
| 28 | + |
| 29 | + // install RBAC types |
| 30 | + _ "k8s.io/kubernetes/pkg/apis/rbac/install" |
| 31 | +) |
| 32 | + |
| 33 | +// TestHelpersRoundTrip confirms that the rbac.New* helper functions produce RBAC objects that match objects |
| 34 | +// that have gone through conversion and defaulting. This is required because these helper functions are |
| 35 | +// used to create the bootstrap RBAC policy which is used during reconciliation. If they produced objects |
| 36 | +// that did not match, reconciliation would incorrectly add duplicate data to the cluster's RBAC policy. |
| 37 | +func TestHelpersRoundTrip(t *testing.T) { |
| 38 | + rb := rbac.NewRoleBinding("role", "ns").Groups("g").SAs("ns", "sa").Users("u").BindingOrDie() |
| 39 | + rbcr := rbac.NewRoleBindingForClusterRole("role", "ns").Groups("g").SAs("ns", "sa").Users("u").BindingOrDie() |
| 40 | + crb := rbac.NewClusterBinding("role").Groups("g").SAs("ns", "sa").Users("u").BindingOrDie() |
| 41 | + |
| 42 | + role := &rbac.Role{ |
| 43 | + Rules: []rbac.PolicyRule{ |
| 44 | + rbac.NewRule("verb").Groups("g").Resources("foo").RuleOrDie(), |
| 45 | + rbac.NewRule("verb").URLs("/foo").RuleOrDie(), |
| 46 | + }, |
| 47 | + } |
| 48 | + clusterRole := &rbac.ClusterRole{ |
| 49 | + Rules: []rbac.PolicyRule{ |
| 50 | + rbac.NewRule("verb").Groups("g").Resources("foo").RuleOrDie(), |
| 51 | + rbac.NewRule("verb").URLs("/foo").RuleOrDie(), |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for _, internalObj := range []runtime.Object{&rb, &rbcr, &crb, role, clusterRole} { |
| 56 | + v1Obj, err := api.Scheme.ConvertToVersion(internalObj, v1.SchemeGroupVersion) |
| 57 | + if err != nil { |
| 58 | + t.Errorf("err on %T: %v", internalObj, err) |
| 59 | + continue |
| 60 | + } |
| 61 | + api.Scheme.Default(v1Obj) |
| 62 | + roundTrippedObj, err := api.Scheme.ConvertToVersion(v1Obj, rbac.SchemeGroupVersion) |
| 63 | + if err != nil { |
| 64 | + t.Errorf("err on %T: %v", internalObj, err) |
| 65 | + continue |
| 66 | + } |
| 67 | + if !reflect.DeepEqual(internalObj, roundTrippedObj) { |
| 68 | + t.Errorf("err on %T: got difference:\n%s", internalObj, diff.ObjectDiff(internalObj, roundTrippedObj)) |
| 69 | + continue |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments