common_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 config
  14. import (
  15. "reflect"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/apimachinery/pkg/types"
  22. clientscheme "k8s.io/client-go/kubernetes/scheme"
  23. "k8s.io/kubernetes/pkg/api/legacyscheme"
  24. "k8s.io/kubernetes/pkg/apis/core"
  25. "k8s.io/kubernetes/pkg/apis/core/validation"
  26. "k8s.io/kubernetes/pkg/securitycontext"
  27. )
  28. func noDefault(*core.Pod) error { return nil }
  29. func TestDecodeSinglePod(t *testing.T) {
  30. grace := int64(30)
  31. enableServiceLinks := v1.DefaultEnableServiceLinks
  32. pod := &v1.Pod{
  33. TypeMeta: metav1.TypeMeta{
  34. APIVersion: "",
  35. },
  36. ObjectMeta: metav1.ObjectMeta{
  37. Name: "test",
  38. UID: "12345",
  39. Namespace: "mynamespace",
  40. },
  41. Spec: v1.PodSpec{
  42. RestartPolicy: v1.RestartPolicyAlways,
  43. DNSPolicy: v1.DNSClusterFirst,
  44. TerminationGracePeriodSeconds: &grace,
  45. Containers: []v1.Container{{
  46. Name: "image",
  47. Image: "test/image",
  48. ImagePullPolicy: "IfNotPresent",
  49. TerminationMessagePath: "/dev/termination-log",
  50. TerminationMessagePolicy: v1.TerminationMessageReadFile,
  51. SecurityContext: securitycontext.ValidSecurityContextWithContainerDefaults(),
  52. }},
  53. SecurityContext: &v1.PodSecurityContext{},
  54. SchedulerName: core.DefaultSchedulerName,
  55. EnableServiceLinks: &enableServiceLinks,
  56. },
  57. Status: v1.PodStatus{
  58. PodIP: "1.2.3.4",
  59. PodIPs: []v1.PodIP{
  60. {
  61. IP: "1.2.3.4",
  62. },
  63. },
  64. },
  65. }
  66. json, err := runtime.Encode(clientscheme.Codecs.LegacyCodec(v1.SchemeGroupVersion), pod)
  67. if err != nil {
  68. t.Errorf("unexpected error: %v", err)
  69. }
  70. parsed, podOut, err := tryDecodeSinglePod(json, noDefault)
  71. if !parsed {
  72. t.Errorf("expected to have parsed file: (%s)", string(json))
  73. }
  74. if err != nil {
  75. t.Errorf("unexpected error: %v (%s)", err, string(json))
  76. }
  77. if !reflect.DeepEqual(pod, podOut) {
  78. t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, podOut, string(json))
  79. }
  80. for _, gv := range legacyscheme.Scheme.PrioritizedVersionsForGroup(v1.GroupName) {
  81. info, _ := runtime.SerializerInfoForMediaType(legacyscheme.Codecs.SupportedMediaTypes(), "application/yaml")
  82. encoder := legacyscheme.Codecs.EncoderForVersion(info.Serializer, gv)
  83. yaml, err := runtime.Encode(encoder, pod)
  84. if err != nil {
  85. t.Errorf("unexpected error: %v", err)
  86. }
  87. parsed, podOut, err = tryDecodeSinglePod(yaml, noDefault)
  88. if !parsed {
  89. t.Errorf("expected to have parsed file: (%s)", string(yaml))
  90. }
  91. if err != nil {
  92. t.Errorf("unexpected error: %v (%s)", err, string(yaml))
  93. }
  94. if !reflect.DeepEqual(pod, podOut) {
  95. t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, podOut, string(yaml))
  96. }
  97. }
  98. }
  99. func TestDecodePodList(t *testing.T) {
  100. grace := int64(30)
  101. enableServiceLinks := v1.DefaultEnableServiceLinks
  102. pod := &v1.Pod{
  103. TypeMeta: metav1.TypeMeta{
  104. APIVersion: "",
  105. },
  106. ObjectMeta: metav1.ObjectMeta{
  107. Name: "test",
  108. UID: "12345",
  109. Namespace: "mynamespace",
  110. },
  111. Spec: v1.PodSpec{
  112. RestartPolicy: v1.RestartPolicyAlways,
  113. DNSPolicy: v1.DNSClusterFirst,
  114. TerminationGracePeriodSeconds: &grace,
  115. Containers: []v1.Container{{
  116. Name: "image",
  117. Image: "test/image",
  118. ImagePullPolicy: "IfNotPresent",
  119. TerminationMessagePath: "/dev/termination-log",
  120. TerminationMessagePolicy: v1.TerminationMessageReadFile,
  121. SecurityContext: securitycontext.ValidSecurityContextWithContainerDefaults(),
  122. }},
  123. SecurityContext: &v1.PodSecurityContext{},
  124. SchedulerName: core.DefaultSchedulerName,
  125. EnableServiceLinks: &enableServiceLinks,
  126. },
  127. Status: v1.PodStatus{
  128. PodIP: "1.2.3.4",
  129. PodIPs: []v1.PodIP{
  130. {
  131. IP: "1.2.3.4",
  132. },
  133. },
  134. },
  135. }
  136. podList := &v1.PodList{
  137. Items: []v1.Pod{*pod},
  138. }
  139. json, err := runtime.Encode(clientscheme.Codecs.LegacyCodec(v1.SchemeGroupVersion), podList)
  140. if err != nil {
  141. t.Errorf("unexpected error: %v", err)
  142. }
  143. parsed, podListOut, err := tryDecodePodList(json, noDefault)
  144. if !parsed {
  145. t.Errorf("expected to have parsed file: (%s)", string(json))
  146. }
  147. if err != nil {
  148. t.Errorf("unexpected error: %v (%s)", err, string(json))
  149. }
  150. if !reflect.DeepEqual(podList, &podListOut) {
  151. t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", podList, &podListOut, string(json))
  152. }
  153. for _, gv := range legacyscheme.Scheme.PrioritizedVersionsForGroup(v1.GroupName) {
  154. info, _ := runtime.SerializerInfoForMediaType(legacyscheme.Codecs.SupportedMediaTypes(), "application/yaml")
  155. encoder := legacyscheme.Codecs.EncoderForVersion(info.Serializer, gv)
  156. yaml, err := runtime.Encode(encoder, podList)
  157. if err != nil {
  158. t.Errorf("unexpected error: %v", err)
  159. }
  160. parsed, podListOut, err = tryDecodePodList(yaml, noDefault)
  161. if !parsed {
  162. t.Errorf("expected to have parsed file: (%s): %v", string(yaml), err)
  163. continue
  164. }
  165. if err != nil {
  166. t.Errorf("unexpected error: %v (%s)", err, string(yaml))
  167. continue
  168. }
  169. if !reflect.DeepEqual(podList, &podListOut) {
  170. t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, &podListOut, string(yaml))
  171. }
  172. }
  173. }
  174. func TestGetSelfLink(t *testing.T) {
  175. var testCases = []struct {
  176. desc string
  177. name string
  178. namespace string
  179. expectedSelfLink string
  180. }{
  181. {
  182. desc: "No namespace specified",
  183. name: "foo",
  184. namespace: "",
  185. expectedSelfLink: "/api/v1/namespaces/default/pods/foo",
  186. },
  187. {
  188. desc: "Namespace specified",
  189. name: "foo",
  190. namespace: "bar",
  191. expectedSelfLink: "/api/v1/namespaces/bar/pods/foo",
  192. },
  193. }
  194. for _, testCase := range testCases {
  195. selfLink := getSelfLink(testCase.name, testCase.namespace)
  196. if testCase.expectedSelfLink != selfLink {
  197. t.Errorf("%s: getSelfLink error, expected: %s, got: %s", testCase.desc, testCase.expectedSelfLink, selfLink)
  198. }
  199. }
  200. }
  201. func TestStaticPodNameGenerate(t *testing.T) {
  202. testCases := []struct {
  203. nodeName types.NodeName
  204. podName string
  205. expected string
  206. overwrite string
  207. shouldErr bool
  208. }{
  209. {
  210. "node1",
  211. "static-pod1",
  212. "static-pod1-node1",
  213. "",
  214. false,
  215. },
  216. {
  217. "Node1",
  218. "static-pod1",
  219. "static-pod1-node1",
  220. "",
  221. false,
  222. },
  223. {
  224. "NODE1",
  225. "static-pod1",
  226. "static-pod1-node1",
  227. "static-pod1-NODE1",
  228. true,
  229. },
  230. }
  231. for _, c := range testCases {
  232. assert.Equal(t, c.expected, generatePodName(c.podName, c.nodeName), "wrong pod name generated")
  233. pod := &core.Pod{}
  234. pod.Name = c.podName
  235. if c.overwrite != "" {
  236. pod.Name = c.overwrite
  237. }
  238. errs := validation.ValidatePod(pod)
  239. if c.shouldErr {
  240. specNameErrored := false
  241. for _, err := range errs {
  242. if err.Field == "metadata.name" {
  243. specNameErrored = true
  244. }
  245. }
  246. assert.NotEmpty(t, specNameErrored, "expecting error")
  247. } else {
  248. for _, err := range errs {
  249. if err.Field == "metadata.name" {
  250. t.Fail()
  251. }
  252. }
  253. }
  254. }
  255. }