github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/runtime/serializer/streaming/streaming_test.go (about) 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 streaming 18 19 import ( 20 "bytes" 21 "io" 22 "io/ioutil" 23 "testing" 24 25 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime" 26 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema" 27 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/util/framer" 28 ) 29 30 type fakeDecoder struct { 31 got []byte 32 obj runtime.Object 33 err error 34 } 35 36 func (d *fakeDecoder) Decode(data []byte, gvk *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) { 37 d.got = data 38 return d.obj, nil, d.err 39 } 40 41 func TestEmptyDecoder(t *testing.T) { 42 buf := bytes.NewBuffer([]byte{}) 43 d := &fakeDecoder{} 44 _, _, err := NewDecoder(ioutil.NopCloser(buf), d).Decode(nil, nil) 45 if err != io.EOF { 46 t.Fatal(err) 47 } 48 } 49 50 func TestDecoder(t *testing.T) { 51 frames := [][]byte{ 52 make([]byte, 1025), 53 make([]byte, 1024*5), 54 make([]byte, 1024*1024*17), 55 make([]byte, 1025), 56 } 57 pr, pw := io.Pipe() 58 fw := framer.NewLengthDelimitedFrameWriter(pw) 59 go func() { 60 for i := range frames { 61 fw.Write(frames[i]) 62 } 63 pw.Close() 64 }() 65 66 r := framer.NewLengthDelimitedFrameReader(pr) 67 d := &fakeDecoder{} 68 dec := NewDecoder(r, d) 69 if _, _, err := dec.Decode(nil, nil); err != nil || !bytes.Equal(d.got, frames[0]) { 70 t.Fatalf("unexpected %v %v", err, len(d.got)) 71 } 72 if _, _, err := dec.Decode(nil, nil); err != nil || !bytes.Equal(d.got, frames[1]) { 73 t.Fatalf("unexpected %v %v", err, len(d.got)) 74 } 75 if _, _, err := dec.Decode(nil, nil); err != ErrObjectTooLarge || !bytes.Equal(d.got, frames[1]) { 76 t.Fatalf("unexpected %v %v", err, len(d.got)) 77 } 78 if _, _, err := dec.Decode(nil, nil); err != nil || !bytes.Equal(d.got, frames[3]) { 79 t.Fatalf("unexpected %v %v", err, len(d.got)) 80 } 81 if _, _, err := dec.Decode(nil, nil); err != io.EOF { 82 t.Fatalf("unexpected %v %v", err, len(d.got)) 83 } 84 }