k8s.io/kubernetes@v1.29.3/pkg/registry/apps/controllerrevision/storage/storage_test.go (about) 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 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 "k8s.io/kubernetes/pkg/apis/apps" 30 api "k8s.io/kubernetes/pkg/apis/core" 31 "k8s.io/kubernetes/pkg/registry/registrytest" 32 ) 33 34 func TestCreate(t *testing.T) { 35 storage, server := newStorage(t) 36 defer server.Terminate(t) 37 defer storage.Store.DestroyFunc() 38 test := genericregistrytest.New(t, storage.Store) 39 var ( 40 valid = stripObjectMeta(newControllerRevision("validname", metav1.NamespaceDefault, newObject(), 0)) 41 badRevision = stripObjectMeta(newControllerRevision("validname", "validns", newObject(), -1)) 42 emptyName = stripObjectMeta(newControllerRevision("", "validns", newObject(), 0)) 43 invalidName = stripObjectMeta(newControllerRevision("NoUppercaseOrSpecialCharsLike=Equals", "validns", newObject(), 0)) 44 emptyNs = stripObjectMeta(newControllerRevision("validname", "", newObject(), 100)) 45 invalidNs = stripObjectMeta(newControllerRevision("validname", "NoUppercaseOrSpecialCharsLike=Equals", newObject(), 100)) 46 nilData = stripObjectMeta(newControllerRevision("validname", "validns", nil, 0)) 47 ) 48 test.TestCreate( 49 valid, 50 badRevision, 51 emptyName, 52 invalidName, 53 emptyNs, 54 invalidNs, 55 nilData) 56 } 57 58 func TestUpdate(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 64 addLabel := func(obj runtime.Object) runtime.Object { 65 rev := obj.(*apps.ControllerRevision) 66 update := &apps.ControllerRevision{ 67 ObjectMeta: rev.ObjectMeta, 68 Data: rev.Data, 69 Revision: rev.Revision, 70 } 71 update.ObjectMeta.Labels = map[string]string{"foo": "bar"} 72 return update 73 } 74 75 updateData := func(obj runtime.Object) runtime.Object { 76 rev := obj.(*apps.ControllerRevision) 77 modified := newObject() 78 ss := modified.(*apps.StatefulSet) 79 ss.Name = "cde" 80 update := &apps.ControllerRevision{ 81 ObjectMeta: rev.ObjectMeta, 82 Data: ss, 83 Revision: rev.Revision + 1, 84 } 85 return update 86 } 87 88 test.TestUpdate(stripObjectMeta(newControllerRevision("validname", metav1.NamespaceDefault, newObject(), 0)), 89 addLabel, 90 updateData) 91 } 92 93 func TestGet(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) 98 test.TestGet(newControllerRevision("valid", metav1.NamespaceDefault, newObject(), 0)) 99 } 100 101 func TestList(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.TestList(newControllerRevision("valid", metav1.NamespaceDefault, newObject(), 0)) 107 } 108 109 func TestDelete(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.TestDelete(newControllerRevision("valid", metav1.NamespaceDefault, newObject(), 0)) 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 newControllerRevision("valid", metav1.NamespaceDefault, newObject(), 0), 124 []labels.Set{ 125 {"foo": "bar"}, 126 }, 127 []labels.Set{ 128 {"hoo": "baz"}, 129 }, 130 []fields.Set{ 131 {"metadata.name": "valid"}, 132 }, 133 []fields.Set{ 134 {"metadata.name": "nomatch"}, 135 }, 136 ) 137 } 138 139 func newControllerRevision(name, namespace string, data runtime.Object, revision int64) *apps.ControllerRevision { 140 return &apps.ControllerRevision{ 141 ObjectMeta: metav1.ObjectMeta{ 142 Name: name, 143 Namespace: namespace, 144 Labels: map[string]string{"foo": "bar"}, 145 }, 146 Data: data, 147 Revision: revision, 148 } 149 } 150 151 func stripObjectMeta(revision *apps.ControllerRevision) *apps.ControllerRevision { 152 revision.ObjectMeta = metav1.ObjectMeta{} 153 return revision 154 } 155 156 func newStorage(t *testing.T) (*REST, *etcd3testing.EtcdTestServer) { 157 etcdStorage, server := registrytest.NewEtcdStorage(t, apps.GroupName) 158 restOptions := generic.RESTOptions{ 159 StorageConfig: etcdStorage, 160 Decorator: generic.UndecoratedStorage, 161 DeleteCollectionWorkers: 1, 162 ResourcePrefix: "controllerrevisions"} 163 storage, err := NewREST(restOptions) 164 if err != nil { 165 t.Fatalf("unexpected error from REST storage: %v", err) 166 } 167 return storage, server 168 } 169 170 func newObject() runtime.Object { 171 return &apps.StatefulSet{ 172 ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: metav1.NamespaceDefault}, 173 Spec: apps.StatefulSetSpec{ 174 Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}}, 175 Template: api.PodTemplateSpec{ 176 Spec: api.PodSpec{ 177 RestartPolicy: api.RestartPolicyAlways, 178 DNSPolicy: api.DNSClusterFirst, 179 }, 180 ObjectMeta: metav1.ObjectMeta{ 181 Labels: map[string]string{"foo": "bar"}, 182 }, 183 }, 184 }, 185 } 186 }