@@ -61,7 +61,8 @@ func TestSelectNodes(t *testing.T) {
61
61
62
62
r , re , err := k .SelectNodes (nodes , 1 , 2 )
63
63
assert .NoError (t , err )
64
- assert .Equal (t , 1 , len (re ))
64
+ assert .Equal (t , 2 , len (r ))
65
+ assert .Equal (t , 2 , len (re ))
65
66
66
67
for nodename , cpus := range r {
67
68
assert .Contains (t , []string {"node1" , "node2" }, nodename )
@@ -84,7 +85,7 @@ func TestSelectNodes(t *testing.T) {
84
85
},
85
86
}
86
87
87
- r , re , err = k .SelectNodes (nodes , 1.3 , 1 )
88
+ r , re , err = k .SelectNodes (nodes , 1.3 , 2 )
88
89
assert .NoError (t , err )
89
90
90
91
for nodename , cpus := range r {
@@ -94,7 +95,6 @@ func TestSelectNodes(t *testing.T) {
94
95
cpu := cpus [0 ]
95
96
assert .Equal (t , cpu .Total (), 13 )
96
97
}
97
-
98
98
}
99
99
100
100
func checkAvgPlan (res map [string ][]types.CPUMap , minCon int , maxCon int , name string ) error {
@@ -112,6 +112,8 @@ func checkAvgPlan(res map[string][]types.CPUMap, minCon int, maxCon int, name st
112
112
}
113
113
if minC != minCon || maxC != maxCon {
114
114
fmt .Println (name )
115
+ fmt .Println ("min: " , minC )
116
+ fmt .Println ("max: " , maxC )
115
117
for k , v := range res {
116
118
fmt .Println (k , ":" , len (v ))
117
119
}
@@ -166,7 +168,6 @@ func TestComplexNodes(t *testing.T) {
166
168
}
167
169
168
170
k , _ = New (coreCfg )
169
-
170
171
// test1
171
172
res1 , changed1 , err := k .SelectNodes (nodes , 1.7 , 7 )
172
173
if err != nil {
@@ -175,7 +176,7 @@ func TestComplexNodes(t *testing.T) {
175
176
if check := checkAvgPlan (res1 , 1 , 2 , "res1" ); check != nil {
176
177
t .Fatalf ("something went wrong" )
177
178
}
178
- assert .Equal (t , len (changed1 ), 4 )
179
+ assert .Equal (t , len (changed1 ), len ( res1 ) )
179
180
180
181
// test2
181
182
// SelectNodes 里有一些副作用, 粗暴地拿一个新的来测试吧
@@ -207,11 +208,14 @@ func TestComplexNodes(t *testing.T) {
207
208
"4" : 10 , "5" : 10 , "6" : 10 , "7" : 10 ,
208
209
},
209
210
}
210
- res2 , rem2 , err := k .SelectNodes (nodes , 1.7 , 11 )
211
- if check := checkAvgPlan (res2 , 1 , 4 , "res2" ); check != nil {
211
+ res2 , changed2 , err := k .SelectNodes (nodes , 1.7 , 11 )
212
+ if err != nil {
213
+ t .Fatalf ("something went wrong" )
214
+ }
215
+ if check := checkAvgPlan (res2 , 2 , 3 , "res2" ); check != nil {
212
216
t .Fatalf ("something went wrong" )
213
217
}
214
- assert .Equal (t , len (rem2 ), 4 )
218
+ assert .Equal (t , len (changed2 ), len ( res2 ) )
215
219
216
220
// test3
217
221
nodes = map [string ]types.CPUMap {
@@ -241,11 +245,15 @@ func TestComplexNodes(t *testing.T) {
241
245
"4" : 10 , "5" : 10 , "6" : 10 , "7" : 10 ,
242
246
},
243
247
}
244
- res3 , rem3 , err := k .SelectNodes (nodes , 1.7 , 23 )
248
+ res3 , changed3 , err := k .SelectNodes (nodes , 1.7 , 23 )
249
+ if err != nil {
250
+ fmt .Println ("May be we dont have plan" )
251
+ fmt .Println (err )
252
+ }
245
253
if check := checkAvgPlan (res3 , 2 , 6 , "res3" ); check != nil {
246
254
t .Fatalf ("something went wrong" )
247
255
}
248
- assert .Equal (t , len (rem3 ), 5 )
256
+ assert .Equal (t , len (changed3 ), len ( res3 ) )
249
257
250
258
// test4
251
259
nodes = map [string ]types.CPUMap {
@@ -280,3 +288,171 @@ func TestComplexNodes(t *testing.T) {
280
288
t .Fatalf ("how to alloc 29 containers when you only have 28?" )
281
289
}
282
290
}
291
+
292
+ func TestEvenPlan (t * testing.T ) {
293
+ coreCfg := types.Config {
294
+ EtcdMachines : []string {"http://127.0.0.1:2379" },
295
+ EtcdLockPrefix : "/eru-core/_lock" ,
296
+ Scheduler : types.SchedConfig {
297
+ LockKey : "/coretest" ,
298
+ LockTTL : 1 ,
299
+ Type : "complex" ,
300
+ },
301
+ }
302
+
303
+ k , merr := New (coreCfg )
304
+ if merr != nil {
305
+ t .Fatalf ("cannot create Potassim instance." , merr )
306
+ }
307
+
308
+ // nodes -- n1: 2, n2: 2
309
+ pod1 := map [string ]types.CPUMap {
310
+ "n1" : types.CPUMap { // 2 containers
311
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
312
+ },
313
+ "n2" : types.CPUMap { // 2 containers
314
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
315
+ },
316
+ }
317
+
318
+ res1 , rem1 , err := k .SelectNodes (pod1 , 1.3 , 2 )
319
+ if err != nil {
320
+ t .Fatalf ("sth wrong" )
321
+ }
322
+ if check := checkAvgPlan (res1 , 1 , 1 , "res1" ); check != nil {
323
+ t .Fatalf ("something went wrong" )
324
+ }
325
+ assert .Equal (t , len (rem1 ), 2 )
326
+
327
+ // nodes -- n1: 4, n2: 5, n3:6, n4: 5
328
+ pod2 := map [string ]types.CPUMap {
329
+ "n1" : types.CPUMap { // 4 containers
330
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
331
+ "4" : 10 , "5" : 10 , "6" : 10 , "7" : 10 ,
332
+ },
333
+ "n2" : types.CPUMap { // 5 containers
334
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
335
+ "4" : 10 , "5" : 10 , "6" : 10 , "7" : 10 ,
336
+ "8" : 10 , "9" : 10 ,
337
+ },
338
+ "n3" : types.CPUMap { // 6 containers
339
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
340
+ "4" : 10 , "5" : 10 , "6" : 10 , "7" : 10 ,
341
+ "8" : 10 , "9" : 10 , "10" : 10 , "11" : 10 ,
342
+ },
343
+ "n4" : types.CPUMap { // 5 containers
344
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
345
+ "4" : 10 , "5" : 10 , "6" : 10 , "7" : 10 ,
346
+ "8" : 10 , "9" : 10 ,
347
+ },
348
+ }
349
+
350
+ res2 , rem2 , err := k .SelectNodes (pod2 , 1.7 , 3 )
351
+ if check := checkAvgPlan (res2 , 1 , 1 , "res2" ); check != nil {
352
+ t .Fatalf ("something went wront" )
353
+ }
354
+ assert .Equal (t , len (rem2 ), 3 )
355
+
356
+ pod3 := map [string ]types.CPUMap {
357
+ "n1" : types.CPUMap { // 4 containers
358
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
359
+ "4" : 10 , "5" : 10 , "6" : 10 , "7" : 10 ,
360
+ },
361
+ "n2" : types.CPUMap { // 5 containers
362
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
363
+ "4" : 10 , "5" : 10 , "6" : 10 , "7" : 10 ,
364
+ "8" : 10 , "9" : 10 ,
365
+ },
366
+ "n3" : types.CPUMap { // 6 containers
367
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
368
+ "4" : 10 , "5" : 10 , "6" : 10 , "7" : 10 ,
369
+ "8" : 10 , "9" : 10 , "10" : 10 , "11" : 10 ,
370
+ },
371
+ "n4" : types.CPUMap { // 5 containers
372
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
373
+ "4" : 10 , "5" : 10 , "6" : 10 , "7" : 10 ,
374
+ "8" : 10 , "9" : 10 ,
375
+ },
376
+ }
377
+ res3 , rem3 , err := k .SelectNodes (pod3 , 1.7 , 8 )
378
+ if check := checkAvgPlan (res3 , 2 , 2 , "res3" ); check != nil {
379
+ t .Fatalf ("something went wront" )
380
+ }
381
+ assert .Equal (t , len (rem3 ), 4 )
382
+
383
+ pod4 := map [string ]types.CPUMap {
384
+ "n1" : types.CPUMap { // 4 containers
385
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
386
+ "4" : 10 , "5" : 10 , "6" : 10 , "7" : 10 ,
387
+ },
388
+ "n2" : types.CPUMap { // 5 containers
389
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
390
+ "4" : 10 , "5" : 10 , "6" : 10 , "7" : 10 ,
391
+ "8" : 10 , "9" : 10 ,
392
+ },
393
+ "n3" : types.CPUMap { // 6 containers
394
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
395
+ "4" : 10 , "5" : 10 , "6" : 10 , "7" : 10 ,
396
+ "8" : 10 , "9" : 10 , "10" : 10 , "11" : 10 ,
397
+ },
398
+ "n4" : types.CPUMap { // 5 containers
399
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
400
+ "4" : 10 , "5" : 10 , "6" : 10 , "7" : 10 ,
401
+ "8" : 10 , "9" : 10 ,
402
+ },
403
+ }
404
+
405
+ res4 , rem4 , err := k .SelectNodes (pod4 , 1.7 , 10 )
406
+ if check := checkAvgPlan (res4 , 2 , 3 , "res4" ); check != nil {
407
+ t .Fatalf ("something went wrong" )
408
+ }
409
+ assert .Equal (t , len (rem4 ), 4 )
410
+ }
411
+
412
+ func TestSpecialCase (t * testing.T ) {
413
+ pod := map [string ]types.CPUMap {
414
+ "n1" : types.CPUMap { // 1 containers
415
+ "0" : 10 , "1" : 10 ,
416
+ },
417
+ "n2" : types.CPUMap { // 3 containers
418
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
419
+ "4" : 10 , "5" : 10 ,
420
+ },
421
+ "n3" : types.CPUMap { // 4 containers
422
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
423
+ "4" : 10 , "5" : 10 , "6" : 10 , "7" : 10 ,
424
+ },
425
+ }
426
+
427
+ coreCfg := types.Config {
428
+ EtcdMachines : []string {"http://127.0.0.1:2379" },
429
+ EtcdLockPrefix : "/eru-core/_lock" ,
430
+ Scheduler : types.SchedConfig {
431
+ LockKey : "/coretest" ,
432
+ LockTTL : 1 ,
433
+ Type : "complex" ,
434
+ },
435
+ }
436
+
437
+ k , _ := New (coreCfg )
438
+ res1 , _ , err := k .SelectNodes (pod , 1.7 , 7 )
439
+ if err != nil {
440
+ t .Fatalf ("something went wrong" )
441
+ }
442
+ checkAvgPlan (res1 , 1 , 3 , "new test 2" )
443
+
444
+ newpod := map [string ]types.CPUMap {
445
+ "n1" : types.CPUMap { // 3 containers
446
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
447
+ "4" : 10 , "5" : 10 ,
448
+ },
449
+ "n2" : types.CPUMap { // 4 containers
450
+ "0" : 10 , "1" : 10 , "2" : 10 , "3" : 10 ,
451
+ "4" : 10 , "5" : 10 , "6" : 10 , "7" : 10 ,
452
+ },
453
+ }
454
+
455
+ res2 , changed2 , _ := k .SelectNodes (newpod , 1.7 , 4 )
456
+ assert .Equal (t , len (res2 ), len (changed2 ))
457
+ checkAvgPlan (res2 , 2 , 2 , "new test 2" )
458
+ }
0 commit comments