-
Notifications
You must be signed in to change notification settings - Fork 38.7k
/
util_test.go
109 lines (96 loc) · 4.17 KB
/
util_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
"fmt"
"reflect"
"testing"
"k8s.io/apimachinery/pkg/util/diff"
utilfeature "k8s.io/apiserver/pkg/util/feature"
featuregatetesting "k8s.io/component-base/featuregate/testing"
"k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/apis/scheduling"
"k8s.io/kubernetes/pkg/features"
)
func TestDropNonPreemptingPriority(t *testing.T) {
pcWithoutNonPreemptingPriority := func() *scheduling.PriorityClass {
return &scheduling.PriorityClass{}
}
pcWithNonPreemptingPriority := func() *scheduling.PriorityClass {
preemptionPolicy := core.PreemptNever
return &scheduling.PriorityClass{
PreemptionPolicy: &preemptionPolicy,
}
}
pcInfo := []struct {
description string
hasNonPreemptingPriority bool
pc func() *scheduling.PriorityClass
}{
{
description: "PriorityClass Without NonPreemptingPriority",
hasNonPreemptingPriority: false,
pc: pcWithoutNonPreemptingPriority,
},
{
description: "PriorityClass With NonPreemptingPriority",
hasNonPreemptingPriority: true,
pc: pcWithNonPreemptingPriority,
},
{
description: "is nil",
hasNonPreemptingPriority: false,
pc: func() *scheduling.PriorityClass { return nil },
},
}
for _, enabled := range []bool{true, false} {
for _, oldPriorityClassInfo := range pcInfo {
for _, newPriorityClassInfo := range pcInfo {
oldPriorityClassHasNonPreemptingPriority, oldPriorityClass := oldPriorityClassInfo.hasNonPreemptingPriority, oldPriorityClassInfo.pc()
newPriorityClassHasNonPreemptingPriority, newPriorityClass := newPriorityClassInfo.hasNonPreemptingPriority, newPriorityClassInfo.pc()
if newPriorityClass == nil {
continue
}
t.Run(fmt.Sprintf("feature enabled=%v, old PriorityClass %v, new PriorityClass %v", enabled, oldPriorityClassInfo.description, newPriorityClassInfo.description), func(t *testing.T) {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.NonPreemptingPriority, enabled)()
DropDisabledFields(newPriorityClass, oldPriorityClass)
// old PriorityClass should never be changed
if !reflect.DeepEqual(oldPriorityClass, oldPriorityClassInfo.pc()) {
t.Errorf("old PriorityClass changed: %v", diff.ObjectReflectDiff(oldPriorityClass, oldPriorityClassInfo.pc()))
}
switch {
case enabled || oldPriorityClassHasNonPreemptingPriority:
// new PriorityClass should not be changed if the feature is enabled, or if the old PriorityClass had NonPreemptingPriority
if !reflect.DeepEqual(newPriorityClass, newPriorityClassInfo.pc()) {
t.Errorf("new PriorityClass changed: %v", diff.ObjectReflectDiff(newPriorityClass, newPriorityClassInfo.pc()))
}
case newPriorityClassHasNonPreemptingPriority:
// new PriorityClass should be changed
if reflect.DeepEqual(newPriorityClass, newPriorityClassInfo.pc()) {
t.Errorf("new PriorityClass was not changed")
}
// new PriorityClass should not have NonPreemptingPriority
if !reflect.DeepEqual(newPriorityClass, pcWithoutNonPreemptingPriority()) {
t.Errorf("new PriorityClass had PriorityClassNonPreemptingPriority: %v", diff.ObjectReflectDiff(newPriorityClass, pcWithoutNonPreemptingPriority()))
}
default:
// new PriorityClass should not need to be changed
if !reflect.DeepEqual(newPriorityClass, newPriorityClassInfo.pc()) {
t.Errorf("new PriorityClass changed: %v", diff.ObjectReflectDiff(newPriorityClass, newPriorityClassInfo.pc()))
}
}
})
}
}
}
}