backward_compatibility_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 v1_test
  14. import (
  15. "testing"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/testing/compat"
  18. "k8s.io/kubernetes/pkg/api/v1"
  19. "k8s.io/kubernetes/pkg/api/validation"
  20. "k8s.io/kubernetes/pkg/runtime"
  21. "k8s.io/kubernetes/pkg/util/validation/field"
  22. )
  23. func TestCompatibility_v1_PodSecurityContext(t *testing.T) {
  24. cases := []struct {
  25. name string
  26. input string
  27. expectedKeys map[string]string
  28. absentKeys []string
  29. }{
  30. {
  31. name: "hostNetwork = true",
  32. input: `
  33. {
  34. "kind":"Pod",
  35. "apiVersion":"v1",
  36. "metadata":{"name":"my-pod-name", "namespace":"my-pod-namespace"},
  37. "spec": {
  38. "hostNetwork": true,
  39. "containers":[{
  40. "name":"a",
  41. "image":"my-container-image"
  42. }]
  43. }
  44. }
  45. `,
  46. expectedKeys: map[string]string{
  47. "spec.hostNetwork": "true",
  48. },
  49. },
  50. {
  51. name: "hostNetwork = false",
  52. input: `
  53. {
  54. "kind":"Pod",
  55. "apiVersion":"v1",
  56. "metadata":{"name":"my-pod-name", "namespace":"my-pod-namespace"},
  57. "spec": {
  58. "hostNetwork": false,
  59. "containers":[{
  60. "name":"a",
  61. "image":"my-container-image"
  62. }]
  63. }
  64. }
  65. `,
  66. absentKeys: []string{
  67. "spec.hostNetwork",
  68. },
  69. },
  70. {
  71. name: "hostIPC = true",
  72. input: `
  73. {
  74. "kind":"Pod",
  75. "apiVersion":"v1",
  76. "metadata":{"name":"my-pod-name", "namespace":"my-pod-namespace"},
  77. "spec": {
  78. "hostIPC": true,
  79. "containers":[{
  80. "name":"a",
  81. "image":"my-container-image"
  82. }]
  83. }
  84. }
  85. `,
  86. expectedKeys: map[string]string{
  87. "spec.hostIPC": "true",
  88. },
  89. },
  90. {
  91. name: "hostIPC = false",
  92. input: `
  93. {
  94. "kind":"Pod",
  95. "apiVersion":"v1",
  96. "metadata":{"name":"my-pod-name", "namespace":"my-pod-namespace"},
  97. "spec": {
  98. "hostIPC": false,
  99. "containers":[{
  100. "name":"a",
  101. "image":"my-container-image"
  102. }]
  103. }
  104. }
  105. `,
  106. absentKeys: []string{
  107. "spec.hostIPC",
  108. },
  109. },
  110. {
  111. name: "hostPID = true",
  112. input: `
  113. {
  114. "kind":"Pod",
  115. "apiVersion":"v1",
  116. "metadata":{"name":"my-pod-name", "namespace":"my-pod-namespace"},
  117. "spec": {
  118. "hostPID": true,
  119. "containers":[{
  120. "name":"a",
  121. "image":"my-container-image"
  122. }]
  123. }
  124. }
  125. `,
  126. expectedKeys: map[string]string{
  127. "spec.hostPID": "true",
  128. },
  129. },
  130. {
  131. name: "hostPID = false",
  132. input: `
  133. {
  134. "kind":"Pod",
  135. "apiVersion":"v1",
  136. "metadata":{"name":"my-pod-name", "namespace":"my-pod-namespace"},
  137. "spec": {
  138. "hostPID": false,
  139. "containers":[{
  140. "name":"a",
  141. "image":"my-container-image"
  142. }]
  143. }
  144. }
  145. `,
  146. absentKeys: []string{
  147. "spec.hostPID",
  148. },
  149. },
  150. {
  151. name: "reseting defaults for pre-v1.1 mirror pods",
  152. input: `
  153. {
  154. "kind":"Pod",
  155. "apiVersion":"v1",
  156. "metadata":{
  157. "name":"my-pod-name",
  158. "namespace":"my-pod-namespace",
  159. "annotations": {
  160. "kubernetes.io/config.mirror": "mirror"
  161. }
  162. },
  163. "spec": {
  164. "containers":[{
  165. "name":"a",
  166. "image":"my-container-image",
  167. "resources": {
  168. "limits": {
  169. "cpu": "100m"
  170. }
  171. }
  172. }]
  173. }
  174. }
  175. `,
  176. absentKeys: []string{
  177. "spec.terminationGracePeriodSeconds",
  178. "spec.containers[0].resources.requests",
  179. },
  180. },
  181. {
  182. name: "preserving defaults for v1.1+ mirror pods",
  183. input: `
  184. {
  185. "kind":"Pod",
  186. "apiVersion":"v1",
  187. "metadata":{
  188. "name":"my-pod-name",
  189. "namespace":"my-pod-namespace",
  190. "annotations": {
  191. "kubernetes.io/config.mirror": "cbe924f710c7e26f7693d6a341bcfad0"
  192. }
  193. },
  194. "spec": {
  195. "containers":[{
  196. "name":"a",
  197. "image":"my-container-image",
  198. "resources": {
  199. "limits": {
  200. "cpu": "100m"
  201. }
  202. }
  203. }]
  204. }
  205. }
  206. `,
  207. expectedKeys: map[string]string{
  208. "spec.terminationGracePeriodSeconds": "30",
  209. "spec.containers[0].resources.requests": "map[cpu:100m]",
  210. },
  211. },
  212. }
  213. validator := func(obj runtime.Object) field.ErrorList {
  214. return validation.ValidatePodSpec(&(obj.(*api.Pod).Spec), field.NewPath("spec"))
  215. }
  216. for _, tc := range cases {
  217. t.Logf("Testing 1.0.0 backward compatibility for %v", tc.name)
  218. compat.TestCompatibility(t, v1.SchemeGroupVersion, []byte(tc.input), validator, tc.expectedKeys, tc.absentKeys)
  219. }
  220. }