mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-14 14:23:37 +00:00
cacher: Move common testing utils to a single file
This commit prepares for when cacher tests are moved here from the `tests` package. Tests in that package redeclare some of the testing utils that exist here, so dedup-ing them. This commit also adapts to any changes in test util signatures. There are still some utils that can be reused but currently are highly specific to some tests. (ex: watch_cache_test.go) Signed-off-by: Madhav Jivrajani <madhav.jiv@gmail.com>
This commit is contained in:
parent
d220ecb415
commit
70978e4af6
@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2023 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 cacher
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/api/apitesting"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/serializer"
|
||||||
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
|
"k8s.io/apiserver/pkg/apis/example"
|
||||||
|
examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
|
||||||
|
example2v1 "k8s.io/apiserver/pkg/apis/example2/v1"
|
||||||
|
"k8s.io/apiserver/pkg/storage"
|
||||||
|
"k8s.io/apiserver/pkg/storage/etcd3"
|
||||||
|
etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
|
||||||
|
storagetesting "k8s.io/apiserver/pkg/storage/testing"
|
||||||
|
"k8s.io/apiserver/pkg/storage/value/encrypt/identity"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
scheme = runtime.NewScheme()
|
||||||
|
codecs = serializer.NewCodecFactory(scheme)
|
||||||
|
errDummy = fmt.Errorf("dummy error")
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
metav1.AddToGroupVersion(scheme, metav1.SchemeGroupVersion)
|
||||||
|
utilruntime.Must(example.AddToScheme(scheme))
|
||||||
|
utilruntime.Must(examplev1.AddToScheme(scheme))
|
||||||
|
utilruntime.Must(example2v1.AddToScheme(scheme))
|
||||||
|
}
|
||||||
|
|
||||||
|
func newPod() runtime.Object { return &example.Pod{} }
|
||||||
|
func newPodList() runtime.Object { return &example.PodList{} }
|
||||||
|
|
||||||
|
func newEtcdTestStorage(t *testing.T, prefix string, pagingEnabled bool) (*etcd3testing.EtcdTestServer, storage.Interface) {
|
||||||
|
server, _ := etcd3testing.NewUnsecuredEtcd3TestClientServer(t)
|
||||||
|
storage := etcd3.New(
|
||||||
|
server.V3Client,
|
||||||
|
apitesting.TestCodec(codecs, examplev1.SchemeGroupVersion),
|
||||||
|
newPod,
|
||||||
|
prefix,
|
||||||
|
schema.GroupResource{Resource: "pods"},
|
||||||
|
identity.NewEncryptCheckTransformer(),
|
||||||
|
pagingEnabled,
|
||||||
|
etcd3.NewDefaultLeaseManagerConfig())
|
||||||
|
return server, storage
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeTestPodWithName(name string) *example.Pod {
|
||||||
|
return &example.Pod{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{Namespace: "ns", Name: name},
|
||||||
|
Spec: storagetesting.DeepEqualSafePodSpec(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func computePodKey(obj *example.Pod) string {
|
||||||
|
return fmt.Sprintf("/pods/%s/%s", obj.Namespace, obj.Name)
|
||||||
|
}
|
@ -38,8 +38,6 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/labels"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/runtime/serializer"
|
|
||||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
"k8s.io/apimachinery/pkg/watch"
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
"k8s.io/apiserver/pkg/apis/example"
|
"k8s.io/apiserver/pkg/apis/example"
|
||||||
@ -51,25 +49,11 @@ import (
|
|||||||
etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
|
etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
|
||||||
"k8s.io/apiserver/pkg/storage/value/encrypt/identity"
|
"k8s.io/apiserver/pkg/storage/value/encrypt/identity"
|
||||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||||
utilflowcontrol "k8s.io/apiserver/pkg/util/flowcontrol"
|
|
||||||
featuregatetesting "k8s.io/component-base/featuregate/testing"
|
featuregatetesting "k8s.io/component-base/featuregate/testing"
|
||||||
"k8s.io/utils/clock"
|
"k8s.io/utils/clock"
|
||||||
"k8s.io/utils/pointer"
|
"k8s.io/utils/pointer"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
scheme = runtime.NewScheme()
|
|
||||||
codecs = serializer.NewCodecFactory(scheme)
|
|
||||||
errDummy = fmt.Errorf("dummy error")
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
metav1.AddToGroupVersion(scheme, metav1.SchemeGroupVersion)
|
|
||||||
utilruntime.Must(example.AddToScheme(scheme))
|
|
||||||
utilruntime.Must(examplev1.AddToScheme(scheme))
|
|
||||||
utilruntime.Must(example2v1.AddToScheme(scheme))
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTestCacher(s storage.Interface) (*Cacher, storage.Versioner, error) {
|
func newTestCacher(s storage.Interface) (*Cacher, storage.Versioner, error) {
|
||||||
prefix := "pods"
|
prefix := "pods"
|
||||||
config := Config{
|
config := Config{
|
||||||
@ -312,7 +296,7 @@ func TestWatchCacheBypass(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEmptyWatchEventCache(t *testing.T) {
|
func TestEmptyWatchEventCache(t *testing.T) {
|
||||||
server, etcdStorage := newEtcdTestStorage(t, etcd3testing.PathPrefix())
|
server, etcdStorage := newEtcdTestStorage(t, etcd3testing.PathPrefix(), true)
|
||||||
defer server.Terminate(t)
|
defer server.Terminate(t)
|
||||||
|
|
||||||
// add a few objects
|
// add a few objects
|
||||||
@ -762,27 +746,6 @@ func TestCacherNoLeakWithMultipleWatchers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWatchInitializationSignal(t *testing.T) {
|
|
||||||
backingStorage := &dummyStorage{}
|
|
||||||
cacher, _, err := newTestCacher(backingStorage)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Couldn't create cacher: %v", err)
|
|
||||||
}
|
|
||||||
defer cacher.Stop()
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
initSignal := utilflowcontrol.NewInitializationSignal()
|
|
||||||
ctx = utilflowcontrol.WithInitializationSignal(ctx, initSignal)
|
|
||||||
|
|
||||||
_, err = cacher.Watch(ctx, "pods/ns", storage.ListOptions{ResourceVersion: "0", Predicate: storage.Everything})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to create watch: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
initSignal.Wait()
|
|
||||||
}
|
|
||||||
|
|
||||||
func testCacherSendBookmarkEvents(t *testing.T, allowWatchBookmarks, expectedBookmarks bool) {
|
func testCacherSendBookmarkEvents(t *testing.T, allowWatchBookmarks, expectedBookmarks bool) {
|
||||||
backingStorage := &dummyStorage{}
|
backingStorage := &dummyStorage{}
|
||||||
cacher, _, err := newTestCacher(backingStorage)
|
cacher, _, err := newTestCacher(backingStorage)
|
||||||
|
@ -18,44 +18,17 @@ package cacher
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/api/apitesting"
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
||||||
"k8s.io/apiserver/pkg/apis/example"
|
"k8s.io/apiserver/pkg/apis/example"
|
||||||
examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
|
|
||||||
"k8s.io/apiserver/pkg/storage"
|
|
||||||
"k8s.io/apiserver/pkg/storage/etcd3"
|
|
||||||
etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
|
|
||||||
"k8s.io/apiserver/pkg/storage/value/encrypt/identity"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func newPod() runtime.Object { return &example.Pod{} }
|
|
||||||
|
|
||||||
func computePodKey(obj *example.Pod) string {
|
|
||||||
return fmt.Sprintf("/pods/%s/%s", obj.Namespace, obj.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func newEtcdTestStorage(t *testing.T, prefix string) (*etcd3testing.EtcdTestServer, storage.Interface) {
|
|
||||||
server, _ := etcd3testing.NewUnsecuredEtcd3TestClientServer(t)
|
|
||||||
storage := etcd3.New(
|
|
||||||
server.V3Client,
|
|
||||||
apitesting.TestCodec(codecs, examplev1.SchemeGroupVersion),
|
|
||||||
newPod, prefix,
|
|
||||||
schema.GroupResource{Resource: "pods"},
|
|
||||||
identity.NewEncryptCheckTransformer(),
|
|
||||||
true,
|
|
||||||
etcd3.NewDefaultLeaseManagerConfig())
|
|
||||||
return server, storage
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCacherListerWatcher(t *testing.T) {
|
func TestCacherListerWatcher(t *testing.T) {
|
||||||
prefix := "pods"
|
prefix := "pods"
|
||||||
fn := func() runtime.Object { return &example.PodList{} }
|
fn := func() runtime.Object { return &example.PodList{} }
|
||||||
server, store := newEtcdTestStorage(t, prefix)
|
server, store := newEtcdTestStorage(t, prefix, true)
|
||||||
defer server.Terminate(t)
|
defer server.Terminate(t)
|
||||||
|
|
||||||
objects := []*example.Pod{
|
objects := []*example.Pod{
|
||||||
@ -89,7 +62,7 @@ func TestCacherListerWatcher(t *testing.T) {
|
|||||||
func TestCacherListerWatcherPagination(t *testing.T) {
|
func TestCacherListerWatcherPagination(t *testing.T) {
|
||||||
prefix := "pods"
|
prefix := "pods"
|
||||||
fn := func() runtime.Object { return &example.PodList{} }
|
fn := func() runtime.Object { return &example.PodList{} }
|
||||||
server, store := newEtcdTestStorage(t, prefix)
|
server, store := newEtcdTestStorage(t, prefix, true)
|
||||||
defer server.Terminate(t)
|
defer server.Terminate(t)
|
||||||
|
|
||||||
// We need the list to be sorted by name to later check the alphabetical order of
|
// We need the list to be sorted by name to later check the alphabetical order of
|
||||||
|
Loading…
Reference in New Issue
Block a user