mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 04:33:26 +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"
|
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/bitmask"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetTopologyHints(t *testing.T) {
|
type testCase struct {
|
||||||
testPod1 := makePod("fakePod", "fakeContainer", "2", "2")
|
name string
|
||||||
testContainer1 := &testPod1.Spec.Containers[0]
|
pod v1.Pod
|
||||||
testPod2 := makePod("fakePod", "fakeContainer", "5", "5")
|
container v1.Container
|
||||||
testContainer2 := &testPod2.Spec.Containers[0]
|
assignments state.ContainerCPUAssignments
|
||||||
testPod3 := makePod("fakePod", "fakeContainer", "7", "7")
|
defaultCPUSet cpuset.CPUSet
|
||||||
testContainer3 := &testPod3.Spec.Containers[0]
|
expectedHints []topologymanager.TopologyHint
|
||||||
testPod4 := makePod("fakePod", "fakeContainer", "11", "11")
|
}
|
||||||
testContainer4 := &testPod4.Spec.Containers[0]
|
|
||||||
|
|
||||||
firstSocketMask, _ := bitmask.NewBitMask(0)
|
func returnMachineInfo() cadvisorapi.MachineInfo {
|
||||||
secondSocketMask, _ := bitmask.NewBitMask(1)
|
return cadvisorapi.MachineInfo{
|
||||||
crossSocketMask, _ := bitmask.NewBitMask(0, 1)
|
|
||||||
|
|
||||||
machineInfo := cadvisorapi.MachineInfo{
|
|
||||||
NumCores: 12,
|
NumCores: 12,
|
||||||
Topology: []cadvisorapi.Node{
|
Topology: []cadvisorapi.Node{
|
||||||
{Id: 0,
|
{Id: 0,
|
||||||
@ -64,15 +60,72 @@ func TestGetTopologyHints(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
tcases := []struct {
|
func TestGetTopologyHints(t *testing.T) {
|
||||||
name string
|
machineInfo := returnMachineInfo()
|
||||||
pod v1.Pod
|
tcases := returnTestCases()
|
||||||
container v1.Container
|
|
||||||
assignments state.ContainerCPUAssignments
|
for _, tc := range tcases {
|
||||||
defaultCPUSet cpuset.CPUSet
|
topology, _ := topology.Discover(&machineInfo)
|
||||||
expectedHints []topologymanager.TopologyHint
|
|
||||||
}{
|
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",
|
name: "Request 2 CPUs, 4 available on NUMA 0, 6 available on NUMA 1",
|
||||||
pod: *testPod1,
|
pod: *testPod1,
|
||||||
@ -231,47 +284,4 @@ func TestGetTopologyHints(t *testing.T) {
|
|||||||
expectedHints: []topologymanager.TopologyHint{},
|
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