Skip to content

Commit 7140d9f

Browse files
AndersCMGS
Anders
authored andcommitted
refactor: resource_test
1 parent c1bb6e3 commit 7140d9f

File tree

1 file changed

+177
-74
lines changed

1 file changed

+177
-74
lines changed

cluster/calcium/resource_test.go

+177-74
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,7 @@ func TestAllocResource(t *testing.T) {
6969
config := types.Config{
7070
LockTimeout: 3,
7171
}
72-
store := &storemocks.Store{}
7372
c.config = config
74-
c.store = store
75-
76-
store.On("CreateLock", mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
77-
store.On("GetNodesByPod", mock.Anything, mock.Anything).Return(nil, types.ErrBadPodType).Once()
78-
_, err := c.doAllocResource(ctx, opts)
79-
assert.Error(t, err)
80-
81-
lock := &dummyLock{}
82-
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
83-
// test get by pod and labels and failed because node not available
8473
n1 := "n2"
8574
n2 := "n2"
8675
nodes := []*types.Node{
@@ -98,23 +87,23 @@ func TestAllocResource(t *testing.T) {
9887
MemCap: 100,
9988
},
10089
}
101-
store.On("GetNodesByPod", mock.Anything, mock.Anything).Return(nil, types.ErrBadPodType).Once()
102-
_, err = c.doAllocResource(ctx, opts)
103-
assert.Error(t, err)
90+
91+
store := c.store.(*storemocks.Store)
92+
defer store.AssertExpectations(t)
93+
94+
testAllocFailedAsGetNodesByPodError(t, c, opts)
10495
store.On("GetNodesByPod", mock.Anything, mock.Anything).Return(nodes, nil)
105-
opts.NodeLabels = map[string]string{"test": "1"}
106-
_, err = c.doAllocResource(ctx, opts)
107-
assert.Error(t, err)
108-
// get nodes by name failed
109-
opts.NodeLabels = nil
96+
97+
testAllocFailedAsCreateLockError(t, c, opts)
98+
store.On("CreateLock", mock.Anything, mock.Anything).Return(&dummyLock{}, nil)
99+
100+
testAllocFailedAsNoLabels(t, c, opts)
101+
102+
// Defines for below.
110103
opts.Nodename = n2
111-
store.On("GetNode",
112-
mock.Anything,
113-
mock.Anything,
114-
mock.Anything).Return(nil, types.ErrNoETCD).Once()
115-
_, err = c.doAllocResource(ctx, opts)
116-
assert.Error(t, err)
117-
// get nodes by name success
104+
testAllocFailedAsGetNodeError(t, c, opts)
105+
106+
// Mocks for all of rest.
118107
store.On("GetNode",
119108
mock.Anything,
120109
mock.Anything,
@@ -139,72 +128,186 @@ func TestAllocResource(t *testing.T) {
139128
{"0": 10},
140129
},
141130
}
142-
// mock MakeDeployStatus
143-
store.On("MakeDeployStatus", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
144-
_, err = c.doAllocResource(ctx, opts)
145-
assert.Error(t, err)
146-
// wrong podType
131+
132+
testAllocFailedAsMakeDeployStatusError(t, c, opts)
147133
store.On("MakeDeployStatus", mock.Anything, mock.Anything, mock.Anything).Return(nodesInfo, nil)
148-
_, err = c.doAllocResource(ctx, opts)
134+
135+
testAllocFailedAsInsufficientMemory(t, c, opts)
136+
137+
sched := c.scheduler.(*schedulermocks.Scheduler)
138+
defer sched.AssertExpectations(t)
139+
140+
total := 3
141+
sched.On("SelectMemoryNodes", mock.Anything, mock.Anything, mock.Anything).Return(nodesInfo, total, nil)
142+
143+
testAllocFailedAsInsufficientCPU(t, c, opts)
144+
sched.On("SelectCPUNodes", mock.Anything, mock.Anything, mock.Anything).Return(nodesInfo, nodeCPUPlans, total, nil)
145+
146+
testAllocFailedAsWrongDeployMethod(t, c, opts)
147+
148+
testAllocFailedAsCommonDivisionError(t, c, opts)
149+
testAllocFailedAsGlobalDivisionError(t, c, opts)
150+
testAllocFailedAsEachDivisionError(t, c, opts)
151+
testAllocFailedAsFillDivisionError(t, c, opts)
152+
153+
// Mocks for all.
154+
opts.DeployMethod = cluster.DeployFill
155+
sched.On("FillDivision", mock.Anything, mock.Anything, mock.Anything).Return(nodesInfo, nil)
156+
157+
testAllocFailedAsUpdateNodeResourceError(t, c, opts)
158+
store.On("UpdateNodeResource",
159+
mock.Anything, mock.Anything, mock.Anything,
160+
mock.Anything, mock.Anything, mock.Anything,
161+
).Return(nil)
162+
163+
testAllocFailedAsSaveProcessingError(t, c, opts)
164+
store.On("SaveProcessing",
165+
mock.Anything, mock.Anything, mock.Anything,
166+
).Return(nil)
167+
168+
// success
169+
nsi, err := c.doAllocResource(ctx, opts)
170+
assert.NoError(t, err)
171+
assert.Len(t, nsi, 1)
172+
assert.Equal(t, nsi[0].Name, n2)
173+
// stupid race condition
174+
}
175+
176+
func testAllocFailedAsGetNodesByPodError(t *testing.T, c *Calcium, opts *types.DeployOptions) {
177+
store := c.store.(*storemocks.Store)
178+
store.On("GetNodesByPod", mock.Anything, mock.Anything).Return(nil, types.ErrBadPodType).Once()
179+
_, err := c.doAllocResource(context.Background(), opts)
149180
assert.Error(t, err)
150-
// mock Schedulers
151-
sched := &schedulermocks.Scheduler{}
152-
c.scheduler = sched
153-
// wrong select
181+
}
182+
183+
func testAllocFailedAsCreateLockError(t *testing.T, c *Calcium, opts *types.DeployOptions) {
184+
store := c.store.(*storemocks.Store)
185+
store.On("CreateLock", mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
186+
_, err := c.doAllocResource(context.Background(), opts)
187+
assert.Error(t, err)
188+
}
189+
190+
func testAllocFailedAsNoLabels(t *testing.T, c *Calcium, opts *types.DeployOptions) {
191+
ori := opts.NodeLabels
192+
defer func() {
193+
opts.NodeLabels = ori
194+
}()
195+
196+
opts.NodeLabels = map[string]string{"test": "1"}
197+
_, err := c.doAllocResource(context.Background(), opts)
198+
assert.Error(t, err)
199+
}
200+
201+
func testAllocFailedAsGetNodeError(t *testing.T, c *Calcium, opts *types.DeployOptions) {
202+
store := c.store.(*storemocks.Store)
203+
store.On("GetNode", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
204+
_, err := c.doAllocResource(context.Background(), opts)
205+
assert.Error(t, err)
206+
}
207+
208+
func testAllocFailedAsMakeDeployStatusError(t *testing.T, c *Calcium, opts *types.DeployOptions) {
209+
store := c.store.(*storemocks.Store)
210+
store.On("MakeDeployStatus", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrBadPodType).Once()
211+
_, err := c.doAllocResource(context.Background(), opts)
212+
assert.Error(t, err)
213+
}
214+
215+
func testAllocFailedAsInsufficientMemory(t *testing.T, c *Calcium, opts *types.DeployOptions) {
216+
sched := c.scheduler.(*schedulermocks.Scheduler)
154217
sched.On("SelectMemoryNodes", mock.Anything, mock.Anything, mock.Anything).Return(nil, 0, types.ErrInsufficientMEM).Once()
155-
_, err = c.doAllocResource(ctx, opts)
218+
_, err := c.doAllocResource(context.Background(), opts)
156219
assert.Error(t, err)
157-
//cpu select
158-
total := 3
159-
sched.On("SelectCPUNodes", mock.Anything, mock.Anything, mock.Anything).Return(nodesInfo, nodeCPUPlans, total, nil)
160-
// wrong DeployMethod
220+
}
221+
222+
func testAllocFailedAsInsufficientCPU(t *testing.T, c *Calcium, opts *types.DeployOptions) {
223+
ori := opts.CPUBind
224+
defer func() {
225+
opts.CPUBind = ori
226+
}()
227+
161228
opts.CPUBind = true
162-
_, err = c.doAllocResource(ctx, opts)
229+
sched := c.scheduler.(*schedulermocks.Scheduler)
230+
sched.On("SelectCPUNodes", mock.Anything, mock.Anything, mock.Anything).Return(nil, nil, 0, types.ErrInsufficientCPU).Once()
231+
_, err := c.doAllocResource(context.Background(), opts)
163232
assert.Error(t, err)
164-
// other methods
165-
sched.On("CommonDivision", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrInsufficientRes)
233+
}
234+
235+
func testAllocFailedAsWrongDeployMethod(t *testing.T, c *Calcium, opts *types.DeployOptions) {
236+
ori := opts.DeployMethod
237+
defer func() {
238+
opts.DeployMethod = ori
239+
}()
240+
241+
opts.DeployMethod = "invalid"
242+
_, err := c.doAllocResource(context.Background(), opts)
243+
assert.Error(t, err)
244+
}
245+
246+
func testAllocFailedAsCommonDivisionError(t *testing.T, c *Calcium, opts *types.DeployOptions) {
247+
ori := opts.DeployMethod
248+
defer func() {
249+
opts.DeployMethod = ori
250+
}()
251+
166252
opts.DeployMethod = cluster.DeployAuto
167-
_, err = c.doAllocResource(ctx, opts)
253+
sched := c.scheduler.(*schedulermocks.Scheduler)
254+
sched.On("CommonDivision", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrInsufficientRes).Once()
255+
_, err := c.doAllocResource(context.Background(), opts)
168256
assert.Error(t, err)
169-
sched.On("GlobalDivision", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrInsufficientRes)
257+
}
258+
259+
func testAllocFailedAsGlobalDivisionError(t *testing.T, c *Calcium, opts *types.DeployOptions) {
260+
ori := opts.DeployMethod
261+
defer func() {
262+
opts.DeployMethod = ori
263+
}()
264+
170265
opts.DeployMethod = cluster.DeployGlobal
171-
_, err = c.doAllocResource(ctx, opts)
266+
sched := c.scheduler.(*schedulermocks.Scheduler)
267+
sched.On("GlobalDivision", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrInsufficientRes)
268+
_, err := c.doAllocResource(context.Background(), opts)
172269
assert.Error(t, err)
173-
sched.On("EachDivision", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrInsufficientRes)
270+
}
271+
272+
func testAllocFailedAsEachDivisionError(t *testing.T, c *Calcium, opts *types.DeployOptions) {
273+
ori := opts.DeployMethod
274+
defer func() {
275+
opts.DeployMethod = ori
276+
}()
277+
174278
opts.DeployMethod = cluster.DeployEach
175-
_, err = c.doAllocResource(ctx, opts)
279+
sched := c.scheduler.(*schedulermocks.Scheduler)
280+
sched.On("EachDivision", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrInsufficientRes)
281+
_, err := c.doAllocResource(context.Background(), opts)
176282
assert.Error(t, err)
177-
// fill division but no nodes failed
178-
sched.On("FillDivision", mock.Anything, mock.Anything, mock.Anything).Return([]types.NodeInfo{}, nil).Once()
283+
}
284+
285+
func testAllocFailedAsFillDivisionError(t *testing.T, c *Calcium, opts *types.DeployOptions) {
286+
ori := opts.DeployMethod
287+
defer func() {
288+
opts.DeployMethod = ori
289+
}()
290+
179291
opts.DeployMethod = cluster.DeployFill
180-
_, err = c.doAllocResource(ctx, opts)
292+
sched := c.scheduler.(*schedulermocks.Scheduler)
293+
sched.On("FillDivision", mock.Anything, mock.Anything, mock.Anything).Return([]types.NodeInfo{}, nil).Once()
294+
_, err := c.doAllocResource(context.Background(), opts)
181295
assert.Error(t, err)
182-
// fill division but UpdateNodeResource failed
183-
sched.On("FillDivision", mock.Anything, mock.Anything, mock.Anything).Return(nodesInfo, nil)
296+
}
297+
298+
func testAllocFailedAsUpdateNodeResourceError(t *testing.T, c *Calcium, opts *types.DeployOptions) {
299+
store := c.store.(*storemocks.Store)
184300
store.On("UpdateNodeResource",
185301
mock.Anything, mock.Anything, mock.Anything,
186302
mock.Anything, mock.Anything, mock.Anything,
187303
).Return(types.ErrNoETCD).Once()
188-
_, err = c.doAllocResource(ctx, opts)
304+
_, err := c.doAllocResource(context.Background(), opts)
189305
assert.Error(t, err)
190-
// fill division sucessed
191-
store.On("UpdateNodeResource",
192-
mock.Anything, mock.Anything, mock.Anything,
193-
mock.Anything, mock.Anything, mock.Anything,
194-
).Return(nil)
195-
// bind process failed
196-
store.On("SaveProcessing",
197-
mock.Anything, mock.Anything, mock.Anything,
198-
).Return(types.ErrNoETCD).Once()
199-
_, err = c.doAllocResource(ctx, opts)
306+
}
307+
308+
func testAllocFailedAsSaveProcessingError(t *testing.T, c *Calcium, opts *types.DeployOptions) {
309+
store := c.store.(*storemocks.Store)
310+
store.On("SaveProcessing", mock.Anything, mock.Anything, mock.Anything).Return(types.ErrNoETCD).Once()
311+
_, err := c.doAllocResource(context.Background(), opts)
200312
assert.Error(t, err)
201-
// bind process
202-
store.On("SaveProcessing",
203-
mock.Anything, mock.Anything, mock.Anything,
204-
).Return(nil)
205-
nsi, err := c.doAllocResource(ctx, opts)
206-
assert.NoError(t, err)
207-
assert.Len(t, nsi, 1)
208-
assert.Equal(t, nsi[0].Name, n2)
209-
// stupid race condition
210313
}

0 commit comments

Comments
 (0)