k8s.io/kubernetes@v1.29.3/pkg/registry/core/serviceaccount/storage/storage_test.go (about) 1 /* 2 Copyright 2014 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 storage 18 19 import ( 20 "testing" 21 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "k8s.io/apimachinery/pkg/fields" 24 "k8s.io/apimachinery/pkg/labels" 25 "k8s.io/apimachinery/pkg/runtime" 26 "k8s.io/apiserver/pkg/registry/generic" 27 genericregistrytest "k8s.io/apiserver/pkg/registry/generic/testing" 28 etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing" 29 api "k8s.io/kubernetes/pkg/apis/core" 30 "k8s.io/kubernetes/pkg/registry/registrytest" 31 ) 32 33 func newStorage(t *testing.T) (*REST, *etcd3testing.EtcdTestServer) { 34 etcdStorage, server := registrytest.NewEtcdStorage(t, "") 35 restOptions := generic.RESTOptions{ 36 StorageConfig: etcdStorage, 37 Decorator: generic.UndecoratedStorage, 38 DeleteCollectionWorkers: 1, 39 ResourcePrefix: "serviceaccounts", 40 } 41 rest, err := NewREST(restOptions, nil, nil, 0, nil, nil, nil, false) 42 if err != nil { 43 t.Fatalf("unexpected error from REST storage: %v", err) 44 } 45 return rest, server 46 } 47 48 func validNewServiceAccount(name string) *api.ServiceAccount { 49 return &api.ServiceAccount{ 50 ObjectMeta: metav1.ObjectMeta{ 51 Name: name, 52 Namespace: metav1.NamespaceDefault, 53 }, 54 Secrets: []api.ObjectReference{}, 55 } 56 } 57 58 func TestCreate(t *testing.T) { 59 storage, server := newStorage(t) 60 defer server.Terminate(t) 61 defer storage.Store.DestroyFunc() 62 test := genericregistrytest.New(t, storage.Store) 63 serviceAccount := validNewServiceAccount("foo") 64 serviceAccount.ObjectMeta = metav1.ObjectMeta{GenerateName: "foo-"} 65 test.TestCreate( 66 // valid 67 serviceAccount, 68 // invalid 69 &api.ServiceAccount{}, 70 &api.ServiceAccount{ 71 ObjectMeta: metav1.ObjectMeta{Name: "name with spaces"}, 72 }, 73 ) 74 } 75 76 func TestUpdate(t *testing.T) { 77 storage, server := newStorage(t) 78 defer server.Terminate(t) 79 defer storage.Store.DestroyFunc() 80 test := genericregistrytest.New(t, storage.Store) 81 test.TestUpdate( 82 // valid 83 validNewServiceAccount("foo"), 84 // updateFunc 85 func(obj runtime.Object) runtime.Object { 86 object := obj.(*api.ServiceAccount) 87 object.Secrets = []api.ObjectReference{{}} 88 return object 89 }, 90 ) 91 } 92 93 func TestDelete(t *testing.T) { 94 storage, server := newStorage(t) 95 defer server.Terminate(t) 96 defer storage.Store.DestroyFunc() 97 test := genericregistrytest.New(t, storage.Store).ReturnDeletedObject() 98 test.TestDelete(validNewServiceAccount("foo")) 99 } 100 101 func TestGet(t *testing.T) { 102 storage, server := newStorage(t) 103 defer server.Terminate(t) 104 defer storage.Store.DestroyFunc() 105 test := genericregistrytest.New(t, storage.Store) 106 test.TestGet(validNewServiceAccount("foo")) 107 } 108 109 func TestList(t *testing.T) { 110 storage, server := newStorage(t) 111 defer server.Terminate(t) 112 defer storage.Store.DestroyFunc() 113 test := genericregistrytest.New(t, storage.Store) 114 test.TestList(validNewServiceAccount("foo")) 115 } 116 117 func TestWatch(t *testing.T) { 118 storage, server := newStorage(t) 119 defer server.Terminate(t) 120 defer storage.Store.DestroyFunc() 121 test := genericregistrytest.New(t, storage.Store) 122 test.TestWatch( 123 validNewServiceAccount("foo"), 124 // matching labels 125 []labels.Set{}, 126 // not matching labels 127 []labels.Set{ 128 {"foo": "bar"}, 129 }, 130 // matching fields 131 []fields.Set{ 132 {"metadata.name": "foo"}, 133 }, 134 // not matching fields 135 []fields.Set{ 136 {"metadata.name": "bar"}, 137 {"name": "foo"}, 138 }, 139 ) 140 } 141 142 func TestShortNames(t *testing.T) { 143 storage, server := newStorage(t) 144 defer server.Terminate(t) 145 defer storage.Store.DestroyFunc() 146 expected := []string{"sa"} 147 registrytest.AssertShortNames(t, storage, expected) 148 }