Skip to content

Commit

Permalink
Merge pull request #57000 from sel/master
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue (batch tested with PRs 57324, 56931, 57000, 57150, 56965). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Fix YAMLDecoder Read behaviour

**What this PR does / why we need it**:

Makes YAMLDecoder adhere to the Read contract by returning the number of bytes
read.

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #56999

**Special notes for your reviewer**:

**Release note**:

```release-note
YAMLDecoder Read now returns the number of bytes read
```
  • Loading branch information
Kubernetes Submit Queue committed Dec 18, 2017
2 parents dcee5ce + e913612 commit 7e8dd6c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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

0 comments on commit 7e8dd6c

Please sign in to comment.