strategy_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. Copyright 2016 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 replicaset
  14. import (
  15. "reflect"
  16. "testing"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/util/validation/field"
  19. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  20. "k8s.io/apiserver/pkg/registry/rest"
  21. "k8s.io/kubernetes/pkg/apis/apps"
  22. api "k8s.io/kubernetes/pkg/apis/core"
  23. )
  24. const (
  25. fakeImageName = "fake-name"
  26. fakeImage = "fakeimage"
  27. replicasetName = "test-replicaset"
  28. namespace = "test-namespace"
  29. )
  30. func TestReplicaSetStrategy(t *testing.T) {
  31. ctx := genericapirequest.NewDefaultContext()
  32. if !Strategy.NamespaceScoped() {
  33. t.Errorf("ReplicaSet must be namespace scoped")
  34. }
  35. if Strategy.AllowCreateOnUpdate() {
  36. t.Errorf("ReplicaSet should not allow create on update")
  37. }
  38. validSelector := map[string]string{"a": "b"}
  39. validPodTemplate := api.PodTemplate{
  40. Template: api.PodTemplateSpec{
  41. ObjectMeta: metav1.ObjectMeta{
  42. Labels: validSelector,
  43. },
  44. Spec: api.PodSpec{
  45. RestartPolicy: api.RestartPolicyAlways,
  46. DNSPolicy: api.DNSClusterFirst,
  47. Containers: []api.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent", TerminationMessagePolicy: api.TerminationMessageReadFile}},
  48. },
  49. },
  50. }
  51. rs := &apps.ReplicaSet{
  52. ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: metav1.NamespaceDefault},
  53. Spec: apps.ReplicaSetSpec{
  54. Selector: &metav1.LabelSelector{MatchLabels: validSelector},
  55. Template: validPodTemplate.Template,
  56. },
  57. Status: apps.ReplicaSetStatus{
  58. Replicas: 1,
  59. ObservedGeneration: int64(10),
  60. },
  61. }
  62. Strategy.PrepareForCreate(ctx, rs)
  63. if rs.Status.Replicas != 0 {
  64. t.Error("ReplicaSet should not allow setting status.replicas on create")
  65. }
  66. if rs.Status.ObservedGeneration != int64(0) {
  67. t.Error("ReplicaSet should not allow setting status.observedGeneration on create")
  68. }
  69. errs := Strategy.Validate(ctx, rs)
  70. if len(errs) != 0 {
  71. t.Errorf("Unexpected error validating %v", errs)
  72. }
  73. invalidRc := &apps.ReplicaSet{
  74. ObjectMeta: metav1.ObjectMeta{Name: "bar", ResourceVersion: "4"},
  75. }
  76. Strategy.PrepareForUpdate(ctx, invalidRc, rs)
  77. errs = Strategy.ValidateUpdate(ctx, invalidRc, rs)
  78. if len(errs) == 0 {
  79. t.Errorf("Expected a validation error")
  80. }
  81. if invalidRc.ResourceVersion != "4" {
  82. t.Errorf("Incoming resource version on update should not be mutated")
  83. }
  84. }
  85. func TestReplicaSetStatusStrategy(t *testing.T) {
  86. ctx := genericapirequest.NewDefaultContext()
  87. if !StatusStrategy.NamespaceScoped() {
  88. t.Errorf("ReplicaSet must be namespace scoped")
  89. }
  90. if StatusStrategy.AllowCreateOnUpdate() {
  91. t.Errorf("ReplicaSet should not allow create on update")
  92. }
  93. validSelector := map[string]string{"a": "b"}
  94. validPodTemplate := api.PodTemplate{
  95. Template: api.PodTemplateSpec{
  96. ObjectMeta: metav1.ObjectMeta{
  97. Labels: validSelector,
  98. },
  99. Spec: api.PodSpec{
  100. RestartPolicy: api.RestartPolicyAlways,
  101. DNSPolicy: api.DNSClusterFirst,
  102. Containers: []api.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent"}},
  103. },
  104. },
  105. }
  106. oldRS := &apps.ReplicaSet{
  107. ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: metav1.NamespaceDefault, ResourceVersion: "10"},
  108. Spec: apps.ReplicaSetSpec{
  109. Replicas: 3,
  110. Selector: &metav1.LabelSelector{MatchLabels: validSelector},
  111. Template: validPodTemplate.Template,
  112. },
  113. Status: apps.ReplicaSetStatus{
  114. Replicas: 1,
  115. ObservedGeneration: int64(10),
  116. },
  117. }
  118. newRS := &apps.ReplicaSet{
  119. ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: metav1.NamespaceDefault, ResourceVersion: "9"},
  120. Spec: apps.ReplicaSetSpec{
  121. Replicas: 1,
  122. Selector: &metav1.LabelSelector{MatchLabels: validSelector},
  123. Template: validPodTemplate.Template,
  124. },
  125. Status: apps.ReplicaSetStatus{
  126. Replicas: 3,
  127. ObservedGeneration: int64(11),
  128. },
  129. }
  130. StatusStrategy.PrepareForUpdate(ctx, newRS, oldRS)
  131. if newRS.Status.Replicas != 3 {
  132. t.Errorf("ReplicaSet status updates should allow change of replicas: %v", newRS.Status.Replicas)
  133. }
  134. if newRS.Spec.Replicas != 3 {
  135. t.Errorf("PrepareForUpdate should have preferred spec")
  136. }
  137. errs := StatusStrategy.ValidateUpdate(ctx, newRS, oldRS)
  138. if len(errs) != 0 {
  139. t.Errorf("Unexpected error %v", errs)
  140. }
  141. }
  142. func TestSelectorImmutability(t *testing.T) {
  143. tests := []struct {
  144. requestInfo genericapirequest.RequestInfo
  145. oldSelectorLabels map[string]string
  146. newSelectorLabels map[string]string
  147. expectedErrorList field.ErrorList
  148. }{
  149. {
  150. genericapirequest.RequestInfo{
  151. APIGroup: "apps",
  152. APIVersion: "v1beta2",
  153. Resource: "replicasets",
  154. },
  155. map[string]string{"a": "b"},
  156. map[string]string{"c": "d"},
  157. field.ErrorList{
  158. &field.Error{
  159. Type: field.ErrorTypeInvalid,
  160. Field: field.NewPath("spec").Child("selector").String(),
  161. BadValue: &metav1.LabelSelector{
  162. MatchLabels: map[string]string{"c": "d"},
  163. MatchExpressions: []metav1.LabelSelectorRequirement{},
  164. },
  165. Detail: "field is immutable",
  166. },
  167. },
  168. },
  169. {
  170. genericapirequest.RequestInfo{
  171. APIGroup: "extensions",
  172. APIVersion: "v1beta1",
  173. Resource: "replicasets",
  174. },
  175. map[string]string{"a": "b"},
  176. map[string]string{"c": "d"},
  177. field.ErrorList{},
  178. },
  179. }
  180. for _, test := range tests {
  181. oldReplicaSet := newReplicaSetWithSelectorLabels(test.oldSelectorLabels)
  182. newReplicaSet := newReplicaSetWithSelectorLabels(test.newSelectorLabels)
  183. context := genericapirequest.NewContext()
  184. context = genericapirequest.WithRequestInfo(context, &test.requestInfo)
  185. errorList := rsStrategy{}.ValidateUpdate(context, newReplicaSet, oldReplicaSet)
  186. if !reflect.DeepEqual(test.expectedErrorList, errorList) {
  187. t.Errorf("Unexpected error list, expected: %v, actual: %v", test.expectedErrorList, errorList)
  188. }
  189. }
  190. }
  191. func newReplicaSetWithSelectorLabels(selectorLabels map[string]string) *apps.ReplicaSet {
  192. return &apps.ReplicaSet{
  193. ObjectMeta: metav1.ObjectMeta{
  194. Name: replicasetName,
  195. Namespace: namespace,
  196. ResourceVersion: "1",
  197. },
  198. Spec: apps.ReplicaSetSpec{
  199. Selector: &metav1.LabelSelector{
  200. MatchLabels: selectorLabels,
  201. MatchExpressions: []metav1.LabelSelectorRequirement{},
  202. },
  203. Template: api.PodTemplateSpec{
  204. ObjectMeta: metav1.ObjectMeta{
  205. Labels: selectorLabels,
  206. },
  207. Spec: api.PodSpec{
  208. RestartPolicy: api.RestartPolicyAlways,
  209. DNSPolicy: api.DNSClusterFirst,
  210. Containers: []api.Container{{Name: fakeImageName, Image: fakeImage, ImagePullPolicy: "IfNotPresent", TerminationMessagePolicy: api.TerminationMessageReadFile}},
  211. },
  212. },
  213. },
  214. }
  215. }
  216. func TestReplicasetDefaultGarbageCollectionPolicy(t *testing.T) {
  217. // Make sure we correctly implement the interface.
  218. // Otherwise a typo could silently change the default.
  219. var gcds rest.GarbageCollectionDeleteStrategy = Strategy
  220. tests := []struct {
  221. requestInfo genericapirequest.RequestInfo
  222. expectedGCPolicy rest.GarbageCollectionPolicy
  223. isNilRequestInfo bool
  224. }{
  225. {
  226. genericapirequest.RequestInfo{
  227. APIGroup: "extensions",
  228. APIVersion: "v1beta1",
  229. Resource: "replicasets",
  230. },
  231. rest.OrphanDependents,
  232. false,
  233. },
  234. {
  235. genericapirequest.RequestInfo{
  236. APIGroup: "apps",
  237. APIVersion: "v1beta2",
  238. Resource: "replicasets",
  239. },
  240. rest.OrphanDependents,
  241. false,
  242. },
  243. {
  244. genericapirequest.RequestInfo{
  245. APIGroup: "apps",
  246. APIVersion: "v1",
  247. Resource: "replicasets",
  248. },
  249. rest.DeleteDependents,
  250. false,
  251. },
  252. {
  253. expectedGCPolicy: rest.DeleteDependents,
  254. isNilRequestInfo: true,
  255. },
  256. }
  257. for _, test := range tests {
  258. context := genericapirequest.NewContext()
  259. if !test.isNilRequestInfo {
  260. context = genericapirequest.WithRequestInfo(context, &test.requestInfo)
  261. }
  262. if got, want := gcds.DefaultGarbageCollectionPolicy(context), test.expectedGCPolicy; got != want {
  263. t.Errorf("%s/%s: DefaultGarbageCollectionPolicy() = %#v, want %#v", test.requestInfo.APIGroup,
  264. test.requestInfo.APIVersion, got, want)
  265. }
  266. }
  267. }