@@ -100,9 +100,194 @@ func TestDescheduleStrategy_Violated(t *testing.T) {
100100 args args
101101 want map [string ]interface {}
102102 }{
103- {name : "One node violating" , d : & Strategy {PolicyName : "test name" , Rules : []v1.TASPolicyRule {{Metricname : "memory" , Operator : "GreaterThan" , Target : 9 }}}, args : args {cache .MockEmptySelfUpdatingCache ()}, want : map [string ]interface {}{"node-1" : nil }},
104- {name : "No nodes violating" , d : & Strategy {PolicyName : "test name" , Rules : []v1.TASPolicyRule {{Metricname : "memory" , Operator : "GreaterThan" , Target : 11 }}}, args : args {cache .MockEmptySelfUpdatingCache ()}, want : map [string ]interface {}{}},
105- {name : "No metric found" , d : & Strategy {PolicyName : "test name" , Rules : []v1.TASPolicyRule {{Metricname : "mem" , Operator : "GreaterThan" , Target : 9 }}}, args : args {cache .MockEmptySelfUpdatingCache ()}, want : map [string ]interface {}{}},
103+ {name : "One node violating" ,
104+ d : strategyRuleDefault ("test name" , "memory" , "GreaterThan" , 9 ),
105+ args : args {cache .MockEmptySelfUpdatingCache ()}, want : map [string ]interface {}{"node-1" : nil }},
106+ {name : "No nodes violating" ,
107+ d : strategyRuleDefault ("test name" , "memory" , "GreaterThan" , 11 ),
108+ args : args {cache .MockEmptySelfUpdatingCache ()}, want : map [string ]interface {}{}},
109+ {name : "No metric found" ,
110+ d : strategyRuleDefault ("test name" , "mem" , "GreaterThan" , 9 ),
111+ args : args {cache .MockEmptySelfUpdatingCache ()}, want : map [string ]interface {}{}},
112+ {name : "One node violating w/ a blank logical operator" ,
113+ d : strategyRule ("test-logic-1" , "" , "memory" , "GreaterThan" , 9 ),
114+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
115+ want : map [string ]interface {}{"node-1" : nil }},
116+ {name : "One node violating w/ anyOf" ,
117+ d : strategyRule ("test-logic-2" , "anyOf" , "memory" , "GreaterThan" , 9 ),
118+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
119+ want : map [string ]interface {}{"node-1" : nil }},
120+ {name : "One node violating w/ allOf" ,
121+ d : strategyRule ("test-logic-3" , "allOf" , "memory" , "GreaterThan" , 9 ),
122+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
123+ want : map [string ]interface {}{"node-1" : nil }},
124+ {name : "no metric w/ blank logic operator" ,
125+ d : strategyRule ("test-logic-4" , "" , "mem" , "GreaterThan" , 9 ),
126+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
127+ want : map [string ]interface {}{}},
128+ {name : "no metric w/ anyOf" ,
129+ d : strategyRule ("test-logic-5" , "anyOf" , "mem" , "GreaterThan" , 9 ),
130+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
131+ want : map [string ]interface {}{}},
132+ {name : "no metric w/ allOf" ,
133+ d : strategyRule ("test-logic-6" , "allOf" , "mem" , "GreaterThan" , 9 ),
134+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
135+ want : map [string ]interface {}{}},
136+ {name : "One node violating the 1st rule w/o logical operator" ,
137+ d : & Strategy {PolicyName : "test-logic-7" ,
138+ Rules : []v1.TASPolicyRule {
139+ metricRules ("memory" , "GreaterThan" , 9 ),
140+ metricRules ("cpu" , "GreaterThan" , 900 )}},
141+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
142+ want : map [string ]interface {}{"node-1" : nil }},
143+ {name : "One node violating the 2nd rule w/o logical operator" ,
144+ d : & Strategy {PolicyName : "test-logic-8" ,
145+ Rules : []v1.TASPolicyRule {
146+ metricRules ("memory" , "GreaterThan" , 90 ),
147+ metricRules ("cpu" , "GreaterThan" , 90 )}},
148+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
149+ want : map [string ]interface {}{"node-1" : nil }},
150+ {name : "One node violating the 1st and 2nd rules w/o logical operator" ,
151+ d : & Strategy {PolicyName : "test-logic-9" ,
152+ Rules : []v1.TASPolicyRule {
153+ metricRules ("memory" , "GreaterThan" , 9 ),
154+ metricRules ("cpu" , "GreaterThan" , 90 )}},
155+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
156+ want : map [string ]interface {}{"node-1" : nil }},
157+ {name : "One node violating the 1st and no metric found for 2nd w/o logical operator" ,
158+ d : & Strategy {PolicyName : "test-logic-10" ,
159+ Rules : []v1.TASPolicyRule {
160+ metricRules ("memory" , "GreaterThan" , 9 ),
161+ metricRules ("cpu-x" , "GreaterThan" , 90 )}},
162+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
163+ want : map [string ]interface {}{"node-1" : nil }},
164+ {name : "No node violating without logical operator" ,
165+ d : & Strategy {PolicyName : "test-logic-11" ,
166+ Rules : []v1.TASPolicyRule {
167+ metricRules ("memory" , "GreaterThan" , 90 ),
168+ metricRules ("cpu" , "GreaterThan" , 900 )}},
169+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
170+ want : map [string ]interface {}{}},
171+ {name : "One node violating the first rule w/ blank logical operator" ,
172+ d : & Strategy {PolicyName : "test-logic-12" ,
173+ LogicalOperator : "" ,
174+ Rules : []v1.TASPolicyRule {
175+ metricRules ("memory" , "GreaterThan" , 9 ),
176+ metricRules ("cpu" , "GreaterThan" , 900 )}},
177+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
178+ want : map [string ]interface {}{"node-1" : nil }},
179+ {name : "One node violating the second rule w/ blank logical operator" ,
180+ d : & Strategy {PolicyName : "test-logic-13" ,
181+ LogicalOperator : "" ,
182+ Rules : []v1.TASPolicyRule {
183+ metricRules ("memory" , "GreaterThan" , 90 ),
184+ metricRules ("cpu" , "GreaterThan" , 90 )}},
185+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
186+ want : map [string ]interface {}{"node-1" : nil }},
187+ {name : "One node violating the 1s and 2nd rules w/ blank logical operator" ,
188+ d : & Strategy {PolicyName : "test-logic-14" ,
189+ LogicalOperator : "" ,
190+ Rules : []v1.TASPolicyRule {
191+ metricRules ("memory" , "GreaterThan" , 9 ),
192+ metricRules ("cpu" , "GreaterThan" , 90 )}},
193+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
194+ want : map [string ]interface {}{"node-1" : nil }},
195+ {name : "One node violating the 1st and no metric found for 2nd w/ blank logical operator" ,
196+ d : & Strategy {PolicyName : "test-logic-15" ,
197+ LogicalOperator : "" ,
198+ Rules : []v1.TASPolicyRule {
199+ metricRules ("memory" , "GreaterThan" , 9 ),
200+ metricRules ("cpu-x" , "GreaterThan" , 90 )}},
201+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
202+ want : map [string ]interface {}{"node-1" : nil }},
203+ {name : "No nodes violating w/ blank logical operator" ,
204+ d : & Strategy {PolicyName : "test-logic-16" ,
205+ LogicalOperator : "" ,
206+ Rules : []v1.TASPolicyRule {
207+ metricRules ("memory" , "GreaterThan" , 90 ),
208+ metricRules ("cpu" , "GreaterThan" , 900 )}},
209+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
210+ want : map [string ]interface {}{}},
211+ {name : "One node violating the first rule with anyOf" ,
212+ d : & Strategy {PolicyName : "test-logic-17" ,
213+ LogicalOperator : "anyOf" ,
214+ Rules : []v1.TASPolicyRule {
215+ metricRules ("memory" , "GreaterThan" , 9 ),
216+ metricRules ("cpu" , "GreaterThan" , 900 )}},
217+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
218+ want : map [string ]interface {}{"node-1" : nil }},
219+ {name : "One node violating the second rule with anyOf" ,
220+ d : & Strategy {PolicyName : "test-logic-18" ,
221+ LogicalOperator : "anyOf" ,
222+ Rules : []v1.TASPolicyRule {
223+ metricRules ("memory" , "GreaterThan" , 90 ),
224+ metricRules ("cpu" , "GreaterThan" , 90 )}},
225+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
226+ want : map [string ]interface {}{"node-1" : nil }},
227+ {name : "One node violating the 1s and 2nd rules with anyOf" ,
228+ d : & Strategy {PolicyName : "test-logic-19" ,
229+ LogicalOperator : "anyOf" ,
230+ Rules : []v1.TASPolicyRule {
231+ metricRules ("memory" , "GreaterThan" , 9 ),
232+ metricRules ("cpu" , "GreaterThan" , 90 )}},
233+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
234+ want : map [string ]interface {}{"node-1" : nil }},
235+ {name : "One node violating the 1st and no metric found for 2nd w/ anyOf" ,
236+ d : & Strategy {PolicyName : "test-logic-20" ,
237+ LogicalOperator : "anyOf" ,
238+ Rules : []v1.TASPolicyRule {
239+ metricRules ("memory" , "GreaterThan" , 9 ),
240+ metricRules ("cpu-x" , "GreaterThan" , 90 )}},
241+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
242+ want : map [string ]interface {}{"node-1" : nil }},
243+ {name : "No nodes violating with anyOf" ,
244+ d : & Strategy {PolicyName : "test-logic-21" ,
245+ LogicalOperator : "anyOf" ,
246+ Rules : []v1.TASPolicyRule {
247+ metricRules ("memory" , "GreaterThan" , 90 ),
248+ metricRules ("cpu" , "GreaterThan" , 900 )}},
249+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
250+ want : map [string ]interface {}{}},
251+ {name : "One node violating the first rule with allOf" ,
252+ d : & Strategy {PolicyName : "test-logic-22" ,
253+ LogicalOperator : "allOf" ,
254+ Rules : []v1.TASPolicyRule {
255+ metricRules ("memory" , "GreaterThan" , 9 ),
256+ metricRules ("cpu" , "GreaterThan" , 900 )}},
257+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
258+ want : map [string ]interface {}{}},
259+ {name : "One node violating the second rule with allOf" ,
260+ d : & Strategy {PolicyName : "test-logic-23" ,
261+ LogicalOperator : "allOf" ,
262+ Rules : []v1.TASPolicyRule {
263+ metricRules ("memory" , "GreaterThan" , 90 ),
264+ metricRules ("cpu" , "GreaterThan" , 90 )}},
265+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
266+ want : map [string ]interface {}{}},
267+ {name : "One node violating the 1s and 2nd rules with allOf" ,
268+ d : & Strategy {PolicyName : "test-logic-24" ,
269+ LogicalOperator : "allOf" ,
270+ Rules : []v1.TASPolicyRule {
271+ metricRules ("memory" , "GreaterThan" , 9 ),
272+ metricRules ("cpu" , "GreaterThan" , 90 )}},
273+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
274+ want : map [string ]interface {}{"node-1" : nil }},
275+ {name : "One node violating the 1st and no metric found for 2nd w/ anyOf" ,
276+ d : & Strategy {PolicyName : "test-logic-25" ,
277+ LogicalOperator : "allOf" ,
278+ Rules : []v1.TASPolicyRule {
279+ metricRules ("memory" , "GreaterThan" , 9 ),
280+ metricRules ("cpu-x" , "GreaterThan" , 90 )}},
281+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
282+ want : map [string ]interface {}{}},
283+ {name : "No nodes violating with allOf" ,
284+ d : & Strategy {PolicyName : "test-logic-26" ,
285+ LogicalOperator : "allOf" ,
286+ Rules : []v1.TASPolicyRule {
287+ metricRules ("memory" , "GreaterThan" , 90 ),
288+ metricRules ("cpu" , "GreaterThan" , 900 )}},
289+ args : args {cache : cache .MockEmptySelfUpdatingCache ()},
290+ want : map [string ]interface {}{}},
106291 }
107292 for _ , tt := range tests {
108293 tt := tt
@@ -111,13 +296,39 @@ func TestDescheduleStrategy_Violated(t *testing.T) {
111296 if err != nil {
112297 klog .InfoS ("testing metric write on cache failed" + err .Error (), "component" , "testing" )
113298 }
299+ err = tt .args .cache .WriteMetric ("cpu" , metrics.NodeMetricsInfo {"node-1" : {Timestamp : time .Now (), Window : 1 ,
300+ Value : * resource .NewQuantity (200 , resource .DecimalSI )}})
301+ if err != nil {
302+ t .Errorf ("Cannot write metric to mock cach for test: %v" , err )
303+ }
114304 if got := tt .d .Violated (tt .args .cache ); ! reflect .DeepEqual (got , tt .want ) {
115305 t .Errorf ("Strategy.Violated() = %v, want %v" , got , tt .want )
116306 }
117307 })
118308 }
119309}
120310
311+ func strategyRuleDefault (policyname , metricname , operator string , target int64 ) * Strategy {
312+ return & Strategy {
313+ PolicyName : policyname ,
314+ Rules : []v1.TASPolicyRule {
315+ metricRules (metricname , operator , target )}}
316+ }
317+ func strategyRule (policyname , logicalOp , metricname , operator string , target int64 ) * Strategy {
318+ return & Strategy {
319+ PolicyName : policyname ,
320+ LogicalOperator : logicalOp ,
321+ Rules : []v1.TASPolicyRule {
322+ metricRules (metricname , operator , target )}}
323+ }
324+
325+ func metricRules (metricname string , operator string , target int64 ) v1.TASPolicyRule {
326+ return v1.TASPolicyRule {
327+ Metricname : metricname ,
328+ Operator : operator ,
329+ Target : target ,
330+ }
331+ }
121332func TestDescheduleStrategy_StrategyType (t * testing.T ) {
122333 tests := []struct {
123334 name string
0 commit comments