strategy_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 deployment
  14. import (
  15. "reflect"
  16. "testing"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/apimachinery/pkg/util/intstr"
  20. "k8s.io/apimachinery/pkg/util/validation/field"
  21. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  22. "k8s.io/apiserver/pkg/registry/rest"
  23. "k8s.io/kubernetes/pkg/apis/apps"
  24. api "k8s.io/kubernetes/pkg/apis/core"
  25. )
  26. const (
  27. fakeImageName = "fake-name"
  28. fakeImage = "fakeimage"
  29. deploymentName = "test-deployment"
  30. namespace = "test-namespace"
  31. )
  32. func TestStatusUpdates(t *testing.T) {
  33. tests := []struct {
  34. old runtime.Object
  35. obj runtime.Object
  36. expected runtime.Object
  37. }{
  38. {
  39. old: newDeployment(map[string]string{"test": "label"}, map[string]string{"test": "annotation"}),
  40. obj: newDeployment(map[string]string{"test": "label", "sneaky": "label"}, map[string]string{"test": "annotation"}),
  41. expected: newDeployment(map[string]string{"test": "label"}, map[string]string{"test": "annotation"}),
  42. },
  43. {
  44. old: newDeployment(map[string]string{"test": "label"}, map[string]string{"test": "annotation"}),
  45. obj: newDeployment(map[string]string{"test": "label"}, map[string]string{"test": "annotation", "sneaky": "annotation"}),
  46. expected: newDeployment(map[string]string{"test": "label"}, map[string]string{"test": "annotation", "sneaky": "annotation"}),
  47. },
  48. }
  49. for _, test := range tests {
  50. deploymentStatusStrategy{}.PrepareForUpdate(genericapirequest.NewContext(), test.obj, test.old)
  51. if !reflect.DeepEqual(test.expected, test.obj) {
  52. t.Errorf("Unexpected object mismatch! Expected:\n%#v\ngot:\n%#v", test.expected, test.obj)
  53. }
  54. }
  55. }
  56. func newDeployment(labels, annotations map[string]string) *apps.Deployment {
  57. return &apps.Deployment{
  58. ObjectMeta: metav1.ObjectMeta{
  59. Name: "test",
  60. Labels: labels,
  61. Annotations: annotations,
  62. },
  63. Spec: apps.DeploymentSpec{
  64. Replicas: 1,
  65. Strategy: apps.DeploymentStrategy{
  66. Type: apps.RecreateDeploymentStrategyType,
  67. },
  68. Template: api.PodTemplateSpec{
  69. Spec: api.PodSpec{
  70. Containers: []api.Container{
  71. {
  72. Name: "test",
  73. Image: "test",
  74. },
  75. },
  76. },
  77. },
  78. },
  79. }
  80. }
  81. func TestSelectorImmutability(t *testing.T) {
  82. tests := []struct {
  83. requestInfo genericapirequest.RequestInfo
  84. oldSelectorLabels map[string]string
  85. newSelectorLabels map[string]string
  86. expectedErrorList field.ErrorList
  87. }{
  88. {
  89. genericapirequest.RequestInfo{
  90. APIGroup: "apps",
  91. APIVersion: "v1beta2",
  92. Resource: "deployments",
  93. },
  94. map[string]string{"a": "b"},
  95. map[string]string{"c": "d"},
  96. field.ErrorList{
  97. &field.Error{
  98. Type: field.ErrorTypeInvalid,
  99. Field: field.NewPath("spec").Child("selector").String(),
  100. BadValue: &metav1.LabelSelector{
  101. MatchLabels: map[string]string{"c": "d"},
  102. MatchExpressions: []metav1.LabelSelectorRequirement{},
  103. },
  104. Detail: "field is immutable",
  105. },
  106. },
  107. },
  108. {
  109. genericapirequest.RequestInfo{
  110. APIGroup: "apps",
  111. APIVersion: "v1beta1",
  112. Resource: "deployments",
  113. },
  114. map[string]string{"a": "b"},
  115. map[string]string{"c": "d"},
  116. field.ErrorList{},
  117. },
  118. {
  119. genericapirequest.RequestInfo{
  120. APIGroup: "extensions",
  121. APIVersion: "v1beta1",
  122. },
  123. map[string]string{"a": "b"},
  124. map[string]string{"c": "d"},
  125. field.ErrorList{},
  126. },
  127. }
  128. for _, test := range tests {
  129. oldDeployment := newDeploymentWithSelectorLabels(test.oldSelectorLabels)
  130. newDeployment := newDeploymentWithSelectorLabels(test.newSelectorLabels)
  131. context := genericapirequest.NewContext()
  132. context = genericapirequest.WithRequestInfo(context, &test.requestInfo)
  133. errorList := deploymentStrategy{}.ValidateUpdate(context, newDeployment, oldDeployment)
  134. if len(test.expectedErrorList) == 0 && len(errorList) == 0 {
  135. continue
  136. }
  137. if !reflect.DeepEqual(test.expectedErrorList, errorList) {
  138. t.Errorf("Unexpected error list, expected: %v, actual: %v", test.expectedErrorList, errorList)
  139. }
  140. }
  141. }
  142. func newDeploymentWithSelectorLabels(selectorLabels map[string]string) *apps.Deployment {
  143. return &apps.Deployment{
  144. ObjectMeta: metav1.ObjectMeta{
  145. Name: deploymentName,
  146. Namespace: namespace,
  147. ResourceVersion: "1",
  148. },
  149. Spec: apps.DeploymentSpec{
  150. Selector: &metav1.LabelSelector{
  151. MatchLabels: selectorLabels,
  152. MatchExpressions: []metav1.LabelSelectorRequirement{},
  153. },
  154. Strategy: apps.DeploymentStrategy{
  155. Type: apps.RollingUpdateDeploymentStrategyType,
  156. RollingUpdate: &apps.RollingUpdateDeployment{
  157. MaxSurge: intstr.FromInt(1),
  158. MaxUnavailable: intstr.FromInt(1),
  159. },
  160. },
  161. Template: api.PodTemplateSpec{
  162. ObjectMeta: metav1.ObjectMeta{
  163. Labels: selectorLabels,
  164. },
  165. Spec: api.PodSpec{
  166. RestartPolicy: api.RestartPolicyAlways,
  167. DNSPolicy: api.DNSDefault,
  168. Containers: []api.Container{{Name: fakeImageName, Image: fakeImage, ImagePullPolicy: api.PullNever, TerminationMessagePolicy: api.TerminationMessageReadFile}},
  169. },
  170. },
  171. },
  172. }
  173. }
  174. func TestDeploymentDefaultGarbageCollectionPolicy(t *testing.T) {
  175. // Make sure we correctly implement the interface.
  176. // Otherwise a typo could silently change the default.
  177. var gcds rest.GarbageCollectionDeleteStrategy = Strategy
  178. tests := []struct {
  179. requestInfo genericapirequest.RequestInfo
  180. expectedGCPolicy rest.GarbageCollectionPolicy
  181. isNilRequestInfo bool
  182. }{
  183. {
  184. genericapirequest.RequestInfo{
  185. APIGroup: "extensions",
  186. APIVersion: "v1beta1",
  187. Resource: "deployments",
  188. },
  189. rest.OrphanDependents,
  190. false,
  191. },
  192. {
  193. genericapirequest.RequestInfo{
  194. APIGroup: "apps",
  195. APIVersion: "v1beta1",
  196. Resource: "deployments",
  197. },
  198. rest.OrphanDependents,
  199. false,
  200. },
  201. {
  202. genericapirequest.RequestInfo{
  203. APIGroup: "apps",
  204. APIVersion: "v1beta2",
  205. Resource: "deployments",
  206. },
  207. rest.OrphanDependents,
  208. false,
  209. },
  210. {
  211. genericapirequest.RequestInfo{
  212. APIGroup: "apps",
  213. APIVersion: "v1",
  214. Resource: "deployments",
  215. },
  216. rest.DeleteDependents,
  217. false,
  218. },
  219. {
  220. expectedGCPolicy: rest.DeleteDependents,
  221. isNilRequestInfo: true,
  222. },
  223. }
  224. for _, test := range tests {
  225. context := genericapirequest.NewContext()
  226. if !test.isNilRequestInfo {
  227. context = genericapirequest.WithRequestInfo(context, &test.requestInfo)
  228. }
  229. if got, want := gcds.DefaultGarbageCollectionPolicy(context), test.expectedGCPolicy; got != want {
  230. t.Errorf("%s/%s: DefaultGarbageCollectionPolicy() = %#v, want %#v", test.requestInfo.APIGroup,
  231. test.requestInfo.APIVersion, got, want)
  232. }
  233. }
  234. }