Skip to content

Commit 0a9bd03

Browse files
author
nolancon
committed
CPU Manager - Updates to unit tests:
- Where previously we called manager.AddContainer(), we now call both manager.Allocate() and manager.AddContainer(). - Some test cases now have two expected errors. One each from Allocate() and AddContainer(). Existing outcomes are unchanged.
1 parent 467f665 commit 0a9bd03

File tree

1 file changed

+63
-39
lines changed

1 file changed

+63
-39
lines changed

pkg/kubelet/cm/cpumanager/cpu_manager_test.go

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -223,34 +223,38 @@ func TestCPUManagerAdd(t *testing.T) {
223223
cpuset.NewCPUSet(),
224224
topologymanager.NewFakeManager())
225225
testCases := []struct {
226-
description string
227-
updateErr error
228-
policy Policy
229-
expCPUSet cpuset.CPUSet
230-
expErr error
226+
description string
227+
updateErr error
228+
policy Policy
229+
expCPUSet cpuset.CPUSet
230+
expAllocateErr error
231+
expAddContainerErr error
231232
}{
232233
{
233-
description: "cpu manager add - no error",
234-
updateErr: nil,
235-
policy: testPolicy,
236-
expCPUSet: cpuset.NewCPUSet(3, 4),
237-
expErr: nil,
234+
description: "cpu manager add - no error",
235+
updateErr: nil,
236+
policy: testPolicy,
237+
expCPUSet: cpuset.NewCPUSet(3, 4),
238+
expAllocateErr: nil,
239+
expAddContainerErr: nil,
238240
},
239241
{
240242
description: "cpu manager add - policy add container error",
241243
updateErr: nil,
242244
policy: &mockPolicy{
243245
err: fmt.Errorf("fake reg error"),
244246
},
245-
expCPUSet: cpuset.NewCPUSet(1, 2, 3, 4),
246-
expErr: fmt.Errorf("fake reg error"),
247+
expCPUSet: cpuset.NewCPUSet(1, 2, 3, 4),
248+
expAllocateErr: fmt.Errorf("fake reg error"),
249+
expAddContainerErr: nil,
247250
},
248251
{
249-
description: "cpu manager add - container update error",
250-
updateErr: fmt.Errorf("fake update error"),
251-
policy: testPolicy,
252-
expCPUSet: cpuset.NewCPUSet(1, 2, 3, 4),
253-
expErr: fmt.Errorf("fake update error"),
252+
description: "cpu manager add - container update error",
253+
updateErr: fmt.Errorf("fake update error"),
254+
policy: testPolicy,
255+
expCPUSet: cpuset.NewCPUSet(1, 2, 3, 4),
256+
expAllocateErr: nil,
257+
expAddContainerErr: fmt.Errorf("fake update error"),
254258
},
255259
}
256260

