codec_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package serializer
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "log"
  18. "os"
  19. "reflect"
  20. "strings"
  21. "testing"
  22. "k8s.io/kubernetes/pkg/api/unversioned"
  23. "k8s.io/kubernetes/pkg/conversion"
  24. "k8s.io/kubernetes/pkg/runtime"
  25. "k8s.io/kubernetes/pkg/util/diff"
  26. "github.com/ghodss/yaml"
  27. "github.com/google/gofuzz"
  28. flag "github.com/spf13/pflag"
  29. )
  30. var fuzzIters = flag.Int("fuzz-iters", 50, "How many fuzzing iterations to do.")
  31. type testMetaFactory struct{}
  32. func (testMetaFactory) Interpret(data []byte) (*unversioned.GroupVersionKind, error) {
  33. findKind := struct {
  34. APIVersion string `json:"myVersionKey,omitempty"`
  35. ObjectKind string `json:"myKindKey,omitempty"`
  36. }{}
  37. // yaml is a superset of json, so we use it to decode here. That way,
  38. // we understand both.
  39. if err := yaml.Unmarshal(data, &findKind); err != nil {
  40. return nil, fmt.Errorf("couldn't get version/kind: %v", err)
  41. }
  42. gv, err := unversioned.ParseGroupVersion(findKind.APIVersion)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return &unversioned.GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: findKind.ObjectKind}, nil
  47. }
  48. // Test a weird version/kind embedding format.
  49. type MyWeirdCustomEmbeddedVersionKindField struct {
  50. ID string `json:"ID,omitempty"`
  51. APIVersion string `json:"myVersionKey,omitempty"`
  52. ObjectKind string `json:"myKindKey,omitempty"`
  53. Z string `json:"Z,omitempty"`
  54. Y uint64 `json:"Y,omitempty"`
  55. }
  56. type TestType1 struct {
  57. MyWeirdCustomEmbeddedVersionKindField `json:",inline"`
  58. A string `json:"A,omitempty"`
  59. B int `json:"B,omitempty"`
  60. C int8 `json:"C,omitempty"`
  61. D int16 `json:"D,omitempty"`
  62. E int32 `json:"E,omitempty"`
  63. F int64 `json:"F,omitempty"`
  64. G uint `json:"G,omitempty"`
  65. H uint8 `json:"H,omitempty"`
  66. I uint16 `json:"I,omitempty"`
  67. J uint32 `json:"J,omitempty"`
  68. K uint64 `json:"K,omitempty"`
  69. L bool `json:"L,omitempty"`
  70. M map[string]int `json:"M,omitempty"`
  71. N map[string]TestType2 `json:"N,omitempty"`
  72. O *TestType2 `json:"O,omitempty"`
  73. P []TestType2 `json:"Q,omitempty"`
  74. }
  75. type TestType2 struct {
  76. A string `json:"A,omitempty"`
  77. B int `json:"B,omitempty"`
  78. }
  79. type ExternalTestType2 struct {
  80. A string `json:"A,omitempty"`
  81. B int `json:"B,omitempty"`
  82. }
  83. type ExternalTestType1 struct {
  84. MyWeirdCustomEmbeddedVersionKindField `json:",inline"`
  85. A string `json:"A,omitempty"`
  86. B int `json:"B,omitempty"`
  87. C int8 `json:"C,omitempty"`
  88. D int16 `json:"D,omitempty"`
  89. E int32 `json:"E,omitempty"`
  90. F int64 `json:"F,omitempty"`
  91. G uint `json:"G,omitempty"`
  92. H uint8 `json:"H,omitempty"`
  93. I uint16 `json:"I,omitempty"`
  94. J uint32 `json:"J,omitempty"`
  95. K uint64 `json:"K,omitempty"`
  96. L bool `json:"L,omitempty"`
  97. M map[string]int `json:"M,omitempty"`
  98. N map[string]ExternalTestType2 `json:"N,omitempty"`
  99. O *ExternalTestType2 `json:"O,omitempty"`
  100. P []ExternalTestType2 `json:"Q,omitempty"`
  101. }
  102. type ExternalInternalSame struct {
  103. MyWeirdCustomEmbeddedVersionKindField `json:",inline"`
  104. A TestType2 `json:"A,omitempty"`
  105. }
  106. // TestObjectFuzzer can randomly populate all the above objects.
  107. var TestObjectFuzzer = fuzz.New().NilChance(.5).NumElements(1, 100).Funcs(
  108. func(j *MyWeirdCustomEmbeddedVersionKindField, c fuzz.Continue) {
  109. c.FuzzNoCustom(j)
  110. j.APIVersion = ""
  111. j.ObjectKind = ""
  112. },
  113. )
  114. func (obj *MyWeirdCustomEmbeddedVersionKindField) GetObjectKind() unversioned.ObjectKind { return obj }
  115. func (obj *MyWeirdCustomEmbeddedVersionKindField) SetGroupVersionKind(gvk unversioned.GroupVersionKind) {
  116. obj.APIVersion, obj.ObjectKind = gvk.ToAPIVersionAndKind()
  117. }
  118. func (obj *MyWeirdCustomEmbeddedVersionKindField) GroupVersionKind() unversioned.GroupVersionKind {
  119. return unversioned.FromAPIVersionAndKind(obj.APIVersion, obj.ObjectKind)
  120. }
  121. func (obj *ExternalInternalSame) GetObjectKind() unversioned.ObjectKind {
  122. return &obj.MyWeirdCustomEmbeddedVersionKindField
  123. }
  124. func (obj *TestType1) GetObjectKind() unversioned.ObjectKind {
  125. return &obj.MyWeirdCustomEmbeddedVersionKindField
  126. }
  127. func (obj *ExternalTestType1) GetObjectKind() unversioned.ObjectKind {
  128. return &obj.MyWeirdCustomEmbeddedVersionKindField
  129. }
  130. func (obj *TestType2) GetObjectKind() unversioned.ObjectKind { return unversioned.EmptyObjectKind }
  131. func (obj *ExternalTestType2) GetObjectKind() unversioned.ObjectKind {
  132. return unversioned.EmptyObjectKind
  133. }
  134. // Returns a new Scheme set up with the test objects.
  135. func GetTestScheme() (*runtime.Scheme, runtime.Codec) {
  136. internalGV := unversioned.GroupVersion{Version: runtime.APIVersionInternal}
  137. externalGV := unversioned.GroupVersion{Version: "v1"}
  138. externalGV2 := unversioned.GroupVersion{Version: "v2"}
  139. s := runtime.NewScheme()
  140. // Ordinarily, we wouldn't add TestType2, but because this is a test and
  141. // both types are from the same package, we need to get it into the system
  142. // so that converter will match it with ExternalType2.
  143. s.AddKnownTypes(internalGV, &TestType1{}, &TestType2{}, &ExternalInternalSame{})
  144. s.AddKnownTypes(externalGV, &ExternalInternalSame{})
  145. s.AddKnownTypeWithName(externalGV.WithKind("TestType1"), &ExternalTestType1{})
  146. s.AddKnownTypeWithName(externalGV.WithKind("TestType2"), &ExternalTestType2{})
  147. s.AddKnownTypeWithName(internalGV.WithKind("TestType3"), &TestType1{})
  148. s.AddKnownTypeWithName(externalGV.WithKind("TestType3"), &ExternalTestType1{})
  149. s.AddKnownTypeWithName(externalGV2.WithKind("TestType1"), &ExternalTestType1{})
  150. s.AddUnversionedTypes(externalGV, &unversioned.Status{})
  151. cf := newCodecFactory(s, newSerializersForScheme(s, testMetaFactory{}))
  152. codec := cf.LegacyCodec(unversioned.GroupVersion{Version: "v1"})
  153. return s, codec
  154. }
  155. func objDiff(a, b interface{}) string {
  156. ab, err := json.Marshal(a)
  157. if err != nil {
  158. panic("a")
  159. }
  160. bb, err := json.Marshal(b)
  161. if err != nil {
  162. panic("b")
  163. }
  164. return diff.StringDiff(string(ab), string(bb))
  165. // An alternate diff attempt, in case json isn't showing you
  166. // the difference. (reflect.DeepEqual makes a distinction between
  167. // nil and empty slices, for example.)
  168. //return diff.StringDiff(
  169. // fmt.Sprintf("%#v", a),
  170. // fmt.Sprintf("%#v", b),
  171. //)
  172. }
  173. var semantic = conversion.EqualitiesOrDie(
  174. func(a, b MyWeirdCustomEmbeddedVersionKindField) bool {
  175. a.APIVersion, a.ObjectKind = "", ""
  176. b.APIVersion, b.ObjectKind = "", ""
  177. return a == b
  178. },
  179. )
  180. func runTest(t *testing.T, source interface{}) {
  181. name := reflect.TypeOf(source).Elem().Name()
  182. TestObjectFuzzer.Fuzz(source)
  183. _, codec := GetTestScheme()
  184. data, err := runtime.Encode(codec, source.(runtime.Object))
  185. if err != nil {
  186. t.Errorf("%v: %v (%#v)", name, err, source)
  187. return
  188. }
  189. obj2, err := runtime.Decode(codec, data)
  190. if err != nil {
  191. t.Errorf("%v: %v (%v)", name, err, string(data))
  192. return
  193. }
  194. if !semantic.DeepEqual(source, obj2) {
  195. t.Errorf("1: %v: diff: %v", name, diff.ObjectGoPrintSideBySide(source, obj2))
  196. return
  197. }
  198. obj3 := reflect.New(reflect.TypeOf(source).Elem()).Interface()
  199. if err := runtime.DecodeInto(codec, data, obj3.(runtime.Object)); err != nil {
  200. t.Errorf("2: %v: %v", name, err)
  201. return
  202. }
  203. if !semantic.DeepEqual(source, obj3) {
  204. t.Errorf("3: %v: diff: %v", name, objDiff(source, obj3))
  205. return
  206. }
  207. }
  208. func TestTypes(t *testing.T) {
  209. table := []interface{}{
  210. &TestType1{},
  211. &ExternalInternalSame{},
  212. }
  213. for _, item := range table {
  214. // Try a few times, since runTest uses random values.
  215. for i := 0; i < *fuzzIters; i++ {
  216. runTest(t, item)
  217. }
  218. }
  219. }
  220. func TestVersionedEncoding(t *testing.T) {
  221. s, _ := GetTestScheme()
  222. cf := newCodecFactory(s, newSerializersForScheme(s, testMetaFactory{}))
  223. encoder, _ := cf.SerializerForFileExtension("json")
  224. codec := cf.CodecForVersions(encoder, nil, unversioned.GroupVersion{Version: "v2"}, nil)
  225. out, err := runtime.Encode(codec, &TestType1{})
  226. if err != nil {
  227. t.Fatal(err)
  228. }
  229. if string(out) != `{"myVersionKey":"v2","myKindKey":"TestType1"}`+"\n" {
  230. t.Fatal(string(out))
  231. }
  232. codec = cf.CodecForVersions(encoder, nil, unversioned.GroupVersion{Version: "v3"}, nil)
  233. _, err = runtime.Encode(codec, &TestType1{})
  234. if err == nil {
  235. t.Fatal(err)
  236. }
  237. // unversioned encode with no versions is written directly to wire
  238. codec = cf.CodecForVersions(encoder, nil, runtime.InternalGroupVersioner, nil)
  239. out, err = runtime.Encode(codec, &TestType1{})
  240. if err != nil {
  241. t.Fatal(err)
  242. }
  243. if string(out) != `{}`+"\n" {
  244. t.Fatal(string(out))
  245. }
  246. }
  247. func TestMultipleNames(t *testing.T) {
  248. _, codec := GetTestScheme()
  249. obj, _, err := codec.Decode([]byte(`{"myKindKey":"TestType3","myVersionKey":"v1","A":"value"}`), nil, nil)
  250. if err != nil {
  251. t.Fatalf("unexpected error: %v", err)
  252. }
  253. internal := obj.(*TestType1)
  254. if internal.A != "value" {
  255. t.Fatalf("unexpected decoded object: %#v", internal)
  256. }
  257. out, err := runtime.Encode(codec, internal)
  258. if err != nil {
  259. t.Fatalf("unexpected error: %v", err)
  260. }
  261. if !strings.Contains(string(out), `"myKindKey":"TestType1"`) {
  262. t.Errorf("unexpected encoded output: %s", string(out))
  263. }
  264. }
  265. func TestConvertTypesWhenDefaultNamesMatch(t *testing.T) {
  266. internalGV := unversioned.GroupVersion{Version: runtime.APIVersionInternal}
  267. externalGV := unversioned.GroupVersion{Version: "v1"}
  268. s := runtime.NewScheme()
  269. // create two names internally, with TestType1 being preferred
  270. s.AddKnownTypeWithName(internalGV.WithKind("TestType1"), &TestType1{})
  271. s.AddKnownTypeWithName(internalGV.WithKind("OtherType1"), &TestType1{})
  272. // create two names externally, with TestType1 being preferred
  273. s.AddKnownTypeWithName(externalGV.WithKind("TestType1"), &ExternalTestType1{})
  274. s.AddKnownTypeWithName(externalGV.WithKind("OtherType1"), &ExternalTestType1{})
  275. ext := &ExternalTestType1{}
  276. ext.APIVersion = "v1"
  277. ext.ObjectKind = "OtherType1"
  278. ext.A = "test"
  279. data, err := json.Marshal(ext)
  280. if err != nil {
  281. t.Fatalf("unexpected error: %v", err)
  282. }
  283. expect := &TestType1{A: "test"}
  284. codec := newCodecFactory(s, newSerializersForScheme(s, testMetaFactory{})).LegacyCodec(unversioned.GroupVersion{Version: "v1"})
  285. obj, err := runtime.Decode(codec, data)
  286. if err != nil {
  287. t.Fatalf("unexpected error: %v", err)
  288. }
  289. if !semantic.DeepEqual(expect, obj) {
  290. t.Errorf("unexpected object: %#v", obj)
  291. }
  292. into := &TestType1{}
  293. if err := runtime.DecodeInto(codec, data, into); err != nil {
  294. t.Fatalf("unexpected error: %v", err)
  295. }
  296. if !semantic.DeepEqual(expect, into) {
  297. t.Errorf("unexpected object: %#v", obj)
  298. }
  299. }
  300. func TestEncode_Ptr(t *testing.T) {
  301. _, codec := GetTestScheme()
  302. tt := &TestType1{A: "I am a pointer object"}
  303. data, err := runtime.Encode(codec, tt)
  304. obj2, err2 := runtime.Decode(codec, data)
  305. if err != nil || err2 != nil {
  306. t.Fatalf("Failure: '%v' '%v'\n%s", err, err2, data)
  307. }
  308. if _, ok := obj2.(*TestType1); !ok {
  309. t.Fatalf("Got wrong type")
  310. }
  311. if !semantic.DeepEqual(obj2, tt) {
  312. t.Errorf("Expected:\n %#v,\n Got:\n %#v", tt, obj2)
  313. }
  314. }
  315. func TestBadJSONRejection(t *testing.T) {
  316. log.SetOutput(os.Stderr)
  317. _, codec := GetTestScheme()
  318. badJSONs := [][]byte{
  319. []byte(`{"myVersionKey":"v1"}`), // Missing kind
  320. []byte(`{"myVersionKey":"v1","myKindKey":"bar"}`), // Unknown kind
  321. []byte(`{"myVersionKey":"bar","myKindKey":"TestType1"}`), // Unknown version
  322. []byte(`{"myKindKey":"TestType1"}`), // Missing version
  323. }
  324. for _, b := range badJSONs {
  325. if _, err := runtime.Decode(codec, b); err == nil {
  326. t.Errorf("Did not reject bad json: %s", string(b))
  327. }
  328. }
  329. badJSONKindMismatch := []byte(`{"myVersionKey":"v1","myKindKey":"ExternalInternalSame"}`)
  330. if err := runtime.DecodeInto(codec, badJSONKindMismatch, &TestType1{}); err == nil {
  331. t.Errorf("Kind is set but doesn't match the object type: %s", badJSONKindMismatch)
  332. }
  333. if err := runtime.DecodeInto(codec, []byte(``), &TestType1{}); err != nil {
  334. t.Errorf("Should allow empty decode: %v", err)
  335. }
  336. if _, _, err := codec.Decode([]byte(``), &unversioned.GroupVersionKind{Kind: "ExternalInternalSame"}, nil); err == nil {
  337. t.Errorf("Did not give error for empty data with only kind default")
  338. }
  339. if _, _, err := codec.Decode([]byte(`{"myVersionKey":"v1"}`), &unversioned.GroupVersionKind{Kind: "ExternalInternalSame"}, nil); err != nil {
  340. t.Errorf("Gave error for version and kind default")
  341. }
  342. if _, _, err := codec.Decode([]byte(`{"myKindKey":"ExternalInternalSame"}`), &unversioned.GroupVersionKind{Version: "v1"}, nil); err != nil {
  343. t.Errorf("Gave error for version and kind default")
  344. }
  345. if _, _, err := codec.Decode([]byte(``), &unversioned.GroupVersionKind{Kind: "ExternalInternalSame", Version: "v1"}, nil); err != nil {
  346. t.Errorf("Gave error for version and kind defaulted: %v", err)
  347. }
  348. if _, err := runtime.Decode(codec, []byte(``)); err == nil {
  349. t.Errorf("Did not give error for empty data")
  350. }
  351. }
  352. // Returns a new Scheme set up with the test objects needed by TestDirectCodec.
  353. func GetDirectCodecTestScheme() *runtime.Scheme {
  354. internalGV := unversioned.GroupVersion{Version: runtime.APIVersionInternal}
  355. externalGV := unversioned.GroupVersion{Version: "v1"}
  356. s := runtime.NewScheme()
  357. // Ordinarily, we wouldn't add TestType2, but because this is a test and
  358. // both types are from the same package, we need to get it into the system
  359. // so that converter will match it with ExternalType2.
  360. s.AddKnownTypes(internalGV, &TestType1{})
  361. s.AddKnownTypes(externalGV, &ExternalTestType1{})
  362. s.AddUnversionedTypes(externalGV, &unversioned.Status{})
  363. return s
  364. }
  365. func TestDirectCodec(t *testing.T) {
  366. s := GetDirectCodecTestScheme()
  367. cf := newCodecFactory(s, newSerializersForScheme(s, testMetaFactory{}))
  368. serializer, _ := cf.SerializerForFileExtension("json")
  369. df := DirectCodecFactory{cf}
  370. ignoredGV, err := unversioned.ParseGroupVersion("ignored group/ignored version")
  371. if err != nil {
  372. t.Fatal(err)
  373. }
  374. directEncoder := df.EncoderForVersion(serializer, ignoredGV)
  375. directDecoder := df.DecoderToVersion(serializer, ignoredGV)
  376. out, err := runtime.Encode(directEncoder, &ExternalTestType1{})
  377. if err != nil {
  378. t.Fatal(err)
  379. }
  380. if string(out) != `{"myVersionKey":"v1","myKindKey":"ExternalTestType1"}`+"\n" {
  381. t.Fatal(string(out))
  382. }
  383. a, _, err := directDecoder.Decode(out, nil, nil)
  384. e := &ExternalTestType1{
  385. MyWeirdCustomEmbeddedVersionKindField: MyWeirdCustomEmbeddedVersionKindField{
  386. APIVersion: "v1",
  387. ObjectKind: "ExternalTestType1",
  388. },
  389. }
  390. if !semantic.DeepEqual(e, a) {
  391. t.Fatalf("expect %v, got %v", e, a)
  392. }
  393. }