mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
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 <k.wiatrzyk@samsung.com>
This commit is contained in:
parent
656a08afdf
commit
35b1f28d0f
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user