common_test.go 7.1 KB

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