@@ -2,7 +2,6 @@ package calcium
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
5
"testing"
7
6
8
7
enginemocks "github.com/projecteru2/core/engine/mocks"
@@ -21,66 +20,86 @@ func TestCalculateCapacity(t *testing.T) {
21
20
c := NewTestCluster ()
22
21
ctx := context .Background ()
23
22
store := c .store .(* storemocks.Store )
24
- rmgr := c .rmgr .(* resourcemocks.Manager )
25
- rmgr .On ("GetNodeResourceInfo" , mock .Anything , mock .Anything , mock .Anything , mock .Anything ).Return (nil , nil , nil , nil )
26
- engine := & enginemocks.API {}
27
23
28
- // pod1 := &types.Pod{Name: "p1"}
24
+ lock := & lockmocks.DistributedLock {}
25
+ lock .On ("Lock" , mock .Anything ).Return (ctx , nil )
26
+ lock .On ("Unlock" , mock .Anything ).Return (nil )
27
+ store .On ("CreateLock" , mock .Anything , mock .Anything ).Return (lock , nil )
28
+
29
+ engine := & enginemocks.API {}
30
+ name := "n1"
29
31
node1 := & types.Node {
30
32
NodeMeta : types.NodeMeta {
31
- Name : "n1" ,
33
+ Name : name ,
32
34
},
33
35
Engine : engine ,
34
36
}
35
37
store .On ("GetNode" , mock .Anything , mock .Anything ).Return (node1 , nil )
36
- lock := & lockmocks.DistributedLock {}
37
- lock .On ("Lock" , mock .Anything ).Return (context .TODO (), nil )
38
- lock .On ("Unlock" , mock .Anything ).Return (nil )
39
- store .On ("CreateLock" , mock .Anything , mock .Anything ).Return (lock , nil )
40
- // failed by call plugin
38
+
41
39
opts := & types.DeployOptions {
42
40
Entrypoint : & types.Entrypoint {
43
41
Name : "entry" ,
44
42
},
45
43
ResourceOpts : types.WorkloadResourceOpts {},
46
44
DeployStrategy : strategy .Auto ,
47
45
NodeFilter : types.NodeFilter {
48
- Includes : []string {"n1" },
46
+ Includes : []string {name },
49
47
},
50
48
Count : 3 ,
51
49
}
52
- rmgr .On ("GetNodesDeployCapacity" , mock .Anything , mock .Anything , mock .Anything ).Return (nil , 0 , errors .New ("not implemented" )).Times (3 )
50
+
51
+ // failed by call plugin
52
+ rmgr := c .rmgr .(* resourcemocks.Manager )
53
+ rmgr .On ("GetNodesDeployCapacity" , mock .Anything , mock .Anything , mock .Anything ).Return (nil , 0 , types .ErrNoETCD ).Once ()
53
54
_ , err := c .CalculateCapacity (ctx , opts )
54
55
assert .Error (t , err )
55
56
56
57
// failed by get deploy status
58
+ nrim := map [string ]* resources.NodeCapacityInfo {
59
+ name : {
60
+ NodeName : name ,
61
+ Capacity : 10 ,
62
+ Usage : 0.5 ,
63
+ Rate : 0.5 ,
64
+ Weight : 100 ,
65
+ },
66
+ }
67
+ rmgr .On ("GetNodesDeployCapacity" , mock .Anything , mock .Anything , mock .Anything ).Return (
68
+ nrim , 100 , nil ).Times (3 )
57
69
store .On ("GetDeployStatus" , mock .Anything , mock .Anything , mock .Anything ).Return (nil , types .ErrNoETCD ).Once ()
58
70
_ , err = c .CalculateCapacity (ctx , opts )
59
71
assert .Error (t , err )
60
- store .On ("GetDeployStatus" , mock .Anything , mock .Anything , mock .Anything ).Return (map [string ]int {"n1" : 0 }, nil )
61
72
62
- // failed by get deploy plan
63
- opts .DeployStrategy = "FAKE"
73
+ // failed by get deploy strategy
74
+ store .On ("GetDeployStatus" , mock .Anything , mock .Anything , mock .Anything ).Return (map [string ]int {name : 0 }, nil )
75
+ opts .Count = - 1
64
76
_ , err = c .CalculateCapacity (ctx , opts )
65
77
assert .Error (t , err )
66
78
79
+ // success
80
+ opts .Count = 1
81
+ _ , err = c .CalculateCapacity (ctx , opts )
82
+ assert .NoError (t , err )
83
+
67
84
// strategy: dummy
68
85
opts .DeployStrategy = strategy .Dummy
86
+
87
+ // failed by GetNodesDeployCapacity
88
+ rmgr .On ("GetNodesDeployCapacity" , mock .Anything , mock .Anything , mock .Anything ).Return (nil , 0 , types .ErrNoETCD ).Once ()
89
+ _ , err = c .CalculateCapacity (ctx , opts )
90
+ assert .Error (t , err )
91
+
92
+ // failed by total <= 0
93
+ rmgr .On ("GetNodesDeployCapacity" , mock .Anything , mock .Anything , mock .Anything ).Return (nil , - 1 , nil ).Once ()
94
+ _ , err = c .CalculateCapacity (ctx , opts )
95
+ assert .Error (t , err )
96
+
97
+ // success
69
98
rmgr .On ("GetNodesDeployCapacity" , mock .Anything , mock .Anything , mock .Anything ).Return (
70
- map [string ]* resources.NodeCapacityInfo {
71
- "n1" : {
72
- NodeName : "n1" ,
73
- Capacity : 10 ,
74
- Usage : 0.5 ,
75
- Rate : 0.5 ,
76
- Weight : 100 ,
77
- },
78
- },
79
- 10 , nil ,
80
- )
99
+ nrim , 10 , nil )
81
100
msg , err := c .CalculateCapacity (ctx , opts )
82
101
assert .NoError (t , err )
83
- assert .Equal (t , msg .NodeCapacities ["n1" ], 10 )
102
+ assert .Equal (t , msg .NodeCapacities [name ], 10 )
84
103
assert .Equal (t , msg .Total , 10 )
85
104
86
105
rmgr .AssertExpectations (t )
0 commit comments