Add documentation and tests to conversion.
This commit is contained in:
@@ -28,31 +28,33 @@ import (
|
||||
|
||||
var fuzzIters = flag.Int("fuzz_iters", 10, "How many fuzzing iterations to do.")
|
||||
|
||||
// Intended to be compatible with the default implementation of MetaInsertionFactory
|
||||
type JSONBase struct {
|
||||
// Test a weird version/kind embedding format.
|
||||
type MyWeirdCustomEmbeddedVersionKindField struct {
|
||||
ID string `yaml:"ID,omitempty" json:"ID,omitempty"`
|
||||
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
|
||||
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
|
||||
APIVersion string `json:"myVersionKey,omitempty" yaml:"myVersionKey,omitempty"`
|
||||
ObjectKind string `json:"myKindKey,omitempty" yaml:"myKindKey,omitempty"`
|
||||
Z string `yaml:"Z,omitempty" json:"Z,omitempty"`
|
||||
Y uint64 `yaml:"Y,omitempty" json:"Y,omitempty"`
|
||||
}
|
||||
|
||||
type TestType1 struct {
|
||||
JSONBase `json:",inline" yaml:",inline"`
|
||||
A string `yaml:"A,omitempty" json:"A,omitempty"`
|
||||
B int `yaml:"B,omitempty" json:"B,omitempty"`
|
||||
C int8 `yaml:"C,omitempty" json:"C,omitempty"`
|
||||
D int16 `yaml:"D,omitempty" json:"D,omitempty"`
|
||||
E int32 `yaml:"E,omitempty" json:"E,omitempty"`
|
||||
F int64 `yaml:"F,omitempty" json:"F,omitempty"`
|
||||
G uint `yaml:"G,omitempty" json:"G,omitempty"`
|
||||
H uint8 `yaml:"H,omitempty" json:"H,omitempty"`
|
||||
I uint16 `yaml:"I,omitempty" json:"I,omitempty"`
|
||||
J uint32 `yaml:"J,omitempty" json:"J,omitempty"`
|
||||
K uint64 `yaml:"K,omitempty" json:"K,omitempty"`
|
||||
L bool `yaml:"L,omitempty" json:"L,omitempty"`
|
||||
M map[string]int `yaml:"M,omitempty" json:"M,omitempty"`
|
||||
N map[string]TestType2 `yaml:"N,omitempty" json:"N,omitempty"`
|
||||
O *TestType2 `yaml:"O,omitempty" json:"O,omitempty"`
|
||||
P []TestType2 `yaml:"Q,omitempty" json:"Q,omitempty"`
|
||||
MyWeirdCustomEmbeddedVersionKindField `json:",inline" yaml:",inline"`
|
||||
A string `yaml:"A,omitempty" json:"A,omitempty"`
|
||||
B int `yaml:"B,omitempty" json:"B,omitempty"`
|
||||
C int8 `yaml:"C,omitempty" json:"C,omitempty"`
|
||||
D int16 `yaml:"D,omitempty" json:"D,omitempty"`
|
||||
E int32 `yaml:"E,omitempty" json:"E,omitempty"`
|
||||
F int64 `yaml:"F,omitempty" json:"F,omitempty"`
|
||||
G uint `yaml:"G,omitempty" json:"G,omitempty"`
|
||||
H uint8 `yaml:"H,omitempty" json:"H,omitempty"`
|
||||
I uint16 `yaml:"I,omitempty" json:"I,omitempty"`
|
||||
J uint32 `yaml:"J,omitempty" json:"J,omitempty"`
|
||||
K uint64 `yaml:"K,omitempty" json:"K,omitempty"`
|
||||
L bool `yaml:"L,omitempty" json:"L,omitempty"`
|
||||
M map[string]int `yaml:"M,omitempty" json:"M,omitempty"`
|
||||
N map[string]TestType2 `yaml:"N,omitempty" json:"N,omitempty"`
|
||||
O *TestType2 `yaml:"O,omitempty" json:"O,omitempty"`
|
||||
P []TestType2 `yaml:"Q,omitempty" json:"Q,omitempty"`
|
||||
}
|
||||
|
||||
type TestType2 struct {
|
||||
@@ -69,39 +71,39 @@ func externalTypeReturn() interface{} {
|
||||
B int `yaml:"B,omitempty" json:"B,omitempty"`
|
||||
}
|
||||
type TestType1 struct {
|
||||
JSONBase `json:",inline" yaml:",inline"`
|
||||
A string `yaml:"A,omitempty" json:"A,omitempty"`
|
||||
B int `yaml:"B,omitempty" json:"B,omitempty"`
|
||||
C int8 `yaml:"C,omitempty" json:"C,omitempty"`
|
||||
D int16 `yaml:"D,omitempty" json:"D,omitempty"`
|
||||
E int32 `yaml:"E,omitempty" json:"E,omitempty"`
|
||||
F int64 `yaml:"F,omitempty" json:"F,omitempty"`
|
||||
G uint `yaml:"G,omitempty" json:"G,omitempty"`
|
||||
H uint8 `yaml:"H,omitempty" json:"H,omitempty"`
|
||||
I uint16 `yaml:"I,omitempty" json:"I,omitempty"`
|
||||
J uint32 `yaml:"J,omitempty" json:"J,omitempty"`
|
||||
K uint64 `yaml:"K,omitempty" json:"K,omitempty"`
|
||||
L bool `yaml:"L,omitempty" json:"L,omitempty"`
|
||||
M map[string]int `yaml:"M,omitempty" json:"M,omitempty"`
|
||||
N map[string]TestType2 `yaml:"N,omitempty" json:"N,omitempty"`
|
||||
O *TestType2 `yaml:"O,omitempty" json:"O,omitempty"`
|
||||
P []TestType2 `yaml:"Q,omitempty" json:"Q,omitempty"`
|
||||
MyWeirdCustomEmbeddedVersionKindField `json:",inline" yaml:",inline"`
|
||||
A string `yaml:"A,omitempty" json:"A,omitempty"`
|
||||
B int `yaml:"B,omitempty" json:"B,omitempty"`
|
||||
C int8 `yaml:"C,omitempty" json:"C,omitempty"`
|
||||
D int16 `yaml:"D,omitempty" json:"D,omitempty"`
|
||||
E int32 `yaml:"E,omitempty" json:"E,omitempty"`
|
||||
F int64 `yaml:"F,omitempty" json:"F,omitempty"`
|
||||
G uint `yaml:"G,omitempty" json:"G,omitempty"`
|
||||
H uint8 `yaml:"H,omitempty" json:"H,omitempty"`
|
||||
I uint16 `yaml:"I,omitempty" json:"I,omitempty"`
|
||||
J uint32 `yaml:"J,omitempty" json:"J,omitempty"`
|
||||
K uint64 `yaml:"K,omitempty" json:"K,omitempty"`
|
||||
L bool `yaml:"L,omitempty" json:"L,omitempty"`
|
||||
M map[string]int `yaml:"M,omitempty" json:"M,omitempty"`
|
||||
N map[string]TestType2 `yaml:"N,omitempty" json:"N,omitempty"`
|
||||
O *TestType2 `yaml:"O,omitempty" json:"O,omitempty"`
|
||||
P []TestType2 `yaml:"Q,omitempty" json:"Q,omitempty"`
|
||||
}
|
||||
return TestType1{}
|
||||
}
|
||||
|
||||
type ExternalInternalSame struct {
|
||||
JSONBase `json:",inline" yaml:",inline"`
|
||||
A TestType2 `yaml:"A,omitempty" json:"A,omitempty"`
|
||||
MyWeirdCustomEmbeddedVersionKindField `json:",inline" yaml:",inline"`
|
||||
A TestType2 `yaml:"A,omitempty" json:"A,omitempty"`
|
||||
}
|
||||
|
||||
// TestObjectFuzzer can randomly populate all the above objects.
|
||||
var TestObjectFuzzer = util.NewFuzzer(
|
||||
func(j *JSONBase) {
|
||||
// We have to customize the randomization of JSONBases because their
|
||||
func(j *MyWeirdCustomEmbeddedVersionKindField) {
|
||||
// We have to customize the randomization of MyWeirdCustomEmbeddedVersionKindFields because their
|
||||
// APIVersion and Kind must remain blank in memory.
|
||||
j.APIVersion = ""
|
||||
j.Kind = ""
|
||||
j.ObjectKind = ""
|
||||
j.ID = util.RandString()
|
||||
},
|
||||
func(u *uint64) {
|
||||
@@ -125,9 +127,32 @@ func GetTestScheme() *Scheme {
|
||||
s.AddKnownTypes("v1", externalTypeReturn(), ExternalInternalSame{})
|
||||
s.ExternalVersion = "v1"
|
||||
s.InternalVersion = ""
|
||||
s.MetaInsertionFactory = testMetaInsertionFactory{}
|
||||
return s
|
||||
}
|
||||
|
||||
type testMetaInsertionFactory struct {
|
||||
MyWeirdCustomEmbeddedVersionKindField struct {
|
||||
APIVersion string `json:"myVersionKey,omitempty" yaml:"myVersionKey,omitempty"`
|
||||
ObjectKind string `json:"myKindKey,omitempty" yaml:"myKindKey,omitempty"`
|
||||
} `json:",inline" yaml:",inline"`
|
||||
}
|
||||
|
||||
// Create returns a new testMetaInsertionFactory with the version and kind fields set.
|
||||
func (testMetaInsertionFactory) Create(version, kind string) interface{} {
|
||||
m := testMetaInsertionFactory{}
|
||||
m.MyWeirdCustomEmbeddedVersionKindField.APIVersion = version
|
||||
m.MyWeirdCustomEmbeddedVersionKindField.ObjectKind = kind
|
||||
return &m
|
||||
}
|
||||
|
||||
// Interpret returns the version and kind information from in, which must be
|
||||
// a testMetaInsertionFactory pointer object.
|
||||
func (testMetaInsertionFactory) Interpret(in interface{}) (version, kind string) {
|
||||
m := in.(*testMetaInsertionFactory)
|
||||
return m.MyWeirdCustomEmbeddedVersionKindField.APIVersion, m.MyWeirdCustomEmbeddedVersionKindField.ObjectKind
|
||||
}
|
||||
|
||||
func objDiff(a, b interface{}) string {
|
||||
ab, err := json.Marshal(a)
|
||||
if err != nil {
|
||||
@@ -162,22 +187,20 @@ func runTest(t *testing.T, source interface{}) {
|
||||
if err != nil {
|
||||
t.Errorf("%v: %v (%v)", name, err, string(data))
|
||||
return
|
||||
} else {
|
||||
if !reflect.DeepEqual(source, obj2) {
|
||||
t.Errorf("1: %v: diff: %v", name, objDiff(source, obj2))
|
||||
return
|
||||
}
|
||||
}
|
||||
if !reflect.DeepEqual(source, obj2) {
|
||||
t.Errorf("1: %v: diff: %v", name, objDiff(source, obj2))
|
||||
return
|
||||
}
|
||||
obj3 := reflect.New(reflect.TypeOf(source).Elem()).Interface()
|
||||
err = s.DecodeInto(data, obj3)
|
||||
if err != nil {
|
||||
t.Errorf("2: %v: %v", name, err)
|
||||
return
|
||||
} else {
|
||||
if !reflect.DeepEqual(source, obj3) {
|
||||
t.Errorf("3: %v: diff: %v", name, objDiff(source, obj3))
|
||||
return
|
||||
}
|
||||
}
|
||||
if !reflect.DeepEqual(source, obj3) {
|
||||
t.Errorf("3: %v: diff: %v", name, objDiff(source, obj3))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,17 +254,17 @@ func TestEncode_Ptr(t *testing.T) {
|
||||
func TestBadJSONRejection(t *testing.T) {
|
||||
s := GetTestScheme()
|
||||
badJSONs := [][]byte{
|
||||
[]byte(`{"apiVersion":"v1"}`), // Missing kind
|
||||
[]byte(`{"kind":"TestType1"}`), // Missing version
|
||||
[]byte(`{"apiVersion":"v1","kind":"bar"}`), // Unknown kind
|
||||
[]byte(`{"apiVersion":"bar","kind":"TestType1"}`), // Unknown version
|
||||
[]byte(`{"myVersionKey":"v1"}`), // Missing kind
|
||||
[]byte(`{"myKindKey":"TestType1"}`), // Missing version
|
||||
[]byte(`{"myVersionKey":"v1","myKindKey":"bar"}`), // Unknown kind
|
||||
[]byte(`{"myVersionKey":"bar","myKindKey":"TestType1"}`), // Unknown version
|
||||
}
|
||||
for _, b := range badJSONs {
|
||||
if _, err := s.Decode(b); err == nil {
|
||||
t.Errorf("Did not reject bad json: %s", string(b))
|
||||
}
|
||||
}
|
||||
badJSONKindMismatch := []byte(`{"apiVersion":"v1","kind": "ExternalInternalSame"}`)
|
||||
badJSONKindMismatch := []byte(`{"myVersionKey":"v1","myKindKey":"ExternalInternalSame"}`)
|
||||
if err := s.DecodeInto(badJSONKindMismatch, &TestType1{}); err == nil {
|
||||
t.Errorf("Kind is set but doesn't match the object type: %s", badJSONKindMismatch)
|
||||
}
|
||||
|
Reference in New Issue
Block a user