mirror of
https://github.com/kubernetes/client-go.git
synced 2025-07-07 12:08:42 +00:00
Merge pull request #66078 from deads2k/client-01-list
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. fix fake dynamic client listing bug The fake dynamic client used for unit testing had a bug that prevented list from working. Added a test and fixed the fake client. @kubernetes/sig-api-machinery-bugs /assign @tnozicka ```release-note NONE ``` Kubernetes-commit: da1bb028304504cbe33e5dab42290e1e8ea12d14
This commit is contained in:
commit
4e3d951df3
@ -33,6 +33,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func NewSimpleDynamicClient(scheme *runtime.Scheme, objects ...runtime.Object) *FakeDynamicClient {
|
func NewSimpleDynamicClient(scheme *runtime.Scheme, objects ...runtime.Object) *FakeDynamicClient {
|
||||||
|
// In order to use List with this client, you have to have the v1.List registered in your scheme. Neat thing though
|
||||||
|
// it does NOT have to be the *same* list
|
||||||
|
scheme.AddKnownTypeWithName(schema.GroupVersionKind{Group: "fake-dynamic-client-group", Version: "v1", Kind: "List"}, &unstructured.UnstructuredList{})
|
||||||
|
|
||||||
codecs := serializer.NewCodecFactory(scheme)
|
codecs := serializer.NewCodecFactory(scheme)
|
||||||
o := testing.NewObjectTracker(scheme, codecs.UniversalDecoder())
|
o := testing.NewObjectTracker(scheme, codecs.UniversalDecoder())
|
||||||
for _, obj := range objects {
|
for _, obj := range objects {
|
||||||
@ -272,11 +276,11 @@ func (c *dynamicResourceClient) List(opts metav1.ListOptions) (*unstructured.Uns
|
|||||||
switch {
|
switch {
|
||||||
case len(c.namespace) == 0:
|
case len(c.namespace) == 0:
|
||||||
obj, err = c.client.Fake.
|
obj, err = c.client.Fake.
|
||||||
Invokes(testing.NewRootListAction(c.resource, schema.GroupVersionKind{Version: "v1", Kind: "List"}, opts), &metav1.Status{Status: "dynamic list fail"})
|
Invokes(testing.NewRootListAction(c.resource, schema.GroupVersionKind{Group: "fake-dynamic-client-group", Version: "v1", Kind: "" /*List is appended by the tracker automatically*/}, opts), &metav1.Status{Status: "dynamic list fail"})
|
||||||
|
|
||||||
case len(c.namespace) > 0:
|
case len(c.namespace) > 0:
|
||||||
obj, err = c.client.Fake.
|
obj, err = c.client.Fake.
|
||||||
Invokes(testing.NewListAction(c.resource, schema.GroupVersionKind{Version: "v1", Kind: "List"}, c.namespace, opts), &metav1.Status{Status: "dynamic list fail"})
|
Invokes(testing.NewListAction(c.resource, schema.GroupVersionKind{Group: "fake-dynamic-client-group", Version: "v1", Kind: "" /*List is appended by the tracker automatically*/}, c.namespace, opts), &metav1.Status{Status: "dynamic list fail"})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -299,13 +303,14 @@ func (c *dynamicResourceClient) List(opts metav1.ListOptions) (*unstructured.Uns
|
|||||||
}
|
}
|
||||||
|
|
||||||
list := &unstructured.UnstructuredList{}
|
list := &unstructured.UnstructuredList{}
|
||||||
for _, item := range entireList.Items {
|
for i := range entireList.Items {
|
||||||
|
item := &entireList.Items[i]
|
||||||
metadata, err := meta.Accessor(item)
|
metadata, err := meta.Accessor(item)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if label.Matches(labels.Set(metadata.GetLabels())) {
|
if label.Matches(labels.Set(metadata.GetLabels())) {
|
||||||
list.Items = append(list.Items, item)
|
list.Items = append(list.Items, *item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return list, nil
|
return list, nil
|
||||||
|
66
dynamic/fake/simple_test.go
Normal file
66
dynamic/fake/simple_test.go
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2018 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 fake
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/api/equality"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
"k8s.io/apimachinery/pkg/util/diff"
|
||||||
|
)
|
||||||
|
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestList(t *testing.T) {
|
||||||
|
scheme := runtime.NewScheme()
|
||||||
|
|
||||||
|
client := NewSimpleDynamicClient(scheme,
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"),
|
||||||
|
newUnstructured("group2/version", "TheKind", "ns-foo", "name2-foo"),
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-bar"),
|
||||||
|
newUnstructured("group/version", "TheKind", "ns-foo", "name-baz"),
|
||||||
|
newUnstructured("group2/version", "TheKind", "ns-foo", "name2-baz"),
|
||||||
|
)
|
||||||
|
listFirst, err := client.Resource(schema.GroupVersionResource{Group: "group", Version: "version", Resource: "thekinds"}).List(metav1.ListOptions{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := []unstructured.Unstructured{
|
||||||
|
*newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"),
|
||||||
|
*newUnstructured("group/version", "TheKind", "ns-foo", "name-bar"),
|
||||||
|
*newUnstructured("group/version", "TheKind", "ns-foo", "name-baz"),
|
||||||
|
}
|
||||||
|
if !equality.Semantic.DeepEqual(listFirst.Items, expected) {
|
||||||
|
t.Fatal(diff.ObjectGoPrintDiff(expected, listFirst.Items))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user