Skip to content

Commit e4d10cf

Browse files
committed
Add test to confirm default content type used by core client
Signed-off-by: Monis Khan <[email protected]>
1 parent 6595fa4 commit e4d10cf

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

pkg/client/tests/clientset_test.go

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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 tests
18+
19+
import (
20+
"context"
21+
"io"
22+
"net/http"
23+
"net/http/httptest"
24+
"sync/atomic"
25+
"testing"
26+
27+
"github.com/google/go-cmp/cmp"
28+
29+
autoscalingv1 "k8s.io/api/autoscaling/v1"
30+
corev1 "k8s.io/api/core/v1"
31+
crv1 "k8s.io/apiextensions-apiserver/examples/client-go/pkg/apis/cr/v1"
32+
crv1client "k8s.io/apiextensions-apiserver/examples/client-go/pkg/client/clientset/versioned"
33+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34+
"k8s.io/client-go/kubernetes"
35+
"k8s.io/client-go/rest"
36+
)
37+
38+
func TestClientContentType(t *testing.T) {
39+
createPodFunc := func(t *testing.T, config *rest.Config) {
40+
client, err := kubernetes.NewForConfig(config)
41+
if err != nil {
42+
t.Fatalf("failed to create REST client: %v", err)
43+
}
44+
45+
_, err = client.CoreV1().Pods("panda").
46+
Create(context.TODO(), &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "snorlax"}}, metav1.CreateOptions{})
47+
if err != nil {
48+
t.Fatal(err)
49+
}
50+
}
51+
52+
updateScaleFunc := func(t *testing.T, config *rest.Config) {
53+
client, err := kubernetes.NewForConfig(config)
54+
if err != nil {
55+
t.Fatalf("failed to create REST client: %v", err)
56+
}
57+
58+
_, err = client.AppsV1().Deployments("panda").
59+
UpdateScale(context.TODO(), "snorlax", &autoscalingv1.Scale{ObjectMeta: metav1.ObjectMeta{Name: "snorlax"}}, metav1.UpdateOptions{})
60+
if err != nil {
61+
t.Fatal(err)
62+
}
63+
}
64+
65+
createExampleViaRESTClientFunc := func(t *testing.T, config *rest.Config) {
66+
kubeClient, err := kubernetes.NewForConfig(config)
67+
if err != nil {
68+
t.Fatalf("failed to create REST client: %v", err)
69+
}
70+
71+
client := crv1client.New(kubeClient.CoreV1().RESTClient())
72+
73+
_, err = client.CrV1().Examples("panda").
74+
Create(context.TODO(), &crv1.Example{ObjectMeta: metav1.ObjectMeta{Name: "snorlax"}}, metav1.CreateOptions{})
75+
if err != nil {
76+
t.Fatal(err)
77+
}
78+
}
79+
80+
tests := []struct {
81+
name string
82+
createFunc func(*testing.T, *rest.Config)
83+
contentType string
84+
expectedPath string
85+
expectContentType string
86+
expectBody string
87+
}{
88+
{
89+
name: "default",
90+
createFunc: createPodFunc,
91+
contentType: "",
92+
expectedPath: "/api/v1/namespaces/panda/pods",
93+
expectContentType: "application/vnd.kubernetes.protobuf",
94+
expectBody: "k8s\x00\n\t\n\x02v1\x12\x03Pod\x12I\n\x17\n\asnorlax\x12\x00\x1a\x00\"\x00*\x002\x008\x00B\x00\x12\x1c\x1a\x002\x00B\x00J\x00R\x00X\x00`\x00h\x00\x82\x01\x00\x8a\x01\x00\x9a\x01\x00\xc2\x01\x00\x1a\x10\n\x00\x1a\x00\"\x00*\x002\x00J\x00Z\x00r\x00\x1a\x00\"\x00",
95+
},
96+
{
97+
name: "json",
98+
createFunc: createPodFunc,
99+
contentType: "application/json",
100+
expectedPath: "/api/v1/namespaces/panda/pods",
101+
expectContentType: "application/json",
102+
expectBody: `{"kind":"Pod","apiVersion":"v1","metadata":{"name":"snorlax","creationTimestamp":null},"spec":{"containers":null},"status":{}}
103+
`,
104+
},
105+
{
106+
name: "default update scale",
107+
createFunc: updateScaleFunc,
108+
contentType: "",
109+
expectedPath: "/apis/apps/v1/namespaces/panda/deployments/snorlax/scale",
110+
expectContentType: "application/vnd.kubernetes.protobuf",
111+
expectBody: "k8s\u0000\n\u0017\n\u000Eautoscaling/v1\u0012\u0005Scale\u0012#\n\u0017\n\asnorlax\u0012\u0000\u001A\u0000\"\u0000*\u00002\u00008\u0000B\u0000\u0012\u0002\b\u0000\u001A\u0004\b\u0000\u0012\u0000\u001A\u0000\"\u0000",
112+
},
113+
{
114+
name: "json update scale",
115+
createFunc: updateScaleFunc,
116+
contentType: "application/json",
117+
expectedPath: "/apis/apps/v1/namespaces/panda/deployments/snorlax/scale",
118+
expectContentType: "application/json",
119+
expectBody: `{"kind":"Scale","apiVersion":"autoscaling/v1","metadata":{"name":"snorlax","creationTimestamp":null},"spec":{},"status":{"replicas":0}}
120+
`,
121+
},
122+
{
123+
name: "default via RESTClient",
124+
createFunc: createExampleViaRESTClientFunc,
125+
contentType: "",
126+
expectedPath: "/api/v1/namespaces/panda/examples",
127+
expectContentType: "application/json",
128+
expectBody: `{"metadata":{"name":"snorlax","creationTimestamp":null},"spec":{"foo":"","bar":false},"status":{}}
129+
`,
130+
},
131+
{
132+
name: "json via RESTClient",
133+
createFunc: createExampleViaRESTClientFunc,
134+
contentType: "application/json",
135+
expectedPath: "/api/v1/namespaces/panda/examples",
136+
expectContentType: "application/json",
137+
expectBody: `{"metadata":{"name":"snorlax","creationTimestamp":null},"spec":{"foo":"","bar":false},"status":{}}
138+
`,
139+
},
140+
}
141+
142+
for _, tc := range tests {
143+
t.Run(tc.name, func(t *testing.T) {
144+
var calls atomic.Uint64
145+
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
146+
calls.Add(1)
147+
148+
if got, want := r.URL.Path, tc.expectedPath; got != want {
149+
t.Errorf("unexpected path, got=%q, want=%q", got, want)
150+
}
151+
152+
if got, want := r.Header.Get("content-type"), tc.expectContentType; got != want {
153+
t.Errorf("unexpected content-type, got=%q, want=%q", got, want)
154+
}
155+
156+
if r.Body == nil {
157+
t.Fatal("request body is nil")
158+
}
159+
body, err := io.ReadAll(r.Body)
160+
if err != nil {
161+
t.Fatal(err)
162+
}
163+
_ = r.Body.Close()
164+
if diff := cmp.Diff(tc.expectBody, string(body)); len(diff) > 0 {
165+
t.Errorf("body diff (-want, +got):\n%s", diff)
166+
}
167+
168+
w.Header().Set("Content-Type", "application/json")
169+
_, _ = w.Write([]byte("{}"))
170+
}))
171+
ts.Start()
172+
defer ts.Close()
173+
174+
config := &rest.Config{
175+
Host: ts.URL,
176+
ContentConfig: rest.ContentConfig{ContentType: tc.contentType},
177+
}
178+
179+
tc.createFunc(t, config)
180+
181+
if calls.Load() != 1 {
182+
t.Errorf("unexpected handler call count: %d", calls.Load())
183+
}
184+
})
185+
}
186+
}

0 commit comments

Comments
 (0)