From 86d02ac36811dfadad089f9a5d2ceb95b1288056 Mon Sep 17 00:00:00 2001 From: Steve Larkin Date: Sun, 10 Dec 2017 13:00:14 +0100 Subject: [PATCH 1/2] Fix YAMLDecoder Read behaviour Make it adhere to the Read contract by returning the number of bytes read. --- .../k8s.io/apimachinery/pkg/util/yaml/decoder.go | 2 +- .../apimachinery/pkg/util/yaml/decoder_test.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder.go b/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder.go index 6ebfaea707d..56de33a7fdf 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder.go @@ -122,7 +122,7 @@ func (d *YAMLDecoder) Read(data []byte) (n int, err error) { if left <= len(data) { copy(data, d.remaining) d.remaining = nil - return len(d.remaining), nil + return left, nil } // caller will need to reread diff --git a/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder_test.go b/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder_test.go index bd4403648f4..1eebd2018f0 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder_test.go @@ -22,12 +22,26 @@ import ( "encoding/json" "fmt" "io" + "io/ioutil" "math/rand" "reflect" "strings" "testing" ) +func TestYAMLDecoder(t *testing.T) { + d := `--- +stuff: 1 + test-foo: 1 +` + s := NewDocumentDecoder(ioutil.NopCloser(bytes.NewReader([]byte(d)))) + b := make([]byte, len(d)) + n, err := s.Read(b) + if err != nil || n != len(d) { + t.Fatalf("unexpected body: %d / %v", n, err) + } +} + func TestSplitYAMLDocument(t *testing.T) { testCases := []struct { input string From e913612003cbe71c9f58dc6c65913f6e2cc4c345 Mon Sep 17 00:00:00 2001 From: Steve Larkin Date: Tue, 12 Dec 2017 19:11:23 +0100 Subject: [PATCH 2/2] Extend YAMLDecoder Read tests --- .../pkg/util/yaml/decoder_test.go | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder_test.go b/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder_test.go index 1eebd2018f0..3c1ad7b2219 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder_test.go @@ -29,16 +29,28 @@ import ( "testing" ) -func TestYAMLDecoder(t *testing.T) { +func TestYAMLDecoderReadBytesLength(t *testing.T) { d := `--- stuff: 1 test-foo: 1 ` - s := NewDocumentDecoder(ioutil.NopCloser(bytes.NewReader([]byte(d)))) - b := make([]byte, len(d)) - n, err := s.Read(b) - if err != nil || n != len(d) { - t.Fatalf("unexpected body: %d / %v", n, err) + testCases := []struct { + bufLen int + expectLen int + expectErr error + }{ + {len(d), len(d), nil}, + {len(d) + 10, len(d), nil}, + {len(d) - 10, len(d) - 10, io.ErrShortBuffer}, + } + + for i, testCase := range testCases { + r := NewDocumentDecoder(ioutil.NopCloser(bytes.NewReader([]byte(d)))) + b := make([]byte, testCase.bufLen) + n, err := r.Read(b) + if err != testCase.expectErr || n != testCase.expectLen { + t.Fatalf("%d: unexpected body: %d / %v", i, n, err) + } } }