defaults_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. Copyright 2017 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 v1beta1_test
  14. import (
  15. "reflect"
  16. "testing"
  17. appsv1beta1 "k8s.io/api/apps/v1beta1"
  18. "k8s.io/api/core/v1"
  19. apiequality "k8s.io/apimachinery/pkg/api/equality"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/apimachinery/pkg/util/intstr"
  22. "k8s.io/kubernetes/pkg/api/legacyscheme"
  23. _ "k8s.io/kubernetes/pkg/apis/apps/install"
  24. . "k8s.io/kubernetes/pkg/apis/apps/v1beta1"
  25. api "k8s.io/kubernetes/pkg/apis/core"
  26. _ "k8s.io/kubernetes/pkg/apis/core/install"
  27. utilpointer "k8s.io/utils/pointer"
  28. )
  29. func TestSetDefaultDeployment(t *testing.T) {
  30. defaultIntOrString := intstr.FromString("25%")
  31. differentIntOrString := intstr.FromInt(5)
  32. period := int64(v1.DefaultTerminationGracePeriodSeconds)
  33. defaultTemplate := v1.PodTemplateSpec{
  34. Spec: v1.PodSpec{
  35. DNSPolicy: v1.DNSClusterFirst,
  36. RestartPolicy: v1.RestartPolicyAlways,
  37. SecurityContext: &v1.PodSecurityContext{},
  38. TerminationGracePeriodSeconds: &period,
  39. SchedulerName: api.DefaultSchedulerName,
  40. },
  41. }
  42. tests := []struct {
  43. original *appsv1beta1.Deployment
  44. expected *appsv1beta1.Deployment
  45. }{
  46. {
  47. original: &appsv1beta1.Deployment{},
  48. expected: &appsv1beta1.Deployment{
  49. Spec: appsv1beta1.DeploymentSpec{
  50. Replicas: utilpointer.Int32Ptr(1),
  51. Strategy: appsv1beta1.DeploymentStrategy{
  52. Type: appsv1beta1.RollingUpdateDeploymentStrategyType,
  53. RollingUpdate: &appsv1beta1.RollingUpdateDeployment{
  54. MaxSurge: &defaultIntOrString,
  55. MaxUnavailable: &defaultIntOrString,
  56. },
  57. },
  58. RevisionHistoryLimit: utilpointer.Int32Ptr(2),
  59. ProgressDeadlineSeconds: utilpointer.Int32Ptr(600),
  60. Template: defaultTemplate,
  61. },
  62. },
  63. },
  64. {
  65. original: &appsv1beta1.Deployment{
  66. Spec: appsv1beta1.DeploymentSpec{
  67. Replicas: utilpointer.Int32Ptr(5),
  68. Strategy: appsv1beta1.DeploymentStrategy{
  69. RollingUpdate: &appsv1beta1.RollingUpdateDeployment{
  70. MaxSurge: &differentIntOrString,
  71. },
  72. },
  73. },
  74. },
  75. expected: &appsv1beta1.Deployment{
  76. Spec: appsv1beta1.DeploymentSpec{
  77. Replicas: utilpointer.Int32Ptr(5),
  78. Strategy: appsv1beta1.DeploymentStrategy{
  79. Type: appsv1beta1.RollingUpdateDeploymentStrategyType,
  80. RollingUpdate: &appsv1beta1.RollingUpdateDeployment{
  81. MaxSurge: &differentIntOrString,
  82. MaxUnavailable: &defaultIntOrString,
  83. },
  84. },
  85. RevisionHistoryLimit: utilpointer.Int32Ptr(2),
  86. ProgressDeadlineSeconds: utilpointer.Int32Ptr(600),
  87. Template: defaultTemplate,
  88. },
  89. },
  90. },
  91. {
  92. original: &appsv1beta1.Deployment{
  93. Spec: appsv1beta1.DeploymentSpec{
  94. Replicas: utilpointer.Int32Ptr(3),
  95. Strategy: appsv1beta1.DeploymentStrategy{
  96. Type: appsv1beta1.RollingUpdateDeploymentStrategyType,
  97. RollingUpdate: nil,
  98. },
  99. },
  100. },
  101. expected: &appsv1beta1.Deployment{
  102. Spec: appsv1beta1.DeploymentSpec{
  103. Replicas: utilpointer.Int32Ptr(3),
  104. Strategy: appsv1beta1.DeploymentStrategy{
  105. Type: appsv1beta1.RollingUpdateDeploymentStrategyType,
  106. RollingUpdate: &appsv1beta1.RollingUpdateDeployment{
  107. MaxSurge: &defaultIntOrString,
  108. MaxUnavailable: &defaultIntOrString,
  109. },
  110. },
  111. RevisionHistoryLimit: utilpointer.Int32Ptr(2),
  112. ProgressDeadlineSeconds: utilpointer.Int32Ptr(600),
  113. Template: defaultTemplate,
  114. },
  115. },
  116. },
  117. {
  118. original: &appsv1beta1.Deployment{
  119. Spec: appsv1beta1.DeploymentSpec{
  120. Replicas: utilpointer.Int32Ptr(5),
  121. Strategy: appsv1beta1.DeploymentStrategy{
  122. Type: appsv1beta1.RecreateDeploymentStrategyType,
  123. },
  124. RevisionHistoryLimit: utilpointer.Int32Ptr(0),
  125. },
  126. },
  127. expected: &appsv1beta1.Deployment{
  128. Spec: appsv1beta1.DeploymentSpec{
  129. Replicas: utilpointer.Int32Ptr(5),
  130. Strategy: appsv1beta1.DeploymentStrategy{
  131. Type: appsv1beta1.RecreateDeploymentStrategyType,
  132. },
  133. RevisionHistoryLimit: utilpointer.Int32Ptr(0),
  134. ProgressDeadlineSeconds: utilpointer.Int32Ptr(600),
  135. Template: defaultTemplate,
  136. },
  137. },
  138. },
  139. {
  140. original: &appsv1beta1.Deployment{
  141. Spec: appsv1beta1.DeploymentSpec{
  142. Replicas: utilpointer.Int32Ptr(5),
  143. Strategy: appsv1beta1.DeploymentStrategy{
  144. Type: appsv1beta1.RecreateDeploymentStrategyType,
  145. },
  146. ProgressDeadlineSeconds: utilpointer.Int32Ptr(30),
  147. RevisionHistoryLimit: utilpointer.Int32Ptr(2),
  148. },
  149. },
  150. expected: &appsv1beta1.Deployment{
  151. Spec: appsv1beta1.DeploymentSpec{
  152. Replicas: utilpointer.Int32Ptr(5),
  153. Strategy: appsv1beta1.DeploymentStrategy{
  154. Type: appsv1beta1.RecreateDeploymentStrategyType,
  155. },
  156. ProgressDeadlineSeconds: utilpointer.Int32Ptr(30),
  157. RevisionHistoryLimit: utilpointer.Int32Ptr(2),
  158. Template: defaultTemplate,
  159. },
  160. },
  161. },
  162. }
  163. for _, test := range tests {
  164. original := test.original
  165. expected := test.expected
  166. obj2 := roundTrip(t, runtime.Object(original))
  167. got, ok := obj2.(*appsv1beta1.Deployment)
  168. if !ok {
  169. t.Errorf("unexpected object: %v", got)
  170. t.FailNow()
  171. }
  172. if !apiequality.Semantic.DeepEqual(got.Spec, expected.Spec) {
  173. t.Errorf("object mismatch!\nexpected:\n\t%+v\ngot:\n\t%+v", got.Spec, expected.Spec)
  174. }
  175. }
  176. }
  177. func TestDefaultDeploymentAvailability(t *testing.T) {
  178. d := roundTrip(t, runtime.Object(&appsv1beta1.Deployment{})).(*appsv1beta1.Deployment)
  179. maxUnavailable, err := intstr.GetValueFromIntOrPercent(d.Spec.Strategy.RollingUpdate.MaxUnavailable, int(*(d.Spec.Replicas)), false)
  180. if err != nil {
  181. t.Fatalf("unexpected error: %v", err)
  182. }
  183. if *(d.Spec.Replicas)-int32(maxUnavailable) <= 0 {
  184. t.Fatalf("the default value of maxUnavailable can lead to no active replicas during rolling update")
  185. }
  186. }
  187. func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
  188. data, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(SchemeGroupVersion), obj)
  189. if err != nil {
  190. t.Errorf("%v\n %#v", err, obj)
  191. return nil
  192. }
  193. obj2, err := runtime.Decode(legacyscheme.Codecs.UniversalDecoder(), data)
  194. if err != nil {
  195. t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj)
  196. return nil
  197. }
  198. obj3 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
  199. err = legacyscheme.Scheme.Convert(obj2, obj3, nil)
  200. if err != nil {
  201. t.Errorf("%v\nSource: %#v", err, obj2)
  202. return nil
  203. }
  204. return obj3
  205. }