@@ -271,10 +275,16 @@ func TestCPUManagerAdd(t *testing.T) {
271275

272276
pod := makePod("fakePod", "fakeContainer", "2", "2")
273277
container := &pod.Spec.Containers[0]
274-
err := mgr.AddContainer(pod, container, "fakeID")
275-
if !reflect.DeepEqual(err, testCase.expErr) {
278+
err := mgr.Allocate(pod, container)
279+
if !reflect.DeepEqual(err, testCase.expAllocateErr) {
280+
t.Errorf("CPU Manager Allocate() error (%v). expected error: %v but got: %v",
281+
testCase.description, testCase.expAllocateErr, err)
282+
}
283+
284+
err = mgr.AddContainer(pod, container, "fakeID")
285+
if !reflect.DeepEqual(err, testCase.expAddContainerErr) {
276286
t.Errorf("CPU Manager AddContainer() error (%v). expected error: %v but got: %v",
277-
testCase.description, testCase.expErr, err)
287+
testCase.description, testCase.expAddContainerErr, err)
278288
}
279289
if !testCase.expCPUSet.Equals(mgr.state.GetDefaultCPUSet()) {
280290
t.Errorf("CPU Manager AddContainer() error (%v). expected cpuset: %v but got: %v",
@@ -494,7 +504,12 @@ func TestCPUManagerAddWithInitContainers(t *testing.T) {
494504
testCase.expCSets...)
495505

496506
for i := range containers {
497-
err := mgr.AddContainer(testCase.pod, &containers[i], containerIDs[i])
507+
err := mgr.Allocate(testCase.pod, &containers[i])
508+
if err != nil {
509+
t.Errorf("StaticPolicy Allocate() error (%v). unexpected error for container id: %v: %v",
510+
testCase.description, containerIDs[i], err)
511+
}
512+
err = mgr.AddContainer(testCase.pod, &containers[i], containerIDs[i])
498513
if err != nil {
499514
t.Errorf("StaticPolicy AddContainer() error (%v). unexpected error for container id: %v: %v",
500515
testCase.description, containerIDs[i], err)
@@ -970,25 +985,28 @@ func TestCPUManagerAddWithResvList(t *testing.T) {
970985
cpuset.NewCPUSet(0),
971986
topologymanager.NewFakeManager())
972987
testCases := []struct {
973-
description string
974-
updateErr error
975-
policy Policy
976-
expCPUSet cpuset.CPUSet
977-
expErr error
988+
description string
989+
updateErr error
990+
policy Policy
991+
expCPUSet cpuset.CPUSet
992+
expAllocateErr error
993+
expAddContainerErr error
978994
}{
979995
{
980-
description: "cpu manager add - no error",
981-
updateErr: nil,
982-
policy: testPolicy,
983-
expCPUSet: cpuset.NewCPUSet(0, 3),
984-
expErr: nil,
996+
description: "cpu manager add - no error",
997+
updateErr: nil,
998+
policy: testPolicy,
999+
expCPUSet: cpuset.NewCPUSet(0, 3),
1000+
expAllocateErr: nil,
1001+
expAddContainerErr: nil,
9851002
},
9861003
{
987-
description: "cpu manager add - container update error",
988-
updateErr: fmt.Errorf("fake update error"),
989-
policy: testPolicy,
990-
expCPUSet: cpuset.NewCPUSet(0, 1, 2, 3),
991-
expErr: fmt.Errorf("fake update error"),
1004+
description: "cpu manager add - container update error",
1005+
updateErr: fmt.Errorf("fake update error"),
1006+
policy: testPolicy,
1007+
expCPUSet: cpuset.NewCPUSet(0, 1, 2, 3),
1008+
expAllocateErr: nil,
1009+
expAddContainerErr: fmt.Errorf("fake update error"),
9921010
},
9931011
}
9941012

@@ -1009,10 +1027,16 @@ func TestCPUManagerAddWithResvList(t *testing.T) {
10091027

10101028
pod := makePod("fakePod", "fakeContainer", "2", "2")
10111029
container := &pod.Spec.Containers[0]
1012-
err := mgr.AddContainer(pod, container, "fakeID")
1013-
if !reflect.DeepEqual(err, testCase.expErr) {
1030+
err := mgr.Allocate(pod, container)
1031+
if !reflect.DeepEqual(err, testCase.expAllocateErr) {
1032+
t.Errorf("CPU Manager Allocate() error (%v). expected error: %v but got: %v",
1033+
testCase.description, testCase.expAllocateErr, err)
1034+
}
1035+
1036+
err = mgr.AddContainer(pod, container, "fakeID")
1037+
if !reflect.DeepEqual(err, testCase.expAddContainerErr) {
10141038
t.Errorf("CPU Manager AddContainer() error (%v). expected error: %v but got: %v",
1015-
testCase.description, testCase.expErr, err)
1039+
testCase.description, testCase.expAddContainerErr, err)
10161040
}
10171041
if !testCase.expCPUSet.Equals(mgr.state.GetDefaultCPUSet()) {
10181042
t.Errorf("CPU Manager AddContainer() error (%v). expected cpuset: %v but got: %v",

0 commit comments

Comments
 (0)