ref_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 container
  14. import (
  15. "testing"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/testapi"
  18. "k8s.io/kubernetes/pkg/api/unversioned"
  19. )
  20. func TestFieldPath(t *testing.T) {
  21. pod := &api.Pod{Spec: api.PodSpec{Containers: []api.Container{
  22. {Name: "foo"},
  23. {Name: "bar"},
  24. {Name: ""},
  25. {Name: "baz"},
  26. }}}
  27. table := map[string]struct {
  28. pod *api.Pod
  29. container *api.Container
  30. path string
  31. success bool
  32. }{
  33. "basic": {pod, &api.Container{Name: "foo"}, "spec.containers{foo}", true},
  34. "basic2": {pod, &api.Container{Name: "baz"}, "spec.containers{baz}", true},
  35. "emptyName": {pod, &api.Container{Name: ""}, "spec.containers[2]", true},
  36. "basicSamePointer": {pod, &pod.Spec.Containers[0], "spec.containers{foo}", true},
  37. "missing": {pod, &api.Container{Name: "qux"}, "", false},
  38. }
  39. for name, item := range table {
  40. res, err := fieldPath(item.pod, item.container)
  41. if item.success == false {
  42. if err == nil {
  43. t.Errorf("%v: unexpected non-error", name)
  44. }
  45. continue
  46. }
  47. if err != nil {
  48. t.Errorf("%v: unexpected error: %v", name, err)
  49. continue
  50. }
  51. if e, a := item.path, res; e != a {
  52. t.Errorf("%v: wanted %v, got %v", name, e, a)
  53. }
  54. }
  55. }
  56. func TestGenerateContainerRef(t *testing.T) {
  57. var (
  58. okPod = api.Pod{
  59. TypeMeta: unversioned.TypeMeta{
  60. Kind: "Pod",
  61. APIVersion: testapi.Default.GroupVersion().String(),
  62. },
  63. ObjectMeta: api.ObjectMeta{
  64. Name: "ok",
  65. Namespace: "test-ns",
  66. UID: "bar",
  67. ResourceVersion: "42",
  68. SelfLink: "/api/" + testapi.Default.GroupVersion().String() + "/pods/foo",
  69. },
  70. Spec: api.PodSpec{
  71. Containers: []api.Container{
  72. {
  73. Name: "by-name",
  74. },
  75. {},
  76. },
  77. },
  78. }
  79. noSelfLinkPod = okPod
  80. defaultedSelfLinkPod = okPod
  81. )
  82. noSelfLinkPod.Kind = ""
  83. noSelfLinkPod.APIVersion = ""
  84. noSelfLinkPod.ObjectMeta.SelfLink = ""
  85. defaultedSelfLinkPod.ObjectMeta.SelfLink = "/api/" + testapi.Default.GroupVersion().String() + "/pods/ok"
  86. cases := []struct {
  87. name string
  88. pod *api.Pod
  89. container *api.Container
  90. expected *api.ObjectReference
  91. success bool
  92. }{
  93. {
  94. name: "by-name",
  95. pod: &okPod,
  96. container: &api.Container{
  97. Name: "by-name",
  98. },
  99. expected: &api.ObjectReference{
  100. Kind: "Pod",
  101. APIVersion: testapi.Default.GroupVersion().String(),
  102. Name: "ok",
  103. Namespace: "test-ns",
  104. UID: "bar",
  105. ResourceVersion: "42",
  106. FieldPath: ".spec.containers{by-name}",
  107. },
  108. success: true,
  109. },
  110. {
  111. name: "no-name",
  112. pod: &okPod,
  113. container: &api.Container{},
  114. expected: &api.ObjectReference{
  115. Kind: "Pod",
  116. APIVersion: testapi.Default.GroupVersion().String(),
  117. Name: "ok",
  118. Namespace: "test-ns",
  119. UID: "bar",
  120. ResourceVersion: "42",
  121. FieldPath: ".spec.containers[1]",
  122. },
  123. success: true,
  124. },
  125. {
  126. name: "no-selflink",
  127. pod: &noSelfLinkPod,
  128. container: &api.Container{},
  129. expected: nil,
  130. success: false,
  131. },
  132. {
  133. name: "defaulted-selflink",
  134. pod: &defaultedSelfLinkPod,
  135. container: &api.Container{
  136. Name: "by-name",
  137. },
  138. expected: &api.ObjectReference{
  139. Kind: "Pod",
  140. APIVersion: testapi.Default.GroupVersion().String(),
  141. Name: "ok",
  142. Namespace: "test-ns",
  143. UID: "bar",
  144. ResourceVersion: "42",
  145. FieldPath: ".spec.containers{by-name}",
  146. },
  147. success: true,
  148. },
  149. {
  150. name: "implicitly-required",
  151. pod: &okPod,
  152. container: &api.Container{
  153. Name: "net",
  154. },
  155. expected: &api.ObjectReference{
  156. Kind: "Pod",
  157. APIVersion: testapi.Default.GroupVersion().String(),
  158. Name: "ok",
  159. Namespace: "test-ns",
  160. UID: "bar",
  161. ResourceVersion: "42",
  162. FieldPath: "implicitly required container net",
  163. },
  164. success: true,
  165. },
  166. }
  167. for _, tc := range cases {
  168. actual, err := GenerateContainerRef(tc.pod, tc.container)
  169. if err != nil {
  170. if tc.success {
  171. t.Errorf("%v: unexpected error: %v", tc.name, err)
  172. }
  173. continue
  174. }
  175. if !tc.success {
  176. t.Errorf("%v: unexpected success", tc.name)
  177. continue
  178. }
  179. if e, a := tc.expected.Kind, actual.Kind; e != a {
  180. t.Errorf("%v: kind: expected %v, got %v", tc.name, e, a)
  181. }
  182. if e, a := tc.expected.APIVersion, actual.APIVersion; e != a {
  183. t.Errorf("%v: apiVersion: expected %v, got %v", tc.name, e, a)
  184. }
  185. if e, a := tc.expected.Name, actual.Name; e != a {
  186. t.Errorf("%v: name: expected %v, got %v", tc.name, e, a)
  187. }
  188. if e, a := tc.expected.Namespace, actual.Namespace; e != a {
  189. t.Errorf("%v: namespace: expected %v, got %v", tc.name, e, a)
  190. }
  191. if e, a := tc.expected.UID, actual.UID; e != a {
  192. t.Errorf("%v: uid: expected %v, got %v", tc.name, e, a)
  193. }
  194. if e, a := tc.expected.ResourceVersion, actual.ResourceVersion; e != a {
  195. t.Errorf("%v: kind: expected %v, got %v", tc.name, e, a)
  196. }
  197. }
  198. }