123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- /*
- Copyright 2017 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package set
- import (
- "reflect"
- "testing"
- rbacv1 "k8s.io/api/rbac/v1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/runtime"
- "k8s.io/cli-runtime/pkg/resource"
- cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
- )
- func TestValidate(t *testing.T) {
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tests := map[string]struct {
- options *SubjectOptions
- expectErr bool
- }{
- "test-missing-subjects": {
- options: &SubjectOptions{
- Users: []string{},
- Groups: []string{},
- ServiceAccounts: []string{},
- },
- expectErr: true,
- },
- "test-invalid-serviceaccounts": {
- options: &SubjectOptions{
- Users: []string{},
- Groups: []string{},
- ServiceAccounts: []string{"foo"},
- },
- expectErr: true,
- },
- "test-missing-serviceaccounts-name": {
- options: &SubjectOptions{
- Users: []string{},
- Groups: []string{},
- ServiceAccounts: []string{"foo:"},
- },
- expectErr: true,
- },
- "test-missing-serviceaccounts-namespace": {
- options: &SubjectOptions{
- Infos: []*resource.Info{
- {
- Object: &rbacv1.ClusterRoleBinding{
- ObjectMeta: metav1.ObjectMeta{
- Name: "clusterrolebinding",
- },
- RoleRef: rbacv1.RoleRef{
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "ClusterRole",
- Name: "role",
- },
- },
- },
- },
- Users: []string{},
- Groups: []string{},
- ServiceAccounts: []string{":foo"},
- },
- expectErr: true,
- },
- "test-valid-case": {
- options: &SubjectOptions{
- Infos: []*resource.Info{
- {
- Object: &rbacv1.RoleBinding{
- ObjectMeta: metav1.ObjectMeta{
- Name: "rolebinding",
- Namespace: "one",
- },
- RoleRef: rbacv1.RoleRef{
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "ClusterRole",
- Name: "role",
- },
- },
- },
- },
- Users: []string{"foo"},
- Groups: []string{"foo"},
- ServiceAccounts: []string{"ns:foo"},
- },
- expectErr: false,
- },
- }
- for name, test := range tests {
- err := test.options.Validate()
- if test.expectErr && err != nil {
- continue
- }
- if !test.expectErr && err != nil {
- t.Errorf("%s: unexpected error: %v", name, err)
- }
- }
- }
- func TestUpdateSubjectForObject(t *testing.T) {
- tests := []struct {
- Name string
- obj runtime.Object
- subjects []rbacv1.Subject
- expected []rbacv1.Subject
- wantErr bool
- }{
- {
- Name: "invalid object type",
- obj: &rbacv1.Role{
- ObjectMeta: metav1.ObjectMeta{
- Name: "role",
- Namespace: "one",
- },
- },
- wantErr: true,
- },
- {
- Name: "add resource with users in rolebinding",
- obj: &rbacv1.RoleBinding{
- ObjectMeta: metav1.ObjectMeta{
- Name: "rolebinding",
- Namespace: "one",
- },
- Subjects: []rbacv1.Subject{
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "User",
- Name: "a",
- },
- },
- },
- subjects: []rbacv1.Subject{
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "User",
- Name: "a",
- },
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "User",
- Name: "b",
- },
- },
- expected: []rbacv1.Subject{
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "User",
- Name: "a",
- },
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "User",
- Name: "b",
- },
- },
- wantErr: false,
- },
- {
- Name: "add resource with groups in rolebinding",
- obj: &rbacv1.RoleBinding{
- ObjectMeta: metav1.ObjectMeta{
- Name: "rolebinding",
- Namespace: "one",
- },
- Subjects: []rbacv1.Subject{
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "Group",
- Name: "a",
- },
- },
- },
- subjects: []rbacv1.Subject{
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "Group",
- Name: "a",
- },
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "Group",
- Name: "b",
- },
- },
- expected: []rbacv1.Subject{
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "Group",
- Name: "a",
- },
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "Group",
- Name: "b",
- },
- },
- wantErr: false,
- },
- {
- Name: "add resource with serviceaccounts in rolebinding",
- obj: &rbacv1.RoleBinding{
- ObjectMeta: metav1.ObjectMeta{
- Name: "rolebinding",
- Namespace: "one",
- },
- Subjects: []rbacv1.Subject{
- {
- Kind: "ServiceAccount",
- Namespace: "one",
- Name: "a",
- },
- },
- },
- subjects: []rbacv1.Subject{
- {
- Kind: "ServiceAccount",
- Namespace: "one",
- Name: "a",
- },
- {
- Kind: "ServiceAccount",
- Namespace: "one",
- Name: "b",
- },
- },
- expected: []rbacv1.Subject{
- {
- Kind: "ServiceAccount",
- Namespace: "one",
- Name: "a",
- },
- {
- Kind: "ServiceAccount",
- Namespace: "one",
- Name: "b",
- },
- },
- wantErr: false,
- },
- {
- Name: "add resource with serviceaccounts in clusterrolebinding",
- obj: &rbacv1.ClusterRoleBinding{
- ObjectMeta: metav1.ObjectMeta{
- Name: "clusterrolebinding",
- },
- Subjects: []rbacv1.Subject{
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "User",
- Name: "a",
- },
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "Group",
- Name: "a",
- },
- },
- },
- subjects: []rbacv1.Subject{
- {
- Kind: "ServiceAccount",
- Namespace: "one",
- Name: "a",
- },
- },
- expected: []rbacv1.Subject{
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "User",
- Name: "a",
- },
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "Group",
- Name: "a",
- },
- {
- Kind: "ServiceAccount",
- Namespace: "one",
- Name: "a",
- },
- },
- wantErr: false,
- },
- }
- for _, tt := range tests {
- if _, err := updateSubjectForObject(tt.obj, tt.subjects, addSubjects); (err != nil) != tt.wantErr {
- t.Errorf("%q. updateSubjectForObject() error = %v, wantErr %v", tt.Name, err, tt.wantErr)
- }
- want := tt.expected
- var got []rbacv1.Subject
- switch t := tt.obj.(type) {
- case *rbacv1.RoleBinding:
- got = t.Subjects
- case *rbacv1.ClusterRoleBinding:
- got = t.Subjects
- }
- if !reflect.DeepEqual(got, want) {
- t.Errorf("%q. updateSubjectForObject() failed", tt.Name)
- t.Errorf("Got: %v", got)
- t.Errorf("Want: %v", want)
- }
- }
- }
- func TestAddSubject(t *testing.T) {
- tests := []struct {
- Name string
- existing []rbacv1.Subject
- subjects []rbacv1.Subject
- expected []rbacv1.Subject
- wantChange bool
- }{
- {
- Name: "add resource with users",
- existing: []rbacv1.Subject{
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "User",
- Name: "a",
- },
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "User",
- Name: "b",
- },
- },
- subjects: []rbacv1.Subject{
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "User",
- Name: "a",
- },
- },
- expected: []rbacv1.Subject{
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "User",
- Name: "a",
- },
- {
- APIGroup: "rbac.authorization.k8s.io",
- Kind: "User",
- Name: "b",
- },
- },
- wantChange: false,
- },
- {
- Name: "add resource with serviceaccounts",
- existing: []rbacv1.Subject{
- {
- Kind: "ServiceAccount",
- Namespace: "one",
- Name: "a",
- },
- {
- Kind: "ServiceAccount",
- Namespace: "one",
- Name: "b",
- },
- },
- subjects: []rbacv1.Subject{
- {
- Kind: "ServiceAccount",
- Namespace: "two",
- Name: "a",
- },
- },
- expected: []rbacv1.Subject{
- {
- Kind: "ServiceAccount",
- Namespace: "one",
- Name: "a",
- },
- {
- Kind: "ServiceAccount",
- Namespace: "one",
- Name: "b",
- },
- {
- Kind: "ServiceAccount",
- Namespace: "two",
- Name: "a",
- },
- },
- wantChange: true,
- },
- }
- for _, tt := range tests {
- changed := false
- got := []rbacv1.Subject{}
- if changed, got = addSubjects(tt.existing, tt.subjects); (changed != false) != tt.wantChange {
- t.Errorf("%q. addSubjects() changed = %v, wantChange = %v", tt.Name, changed, tt.wantChange)
- }
- want := tt.expected
- if !reflect.DeepEqual(got, want) {
- t.Errorf("%q. addSubjects() failed", tt.Name)
- t.Errorf("Got: %v", got)
- t.Errorf("Want: %v", want)
- }
- }
- }
|