From 35b1f28d0f4a669b3c06e3afe8c678d10dab8d11 Mon Sep 17 00:00:00 2001 From: "sw.han" Date: Fri, 2 Oct 2020 11:40:33 +0200 Subject: [PATCH] Refactor topology hints tests for cpumanager * Extract common tests cases that will be used for both GetTopologyHints() and GetPodTopologyHints() * Extract machineInfo as it will be used for both functions as well Signed-off-by: Krzysztof Wiatrzyk --- .../cm/cpumanager/topology_hints_test.go | 140 ++++++++++-------- 1 file changed, 75 insertions(+), 65 deletions(-) diff --git a/pkg/kubelet/cm/cpumanager/topology_hints_test.go b/pkg/kubelet/cm/cpumanager/topology_hints_test.go index 68f18334454..6991db3a626 100644 --- a/pkg/kubelet/cm/cpumanager/topology_hints_test.go +++ b/pkg/kubelet/cm/cpumanager/topology_hints_test.go @@ -31,21 +31,17 @@ import ( "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/bitmask" ) -func TestGetTopologyHints(t *testing.T) { - testPod1 := makePod("fakePod", "fakeContainer", "2", "2") - testContainer1 := &testPod1.Spec.Containers[0] - testPod2 := makePod("fakePod", "fakeContainer", "5", "5") - testContainer2 := &testPod2.Spec.Containers[0] - testPod3 := makePod("fakePod", "fakeContainer", "7", "7") - testContainer3 := &testPod3.Spec.Containers[0] - testPod4 := makePod("fakePod", "fakeContainer", "11", "11") - testContainer4 := &testPod4.Spec.Containers[0] +type testCase struct { + name string + pod v1.Pod + container v1.Container + assignments state.ContainerCPUAssignments + defaultCPUSet cpuset.CPUSet + expectedHints []topologymanager.TopologyHint +} - firstSocketMask, _ := bitmask.NewBitMask(0) - secondSocketMask, _ := bitmask.NewBitMask(1) - crossSocketMask, _ := bitmask.NewBitMask(0, 1) - - machineInfo := cadvisorapi.MachineInfo{ +func returnMachineInfo() cadvisorapi.MachineInfo { + return cadvisorapi.MachineInfo{ NumCores: 12, Topology: []cadvisorapi.Node{ {Id: 0, @@ -64,15 +60,72 @@ func TestGetTopologyHints(t *testing.T) { }, }, } +} - tcases := []struct { - name string - pod v1.Pod - container v1.Container - assignments state.ContainerCPUAssignments - defaultCPUSet cpuset.CPUSet - expectedHints []topologymanager.TopologyHint - }{ +func TestGetTopologyHints(t *testing.T) { + machineInfo := returnMachineInfo() + tcases := returnTestCases() + + for _, tc := range tcases { + topology, _ := topology.Discover(&machineInfo) + + var activePods []*v1.Pod + for p := range tc.assignments { + pod := v1.Pod{} + pod.UID = types.UID(p) + for c := range tc.assignments[p] { + container := v1.Container{} + container.Name = c + pod.Spec.Containers = append(pod.Spec.Containers, container) + } + activePods = append(activePods, &pod) + } + + m := manager{ + policy: &staticPolicy{ + topology: topology, + }, + state: &mockState{ + assignments: tc.assignments, + defaultCPUSet: tc.defaultCPUSet, + }, + topology: topology, + activePods: func() []*v1.Pod { return activePods }, + podStatusProvider: mockPodStatusProvider{}, + sourcesReady: &sourcesReadyStub{}, + } + + hints := m.GetTopologyHints(&tc.pod, &tc.container)[string(v1.ResourceCPU)] + if len(tc.expectedHints) == 0 && len(hints) == 0 { + continue + } + sort.SliceStable(hints, func(i, j int) bool { + return hints[i].LessThan(hints[j]) + }) + sort.SliceStable(tc.expectedHints, func(i, j int) bool { + return tc.expectedHints[i].LessThan(tc.expectedHints[j]) + }) + if !reflect.DeepEqual(tc.expectedHints, hints) { + t.Errorf("Expected in result to be %v , got %v", tc.expectedHints, hints) + } + } +} + +func returnTestCases() []testCase { + testPod1 := makePod("fakePod", "fakeContainer", "2", "2") + testContainer1 := &testPod1.Spec.Containers[0] + testPod2 := makePod("fakePod", "fakeContainer", "5", "5") + testContainer2 := &testPod2.Spec.Containers[0] + testPod3 := makePod("fakePod", "fakeContainer", "7", "7") + testContainer3 := &testPod3.Spec.Containers[0] + testPod4 := makePod("fakePod", "fakeContainer", "11", "11") + testContainer4 := &testPod4.Spec.Containers[0] + + firstSocketMask, _ := bitmask.NewBitMask(0) + secondSocketMask, _ := bitmask.NewBitMask(1) + crossSocketMask, _ := bitmask.NewBitMask(0, 1) + + return []testCase{ { name: "Request 2 CPUs, 4 available on NUMA 0, 6 available on NUMA 1", pod: *testPod1, @@ -231,47 +284,4 @@ func TestGetTopologyHints(t *testing.T) { expectedHints: []topologymanager.TopologyHint{}, }, } - for _, tc := range tcases { - topology, _ := topology.Discover(&machineInfo) - - var activePods []*v1.Pod - for p := range tc.assignments { - pod := v1.Pod{} - pod.UID = types.UID(p) - for c := range tc.assignments[p] { - container := v1.Container{} - container.Name = c - pod.Spec.Containers = append(pod.Spec.Containers, container) - } - activePods = append(activePods, &pod) - } - - m := manager{ - policy: &staticPolicy{ - topology: topology, - }, - state: &mockState{ - assignments: tc.assignments, - defaultCPUSet: tc.defaultCPUSet, - }, - topology: topology, - activePods: func() []*v1.Pod { return activePods }, - podStatusProvider: mockPodStatusProvider{}, - sourcesReady: &sourcesReadyStub{}, - } - - hints := m.GetTopologyHints(&tc.pod, &tc.container)[string(v1.ResourceCPU)] - if len(tc.expectedHints) == 0 && len(hints) == 0 { - continue - } - sort.SliceStable(hints, func(i, j int) bool { - return hints[i].LessThan(hints[j]) - }) - sort.SliceStable(tc.expectedHints, func(i, j int) bool { - return tc.expectedHints[i].LessThan(tc.expectedHints[j]) - }) - if !reflect.DeepEqual(tc.expectedHints, hints) { - t.Errorf("Expected in result to be %v , got %v", tc.expectedHints, hints) - } - } }