Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix YAMLDecoder Read behaviour #57000

Merged
merged 2 commits into from
Dec 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,38 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"reflect"
"strings"
"testing"
)

func TestYAMLDecoderReadBytesLength(t *testing.T) {
d := `---
stuff: 1
test-foo: 1
`
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)
}
}
}

func TestSplitYAMLDocument(t *testing.T) {
testCases := []struct {
input string
Expand Down