strategy_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 poddisruptionbudget
  14. import (
  15. "testing"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/apimachinery/pkg/util/intstr"
  18. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  19. "k8s.io/kubernetes/pkg/apis/policy"
  20. )
  21. func TestPodDisruptionBudgetStrategy(t *testing.T) {
  22. ctx := genericapirequest.NewDefaultContext()
  23. if !Strategy.NamespaceScoped() {
  24. t.Errorf("PodDisruptionBudget must be namespace scoped")
  25. }
  26. if Strategy.AllowCreateOnUpdate() {
  27. t.Errorf("PodDisruptionBudget should not allow create on update")
  28. }
  29. validSelector := map[string]string{"a": "b"}
  30. minAvailable := intstr.FromInt(3)
  31. pdb := &policy.PodDisruptionBudget{
  32. ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: metav1.NamespaceDefault},
  33. Spec: policy.PodDisruptionBudgetSpec{
  34. MinAvailable: &minAvailable,
  35. Selector: &metav1.LabelSelector{MatchLabels: validSelector},
  36. },
  37. }
  38. Strategy.PrepareForCreate(ctx, pdb)
  39. errs := Strategy.Validate(ctx, pdb)
  40. if len(errs) != 0 {
  41. t.Errorf("Unexpected error validating %v", errs)
  42. }
  43. newPdb := &policy.PodDisruptionBudget{
  44. ObjectMeta: metav1.ObjectMeta{Name: pdb.Name, Namespace: pdb.Namespace},
  45. Spec: pdb.Spec,
  46. Status: policy.PodDisruptionBudgetStatus{
  47. DisruptionsAllowed: 1,
  48. CurrentHealthy: 3,
  49. DesiredHealthy: 3,
  50. ExpectedPods: 3,
  51. },
  52. }
  53. // Nothing in Spec changes: OK
  54. Strategy.PrepareForUpdate(ctx, newPdb, pdb)
  55. errs = Strategy.ValidateUpdate(ctx, newPdb, pdb)
  56. if len(errs) != 0 {
  57. t.Errorf("Unexpected error updating PodDisruptionBudget.")
  58. }
  59. // Changing the selector? OK
  60. newPdb.Spec.Selector = &metav1.LabelSelector{MatchLabels: map[string]string{"a": "bar"}}
  61. Strategy.PrepareForUpdate(ctx, newPdb, pdb)
  62. errs = Strategy.ValidateUpdate(ctx, newPdb, pdb)
  63. if len(errs) != 0 {
  64. t.Errorf("Expected no error on changing selector on poddisruptionbudgets.")
  65. }
  66. newPdb.Spec.Selector = pdb.Spec.Selector
  67. // Changing MinAvailable? OK
  68. newMinAvailable := intstr.FromString("28%")
  69. newPdb.Spec.MinAvailable = &newMinAvailable
  70. Strategy.PrepareForUpdate(ctx, newPdb, pdb)
  71. errs = Strategy.ValidateUpdate(ctx, newPdb, pdb)
  72. if len(errs) != 0 {
  73. t.Errorf("Expected no error updating MinAvailable on poddisruptionbudgets.")
  74. }
  75. // Changing MinAvailable to MaxAvailable? OK
  76. maxUnavailable := intstr.FromString("28%")
  77. newPdb.Spec.MaxUnavailable = &maxUnavailable
  78. newPdb.Spec.MinAvailable = nil
  79. Strategy.PrepareForUpdate(ctx, newPdb, pdb)
  80. errs = Strategy.ValidateUpdate(ctx, newPdb, pdb)
  81. if len(errs) != 0 {
  82. t.Errorf("Expected no error updating replacing MinAvailable with MaxUnavailable on poddisruptionbudgets.")
  83. }
  84. }
  85. func TestPodDisruptionBudgetStatusStrategy(t *testing.T) {
  86. ctx := genericapirequest.NewDefaultContext()
  87. if !StatusStrategy.NamespaceScoped() {
  88. t.Errorf("PodDisruptionBudgetStatus must be namespace scoped")
  89. }
  90. if StatusStrategy.AllowCreateOnUpdate() {
  91. t.Errorf("PodDisruptionBudgetStatus should not allow create on update")
  92. }
  93. oldMinAvailable := intstr.FromInt(3)
  94. newMinAvailable := intstr.FromInt(2)
  95. validSelector := map[string]string{"a": "b"}
  96. oldPdb := &policy.PodDisruptionBudget{
  97. ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: metav1.NamespaceDefault, ResourceVersion: "10"},
  98. Spec: policy.PodDisruptionBudgetSpec{
  99. Selector: &metav1.LabelSelector{MatchLabels: validSelector},
  100. MinAvailable: &oldMinAvailable,
  101. },
  102. Status: policy.PodDisruptionBudgetStatus{
  103. DisruptionsAllowed: 1,
  104. CurrentHealthy: 3,
  105. DesiredHealthy: 3,
  106. ExpectedPods: 3,
  107. },
  108. }
  109. newPdb := &policy.PodDisruptionBudget{
  110. ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: metav1.NamespaceDefault, ResourceVersion: "9"},
  111. Spec: policy.PodDisruptionBudgetSpec{
  112. Selector: &metav1.LabelSelector{MatchLabels: validSelector},
  113. MinAvailable: &newMinAvailable,
  114. },
  115. Status: policy.PodDisruptionBudgetStatus{
  116. DisruptionsAllowed: 0,
  117. CurrentHealthy: 2,
  118. DesiredHealthy: 3,
  119. ExpectedPods: 3,
  120. },
  121. }
  122. StatusStrategy.PrepareForUpdate(ctx, newPdb, oldPdb)
  123. if newPdb.Status.CurrentHealthy != 2 {
  124. t.Errorf("PodDisruptionBudget status updates should allow change of CurrentHealthy: %v", newPdb.Status.CurrentHealthy)
  125. }
  126. if newPdb.Spec.MinAvailable.IntValue() != 3 {
  127. t.Errorf("PodDisruptionBudget status updates should not clobber spec: %v", newPdb.Spec)
  128. }
  129. errs := StatusStrategy.ValidateUpdate(ctx, newPdb, oldPdb)
  130. if len(errs) != 0 {
  131. t.Errorf("Unexpected error %v", errs)
  132. }
  133. }