Skip to content

Commit e38b7dc

Browse files
committed
integration/api: check field items in empty List
Signed-off-by: xyz-li <[email protected]>
1 parent 4369159 commit e38b7dc

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Copyright 2024 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 apimachinery
18+
19+
import (
20+
"context"
21+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
22+
"testing"
23+
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/runtime/schema"
26+
"k8s.io/apimachinery/pkg/util/sets"
27+
"k8s.io/client-go/dynamic"
28+
"k8s.io/client-go/kubernetes"
29+
kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
30+
"k8s.io/kubernetes/test/integration/framework"
31+
)
32+
33+
func TestCheckFieldItemsInEmptyList(t *testing.T) {
34+
server := kubeapiservertesting.StartTestServerOrDie(t, nil, []string{
35+
"--runtime-config=api/all=true",
36+
}, framework.SharedEtcd())
37+
38+
defer server.TearDownFn()
39+
40+
clientSet, err := kubernetes.NewForConfig(server.ClientConfig)
41+
if err != nil {
42+
t.Fatal(err)
43+
}
44+
45+
dynamicClient, err := dynamic.NewForConfig(server.ClientConfig)
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
50+
_, lists, err := clientSet.Discovery().ServerGroupsAndResources()
51+
if err != nil {
52+
t.Fatal(err)
53+
}
54+
55+
for _, resources := range lists {
56+
for _, resource := range resources.APIResources {
57+
gv, err := schema.ParseGroupVersion(resources.GroupVersion)
58+
if err != nil {
59+
t.Fatal(err)
60+
}
61+
gvr := schema.GroupVersionResource{
62+
Group: gv.Group,
63+
Version: gv.Version,
64+
Resource: resource.Name,
65+
}
66+
67+
if !sets.NewString(resource.Verbs...).Has("list") {
68+
t.Logf("skip gvr: %s", gvr)
69+
continue
70+
}
71+
72+
var list *unstructured.UnstructuredList
73+
if resource.Namespaced {
74+
list, err = dynamicClient.Resource(gvr).Namespace("non-exist").List(context.Background(),
75+
metav1.ListOptions{LabelSelector: "non-exist=non-exist"})
76+
} else {
77+
list, err = dynamicClient.Resource(gvr).List(context.Background(),
78+
metav1.ListOptions{LabelSelector: "non-exist=non-exist"})
79+
}
80+
81+
if err != nil {
82+
t.Fatal(err)
83+
}
84+
85+
if list == nil {
86+
t.Fatalf("gvr: %s, list is nil", gvr)
87+
}
88+
89+
// Field `items` in List object should be a zero-length array when no obj in etcd.
90+
if list.Items == nil {
91+
t.Fatalf("gvr: %s, fields items of list is nil", gvr)
92+
}
93+
94+
if len(list.Items) > 0 {
95+
t.Fatalf("gvr: %s, fields items should be a zero-length array", gvr)
96+
}
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)