@@ -21,164 +21,180 @@ import (
21
21
"testing"
22
22
23
23
v1 "k8s.io/api/core/v1"
24
+ "k8s.io/apimachinery/pkg/runtime"
25
+
24
26
"sigs.k8s.io/descheduler/pkg/api"
25
27
)
26
28
27
29
func TestValidateLowNodeUtilizationPluginConfig (t * testing.T ) {
28
30
extendedResource := v1 .ResourceName ("example.com/foo" )
29
31
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
34
35
}{
35
36
{
36
37
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
+ },
44
47
},
45
48
errInfo : fmt .Errorf ("thresholds config is not valid: %v" , fmt .Errorf (
46
49
"%v threshold not in [%v, %v] range" , v1 .ResourceMemory , MinResourcePercentage , MaxResourcePercentage )),
47
50
},
48
51
{
49
52
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
+ },
58
63
},
59
64
errInfo : fmt .Errorf ("thresholds and targetThresholds configured different resources" ),
60
65
},
61
66
{
62
67
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
+ },
70
77
},
71
78
errInfo : fmt .Errorf ("thresholds and targetThresholds configured different resources" ),
72
79
},
73
80
{
74
81
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
+ },
82
91
},
83
92
errInfo : fmt .Errorf ("thresholds' %v percentage is greater than targetThresholds'" , v1 .ResourceCPU ),
84
93
},
85
94
{
86
95
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
+ },
95
106
},
96
107
errInfo : fmt .Errorf ("thresholds and targetThresholds configured different resources" ),
97
108
},
98
109
{
99
110
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
+ },
108
121
},
109
122
errInfo : fmt .Errorf ("thresholds and targetThresholds configured different resources" ),
110
123
},
111
124
{
112
125
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
+ },
122
137
},
123
138
errInfo : fmt .Errorf ("thresholds and targetThresholds configured different resources" ),
124
139
},
125
140
{
126
141
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
+ },
136
153
},
137
154
errInfo : fmt .Errorf ("thresholds' %v percentage is greater than targetThresholds'" , extendedResource ),
138
155
},
139
156
{
140
157
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
+ },
148
167
},
149
168
errInfo : nil ,
150
169
},
151
170
{
152
171
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
+ },
162
183
},
163
184
errInfo : nil ,
164
185
},
165
186
}
166
187
167
188
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 )
178
197
}
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
+ })
183
199
}
184
200
}
0 commit comments