help_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 meta_test
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/api/meta"
  19. "k8s.io/kubernetes/pkg/api/unversioned"
  20. "k8s.io/kubernetes/pkg/api/v1"
  21. "k8s.io/kubernetes/pkg/runtime"
  22. "k8s.io/kubernetes/pkg/util/diff"
  23. "github.com/google/gofuzz"
  24. )
  25. func TestIsList(t *testing.T) {
  26. tests := []struct {
  27. obj runtime.Object
  28. isList bool
  29. }{
  30. {&api.PodList{}, true},
  31. {&api.Pod{}, false},
  32. }
  33. for _, item := range tests {
  34. if e, a := item.isList, meta.IsListType(item.obj); e != a {
  35. t.Errorf("%v: Expected %v, got %v", reflect.TypeOf(item.obj), e, a)
  36. }
  37. }
  38. }
  39. func TestExtractList(t *testing.T) {
  40. pl := &api.PodList{
  41. Items: []api.Pod{
  42. {ObjectMeta: api.ObjectMeta{Name: "1"}},
  43. {ObjectMeta: api.ObjectMeta{Name: "2"}},
  44. {ObjectMeta: api.ObjectMeta{Name: "3"}},
  45. },
  46. }
  47. list, err := meta.ExtractList(pl)
  48. if err != nil {
  49. t.Fatalf("Unexpected error %v", err)
  50. }
  51. if e, a := len(list), len(pl.Items); e != a {
  52. t.Fatalf("Expected %v, got %v", e, a)
  53. }
  54. for i := range list {
  55. if e, a := list[i].(*api.Pod).Name, pl.Items[i].Name; e != a {
  56. t.Fatalf("Expected %v, got %v", e, a)
  57. }
  58. }
  59. }
  60. func TestExtractListV1(t *testing.T) {
  61. pl := &v1.PodList{
  62. Items: []v1.Pod{
  63. {ObjectMeta: v1.ObjectMeta{Name: "1"}},
  64. {ObjectMeta: v1.ObjectMeta{Name: "2"}},
  65. {ObjectMeta: v1.ObjectMeta{Name: "3"}},
  66. },
  67. }
  68. list, err := meta.ExtractList(pl)
  69. if err != nil {
  70. t.Fatalf("Unexpected error %v", err)
  71. }
  72. if e, a := len(list), len(pl.Items); e != a {
  73. t.Fatalf("Expected %v, got %v", e, a)
  74. }
  75. for i := range list {
  76. if e, a := list[i].(*v1.Pod).Name, pl.Items[i].Name; e != a {
  77. t.Fatalf("Expected %v, got %v", e, a)
  78. }
  79. }
  80. }
  81. func TestExtractListGeneric(t *testing.T) {
  82. pl := &api.List{
  83. Items: []runtime.Object{
  84. &api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}},
  85. &api.Service{ObjectMeta: api.ObjectMeta{Name: "2"}},
  86. },
  87. }
  88. list, err := meta.ExtractList(pl)
  89. if err != nil {
  90. t.Fatalf("Unexpected error %v", err)
  91. }
  92. if e, a := len(list), len(pl.Items); e != a {
  93. t.Fatalf("Expected %v, got %v", e, a)
  94. }
  95. if obj, ok := list[0].(*api.Pod); !ok {
  96. t.Fatalf("Expected list[0] to be *api.Pod, it is %#v", obj)
  97. }
  98. if obj, ok := list[1].(*api.Service); !ok {
  99. t.Fatalf("Expected list[1] to be *api.Service, it is %#v", obj)
  100. }
  101. }
  102. func TestExtractListGenericV1(t *testing.T) {
  103. pl := &v1.List{
  104. Items: []runtime.RawExtension{
  105. {Raw: []byte("foo")},
  106. {Raw: []byte("bar")},
  107. {Object: &v1.Pod{ObjectMeta: v1.ObjectMeta{Name: "other"}}},
  108. },
  109. }
  110. list, err := meta.ExtractList(pl)
  111. if err != nil {
  112. t.Fatalf("Unexpected error %v", err)
  113. }
  114. if e, a := len(list), len(pl.Items); e != a {
  115. t.Fatalf("Expected %v, got %v", e, a)
  116. }
  117. if obj, ok := list[0].(*runtime.Unknown); !ok {
  118. t.Fatalf("Expected list[0] to be *runtime.Unknown, it is %#v", obj)
  119. }
  120. if obj, ok := list[1].(*runtime.Unknown); !ok {
  121. t.Fatalf("Expected list[1] to be *runtime.Unknown, it is %#v", obj)
  122. }
  123. if obj, ok := list[2].(*v1.Pod); !ok {
  124. t.Fatalf("Expected list[2] to be *runtime.Unknown, it is %#v", obj)
  125. }
  126. }
  127. type fakePtrInterfaceList struct {
  128. Items *[]runtime.Object
  129. }
  130. func (obj fakePtrInterfaceList) GetObjectKind() unversioned.ObjectKind {
  131. return unversioned.EmptyObjectKind
  132. }
  133. func TestExtractListOfInterfacePtrs(t *testing.T) {
  134. pl := &fakePtrInterfaceList{
  135. Items: &[]runtime.Object{},
  136. }
  137. list, err := meta.ExtractList(pl)
  138. if err != nil {
  139. t.Fatalf("Unexpected error %v", err)
  140. }
  141. if len(list) > 0 {
  142. t.Fatalf("Expected empty list, got %#v", list)
  143. }
  144. }
  145. type fakePtrValueList struct {
  146. Items []*api.Pod
  147. }
  148. func (obj fakePtrValueList) GetObjectKind() unversioned.ObjectKind {
  149. return unversioned.EmptyObjectKind
  150. }
  151. func TestExtractListOfValuePtrs(t *testing.T) {
  152. pl := &fakePtrValueList{
  153. Items: []*api.Pod{
  154. {ObjectMeta: api.ObjectMeta{Name: "1"}},
  155. {ObjectMeta: api.ObjectMeta{Name: "2"}},
  156. },
  157. }
  158. list, err := meta.ExtractList(pl)
  159. if err != nil {
  160. t.Fatalf("Unexpected error %v", err)
  161. }
  162. if e, a := len(list), len(pl.Items); e != a {
  163. t.Fatalf("Expected %v, got %v", e, a)
  164. }
  165. for i := range list {
  166. if obj, ok := list[i].(*api.Pod); !ok {
  167. t.Fatalf("Expected list[%d] to be *api.Pod, it is %#v", i, obj)
  168. }
  169. }
  170. }
  171. func TestSetList(t *testing.T) {
  172. pl := &api.PodList{}
  173. list := []runtime.Object{
  174. &api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}},
  175. &api.Pod{ObjectMeta: api.ObjectMeta{Name: "2"}},
  176. &api.Pod{ObjectMeta: api.ObjectMeta{Name: "3"}},
  177. }
  178. err := meta.SetList(pl, list)
  179. if err != nil {
  180. t.Fatalf("Unexpected error %v", err)
  181. }
  182. if e, a := len(list), len(pl.Items); e != a {
  183. t.Fatalf("Expected %v, got %v", e, a)
  184. }
  185. for i := range list {
  186. if e, a := list[i].(*api.Pod).Name, pl.Items[i].Name; e != a {
  187. t.Fatalf("Expected %v, got %v", e, a)
  188. }
  189. }
  190. }
  191. func TestSetListToRuntimeObjectArray(t *testing.T) {
  192. pl := &api.List{}
  193. list := []runtime.Object{
  194. &api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}},
  195. &api.Pod{ObjectMeta: api.ObjectMeta{Name: "2"}},
  196. &api.Pod{ObjectMeta: api.ObjectMeta{Name: "3"}},
  197. }
  198. err := meta.SetList(pl, list)
  199. if err != nil {
  200. t.Fatalf("Unexpected error %v", err)
  201. }
  202. if e, a := len(list), len(pl.Items); e != a {
  203. t.Fatalf("Expected %v, got %v", e, a)
  204. }
  205. for i := range list {
  206. if e, a := list[i], pl.Items[i]; e != a {
  207. t.Fatalf("%d: unmatched: %s", i, diff.ObjectDiff(e, a))
  208. }
  209. }
  210. }
  211. func TestSetExtractListRoundTrip(t *testing.T) {
  212. fuzzer := fuzz.New().NilChance(0).NumElements(1, 5)
  213. for i := 0; i < 5; i++ {
  214. start := &api.PodList{}
  215. fuzzer.Fuzz(&start.Items)
  216. list, err := meta.ExtractList(start)
  217. if err != nil {
  218. t.Errorf("Unexpected error %v", err)
  219. continue
  220. }
  221. got := &api.PodList{}
  222. err = meta.SetList(got, list)
  223. if err != nil {
  224. t.Errorf("Unexpected error %v", err)
  225. continue
  226. }
  227. if e, a := start, got; !reflect.DeepEqual(e, a) {
  228. t.Fatalf("Expected %#v, got %#v", e, a)
  229. }
  230. }
  231. }