mirror of
https://github.com/kubernetes/client-go.git
synced 2025-06-27 07:28:14 +00:00
Merge pull request #125102 from 0xMALVEE/unit-tests-listers.go
Add unit tests to client-go/tools/cache/listers.go Kubernetes-commit: 0b00454617397cea2e9e6ed75b232a4e1e9cfb6d
This commit is contained in:
commit
92865bf125
2
go.mod
2
go.mod
@ -26,7 +26,7 @@ require (
|
|||||||
google.golang.org/protobuf v1.36.5
|
google.golang.org/protobuf v1.36.5
|
||||||
gopkg.in/evanphx/json-patch.v4 v4.12.0
|
gopkg.in/evanphx/json-patch.v4 v4.12.0
|
||||||
k8s.io/api v0.0.0-20250311213050-d471f34256a2
|
k8s.io/api v0.0.0-20250311213050-d471f34256a2
|
||||||
k8s.io/apimachinery v0.0.0-20250311092730-6e3d6ca42453
|
k8s.io/apimachinery v0.0.0-20250312043753-2687636770f8
|
||||||
k8s.io/klog/v2 v2.130.1
|
k8s.io/klog/v2 v2.130.1
|
||||||
k8s.io/kube-openapi v0.0.0-20250304201544-e5f78fe3ede9
|
k8s.io/kube-openapi v0.0.0-20250304201544-e5f78fe3ede9
|
||||||
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738
|
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738
|
||||||
|
4
go.sum
4
go.sum
@ -148,8 +148,8 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
k8s.io/api v0.0.0-20250311213050-d471f34256a2 h1:oZ7qygNgz2kT79G35s6WdqjgHLRUhagPG1sncLLt8f4=
|
k8s.io/api v0.0.0-20250311213050-d471f34256a2 h1:oZ7qygNgz2kT79G35s6WdqjgHLRUhagPG1sncLLt8f4=
|
||||||
k8s.io/api v0.0.0-20250311213050-d471f34256a2/go.mod h1:GoBXZTTPdVLaqx1aHmJoO20ucV0hWHvJxYByY7F2Aqk=
|
k8s.io/api v0.0.0-20250311213050-d471f34256a2/go.mod h1:GoBXZTTPdVLaqx1aHmJoO20ucV0hWHvJxYByY7F2Aqk=
|
||||||
k8s.io/apimachinery v0.0.0-20250311092730-6e3d6ca42453 h1:OHdFWi18BE714KkhhYcPoYVPHfF86ACb1d4zveVAHZs=
|
k8s.io/apimachinery v0.0.0-20250312043753-2687636770f8 h1:5Mh2OhmA4UBjm84f2wHYwwaAhA7Unw+PpIyiAJuqeUg=
|
||||||
k8s.io/apimachinery v0.0.0-20250311092730-6e3d6ca42453/go.mod h1:S2OIkExGqJOXYSYcAJwQ9zWcc6BkBUdTJUu4M7z0cvo=
|
k8s.io/apimachinery v0.0.0-20250312043753-2687636770f8/go.mod h1:S2OIkExGqJOXYSYcAJwQ9zWcc6BkBUdTJUu4M7z0cvo=
|
||||||
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
|
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
|
||||||
k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
|
k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
|
||||||
k8s.io/kube-openapi v0.0.0-20250304201544-e5f78fe3ede9 h1:t0huyHnz6HsokckRxAF1bY0cqPFwzINKCL7yltEjZQc=
|
k8s.io/kube-openapi v0.0.0-20250304201544-e5f78fe3ede9 h1:t0huyHnz6HsokckRxAF1bY0cqPFwzINKCL7yltEjZQc=
|
||||||
|
199
tools/cache/listers_test.go
vendored
Normal file
199
tools/cache/listers_test.go
vendored
Normal file
@ -0,0 +1,199 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2024 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 cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
schema "k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestListersListAll(t *testing.T) {
|
||||||
|
mkObj := func(id string, val string) testStoreObject {
|
||||||
|
return testStoreObject{id: id, val: val}
|
||||||
|
}
|
||||||
|
|
||||||
|
store := NewStore(testStoreKeyFunc)
|
||||||
|
|
||||||
|
err := store.Add(mkObj("foo", "bar"))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("store obj add failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = store.Add(mkObj("foo-1", "bar-1"))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("store obj add failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedOutput := []testStoreObject{
|
||||||
|
mkObj("foo", "bar"),
|
||||||
|
mkObj("foo-1", "bar-1"),
|
||||||
|
}
|
||||||
|
actualOutput := []testStoreObject{}
|
||||||
|
|
||||||
|
err = ListAll(store, labels.Everything(), func(obj interface{}) {
|
||||||
|
actualOutput = append(actualOutput, obj.(testStoreObject))
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(expectedOutput, actualOutput) {
|
||||||
|
t.Fatalf("unexpected object has been returned expected = %v actual = %v, diff = %v", expectedOutput, actualOutput, cmp.Diff(expectedOutput, actualOutput))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListersListAllByNamespace(t *testing.T) {
|
||||||
|
objs := []*unstructured.Unstructured{
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"),
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-foo1"),
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-bar", "name-bar"),
|
||||||
|
}
|
||||||
|
indexer := NewIndexer(MetaNamespaceKeyFunc, Indexers{})
|
||||||
|
for _, obj := range objs {
|
||||||
|
err := indexer.Add(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedOutput := []*unstructured.Unstructured{
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"),
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-foo1"),
|
||||||
|
}
|
||||||
|
namespaceToList := "ns-foo"
|
||||||
|
|
||||||
|
actualOutput := []*unstructured.Unstructured{}
|
||||||
|
appendFn := func(obj interface{}) {
|
||||||
|
actualOutput = append(actualOutput, obj.(*unstructured.Unstructured))
|
||||||
|
}
|
||||||
|
|
||||||
|
err := ListAllByNamespace(indexer, namespaceToList, labels.Everything(), appendFn)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(expectedOutput, actualOutput) {
|
||||||
|
t.Fatalf("unexpected object has been returned expected = %v actual = %v, diff = %v", expectedOutput, actualOutput, cmp.Diff(expectedOutput, actualOutput))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGenericListerListMethod(t *testing.T) {
|
||||||
|
objs := []*unstructured.Unstructured{
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"),
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-foo1"),
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-bar", "name-bar"),
|
||||||
|
}
|
||||||
|
indexer := NewIndexer(MetaNamespaceKeyFunc, Indexers{})
|
||||||
|
for _, obj := range objs {
|
||||||
|
err := indexer.Add(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
target := NewGenericLister(indexer, schema.GroupResource{Group: "group", Resource: "resource"})
|
||||||
|
|
||||||
|
expectedOutput := []runtime.Object{
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"),
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-foo1"),
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-bar", "name-bar"),
|
||||||
|
}
|
||||||
|
actualOutput, err := target.List(labels.Everything())
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(expectedOutput, actualOutput) {
|
||||||
|
t.Fatalf("unexpected object has been returned expected = %v actual = %v, diff = %v", expectedOutput, actualOutput, cmp.Diff(expectedOutput, actualOutput))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGenericListerByNamespaceMethod(t *testing.T) {
|
||||||
|
objs := []*unstructured.Unstructured{
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"),
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-faa", "name-faa1"),
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-bar", "name-bar"),
|
||||||
|
}
|
||||||
|
indexer := NewIndexer(MetaNamespaceKeyFunc, Indexers{})
|
||||||
|
for _, obj := range objs {
|
||||||
|
err := indexer.Add(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
target := NewGenericLister(indexer, schema.GroupResource{Group: "group", Resource: "resource"})
|
||||||
|
|
||||||
|
expectedOutput := []runtime.Object{
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"),
|
||||||
|
}
|
||||||
|
namespaceToList := "ns-foo"
|
||||||
|
actualOutput, err := target.ByNamespace(namespaceToList).List(labels.Everything())
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(expectedOutput, actualOutput) {
|
||||||
|
t.Fatalf("unexpected object has been returned expected = %v actual = %v, diff = %v", expectedOutput, actualOutput, cmp.Diff(expectedOutput, actualOutput))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGenericListerGetMethod(t *testing.T) {
|
||||||
|
objs := []*unstructured.Unstructured{
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"),
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-faa", "name-faa1"),
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-bar", "name-bar"),
|
||||||
|
}
|
||||||
|
indexer := NewIndexer(MetaNamespaceKeyFunc, Indexers{})
|
||||||
|
for _, obj := range objs {
|
||||||
|
err := indexer.Add(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
target := NewGenericLister(indexer, schema.GroupResource{Group: "group", Resource: "resource"})
|
||||||
|
|
||||||
|
expectedOutput := []runtime.Object{
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"),
|
||||||
|
}[0]
|
||||||
|
key := "ns-foo/name-foo"
|
||||||
|
actualOutput, err := target.Get(key)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(expectedOutput, actualOutput) {
|
||||||
|
t.Fatalf("unexpected object has been returned expected = %v actual = %v, diff = %v", expectedOutput, actualOutput, cmp.Diff(expectedOutput, actualOutput))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newUnstructured(apiVersion, kind, namespace, name string) *unstructured.Unstructured {
|
||||||
|
return &unstructured.Unstructured{
|
||||||
|
Object: map[string]interface{}{
|
||||||
|
"apiVersion": apiVersion,
|
||||||
|
"kind": kind,
|
||||||
|
"metadata": map[string]interface{}{
|
||||||
|
"namespace": namespace,
|
||||||
|
"name": name,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user