util_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2019 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 util
  14. import (
  15. "fmt"
  16. "reflect"
  17. "testing"
  18. "k8s.io/apimachinery/pkg/util/diff"
  19. utilfeature "k8s.io/apiserver/pkg/util/feature"
  20. featuregatetesting "k8s.io/component-base/featuregate/testing"
  21. "k8s.io/kubernetes/pkg/apis/core"
  22. "k8s.io/kubernetes/pkg/apis/scheduling"
  23. "k8s.io/kubernetes/pkg/features"
  24. )
  25. func TestDropNonPreemptingPriority(t *testing.T) {
  26. pcWithoutNonPreemptingPriority := func() *scheduling.PriorityClass {
  27. return &scheduling.PriorityClass{}
  28. }
  29. pcWithNonPreemptingPriority := func() *scheduling.PriorityClass {
  30. preemptionPolicy := core.PreemptNever
  31. return &scheduling.PriorityClass{
  32. PreemptionPolicy: &preemptionPolicy,
  33. }
  34. }
  35. pcInfo := []struct {
  36. description string
  37. hasNonPreemptingPriority bool
  38. pc func() *scheduling.PriorityClass
  39. }{
  40. {
  41. description: "PriorityClass Without NonPreemptingPriority",
  42. hasNonPreemptingPriority: false,
  43. pc: pcWithoutNonPreemptingPriority,
  44. },
  45. {
  46. description: "PriorityClass With NonPreemptingPriority",
  47. hasNonPreemptingPriority: true,
  48. pc: pcWithNonPreemptingPriority,
  49. },
  50. {
  51. description: "is nil",
  52. hasNonPreemptingPriority: false,
  53. pc: func() *scheduling.PriorityClass { return nil },
  54. },
  55. }
  56. for _, enabled := range []bool{true, false} {
  57. for _, oldPriorityClassInfo := range pcInfo {
  58. for _, newPriorityClassInfo := range pcInfo {
  59. oldPriorityClassHasNonPreemptingPriority, oldPriorityClass := oldPriorityClassInfo.hasNonPreemptingPriority, oldPriorityClassInfo.pc()
  60. newPriorityClassHasNonPreemptingPriority, newPriorityClass := newPriorityClassInfo.hasNonPreemptingPriority, newPriorityClassInfo.pc()
  61. if newPriorityClass == nil {
  62. continue
  63. }
  64. t.Run(fmt.Sprintf("feature enabled=%v, old PriorityClass %v, new PriorityClass %v", enabled, oldPriorityClassInfo.description, newPriorityClassInfo.description), func(t *testing.T) {
  65. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.NonPreemptingPriority, enabled)()
  66. DropDisabledFields(newPriorityClass, oldPriorityClass)
  67. // old PriorityClass should never be changed
  68. if !reflect.DeepEqual(oldPriorityClass, oldPriorityClassInfo.pc()) {
  69. t.Errorf("old PriorityClass changed: %v", diff.ObjectReflectDiff(oldPriorityClass, oldPriorityClassInfo.pc()))
  70. }
  71. switch {
  72. case enabled || oldPriorityClassHasNonPreemptingPriority:
  73. // new PriorityClass should not be changed if the feature is enabled, or if the old PriorityClass had NonPreemptingPriority
  74. if !reflect.DeepEqual(newPriorityClass, newPriorityClassInfo.pc()) {
  75. t.Errorf("new PriorityClass changed: %v", diff.ObjectReflectDiff(newPriorityClass, newPriorityClassInfo.pc()))
  76. }
  77. case newPriorityClassHasNonPreemptingPriority:
  78. // new PriorityClass should be changed
  79. if reflect.DeepEqual(newPriorityClass, newPriorityClassInfo.pc()) {
  80. t.Errorf("new PriorityClass was not changed")
  81. }
  82. // new PriorityClass should not have NonPreemptingPriority
  83. if !reflect.DeepEqual(newPriorityClass, pcWithoutNonPreemptingPriority()) {
  84. t.Errorf("new PriorityClass had PriorityClassNonPreemptingPriority: %v", diff.ObjectReflectDiff(newPriorityClass, pcWithoutNonPreemptingPriority()))
  85. }
  86. default:
  87. // new PriorityClass should not need to be changed
  88. if !reflect.DeepEqual(newPriorityClass, newPriorityClassInfo.pc()) {
  89. t.Errorf("new PriorityClass changed: %v", diff.ObjectReflectDiff(newPriorityClass, newPriorityClassInfo.pc()))
  90. }
  91. }
  92. })
  93. }
  94. }
  95. }
  96. }