2014-07-19 01:51:28 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-09-11 17:02:53 +00:00
|
|
|
package runtime_test
|
2014-07-19 01:51:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-09-11 17:02:53 +00:00
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-12-03 13:41:57 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-07-19 01:51:28 +00:00
|
|
|
)
|
|
|
|
|
2014-09-18 10:08:10 +00:00
|
|
|
type EmbeddedTest struct {
|
2014-12-03 13:41:57 +00:00
|
|
|
runtime.TypeMeta
|
|
|
|
ID string
|
|
|
|
Object runtime.EmbeddedObject
|
|
|
|
EmptyObject runtime.EmbeddedObject
|
2014-09-11 17:02:53 +00:00
|
|
|
}
|
|
|
|
|
2014-09-18 10:08:10 +00:00
|
|
|
type EmbeddedTestExternal struct {
|
2014-12-01 05:31:52 +00:00
|
|
|
runtime.TypeMeta `json:",inline"`
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
Object runtime.RawExtension `json:"object,omitempty"`
|
|
|
|
EmptyObject runtime.RawExtension `json:"emptyObject,omitempty"`
|
2014-09-05 23:11:30 +00:00
|
|
|
}
|
|
|
|
|
2014-12-03 13:41:57 +00:00
|
|
|
type ObjectTest struct {
|
|
|
|
runtime.TypeMeta
|
|
|
|
|
|
|
|
ID string
|
|
|
|
Items []runtime.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
type ObjectTestExternal struct {
|
|
|
|
runtime.TypeMeta `yaml:",inline" json:",inline"`
|
|
|
|
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
Items []runtime.RawExtension `json:"items,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*ObjectTest) IsAnAPIObject() {}
|
|
|
|
func (*ObjectTestExternal) IsAnAPIObject() {}
|
2014-09-18 10:08:10 +00:00
|
|
|
func (*EmbeddedTest) IsAnAPIObject() {}
|
|
|
|
func (*EmbeddedTestExternal) IsAnAPIObject() {}
|
2014-09-05 23:11:30 +00:00
|
|
|
|
2014-12-03 13:41:57 +00:00
|
|
|
func TestDecodeEmptyRawExtensionAsObject(t *testing.T) {
|
|
|
|
s := runtime.NewScheme()
|
|
|
|
s.AddKnownTypes("", &ObjectTest{})
|
|
|
|
s.AddKnownTypeWithName("v1test", "ObjectTest", &ObjectTestExternal{})
|
|
|
|
|
2015-04-29 03:15:16 +00:00
|
|
|
obj, err := s.Decode([]byte(`{"kind":"ObjectTest","apiVersion":"v1test","items":[{}]}`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
test := obj.(*ObjectTest)
|
|
|
|
if unk, ok := test.Items[0].(*runtime.Unknown); !ok || unk.Kind != "" || unk.APIVersion != "" || string(unk.RawJSON) != "{}" {
|
|
|
|
t.Fatalf("unexpected object: %#v", test.Items[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err = s.Decode([]byte(`{"kind":"ObjectTest","apiVersion":"v1test","items":[{"kind":"Other","apiVersion":"v1"}]}`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
test = obj.(*ObjectTest)
|
|
|
|
if unk, ok := test.Items[0].(*runtime.Unknown); !ok || unk.Kind != "Other" || unk.APIVersion != "v1" || string(unk.RawJSON) != `{"kind":"Other","apiVersion":"v1"}` {
|
|
|
|
t.Fatalf("unexpected object: %#v", test.Items[0])
|
2014-12-03 13:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArrayOfRuntimeObject(t *testing.T) {
|
|
|
|
s := runtime.NewScheme()
|
|
|
|
s.AddKnownTypes("", &EmbeddedTest{})
|
|
|
|
s.AddKnownTypeWithName("v1test", "EmbeddedTest", &EmbeddedTestExternal{})
|
|
|
|
s.AddKnownTypes("", &ObjectTest{})
|
|
|
|
s.AddKnownTypeWithName("v1test", "ObjectTest", &ObjectTestExternal{})
|
|
|
|
|
|
|
|
internal := &ObjectTest{
|
|
|
|
Items: []runtime.Object{
|
|
|
|
&EmbeddedTest{ID: "foo"},
|
|
|
|
&EmbeddedTest{ID: "bar"},
|
|
|
|
// TODO: until YAML is removed, this JSON must be in ascending key order to ensure consistent roundtrip serialization
|
|
|
|
&runtime.Unknown{RawJSON: []byte(`{"apiVersion":"unknown","foo":"bar","kind":"OtherTest"}`)},
|
|
|
|
&ObjectTest{
|
|
|
|
Items: []runtime.Object{
|
|
|
|
&EmbeddedTest{ID: "baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
wire, err := s.EncodeToVersion(internal, "v1test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
t.Logf("Wire format is:\n%s\n", string(wire))
|
|
|
|
|
|
|
|
obj := &ObjectTestExternal{}
|
|
|
|
if err := json.Unmarshal(wire, obj); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2015-04-29 03:15:16 +00:00
|
|
|
t.Logf("exact wire is: %s", string(obj.Items[0].RawJSON))
|
2014-12-03 13:41:57 +00:00
|
|
|
|
|
|
|
decoded, err := s.Decode(wire)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2015-04-29 03:15:16 +00:00
|
|
|
list, err := runtime.ExtractList(decoded)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if errs := runtime.DecodeList(list, s); len(errs) > 0 {
|
|
|
|
t.Fatalf("unexpected error: %v", errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
list2, err := runtime.ExtractList(list[3])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if errs := runtime.DecodeList(list2, s); len(errs) > 0 {
|
|
|
|
t.Fatalf("unexpected error: %v", errs)
|
|
|
|
}
|
|
|
|
if err := runtime.SetList(list[3], list2); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2014-12-03 13:41:57 +00:00
|
|
|
|
|
|
|
internal.Items[2].(*runtime.Unknown).Kind = "OtherTest"
|
|
|
|
internal.Items[2].(*runtime.Unknown).APIVersion = "unknown"
|
2015-04-29 03:15:16 +00:00
|
|
|
if e, a := internal.Items, list; !reflect.DeepEqual(e, a) {
|
2014-12-03 13:41:57 +00:00
|
|
|
t.Errorf("mismatched decoded: %s", util.ObjectDiff(e, a))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 21:43:35 +00:00
|
|
|
func TestEmbeddedObject(t *testing.T) {
|
2014-12-03 13:41:57 +00:00
|
|
|
s := runtime.NewScheme()
|
2014-09-05 23:11:30 +00:00
|
|
|
s.AddKnownTypes("", &EmbeddedTest{})
|
2014-09-18 10:08:10 +00:00
|
|
|
s.AddKnownTypeWithName("v1test", "EmbeddedTest", &EmbeddedTestExternal{})
|
2014-07-19 01:51:28 +00:00
|
|
|
|
|
|
|
outer := &EmbeddedTest{
|
2014-12-03 13:41:57 +00:00
|
|
|
ID: "outer",
|
2014-09-18 10:08:10 +00:00
|
|
|
Object: runtime.EmbeddedObject{
|
2014-07-19 01:51:28 +00:00
|
|
|
&EmbeddedTest{
|
2014-12-03 13:41:57 +00:00
|
|
|
ID: "inner",
|
2014-07-19 01:51:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-11 17:02:53 +00:00
|
|
|
wire, err := s.EncodeToVersion(outer, "v1test")
|
2014-07-19 01:51:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected encode error '%v'", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Wire format is:\n%v\n", string(wire))
|
|
|
|
|
2014-09-05 23:11:30 +00:00
|
|
|
decoded, err := s.Decode(wire)
|
2014-07-19 01:51:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected decode error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, a := outer, decoded; !reflect.DeepEqual(e, a) {
|
|
|
|
t.Errorf("Expected: %#v but got %#v", e, a)
|
|
|
|
}
|
|
|
|
|
2014-09-18 10:08:10 +00:00
|
|
|
// test JSON decoding of the external object, which should preserve
|
|
|
|
// raw bytes
|
|
|
|
var externalViaJSON EmbeddedTestExternal
|
|
|
|
err = json.Unmarshal(wire, &externalViaJSON)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected decode error %v", err)
|
|
|
|
}
|
2014-12-03 13:43:09 +00:00
|
|
|
if externalViaJSON.Kind == "" || externalViaJSON.APIVersion == "" || externalViaJSON.ID != "outer" {
|
2014-09-18 10:08:10 +00:00
|
|
|
t.Errorf("Expected objects to have type info set, got %#v", externalViaJSON)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(externalViaJSON.EmptyObject.RawJSON, []byte("null")) || len(externalViaJSON.Object.RawJSON) == 0 {
|
|
|
|
t.Errorf("Expected deserialization of nested objects into bytes, got %#v", externalViaJSON)
|
|
|
|
}
|
|
|
|
|
2014-09-02 17:55:27 +00:00
|
|
|
// test JSON decoding, too, since Decode uses yaml unmarshalling.
|
2014-09-18 10:08:10 +00:00
|
|
|
// Generic Unmarshalling of JSON cannot load the nested objects because there is
|
|
|
|
// no default schema set. Consumers wishing to get direct JSON decoding must use
|
|
|
|
// the external representation
|
2014-07-19 01:51:28 +00:00
|
|
|
var decodedViaJSON EmbeddedTest
|
|
|
|
err = json.Unmarshal(wire, &decodedViaJSON)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected decode error %v", err)
|
|
|
|
}
|
2014-09-18 10:08:10 +00:00
|
|
|
if a := decodedViaJSON; a.Object.Object != nil || a.EmptyObject.Object != nil {
|
|
|
|
t.Errorf("Expected embedded objects to be nil: %#v", a)
|
2014-07-19 01:51:28 +00:00
|
|
|
}
|
|
|
|
}
|