group_version_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Copyright 2015 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 unversioned
  14. import (
  15. "encoding/json"
  16. "reflect"
  17. "testing"
  18. "github.com/ugorji/go/codec"
  19. )
  20. func TestGroupVersionParse(t *testing.T) {
  21. tests := []struct {
  22. input string
  23. out GroupVersion
  24. err func(error) bool
  25. }{
  26. {input: "v1", out: GroupVersion{Version: "v1"}},
  27. {input: "v2", out: GroupVersion{Version: "v2"}},
  28. {input: "/v1", out: GroupVersion{Version: "v1"}},
  29. {input: "v1/", out: GroupVersion{Group: "v1"}},
  30. {input: "/v1/", err: func(err error) bool { return err.Error() == "unexpected GroupVersion string: /v1/" }},
  31. {input: "v1/a", out: GroupVersion{Group: "v1", Version: "a"}},
  32. }
  33. for i, test := range tests {
  34. out, err := ParseGroupVersion(test.input)
  35. if test.err == nil && err != nil || err == nil && test.err != nil {
  36. t.Errorf("%d: unexpected error: %v", i, err)
  37. continue
  38. }
  39. if test.err != nil && !test.err(err) {
  40. t.Errorf("%d: unexpected error: %v", i, err)
  41. continue
  42. }
  43. if out != test.out {
  44. t.Errorf("%d: unexpected output: %#v", i, out)
  45. }
  46. }
  47. }
  48. func TestGroupResourceParse(t *testing.T) {
  49. tests := []struct {
  50. input string
  51. out GroupResource
  52. }{
  53. {input: "v1", out: GroupResource{Resource: "v1"}},
  54. {input: ".v1", out: GroupResource{Group: "v1"}},
  55. {input: "v1.", out: GroupResource{Resource: "v1"}},
  56. {input: "v1.a", out: GroupResource{Group: "a", Resource: "v1"}},
  57. {input: "b.v1.a", out: GroupResource{Group: "v1.a", Resource: "b"}},
  58. }
  59. for i, test := range tests {
  60. out := ParseGroupResource(test.input)
  61. if out != test.out {
  62. t.Errorf("%d: unexpected output: %#v", i, out)
  63. }
  64. }
  65. }
  66. func TestParseResourceArg(t *testing.T) {
  67. tests := []struct {
  68. input string
  69. gvr *GroupVersionResource
  70. gr GroupResource
  71. }{
  72. {input: "v1", gr: GroupResource{Resource: "v1"}},
  73. {input: ".v1", gr: GroupResource{Group: "v1"}},
  74. {input: "v1.", gr: GroupResource{Resource: "v1"}},
  75. {input: "v1.a", gr: GroupResource{Group: "a", Resource: "v1"}},
  76. {input: "b.v1.a", gvr: &GroupVersionResource{Group: "a", Version: "v1", Resource: "b"}, gr: GroupResource{Group: "v1.a", Resource: "b"}},
  77. }
  78. for i, test := range tests {
  79. gvr, gr := ParseResourceArg(test.input)
  80. if (gvr != nil && test.gvr == nil) || (gvr == nil && test.gvr != nil) || (test.gvr != nil && *gvr != *test.gvr) {
  81. t.Errorf("%d: unexpected output: %#v", i, gvr)
  82. }
  83. if gr != test.gr {
  84. t.Errorf("%d: unexpected output: %#v", i, gr)
  85. }
  86. }
  87. }
  88. type GroupVersionHolder struct {
  89. GV GroupVersion `json:"val"`
  90. }
  91. func TestGroupVersionUnmarshalJSON(t *testing.T) {
  92. cases := []struct {
  93. input []byte
  94. expect GroupVersion
  95. }{
  96. {[]byte(`{"val": "v1"}`), GroupVersion{"", "v1"}},
  97. {[]byte(`{"val": "extensions/v1beta1"}`), GroupVersion{"extensions", "v1beta1"}},
  98. }
  99. for _, c := range cases {
  100. var result GroupVersionHolder
  101. // test golang lib's JSON codec
  102. if err := json.Unmarshal([]byte(c.input), &result); err != nil {
  103. t.Errorf("JSON codec failed to unmarshal input '%v': %v", c.input, err)
  104. }
  105. if !reflect.DeepEqual(result.GV, c.expect) {
  106. t.Errorf("JSON codec failed to unmarshal input '%s': expected %+v, got %+v", c.input, c.expect, result.GV)
  107. }
  108. // test the Ugorji codec
  109. if err := codec.NewDecoderBytes(c.input, new(codec.JsonHandle)).Decode(&result); err != nil {
  110. t.Errorf("Ugorji codec failed to unmarshal input '%v': %v", c.input, err)
  111. }
  112. if !reflect.DeepEqual(result.GV, c.expect) {
  113. t.Errorf("Ugorji codec failed to unmarshal input '%s': expected %+v, got %+v", c.input, c.expect, result.GV)
  114. }
  115. }
  116. }
  117. func TestGroupVersionMarshalJSON(t *testing.T) {
  118. cases := []struct {
  119. input GroupVersion
  120. expect []byte
  121. }{
  122. {GroupVersion{"", "v1"}, []byte(`{"val":"v1"}`)},
  123. {GroupVersion{"extensions", "v1beta1"}, []byte(`{"val":"extensions/v1beta1"}`)},
  124. }
  125. for _, c := range cases {
  126. input := GroupVersionHolder{c.input}
  127. result, err := json.Marshal(&input)
  128. if err != nil {
  129. t.Errorf("Failed to marshal input '%v': %v", input, err)
  130. }
  131. if !reflect.DeepEqual(result, c.expect) {
  132. t.Errorf("Failed to marshal input '%+v': expected: %s, got: %s", input, c.expect, result)
  133. }
  134. }
  135. }