strategy_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 statefulset
  14. import (
  15. "testing"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  18. "k8s.io/apiserver/pkg/registry/rest"
  19. "k8s.io/kubernetes/pkg/apis/apps"
  20. api "k8s.io/kubernetes/pkg/apis/core"
  21. )
  22. func TestStatefulSetStrategy(t *testing.T) {
  23. ctx := genericapirequest.NewDefaultContext()
  24. if !Strategy.NamespaceScoped() {
  25. t.Errorf("StatefulSet must be namespace scoped")
  26. }
  27. if Strategy.AllowCreateOnUpdate() {
  28. t.Errorf("StatefulSet should not allow create on update")
  29. }
  30. validSelector := map[string]string{"a": "b"}
  31. validPodTemplate := api.PodTemplate{
  32. Template: api.PodTemplateSpec{
  33. ObjectMeta: metav1.ObjectMeta{
  34. Labels: validSelector,
  35. },
  36. Spec: api.PodSpec{
  37. RestartPolicy: api.RestartPolicyAlways,
  38. DNSPolicy: api.DNSClusterFirst,
  39. Containers: []api.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent"}},
  40. },
  41. },
  42. }
  43. ps := &apps.StatefulSet{
  44. ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: metav1.NamespaceDefault},
  45. Spec: apps.StatefulSetSpec{
  46. PodManagementPolicy: apps.OrderedReadyPodManagement,
  47. Selector: &metav1.LabelSelector{MatchLabels: validSelector},
  48. Template: validPodTemplate.Template,
  49. UpdateStrategy: apps.StatefulSetUpdateStrategy{Type: apps.RollingUpdateStatefulSetStrategyType},
  50. },
  51. Status: apps.StatefulSetStatus{Replicas: 3},
  52. }
  53. Strategy.PrepareForCreate(ctx, ps)
  54. if ps.Status.Replicas != 0 {
  55. t.Error("StatefulSet should not allow setting status.replicas on create")
  56. }
  57. errs := Strategy.Validate(ctx, ps)
  58. if len(errs) != 0 {
  59. t.Errorf("unexpected error validating %v", errs)
  60. }
  61. // Just Spec.Replicas is allowed to change
  62. validPs := &apps.StatefulSet{
  63. ObjectMeta: metav1.ObjectMeta{Name: ps.Name, Namespace: ps.Namespace, ResourceVersion: "1", Generation: 1},
  64. Spec: apps.StatefulSetSpec{
  65. PodManagementPolicy: apps.OrderedReadyPodManagement,
  66. Selector: ps.Spec.Selector,
  67. Template: validPodTemplate.Template,
  68. UpdateStrategy: apps.StatefulSetUpdateStrategy{Type: apps.RollingUpdateStatefulSetStrategyType},
  69. },
  70. Status: apps.StatefulSetStatus{Replicas: 4},
  71. }
  72. Strategy.PrepareForUpdate(ctx, validPs, ps)
  73. errs = Strategy.ValidateUpdate(ctx, validPs, ps)
  74. if len(errs) != 0 {
  75. t.Errorf("updating spec.Replicas is allowed on a statefulset: %v", errs)
  76. }
  77. validPs.Spec.Selector = &metav1.LabelSelector{MatchLabels: map[string]string{"a": "bar"}}
  78. Strategy.PrepareForUpdate(ctx, validPs, ps)
  79. errs = Strategy.ValidateUpdate(ctx, validPs, ps)
  80. if len(errs) == 0 {
  81. t.Errorf("expected a validation error since updates are disallowed on statefulsets.")
  82. }
  83. }
  84. func TestStatefulsetDefaultGarbageCollectionPolicy(t *testing.T) {
  85. // Make sure we correctly implement the interface.
  86. // Otherwise a typo could silently change the default.
  87. var gcds rest.GarbageCollectionDeleteStrategy = Strategy
  88. tests := []struct {
  89. requestInfo genericapirequest.RequestInfo
  90. expectedGCPolicy rest.GarbageCollectionPolicy
  91. isNilRequestInfo bool
  92. }{
  93. {
  94. genericapirequest.RequestInfo{
  95. APIGroup: "apps",
  96. APIVersion: "v1beta1",
  97. Resource: "statefulsets",
  98. },
  99. rest.OrphanDependents,
  100. false,
  101. },
  102. {
  103. genericapirequest.RequestInfo{
  104. APIGroup: "apps",
  105. APIVersion: "v1beta2",
  106. Resource: "statefulsets",
  107. },
  108. rest.OrphanDependents,
  109. false,
  110. },
  111. {
  112. genericapirequest.RequestInfo{
  113. APIGroup: "apps",
  114. APIVersion: "v1",
  115. Resource: "statefulsets",
  116. },
  117. rest.DeleteDependents,
  118. false,
  119. },
  120. {
  121. expectedGCPolicy: rest.DeleteDependents,
  122. isNilRequestInfo: true,
  123. },
  124. }
  125. for _, test := range tests {
  126. context := genericapirequest.NewContext()
  127. if !test.isNilRequestInfo {
  128. context = genericapirequest.WithRequestInfo(context, &test.requestInfo)
  129. }
  130. if got, want := gcds.DefaultGarbageCollectionPolicy(context), test.expectedGCPolicy; got != want {
  131. t.Errorf("%s/%s: DefaultGarbageCollectionPolicy() = %#v, want %#v", test.requestInfo.APIGroup,
  132. test.requestInfo.APIVersion, got, want)
  133. }
  134. }
  135. }
  136. func TestStatefulSetStatusStrategy(t *testing.T) {
  137. ctx := genericapirequest.NewDefaultContext()
  138. if !StatusStrategy.NamespaceScoped() {
  139. t.Errorf("StatefulSet must be namespace scoped")
  140. }
  141. if StatusStrategy.AllowCreateOnUpdate() {
  142. t.Errorf("StatefulSet should not allow create on update")
  143. }
  144. validSelector := map[string]string{"a": "b"}
  145. validPodTemplate := api.PodTemplate{
  146. Template: api.PodTemplateSpec{
  147. ObjectMeta: metav1.ObjectMeta{
  148. Labels: validSelector,
  149. },
  150. Spec: api.PodSpec{
  151. RestartPolicy: api.RestartPolicyAlways,
  152. DNSPolicy: api.DNSClusterFirst,
  153. Containers: []api.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent"}},
  154. },
  155. },
  156. }
  157. oldPS := &apps.StatefulSet{
  158. ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: metav1.NamespaceDefault, ResourceVersion: "10"},
  159. Spec: apps.StatefulSetSpec{
  160. Replicas: 3,
  161. Selector: &metav1.LabelSelector{MatchLabels: validSelector},
  162. Template: validPodTemplate.Template,
  163. UpdateStrategy: apps.StatefulSetUpdateStrategy{Type: apps.RollingUpdateStatefulSetStrategyType},
  164. },
  165. Status: apps.StatefulSetStatus{
  166. Replicas: 1,
  167. },
  168. }
  169. newPS := &apps.StatefulSet{
  170. ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: metav1.NamespaceDefault, ResourceVersion: "9"},
  171. Spec: apps.StatefulSetSpec{
  172. Replicas: 1,
  173. Selector: &metav1.LabelSelector{MatchLabels: validSelector},
  174. Template: validPodTemplate.Template,
  175. UpdateStrategy: apps.StatefulSetUpdateStrategy{Type: apps.RollingUpdateStatefulSetStrategyType},
  176. },
  177. Status: apps.StatefulSetStatus{
  178. Replicas: 2,
  179. },
  180. }
  181. StatusStrategy.PrepareForUpdate(ctx, newPS, oldPS)
  182. if newPS.Status.Replicas != 2 {
  183. t.Errorf("StatefulSet status updates should allow change of pods: %v", newPS.Status.Replicas)
  184. }
  185. if newPS.Spec.Replicas != 3 {
  186. t.Errorf("StatefulSet status updates should not clobber spec: %v", newPS.Spec)
  187. }
  188. errs := StatusStrategy.ValidateUpdate(ctx, newPS, oldPS)
  189. if len(errs) != 0 {
  190. t.Errorf("unexpected error %v", errs)
  191. }
  192. }