Skip to content

Commit 17d9b15

Browse files
authored
Merge pull request #1644 from ingvagabund/nodeutilization-arg-validation
nodeutilization: invoke ValidateLowNodeUtilizationArgs instead of validateLowNodeUtilizationThresholds to make the test more generic
2 parents b8e3c0b + b935c7d commit 17d9b15

File tree

1 file changed

+113
-97
lines changed

1 file changed

+113
-97
lines changed

pkg/framework/plugins/nodeutilization/validation_test.go

+113-97
Original file line numberDiff line numberDiff line change
@@ -21,164 +21,180 @@ import (
2121
"testing"
2222

2323
v1 "k8s.io/api/core/v1"
24+
"k8s.io/apimachinery/pkg/runtime"
25+
2426
"sigs.k8s.io/descheduler/pkg/api"
2527
)
2628

2729
func TestValidateLowNodeUtilizationPluginConfig(t *testing.T) {
2830
extendedResource := v1.ResourceName("example.com/foo")
2931
tests := []struct {
30-
name string
31-
thresholds api.ResourceThresholds
32-
targetThresholds api.ResourceThresholds
33-
errInfo error
32+
name string
33+
args *LowNodeUtilizationArgs
34+
errInfo error
3435
}{
3536
{
3637
name: "passing invalid thresholds",
37-
thresholds: api.ResourceThresholds{
38-
v1.ResourceCPU: 20,
39-
v1.ResourceMemory: 120,
40-
},
41-
targetThresholds: api.ResourceThresholds{
42-
v1.ResourceCPU: 80,
43-
v1.ResourceMemory: 80,
38+
args: &LowNodeUtilizationArgs{
39+
Thresholds: api.ResourceThresholds{
40+
v1.ResourceCPU: 20,
41+
v1.ResourceMemory: 120,
42+
},
43+
TargetThresholds: api.ResourceThresholds{
44+
v1.ResourceCPU: 80,
45+
v1.ResourceMemory: 80,
46+
},
4447
},
4548
errInfo: fmt.Errorf("thresholds config is not valid: %v", fmt.Errorf(
4649
"%v threshold not in [%v, %v] range", v1.ResourceMemory, MinResourcePercentage, MaxResourcePercentage)),
4750
},
4851
{
4952
name: "thresholds and targetThresholds configured different num of resources",
50-
thresholds: api.ResourceThresholds{
51-
v1.ResourceCPU: 20,
52-
v1.ResourceMemory: 20,
53-
},
54-
targetThresholds: api.ResourceThresholds{
55-
v1.ResourceCPU: 80,
56-
v1.ResourceMemory: 80,
57-
v1.ResourcePods: 80,
53+
args: &LowNodeUtilizationArgs{
54+
Thresholds: api.ResourceThresholds{
55+
v1.ResourceCPU: 20,
56+
v1.ResourceMemory: 20,
57+
},
58+
TargetThresholds: api.ResourceThresholds{
59+
v1.ResourceCPU: 80,
60+
v1.ResourceMemory: 80,
61+
v1.ResourcePods: 80,
62+
},
5863
},
5964
errInfo: fmt.Errorf("thresholds and targetThresholds configured different resources"),
6065
},
6166
{
6267
name: "thresholds and targetThresholds configured different resources",
63-
thresholds: api.ResourceThresholds{
64-
v1.ResourceCPU: 20,
65-
v1.ResourceMemory: 20,
66-
},
67-
targetThresholds: api.ResourceThresholds{
68-
v1.ResourceCPU: 80,
69-
v1.ResourcePods: 80,
68+
args: &LowNodeUtilizationArgs{
69+
Thresholds: api.ResourceThresholds{
70+
v1.ResourceCPU: 20,
71+
v1.ResourceMemory: 20,
72+
},
73+
TargetThresholds: api.ResourceThresholds{
74+
v1.ResourceCPU: 80,
75+
v1.ResourcePods: 80,
76+
},
7077
},
7178
errInfo: fmt.Errorf("thresholds and targetThresholds configured different resources"),
7279
},
7380
{
7481
name: "thresholds' CPU config value is greater than targetThresholds'",
75-
thresholds: api.ResourceThresholds{
76-
v1.ResourceCPU: 90,
77-
v1.ResourceMemory: 20,
78-
},
79-
targetThresholds: api.ResourceThresholds{
80-
v1.ResourceCPU: 80,
81-
v1.ResourceMemory: 80,
82+
args: &LowNodeUtilizationArgs{
83+
Thresholds: api.ResourceThresholds{
84+
v1.ResourceCPU: 90,
85+
v1.ResourceMemory: 20,
86+
},
87+
TargetThresholds: api.ResourceThresholds{
88+
v1.ResourceCPU: 80,
89+
v1.ResourceMemory: 80,
90+
},
8291
},
8392
errInfo: fmt.Errorf("thresholds' %v percentage is greater than targetThresholds'", v1.ResourceCPU),
8493
},
8594
{
8695
name: "only thresholds configured extended resource",
87-
thresholds: api.ResourceThresholds{
88-
v1.ResourceCPU: 20,
89-
v1.ResourceMemory: 20,
90-
extendedResource: 20,
91-
},
92-
targetThresholds: api.ResourceThresholds{
93-
v1.ResourceCPU: 80,
94-
v1.ResourceMemory: 80,
96+
args: &LowNodeUtilizationArgs{
97+
Thresholds: api.ResourceThresholds{
98+
v1.ResourceCPU: 20,
99+
v1.ResourceMemory: 20,
100+
extendedResource: 20,
101+
},
102+
TargetThresholds: api.ResourceThresholds{
103+
v1.ResourceCPU: 80,
104+
v1.ResourceMemory: 80,
105+
},
95106
},
96107
errInfo: fmt.Errorf("thresholds and targetThresholds configured different resources"),
97108
},
98109
{
99110
name: "only targetThresholds configured extended resource",
100-
thresholds: api.ResourceThresholds{
101-
v1.ResourceCPU: 20,
102-
v1.ResourceMemory: 20,
103-
},
104-
targetThresholds: api.ResourceThresholds{
105-
v1.ResourceCPU: 80,
106-
v1.ResourceMemory: 80,
107-
extendedResource: 80,
111+
args: &LowNodeUtilizationArgs{
112+
Thresholds: api.ResourceThresholds{
113+
v1.ResourceCPU: 20,
114+
v1.ResourceMemory: 20,
115+
},
116+
TargetThresholds: api.ResourceThresholds{
117+
v1.ResourceCPU: 80,
118+
v1.ResourceMemory: 80,
119+
extendedResource: 80,
120+
},
108121
},
109122
errInfo: fmt.Errorf("thresholds and targetThresholds configured different resources"),
110123
},
111124
{
112125
name: "thresholds and targetThresholds configured different extended resources",
113-
thresholds: api.ResourceThresholds{
114-
v1.ResourceCPU: 20,
115-
v1.ResourceMemory: 20,
116-
extendedResource: 20,
117-
},
118-
targetThresholds: api.ResourceThresholds{
119-
v1.ResourceCPU: 80,
120-
v1.ResourceMemory: 80,
121-
"example.com/bar": 80,
126+
args: &LowNodeUtilizationArgs{
127+
Thresholds: api.ResourceThresholds{
128+
v1.ResourceCPU: 20,
129+
v1.ResourceMemory: 20,
130+
extendedResource: 20,
131+
},
132+
TargetThresholds: api.ResourceThresholds{
133+
v1.ResourceCPU: 80,
134+
v1.ResourceMemory: 80,
135+
"example.com/bar": 80,
136+
},
122137
},
123138
errInfo: fmt.Errorf("thresholds and targetThresholds configured different resources"),
124139
},
125140
{
126141
name: "thresholds' extended resource config value is greater than targetThresholds'",
127-
thresholds: api.ResourceThresholds{
128-
v1.ResourceCPU: 20,
129-
v1.ResourceMemory: 20,
130-
extendedResource: 90,
131-
},
132-
targetThresholds: api.ResourceThresholds{
133-
v1.ResourceCPU: 80,
134-
v1.ResourceMemory: 80,
135-
extendedResource: 20,
142+
args: &LowNodeUtilizationArgs{
143+
Thresholds: api.ResourceThresholds{
144+
v1.ResourceCPU: 20,
145+
v1.ResourceMemory: 20,
146+
extendedResource: 90,
147+
},
148+
TargetThresholds: api.ResourceThresholds{
149+
v1.ResourceCPU: 80,
150+
v1.ResourceMemory: 80,
151+
extendedResource: 20,
152+
},
136153
},
137154
errInfo: fmt.Errorf("thresholds' %v percentage is greater than targetThresholds'", extendedResource),
138155
},
139156
{
140157
name: "passing valid plugin config",
141-
thresholds: api.ResourceThresholds{
142-
v1.ResourceCPU: 20,
143-
v1.ResourceMemory: 20,
144-
},
145-
targetThresholds: api.ResourceThresholds{
146-
v1.ResourceCPU: 80,
147-
v1.ResourceMemory: 80,
158+
args: &LowNodeUtilizationArgs{
159+
Thresholds: api.ResourceThresholds{
160+
v1.ResourceCPU: 20,
161+
v1.ResourceMemory: 20,
162+
},
163+
TargetThresholds: api.ResourceThresholds{
164+
v1.ResourceCPU: 80,
165+
v1.ResourceMemory: 80,
166+
},
148167
},
149168
errInfo: nil,
150169
},
151170
{
152171
name: "passing valid plugin config with extended resource",
153-
thresholds: api.ResourceThresholds{
154-
v1.ResourceCPU: 20,
155-
v1.ResourceMemory: 20,
156-
extendedResource: 20,
157-
},
158-
targetThresholds: api.ResourceThresholds{
159-
v1.ResourceCPU: 80,
160-
v1.ResourceMemory: 80,
161-
extendedResource: 80,
172+
args: &LowNodeUtilizationArgs{
173+
Thresholds: api.ResourceThresholds{
174+
v1.ResourceCPU: 20,
175+
v1.ResourceMemory: 20,
176+
extendedResource: 20,
177+
},
178+
TargetThresholds: api.ResourceThresholds{
179+
v1.ResourceCPU: 80,
180+
v1.ResourceMemory: 80,
181+
extendedResource: 80,
182+
},
162183
},
163184
errInfo: nil,
164185
},
165186
}
166187

167188
for _, testCase := range tests {
168-
args := &LowNodeUtilizationArgs{
169-
Thresholds: testCase.thresholds,
170-
TargetThresholds: testCase.targetThresholds,
171-
}
172-
validateErr := validateLowNodeUtilizationThresholds(args.Thresholds, args.TargetThresholds, false)
173-
174-
if validateErr == nil || testCase.errInfo == nil {
175-
if validateErr != testCase.errInfo {
176-
t.Errorf("expected validity of plugin config: thresholds %#v targetThresholds %#v to be %v but got %v instead",
177-
testCase.thresholds, testCase.targetThresholds, testCase.errInfo, validateErr)
189+
t.Run(testCase.name, func(t *testing.T) {
190+
validateErr := ValidateLowNodeUtilizationArgs(runtime.Object(testCase.args))
191+
if validateErr == nil || testCase.errInfo == nil {
192+
if validateErr != testCase.errInfo {
193+
t.Errorf("expected validity of plugin config: %v but got %v instead", testCase.errInfo, validateErr)
194+
}
195+
} else if validateErr.Error() != testCase.errInfo.Error() {
196+
t.Errorf("expected validity of plugin config: %v but got %v instead", testCase.errInfo, validateErr)
178197
}
179-
} else if validateErr.Error() != testCase.errInfo.Error() {
180-
t.Errorf("expected validity of plugin config: thresholds %#v targetThresholds %#v to be %v but got %v instead",
181-
testCase.thresholds, testCase.targetThresholds, testCase.errInfo, validateErr)
182-
}
198+
})
183199
}
184200
}

0 commit comments

Comments
 (0)