mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 19:31:44 +00:00
Merge pull request #57000 from sel/master
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 ```
This commit is contained in:
commit
7e8dd6cd4a
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user