RecognizingDecoder didn't handle ambiguous input

YAML is not guaranteed to be recognizable, so we need to bump JSON and
protobuf above it in the decoding order. Add a unit test.
This commit is contained in:
Clayton Coleman
2016-04-23 14:54:54 -04:00
parent 7e1089bb75
commit 4ad5565c41
4 changed files with 101 additions and 36 deletions

View File

@@ -17,6 +17,7 @@ limitations under the License.
package api_test
import (
"bytes"
"encoding/hex"
"math/rand"
"testing"
@@ -41,6 +42,28 @@ func init() {
})
}
func TestUniversalDeserializer(t *testing.T) {
expected := &v1.Pod{ObjectMeta: v1.ObjectMeta{Name: "test"}}
d := api.Codecs.UniversalDeserializer()
for _, mediaType := range []string{"application/json", "application/yaml", "application/vnd.kubernetes.protobuf"} {
e, ok := api.Codecs.SerializerForMediaType(mediaType, nil)
if !ok {
t.Fatal(mediaType)
}
buf := &bytes.Buffer{}
if err := e.EncodeToStream(expected, buf); err != nil {
t.Fatalf("%s: %v", mediaType, err)
}
obj, _, err := d.Decode(buf.Bytes(), &unversioned.GroupVersionKind{Kind: "Pod", Version: "v1"}, nil)
if err != nil {
t.Fatalf("%s: %v", mediaType, err)
}
if !api.Semantic.DeepEqual(expected, obj) {
t.Fatalf("%s: %#v", mediaType, obj)
}
}
}
func TestProtobufRoundTrip(t *testing.T) {
obj := &v1.Pod{}
apitesting.FuzzerFor(t, v1.SchemeGroupVersion, rand.NewSource(benchmarkSeed)).Fuzz(obj)