|
| 1 | +/* |
| 2 | +Copyright 2024. |
| 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 support |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/onsi/gomega" |
| 23 | + |
| 24 | + rbacv1 "k8s.io/api/rbac/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | +) |
| 27 | + |
| 28 | +func TestCreateUserRoleBinding(t *testing.T) { |
| 29 | + |
| 30 | + test := NewTest(t) |
| 31 | + |
| 32 | + role := &rbacv1.Role{ |
| 33 | + TypeMeta: metav1.TypeMeta{ |
| 34 | + APIVersion: rbacv1.SchemeGroupVersion.String(), |
| 35 | + Kind: "Role", |
| 36 | + }, |
| 37 | + ObjectMeta: metav1.ObjectMeta{ |
| 38 | + Name: "role1", |
| 39 | + Namespace: "ns-1", |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + rb := CreateUserRoleBinding(test, "ns-1", "user-1", role) |
| 44 | + |
| 45 | + test.Expect(rb).To(gomega.Not(gomega.BeNil())) |
| 46 | + test.Expect(rb.GenerateName).To(gomega.Equal("rb-")) |
| 47 | + test.Expect(rb.Namespace).To(gomega.Equal("ns-1")) |
| 48 | + |
| 49 | + test.Expect(rb.RoleRef.APIGroup).To(gomega.Equal(rbacv1.SchemeGroupVersion.Group)) |
| 50 | + test.Expect(rb.RoleRef.Kind).To(gomega.Equal("Role")) |
| 51 | + test.Expect(rb.RoleRef.Name).To(gomega.Equal("role1")) |
| 52 | + |
| 53 | + test.Expect(rb.Subjects[0].APIGroup).To(gomega.Equal(rbacv1.SchemeGroupVersion.Group)) |
| 54 | + test.Expect(rb.Subjects[0].Kind).To(gomega.Equal("User")) |
| 55 | + test.Expect(rb.Subjects[0].Name).To(gomega.Equal("user-1")) |
| 56 | +} |
| 57 | + |
| 58 | +func TestCreateUserClusterRoleBinding(t *testing.T) { |
| 59 | + |
| 60 | + test := NewTest(t) |
| 61 | + |
| 62 | + crole := &rbacv1.ClusterRole{ |
| 63 | + TypeMeta: metav1.TypeMeta{ |
| 64 | + APIVersion: rbacv1.SchemeGroupVersion.String(), |
| 65 | + Kind: "ClusterRole", |
| 66 | + }, |
| 67 | + ObjectMeta: metav1.ObjectMeta{ |
| 68 | + Name: "role1", |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + rb := CreateUserClusterRoleBinding(test, "user-1", crole) |
| 73 | + |
| 74 | + test.Expect(rb).To(gomega.Not(gomega.BeNil())) |
| 75 | + test.Expect(rb.GenerateName).To(gomega.Equal("crb-")) |
| 76 | + |
| 77 | + test.Expect(rb.RoleRef.APIGroup).To(gomega.Equal(rbacv1.SchemeGroupVersion.Group)) |
| 78 | + test.Expect(rb.RoleRef.Kind).To(gomega.Equal("ClusterRole")) |
| 79 | + test.Expect(rb.RoleRef.Name).To(gomega.Equal("role1")) |
| 80 | + |
| 81 | + test.Expect(rb.Subjects[0].APIGroup).To(gomega.Equal(rbacv1.SchemeGroupVersion.Group)) |
| 82 | + test.Expect(rb.Subjects[0].Kind).To(gomega.Equal("User")) |
| 83 | + test.Expect(rb.Subjects[0].Name).To(gomega.Equal("user-1")) |
| 84 | +} |
0 commit comments