@@ -114,7 +114,7 @@ func TestValidatePodTopologySpreadArgs(t *testing.T) {
114
114
},
115
115
},
116
116
},
117
- "max skew less than zero" : {
117
+ "maxSkew less than zero" : {
118
118
args : & config.PodTopologySpreadArgs {
119
119
DefaultConstraints : []v1.TopologySpreadConstraint {
120
120
{
@@ -138,7 +138,7 @@ func TestValidatePodTopologySpreadArgs(t *testing.T) {
138
138
},
139
139
wantErr : `defaultConstraints[0].topologyKey: Required value: can not be empty` ,
140
140
},
141
- "WhenUnsatisfiable is empty" : {
141
+ "whenUnsatisfiable is empty" : {
142
142
args : & config.PodTopologySpreadArgs {
143
143
DefaultConstraints : []v1.TopologySpreadConstraint {
144
144
{
@@ -150,7 +150,7 @@ func TestValidatePodTopologySpreadArgs(t *testing.T) {
150
150
},
151
151
wantErr : `defaultConstraints[0].whenUnsatisfiable: Required value: can not be empty` ,
152
152
},
153
- "WhenUnsatisfiable contains unsupported action" : {
153
+ "whenUnsatisfiable contains unsupported action" : {
154
154
args : & config.PodTopologySpreadArgs {
155
155
DefaultConstraints : []v1.TopologySpreadConstraint {
156
156
{
@@ -206,17 +206,285 @@ func TestValidatePodTopologySpreadArgs(t *testing.T) {
206
206
}
207
207
}
208
208
209
+ func TestValidateRequestedToCapacityRatioArgs (t * testing.T ) {
210
+ cases := map [string ]struct {
211
+ args config.RequestedToCapacityRatioArgs
212
+ wantErr string
213
+ }{
214
+ "valid config" : {
215
+ args : config.RequestedToCapacityRatioArgs {
216
+ Shape : []config.UtilizationShapePoint {
217
+ {
218
+ Utilization : 20 ,
219
+ Score : 5 ,
220
+ },
221
+ {
222
+ Utilization : 30 ,
223
+ Score : 3 ,
224
+ },
225
+ {
226
+ Utilization : 50 ,
227
+ Score : 2 ,
228
+ },
229
+ },
230
+ Resources : []config.ResourceSpec {
231
+ {
232
+ Name : "custom-resource" ,
233
+ Weight : 5 ,
234
+ },
235
+ },
236
+ },
237
+ },
238
+ "no shape points" : {
239
+ args : config.RequestedToCapacityRatioArgs {
240
+ Shape : []config.UtilizationShapePoint {},
241
+ Resources : []config.ResourceSpec {
242
+ {
243
+ Name : "custom" ,
244
+ Weight : 5 ,
245
+ },
246
+ },
247
+ },
248
+ wantErr : `at least one point must be specified` ,
249
+ },
250
+ "utilization less than min" : {
251
+ args : config.RequestedToCapacityRatioArgs {
252
+ Shape : []config.UtilizationShapePoint {
253
+ {
254
+ Utilization : - 10 ,
255
+ Score : 3 ,
256
+ },
257
+ {
258
+ Utilization : 10 ,
259
+ Score : 2 ,
260
+ },
261
+ },
262
+ },
263
+ wantErr : `utilization values must not be less than 0. Utilization[0]==-10` ,
264
+ },
265
+ "utilization greater than max" : {
266
+ args : config.RequestedToCapacityRatioArgs {
267
+ Shape : []config.UtilizationShapePoint {
268
+ {
269
+ Utilization : 10 ,
270
+ Score : 3 ,
271
+ },
272
+ {
273
+ Utilization : 110 ,
274
+ Score : 2 ,
275
+ },
276
+ },
277
+ },
278
+ wantErr : `utilization values must not be greater than 100. Utilization[1]==110` ,
279
+ },
280
+ "Utilization values in non-increasing order" : {
281
+ args : config.RequestedToCapacityRatioArgs {
282
+ Shape : []config.UtilizationShapePoint {
283
+ {
284
+ Utilization : 30 ,
285
+ Score : 3 ,
286
+ },
287
+ {
288
+ Utilization : 20 ,
289
+ Score : 2 ,
290
+ },
291
+ {
292
+ Utilization : 10 ,
293
+ Score : 1 ,
294
+ },
295
+ },
296
+ },
297
+ wantErr : `utilization values must be sorted. Utilization[0]==30 >= Utilization[1]==20` ,
298
+ },
299
+ "duplicated utilization values" : {
300
+ args : config.RequestedToCapacityRatioArgs {
301
+ Shape : []config.UtilizationShapePoint {
302
+ {
303
+ Utilization : 10 ,
304
+ Score : 3 ,
305
+ },
306
+ {
307
+ Utilization : 20 ,
308
+ Score : 2 ,
309
+ },
310
+ {
311
+ Utilization : 20 ,
312
+ Score : 1 ,
313
+ },
314
+ },
315
+ },
316
+ wantErr : `utilization values must be sorted. Utilization[1]==20 >= Utilization[2]==20` ,
317
+ },
318
+ "score less than min" : {
319
+ args : config.RequestedToCapacityRatioArgs {
320
+ Shape : []config.UtilizationShapePoint {
321
+ {
322
+ Utilization : 10 ,
323
+ Score : - 1 ,
324
+ },
325
+ {
326
+ Utilization : 20 ,
327
+ Score : 2 ,
328
+ },
329
+ },
330
+ },
331
+ wantErr : `score values must not be less than 0. Score[0]==-1` ,
332
+ },
333
+ "score greater than max" : {
334
+ args : config.RequestedToCapacityRatioArgs {
335
+ Shape : []config.UtilizationShapePoint {
336
+ {
337
+ Utilization : 10 ,
338
+ Score : 3 ,
339
+ },
340
+ {
341
+ Utilization : 20 ,
342
+ Score : 11 ,
343
+ },
344
+ },
345
+ },
346
+ wantErr : `score values must not be greater than 10. Score[1]==11` ,
347
+ },
348
+ "resources weight less than 1" : {
349
+ args : config.RequestedToCapacityRatioArgs {
350
+ Shape : []config.UtilizationShapePoint {
351
+ {
352
+ Utilization : 10 ,
353
+ Score : 1 ,
354
+ },
355
+ },
356
+ Resources : []config.ResourceSpec {
357
+ {
358
+ Name : "custom" ,
359
+ Weight : 0 ,
360
+ },
361
+ },
362
+ },
363
+ wantErr : `resource custom weight 0 must not be less than 1` ,
364
+ },
365
+ }
366
+
367
+ for name , tc := range cases {
368
+ t .Run (name , func (t * testing.T ) {
369
+ err := ValidateRequestedToCapacityRatioArgs (tc .args )
370
+ assertErr (t , tc .wantErr , err )
371
+ })
372
+ }
373
+ }
374
+
375
+ func TestValidateNodeResourcesLeastAllocatedArgs (t * testing.T ) {
376
+ cases := map [string ]struct {
377
+ args * config.NodeResourcesLeastAllocatedArgs
378
+ wantErr string
379
+ }{
380
+ "valid config" : {
381
+ args : & config.NodeResourcesLeastAllocatedArgs {
382
+ Resources : []config.ResourceSpec {
383
+ {
384
+ Name : "cpu" ,
385
+ Weight : 50 ,
386
+ },
387
+ {
388
+ Name : "memory" ,
389
+ Weight : 30 ,
390
+ },
391
+ },
392
+ },
393
+ },
394
+ "weight less than min" : {
395
+ args : & config.NodeResourcesLeastAllocatedArgs {
396
+ Resources : []config.ResourceSpec {
397
+ {
398
+ Name : "cpu" ,
399
+ Weight : 0 ,
400
+ },
401
+ },
402
+ },
403
+ wantErr : `resource Weight of cpu should be a positive value, got 0` ,
404
+ },
405
+ "weight more than max" : {
406
+ args : & config.NodeResourcesLeastAllocatedArgs {
407
+ Resources : []config.ResourceSpec {
408
+ {
409
+ Name : "memory" ,
410
+ Weight : 101 ,
411
+ },
412
+ },
413
+ },
414
+ wantErr : `resource Weight of memory should be less than 100, got 101` ,
415
+ },
416
+ }
417
+
418
+ for name , tc := range cases {
419
+ t .Run (name , func (t * testing.T ) {
420
+ err := ValidateNodeResourcesLeastAllocatedArgs (tc .args )
421
+ assertErr (t , tc .wantErr , err )
422
+ })
423
+ }
424
+ }
425
+
426
+ func TestValidateNodeResourcesMostAllocatedArgs (t * testing.T ) {
427
+ cases := map [string ]struct {
428
+ args * config.NodeResourcesMostAllocatedArgs
429
+ wantErr string
430
+ }{
431
+ "valid config" : {
432
+ args : & config.NodeResourcesMostAllocatedArgs {
433
+ Resources : []config.ResourceSpec {
434
+ {
435
+ Name : "cpu" ,
436
+ Weight : 70 ,
437
+ },
438
+ {
439
+ Name : "memory" ,
440
+ Weight : 40 ,
441
+ },
442
+ },
443
+ },
444
+ },
445
+ "weight less than min" : {
446
+ args : & config.NodeResourcesMostAllocatedArgs {
447
+ Resources : []config.ResourceSpec {
448
+ {
449
+ Name : "cpu" ,
450
+ Weight : - 1 ,
451
+ },
452
+ },
453
+ },
454
+ wantErr : `resource Weight of cpu should be a positive value, got -1` ,
455
+ },
456
+ "weight more than max" : {
457
+ args : & config.NodeResourcesMostAllocatedArgs {
458
+ Resources : []config.ResourceSpec {
459
+ {
460
+ Name : "memory" ,
461
+ Weight : 110 ,
462
+ },
463
+ },
464
+ },
465
+ wantErr : `resource Weight of memory should be less than 100, got 110` ,
466
+ },
467
+ }
468
+
469
+ for name , tc := range cases {
470
+ t .Run (name , func (t * testing.T ) {
471
+ err := ValidateNodeResourcesMostAllocatedArgs (tc .args )
472
+ assertErr (t , tc .wantErr , err )
473
+ })
474
+ }
475
+ }
476
+
209
477
func assertErr (t * testing.T , wantErr string , gotErr error ) {
210
478
if wantErr == "" {
211
479
if gotErr != nil {
212
- t .Fatalf ("wanted err to be: 'nil', got: '%s' " , gotErr .Error ())
480
+ t .Fatalf ("\n want err to be:\n \t nil \n got: \n \t %s " , gotErr .Error ())
213
481
}
214
482
} else {
215
483
if gotErr == nil {
216
- t .Fatalf ("wanted err to be: '%s', got: nil " , wantErr )
484
+ t .Fatalf ("\n want err to be:\n \t %s \n got: \n \t nil " , wantErr )
217
485
}
218
486
if gotErr .Error () != wantErr {
219
- t .Errorf ("wanted err to be: '%s', got '%s' " , wantErr , gotErr .Error ())
487
+ t .Errorf ("\n want err to be:\n \t %s \n got: \n \t %s " , wantErr , gotErr .Error ())
220
488
}
221
489
}
222
490
}
0 commit comments