Debian Codegraph @

pkg/endpoints/handlers/responsewriters/writers_test.go

raw
1 /*
2 Copyright 2016 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 responsewriters
18
19 import (
20 "bytes"
21 "compress/gzip"
22 "context"
23 "encoding/hex"
24 "encoding/json"
25 "errors"
26 "fmt"
27 "io"
28 "io/ioutil"
29 "math/rand"
30 "net/http"
31 "net/http/httptest"
32 "net/url"
33 "os"
34 "reflect"
35 "strconv"
36 "testing"
37 "time"
38
39 "github.com/google/go-cmp/cmp"
40 v1 "k8s.io/api/core/v1"
41 kerrors "k8s.io/apimachinery/pkg/api/errors"
42 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
43 testapigroupv1 "k8s.io/apimachinery/pkg/apis/testapigroup/v1"
44 "k8s.io/apimachinery/pkg/runtime"
45 "k8s.io/apimachinery/pkg/runtime/schema"
46 jsonserializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
47 "k8s.io/apimachinery/pkg/runtime/serializer/protobuf"
48 rand2 "k8s.io/apimachinery/pkg/util/rand"
49 "k8s.io/apimachinery/pkg/util/uuid"
50 "k8s.io/apiserver/pkg/features"
51 utilfeature "k8s.io/apiserver/pkg/util/feature"
52 featuregatetesting "k8s.io/component-base/featuregate/testing"
53 )
54
55 const benchmarkSeed = 100
56
57 func TestSerializeObjectParallel(t *testing.T) {
58 largePayload := bytes.Repeat([]byte("0123456789abcdef"), defaultGzipThresholdBytes/16+1)
59 type test struct {
60 name string
61
62 mediaType string
63 out []byte
64 outErrs []error
65 req *http.Request
66 statusCode int
67 object runtime.Object
68
69 wantCode int
70 wantHeaders http.Header
71 }
72 newTest := func() test {
73 return test{
74 name: "compress on gzip",
75 out: largePayload,
76 mediaType: "application/json",
77 req: &http.Request{
78 Header: http.Header{
79 "Accept-Encoding": []string{"gzip"},
80 },
81 URL: &url.URL{Path: "/path"},
82 },
83 wantCode: http.StatusOK,
84 wantHeaders: http.Header{
85 "Content-Type": []string{"application/json"},
86 "Content-Encoding": []string{"gzip"},
87 "Vary": []string{"Accept-Encoding"},
88 },
89 }
90 }
91 featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.APIResponseCompression, true)
92 for i := 0; i < 100; i++ {
93 ctt := newTest()
94 t.Run(ctt.name, func(t *testing.T) {
95 defer func() {
96 if r := recover(); r != nil {
97 t.Fatalf("recovered from err %v", r)
98 }
99 }()
100 t.Parallel()
101
102 encoder := &fakeEncoder{
103 buf: ctt.out,
104 errs: ctt.outErrs,
105 }
106 if ctt.statusCode == 0 {
107 ctt.statusCode = http.StatusOK
108 }
109 recorder := &fakeResponseRecorder{
110 ResponseRecorder: httptest.NewRecorder(),
111 fe: encoder,
112 errorAfterEncoding: true,
113 }
114 SerializeObject(ctt.mediaType, encoder, recorder, ctt.req, ctt.statusCode, ctt.object)
115 result := recorder.Result()
116 if result.StatusCode != ctt.wantCode {
117 t.Fatalf("unexpected code: %v", result.StatusCode)
118 }
119 if !reflect.DeepEqual(result.Header, ctt.wantHeaders) {
120 t.Fatal(cmp.Diff(ctt.wantHeaders, result.Header))
121 }
122 })
123 }
124 }
125
126 func TestSerializeObject(t *testing.T) {
127 smallPayload := []byte("{test-object,test-object}")
128 largePayload := bytes.Repeat([]byte("0123456789abcdef"), defaultGzipThresholdBytes/16+1)
129 tests := []struct {
130 name string
131
132 compressionEnabled bool
133
134 mediaType string
135 out []byte
136 outErrs []error
137 req *http.Request
138 statusCode int
139 object runtime.Object
140
141 wantCode int
142 wantHeaders http.Header
143 wantBody []byte
144 }{
145 {
146 name: "serialize object",
147 out: smallPayload,
148 req: &http.Request{Header: http.Header{}, URL: &url.URL{Path: "/path"}},
149 wantCode: http.StatusOK,
150 wantHeaders: http.Header{"Content-Type": []string{""}},
151 wantBody: smallPayload,
152 },
153
154 {
155 name: "return content type",
156 out: smallPayload,
157 mediaType: "application/json",
158 req: &http.Request{Header: http.Header{}, URL: &url.URL{Path: "/path"}},
159 wantCode: http.StatusOK,
160 wantHeaders: http.Header{"Content-Type": []string{"application/json"}},
161 wantBody: smallPayload,
162 },
163
164 {
165 name: "return status code",
166 statusCode: http.StatusBadRequest,
167 out: smallPayload,
168 mediaType: "application/json",
169 req: &http.Request{Header: http.Header{}, URL: &url.URL{Path: "/path"}},
170 wantCode: http.StatusBadRequest,
171 wantHeaders: http.Header{"Content-Type": []string{"application/json"}},
172 wantBody: smallPayload,
173 },
174
175 {
176 name: "fail to encode object",
177 out: smallPayload,
178 outErrs: []error{fmt.Errorf("bad")},
179 mediaType: "application/json",
180 req: &http.Request{Header: http.Header{}, URL: &url.URL{Path: "/path"}},
181 wantCode: http.StatusInternalServerError,
182 wantHeaders: http.Header{"Content-Type": []string{"application/json"}},
183 wantBody: smallPayload,
184 },
185
186 {
187 name: "fail to encode object or status",
188 out: smallPayload,
189 outErrs: []error{fmt.Errorf("bad"), fmt.Errorf("bad2")},
190 mediaType: "application/json",
191 req: &http.Request{Header: http.Header{}, URL: &url.URL{Path: "/path"}},
192 wantCode: http.StatusInternalServerError,
193 wantHeaders: http.Header{"Content-Type": []string{"text/plain"}},
194 wantBody: []byte(": bad"),
195 },
196
197 {
198 name: "fail to encode object or status with status code",
199 out: smallPayload,
200 outErrs: []error{kerrors.NewNotFound(schema.GroupResource{}, "test"), fmt.Errorf("bad2")},
201 mediaType: "application/json",
202 req: &http.Request{Header: http.Header{}, URL: &url.URL{Path: "/path"}},
203 statusCode: http.StatusOK,
204 wantCode: http.StatusNotFound,
205 wantHeaders: http.Header{"Content-Type": []string{"text/plain"}},
206 wantBody: []byte("NotFound: \"test\" not found"),
207 },
208
209 {
210 name: "fail to encode object or status with status code and keeps previous error",
211 out: smallPayload,
212 outErrs: []error{kerrors.NewNotFound(schema.GroupResource{}, "test"), fmt.Errorf("bad2")},
213 mediaType: "application/json",
214 req: &http.Request{Header: http.Header{}, URL: &url.URL{Path: "/path"}},
215 statusCode: http.StatusNotAcceptable,
216 wantCode: http.StatusNotAcceptable,
217 wantHeaders: http.Header{"Content-Type": []string{"text/plain"}},
218 wantBody: []byte("NotFound: \"test\" not found"),
219 },
220
221 {
222 name: "compression requires feature gate",
223 out: largePayload,
224 mediaType: "application/json",
225 req: &http.Request{
226 Header: http.Header{
227 "Accept-Encoding": []string{"gzip"},
228 },
229 URL: &url.URL{Path: "/path"},
230 },
231 wantCode: http.StatusOK,
232 wantHeaders: http.Header{"Content-Type": []string{"application/json"}},
233 wantBody: largePayload,
234 },
235
236 {
237 name: "compress on gzip",
238 compressionEnabled: true,
239 out: largePayload,
240 mediaType: "application/json",
241 req: &http.Request{
242 Header: http.Header{
243 "Accept-Encoding": []string{"gzip"},
244 },
245 URL: &url.URL{Path: "/path"},
246 },
247 wantCode: http.StatusOK,
248 wantHeaders: http.Header{
249 "Content-Type": []string{"application/json"},
250 "Content-Encoding": []string{"gzip"},
251 "Vary": []string{"Accept-Encoding"},
252 },
253 wantBody: gzipContent(largePayload, defaultGzipContentEncodingLevel),
254 },
255
256 {
257 name: "compression is not performed on small objects",
258 compressionEnabled: true,
259 out: smallPayload,
260 mediaType: "application/json",
261 req: &http.Request{
262 Header: http.Header{
263 "Accept-Encoding": []string{"gzip"},
264 },
265 URL: &url.URL{Path: "/path"},
266 },
267 wantCode: http.StatusOK,
268 wantHeaders: http.Header{
269 "Content-Type": []string{"application/json"},
270 },
271 wantBody: smallPayload,
272 },
273
274 {
275 name: "compress when multiple encodings are requested",
276 compressionEnabled: true,
277 out: largePayload,
278 mediaType: "application/json",
279 req: &http.Request{
280 Header: http.Header{
281 "Accept-Encoding": []string{"deflate, , gzip,"},
282 },
283 URL: &url.URL{Path: "/path"},
284 },
285 wantCode: http.StatusOK,
286 wantHeaders: http.Header{
287 "Content-Type": []string{"application/json"},
288 "Content-Encoding": []string{"gzip"},
289 "Vary": []string{"Accept-Encoding"},
290 },
291 wantBody: gzipContent(largePayload, defaultGzipContentEncodingLevel),
292 },
293
294 {
295 name: "ignore compression on deflate",
296 compressionEnabled: true,
297 out: largePayload,
298 mediaType: "application/json",
299 req: &http.Request{
300 Header: http.Header{
301 "Accept-Encoding": []string{"deflate"},
302 },
303 URL: &url.URL{Path: "/path"},
304 },
305 wantCode: http.StatusOK,
306 wantHeaders: http.Header{
307 "Content-Type": []string{"application/json"},
308 },
309 wantBody: largePayload,
310 },
311
312 {
313 name: "ignore compression on unrecognized types",
314 compressionEnabled: true,
315 out: largePayload,
316 mediaType: "application/json",
317 req: &http.Request{
318 Header: http.Header{
319 "Accept-Encoding": []string{", , other, nothing, what, "},
320 },
321 URL: &url.URL{Path: "/path"},
322 },
323 wantCode: http.StatusOK,
324 wantHeaders: http.Header{
325 "Content-Type": []string{"application/json"},
326 },
327 wantBody: largePayload,
328 },
329
330 {
331 name: "errors are compressed",
332 compressionEnabled: true,
333 statusCode: http.StatusInternalServerError,
334 out: smallPayload,
335 outErrs: []error{errors.New(string(largePayload)), errors.New("bad2")},
336 mediaType: "application/json",
337 req: &http.Request{
338 Header: http.Header{
339 "Accept-Encoding": []string{"gzip"},
340 },
341 URL: &url.URL{Path: "/path"},
342 },
343 wantCode: http.StatusInternalServerError,
344 wantHeaders: http.Header{
345 "Content-Type": []string{"text/plain"},
346 "Content-Encoding": []string{"gzip"},
347 "Vary": []string{"Accept-Encoding"},
348 },
349 wantBody: gzipContent([]byte(": "+string(largePayload)), defaultGzipContentEncodingLevel),
350 },
351 }
352 for _, tt := range tests {
353 t.Run(tt.name, func(t *testing.T) {
354 featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.APIResponseCompression, tt.compressionEnabled)
355
356 encoder := &fakeEncoder{
357 buf: tt.out,
358 errs: tt.outErrs,
359 }
360 if tt.statusCode == 0 {
361 tt.statusCode = http.StatusOK
362 }
363 recorder := httptest.NewRecorder()
364 SerializeObject(tt.mediaType, encoder, recorder, tt.req, tt.statusCode, tt.object)
365 result := recorder.Result()
366 if result.StatusCode != tt.wantCode {
367 t.Fatalf("unexpected code: %v", result.StatusCode)
368 }
369 if !reflect.DeepEqual(result.Header, tt.wantHeaders) {
370 t.Fatal(cmp.Diff(tt.wantHeaders, result.Header))
371 }
372 body, _ := ioutil.ReadAll(result.Body)
373 if !bytes.Equal(tt.wantBody, body) {
374 t.Fatalf("wanted:\n%s\ngot:\n%s", hex.Dump(tt.wantBody), hex.Dump(body))
375 }
376 })
377 }
378 }
379
380 func TestDeferredResponseWriter_Write(t *testing.T) {
381 smallChunk := bytes.Repeat([]byte("b"), defaultGzipThresholdBytes-1)
382 largeChunk := bytes.Repeat([]byte("b"), defaultGzipThresholdBytes+1)
383
384 tests := []struct {
385 name string
386 chunks [][]byte
387 expectGzip bool
388 expectHeaders http.Header
389 }{
390 {
391 name: "no writes",
392 chunks: nil,
393 expectGzip: false,
394 expectHeaders: http.Header{},
395 },
396 {
397 name: "one empty write",
398 chunks: [][]byte{{}},
399 expectGzip: false,
400 expectHeaders: http.Header{
401 "Content-Type": []string{"text/plain"},
402 },
403 },
404 {
405 name: "one single byte write",
406 chunks: [][]byte{{'{'}},
407 expectGzip: false,
408 expectHeaders: http.Header{
409 "Content-Type": []string{"text/plain"},
410 },
411 },
412 {
413 name: "one small chunk write",
414 chunks: [][]byte{smallChunk},
415 expectGzip: false,
416 expectHeaders: http.Header{
417 "Content-Type": []string{"text/plain"},
418 },
419 },
420 {
421 name: "two small chunk writes",
422 chunks: [][]byte{smallChunk, smallChunk},
423 expectGzip: false,
424 expectHeaders: http.Header{
425 "Content-Type": []string{"text/plain"},
426 },
427 },
428 {
429 name: "one single byte and one small chunk write",
430 chunks: [][]byte{{'{'}, smallChunk},
431 expectGzip: false,
432 expectHeaders: http.Header{
433 "Content-Type": []string{"text/plain"},
434 },
435 },
436 {
437 name: "two single bytes and one small chunk write",
438 chunks: [][]byte{{'{'}, {'{'}, smallChunk},
439 expectGzip: true,
440 expectHeaders: http.Header{
441 "Content-Type": []string{"text/plain"},
442 "Content-Encoding": []string{"gzip"},
443 "Vary": []string{"Accept-Encoding"},
444 },
445 },
446 {
447 name: "one large chunk writes",
448 chunks: [][]byte{largeChunk},
449 expectGzip: true,
450 expectHeaders: http.Header{
451 "Content-Type": []string{"text/plain"},
452 "Content-Encoding": []string{"gzip"},
453 "Vary": []string{"Accept-Encoding"},
454 },
455 },
456 {
457 name: "two large chunk writes",
458 chunks: [][]byte{largeChunk, largeChunk},
459 expectGzip: true,
460 expectHeaders: http.Header{
461 "Content-Type": []string{"text/plain"},
462 "Content-Encoding": []string{"gzip"},
463 "Vary": []string{"Accept-Encoding"},
464 },
465 },
466 {
467 name: "one small chunk and one large chunk write",
468 chunks: [][]byte{smallChunk, largeChunk},
469 expectGzip: false,
470 expectHeaders: http.Header{
471 "Content-Type": []string{"text/plain"},
472 },
473 },
474 }
475
476 for _, tt := range tests {
477 t.Run(tt.name, func(t *testing.T) {
478 mockResponseWriter := httptest.NewRecorder()
479
480 drw := &deferredResponseWriter{
481 mediaType: "text/plain",
482 statusCode: 200,
483 contentEncoding: "gzip",
484 hw: mockResponseWriter,
485 ctx: context.Background(),
486 }
487
488 fullPayload := []byte{}
489
490 for _, chunk := range tt.chunks {
491 n, err := drw.Write(chunk)
492
493 if err != nil {
494 t.Fatalf("unexpected error while writing chunk: %v", err)
495 }
496 if n != len(chunk) {
497 t.Errorf("write is not complete, expected: %d bytes, written: %d bytes", len(chunk), n)
498 }
499
500 fullPayload = append(fullPayload, chunk...)
501 }
502
503 err := drw.Close()
504 if err != nil {
505 t.Fatalf("unexpected error when closing deferredResponseWriter: %v", err)
506 }
507
508 res := mockResponseWriter.Result()
509
510 if res.StatusCode != http.StatusOK {
511 t.Fatalf("status code is not writtend properly, expected: 200, got: %d", res.StatusCode)
512 }
513 if !reflect.DeepEqual(res.Header, tt.expectHeaders) {
514 t.Fatal(cmp.Diff(tt.expectHeaders, res.Header))
515 }
516
517 resBytes, err := io.ReadAll(res.Body)
518 if err != nil {
519 t.Fatalf("unexpected error occurred while reading response body: %v", err)
520 }
521
522 if tt.expectGzip {
523 gr, err := gzip.NewReader(bytes.NewReader(resBytes))
524 if err != nil {
525 t.Fatalf("failed to create gzip reader: %v", err)
526 }
527
528 decompressed, err := io.ReadAll(gr)
529 if err != nil {
530 t.Fatalf("failed to decompress: %v", err)
531 }
532
533 if !bytes.Equal(fullPayload, decompressed) {
534 t.Errorf("payload mismatch, expected: %s, got: %s", fullPayload, decompressed)
535 }
536 } else {
537 if !bytes.Equal(fullPayload, resBytes) {
538 t.Errorf("payload mismatch, expected: %s, got: %s", fullPayload, resBytes)
539 }
540 }
541 })
542 }
543 }
544
545 func benchmarkChunkingGzip(b *testing.B, count int, chunk []byte) {
546 mockResponseWriter := httptest.NewRecorder()
547 mockResponseWriter.Body = nil
548
549 drw := &deferredResponseWriter{
550 mediaType: "text/plain",
551 statusCode: 200,
552 contentEncoding: "gzip",
553 hw: mockResponseWriter,
554 ctx: context.Background(),
555 }
556 b.ResetTimer()
557 for i := 0; i < count; i++ {
558 n, err := drw.Write(chunk)
559 if err != nil {
560 b.Fatalf("unexpected error while writing chunk: %v", err)
561 }
562 if n != len(chunk) {
563 b.Errorf("write is not complete, expected: %d bytes, written: %d bytes", len(chunk), n)
564 }
565 }
566 err := drw.Close()
567 if err != nil {
568 b.Fatalf("unexpected error when closing deferredResponseWriter: %v", err)
569 }
570 res := mockResponseWriter.Result()
571 if res.StatusCode != http.StatusOK {
572 b.Fatalf("status code is not writtend properly, expected: 200, got: %d", res.StatusCode)
573 }
574 }
575
576 func BenchmarkChunkingGzip(b *testing.B) {
577 tests := []struct {
578 count int
579 size int
580 }{
581 {
582 count: 100,
583 size: 1_000,
584 },
585 {
586 count: 100,
587 size: 100_000,
588 },
589 {
590 count: 1_000,
591 size: 100_000,
592 },
593 {
594 count: 1_000,
595 size: 1_000_000,
596 },
597 {
598 count: 10_000,
599 size: 100_000,
600 },
601 {
602 count: 100_000,
603 size: 10_000,
604 },
605 {
606 count: 1,
607 size: 100_000,
608 },
609 {
610 count: 1,
611 size: 1_000_000,
612 },
613 {
614 count: 1,
615 size: 10_000_000,
616 },
617 {
618 count: 1,
619 size: 100_000_000,
620 },
621 {
622 count: 1,
623 size: 1_000_000_000,
624 },
625 }
626
627 for _, t := range tests {
628 b.Run(fmt.Sprintf("Count=%d/Size=%d", t.count, t.size), func(b *testing.B) {
629 chunk := []byte(rand2.String(t.size))
630 benchmarkChunkingGzip(b, t.count, chunk)
631 })
632 }
633 }
634
635 func randTime(t *time.Time, r *rand.Rand) {
636 *t = time.Unix(r.Int63n(1000*365*24*60*60), r.Int63())
637 }
638
639 func randIP(s *string, r *rand.Rand) {
640 *s = fmt.Sprintf("10.20.%d.%d", r.Int31n(256), r.Int31n(256))
641 }
642
643 // randPod changes fields in pod to mimic another pod from the same replicaset.
644 // The list fields here has been generated by picking two pods in the same replicaset
645 // and checking diff of their jsons.
646 func randPod(b *testing.B, pod *v1.Pod, r *rand.Rand) {
647 pod.Name = fmt.Sprintf("%s-%x", pod.GenerateName, r.Int63n(1000))
648 pod.UID = uuid.NewUUID()
649 pod.ResourceVersion = strconv.Itoa(r.Int())
650 pod.Spec.NodeName = fmt.Sprintf("some-node-prefix-%x", r.Int63n(1000))
651
652 randTime(&pod.CreationTimestamp.Time, r)
653 randTime(&pod.Status.StartTime.Time, r)
654 for i := range pod.Status.Conditions {
655 randTime(&pod.Status.Conditions[i].LastTransitionTime.Time, r)
656 }
657 for i := range pod.Status.ContainerStatuses {
658 containerStatus := &pod.Status.ContainerStatuses[i]
659 state := &containerStatus.State
660 if state.Running != nil {
661 randTime(&state.Running.StartedAt.Time, r)
662 }
663 containerStatus.ContainerID = fmt.Sprintf("docker://%x%x%x%x", r.Int63(), r.Int63(), r.Int63(), r.Int63())
664 }
665 for i := range pod.ManagedFields {
666 randTime(&pod.ManagedFields[i].Time.Time, r)
667 }
668
669 randIP(&pod.Status.HostIP, r)
670 randIP(&pod.Status.PodIP, r)
671 }
672
673 func benchmarkItems(b *testing.B, file string, n int) *v1.PodList {
674 pod := v1.Pod{}
675 f, err := os.Open(file)
676 if err != nil {
677 b.Fatalf("Failed to open %q: %v", file, err)
678 }
679 defer f.Close()
680 err = json.NewDecoder(f).Decode(&pod)
681 if err != nil {
682 b.Fatalf("Failed to decode %q: %v", file, err)
683 }
684
685 list := &v1.PodList{
686 Items: make([]v1.Pod, n),
687 }
688
689 r := rand.New(rand.NewSource(benchmarkSeed))
690 for i := 0; i < n; i++ {
691 list.Items[i] = *pod.DeepCopy()
692 randPod(b, &list.Items[i], r)
693 }
694 return list
695 }
696
697 func toProtoBuf(b *testing.B, list *v1.PodList) []byte {
698 out, err := list.Marshal()
699 if err != nil {
700 b.Fatalf("Failed to marshal list to protobuf: %v", err)
701 }
702 return out
703 }
704
705 func toJSON(b *testing.B, list *v1.PodList) []byte {
706 out, err := json.Marshal(list)
707 if err != nil {
708 b.Fatalf("Failed to marshal list to json: %v", err)
709 }
710 return out
711 }
712
713 func benchmarkSerializeObject(b *testing.B, payload []byte) {
714 input, output := len(payload), len(gzipContent(payload, defaultGzipContentEncodingLevel))
715 b.Logf("Payload size: %d, expected output size: %d, ratio: %.2f", input, output, float64(output)/float64(input))
716
717 req := &http.Request{
718 Header: http.Header{
719 "Accept-Encoding": []string{"gzip"},
720 },
721 URL: &url.URL{Path: "/path"},
722 }
723 featuregatetesting.SetFeatureGateDuringTest(b, utilfeature.DefaultFeatureGate, features.APIResponseCompression, true)
724
725 encoder := &fakeEncoder{
726 buf: payload,
727 }
728
729 b.ResetTimer()
730 for i := 0; i < b.N; i++ {
731 recorder := httptest.NewRecorder()
732 SerializeObject("application/json", encoder, recorder, req, http.StatusOK, nil /* object */)
733 result := recorder.Result()
734 if result.StatusCode != http.StatusOK {
735 b.Fatalf("incorrect status code: got %v; want: %v", result.StatusCode, http.StatusOK)
736 }
737 }
738 }
739
740 func BenchmarkSerializeObject1000PodsPB(b *testing.B) {
741 benchmarkSerializeObject(b, toProtoBuf(b, benchmarkItems(b, "testdata/pod.json", 1000)))
742 }
743 func BenchmarkSerializeObject10000PodsPB(b *testing.B) {
744 benchmarkSerializeObject(b, toProtoBuf(b, benchmarkItems(b, "testdata/pod.json", 10000)))
745 }
746 func BenchmarkSerializeObject100000PodsPB(b *testing.B) {
747 benchmarkSerializeObject(b, toProtoBuf(b, benchmarkItems(b, "testdata/pod.json", 100000)))
748 }
749
750 func BenchmarkSerializeObject1000PodsJSON(b *testing.B) {
751 benchmarkSerializeObject(b, toJSON(b, benchmarkItems(b, "testdata/pod.json", 1000)))
752 }
753 func BenchmarkSerializeObject10000PodsJSON(b *testing.B) {
754 benchmarkSerializeObject(b, toJSON(b, benchmarkItems(b, "testdata/pod.json", 10000)))
755 }
756 func BenchmarkSerializeObject100000PodsJSON(b *testing.B) {
757 benchmarkSerializeObject(b, toJSON(b, benchmarkItems(b, "testdata/pod.json", 100000)))
758 }
759
760 type fakeResponseRecorder struct {
761 *httptest.ResponseRecorder
762 fe *fakeEncoder
763 errorAfterEncoding bool
764 }
765
766 func (frw *fakeResponseRecorder) Write(buf []byte) (int, error) {
767 if frw.errorAfterEncoding && frw.fe.encodeCalled {
768 return 0, errors.New("returning a requested error")
769 }
770 return frw.ResponseRecorder.Write(buf)
771 }
772
773 type fakeEncoder struct {
774 obj runtime.Object
775 buf []byte
776 errs []error
777
778 encodeCalled bool
779 }
780
781 func (e *fakeEncoder) Encode(obj runtime.Object, w io.Writer) error {
782 e.obj = obj
783 if len(e.errs) > 0 {
784 err := e.errs[0]
785 e.errs = e.errs[1:]
786 return err
787 }
788 _, err := w.Write(e.buf)
789 e.encodeCalled = true
790 return err
791 }
792
793 func (e *fakeEncoder) Identifier() runtime.Identifier {
794 return runtime.Identifier("fake")
795 }
796
797 func gzipContent(data []byte, level int) []byte {
798 buf := &bytes.Buffer{}
799 gw, err := gzip.NewWriterLevel(buf, level)
800 if err != nil {
801 panic(err)
802 }
803 if _, err := gw.Write(data); err != nil {
804 panic(err)
805 }
806 if err := gw.Close(); err != nil {
807 panic(err)
808 }
809 return buf.Bytes()
810 }
811
812 func TestStreamingGzipIntegration(t *testing.T) {
813 largeChunk := bytes.Repeat([]byte("b"), defaultGzipThresholdBytes+1)
814 tcs := []struct {
815 name string
816 serializer runtime.Encoder
817 object runtime.Object
818 expectGzip bool
819 expectStreaming bool
820 }{
821 {
822 name: "JSON, small object, default -> no gzip",
823 serializer: jsonserializer.NewSerializerWithOptions(jsonserializer.DefaultMetaFactory, nil, nil, jsonserializer.SerializerOptions{}),
824 object: &testapigroupv1.CarpList{},
825 expectGzip: false,
826 expectStreaming: false,
827 },
828 {
829 name: "JSON, small object, streaming -> no gzip",
830 serializer: jsonserializer.NewSerializerWithOptions(jsonserializer.DefaultMetaFactory, nil, nil, jsonserializer.SerializerOptions{StreamingCollectionsEncoding: true}),
831 object: &testapigroupv1.CarpList{},
832 expectGzip: false,
833 expectStreaming: true,
834 },
835 {
836 name: "JSON, large object, default -> gzip",
837 serializer: jsonserializer.NewSerializerWithOptions(jsonserializer.DefaultMetaFactory, nil, nil, jsonserializer.SerializerOptions{}),
838 object: &testapigroupv1.CarpList{TypeMeta: metav1.TypeMeta{Kind: string(largeChunk)}},
839 expectGzip: true,
840 expectStreaming: false,
841 },
842 {
843 name: "JSON, large object, streaming -> gzip",
844 serializer: jsonserializer.NewSerializerWithOptions(jsonserializer.DefaultMetaFactory, nil, nil, jsonserializer.SerializerOptions{StreamingCollectionsEncoding: true}),
845 object: &testapigroupv1.CarpList{TypeMeta: metav1.TypeMeta{Kind: string(largeChunk)}},
846 expectGzip: true,
847 expectStreaming: true,
848 },
849 {
850 name: "Protobuf, small object, default -> no gzip",
851 serializer: protobuf.NewSerializerWithOptions(nil, nil, protobuf.SerializerOptions{}),
852 object: &testapigroupv1.CarpList{},
853 expectGzip: false,
854 expectStreaming: false,
855 },
856 {
857 name: "Protobuf, small object, streaming -> no gzip",
858 serializer: protobuf.NewSerializerWithOptions(nil, nil, protobuf.SerializerOptions{StreamingCollectionsEncoding: true}),
859 object: &testapigroupv1.CarpList{},
860 expectGzip: false,
861 expectStreaming: true,
862 },
863 {
864 name: "Protobuf, large object, default -> gzip",
865 serializer: protobuf.NewSerializerWithOptions(nil, nil, protobuf.SerializerOptions{}),
866 object: &testapigroupv1.CarpList{TypeMeta: metav1.TypeMeta{Kind: string(largeChunk)}},
867 expectGzip: true,
868 expectStreaming: false,
869 },
870 {
871 name: "Protobuf, large object, streaming -> gzip",
872 serializer: protobuf.NewSerializerWithOptions(nil, nil, protobuf.SerializerOptions{StreamingCollectionsEncoding: true}),
873 object: &testapigroupv1.CarpList{TypeMeta: metav1.TypeMeta{Kind: string(largeChunk)}},
874 expectGzip: true,
875 expectStreaming: true,
876 },
877 }
878 for _, tc := range tcs {
879 t.Run(tc.name, func(t *testing.T) {
880 mockResponseWriter := httptest.NewRecorder()
881 drw := &deferredResponseWriter{
882 mediaType: "text/plain",
883 statusCode: 200,
884 contentEncoding: "gzip",
885 hw: mockResponseWriter,
886 ctx: context.Background(),
887 }
888 counter := &writeCounter{Writer: drw}
889 err := tc.serializer.Encode(tc.object, counter)
890 if err != nil {
891 t.Fatal(err)
892 }
893 encoding := mockResponseWriter.Header().Get("Content-Encoding")
894 if (encoding == "gzip") != tc.expectGzip {
895 t.Errorf("Expect gzip: %v, got: %q", tc.expectGzip, encoding)
896 }
897 if counter.writeCount < 1 {
898 t.Fatalf("Expect at least 1 write")
899 }
900 if (counter.writeCount > 1) != tc.expectStreaming {
901 t.Errorf("Expect streaming: %v, got write count: %d", tc.expectStreaming, counter.writeCount)
902 }
903 })
904 }
905 }
906
907 type writeCounter struct {
908 writeCount int
909 io.Writer
910 }
911
912 func (b *writeCounter) Write(data []byte) (int, error) {
913 b.writeCount++
914 return b.Writer.Write(data)
915 }