mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-04 10:47:25 +00:00
Added policy_options and policy_static unit tests
This commit is contained in:
@@ -118,6 +118,18 @@ func TestPolicyOptionsAvailable(t *testing.T) {
|
|||||||
featureGateEnable: true,
|
featureGateEnable: true,
|
||||||
expectedAvailable: false,
|
expectedAvailable: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
option: StrictCPUReservationOption,
|
||||||
|
featureGate: pkgfeatures.CPUManagerPolicyAlphaOptions,
|
||||||
|
featureGateEnable: true,
|
||||||
|
expectedAvailable: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
option: StrictCPUReservationOption,
|
||||||
|
featureGate: pkgfeatures.CPUManagerPolicyBetaOptions,
|
||||||
|
featureGateEnable: true,
|
||||||
|
expectedAvailable: false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, testCase := range testCases {
|
for _, testCase := range testCases {
|
||||||
t.Run(testCase.option, func(t *testing.T) {
|
t.Run(testCase.option, func(t *testing.T) {
|
||||||
|
@@ -247,19 +247,15 @@ func (p *staticPolicy) validateState(s state.State) error {
|
|||||||
// the set of CPUs stored in the state.
|
// the set of CPUs stored in the state.
|
||||||
totalKnownCPUs := tmpDefaultCPUset.Clone()
|
totalKnownCPUs := tmpDefaultCPUset.Clone()
|
||||||
tmpCPUSets := []cpuset.CPUSet{}
|
tmpCPUSets := []cpuset.CPUSet{}
|
||||||
tmpAllCPUs := p.topology.CPUDetails.CPUs()
|
|
||||||
for pod := range tmpAssignments {
|
for pod := range tmpAssignments {
|
||||||
for _, cset := range tmpAssignments[pod] {
|
for _, cset := range tmpAssignments[pod] {
|
||||||
tmpCPUSets = append(tmpCPUSets, cset)
|
tmpCPUSets = append(tmpCPUSets, cset)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
totalKnownCPUs = totalKnownCPUs.Union(tmpCPUSets...)
|
totalKnownCPUs = totalKnownCPUs.Union(tmpCPUSets...)
|
||||||
if p.options.StrictCPUReservation {
|
if !totalKnownCPUs.Equals(allCPUs) {
|
||||||
tmpAllCPUs = tmpAllCPUs.Difference(p.reservedCPUs)
|
|
||||||
}
|
|
||||||
if !totalKnownCPUs.Equals(tmpAllCPUs) {
|
|
||||||
return fmt.Errorf("current set of available CPUs \"%s\" doesn't match with CPUs in state \"%s\"",
|
return fmt.Errorf("current set of available CPUs \"%s\" doesn't match with CPUs in state \"%s\"",
|
||||||
tmpAllCPUs.String(), totalKnownCPUs.String())
|
allCPUs.String(), totalKnownCPUs.String())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -107,6 +107,15 @@ func TestStaticPolicyStart(t *testing.T) {
|
|||||||
stDefaultCPUSet: cpuset.New(0, 1),
|
stDefaultCPUSet: cpuset.New(0, 1),
|
||||||
expErr: fmt.Errorf("not all reserved cpus: \"0,6\" are present in defaultCpuSet: \"0-1\""),
|
expErr: fmt.Errorf("not all reserved cpus: \"0,6\" are present in defaultCpuSet: \"0-1\""),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "some of reserved cores are present in available cpuset (StrictCPUReservationOption)",
|
||||||
|
topo: topoDualSocketHT,
|
||||||
|
numReservedCPUs: 2,
|
||||||
|
options: map[string]string{StrictCPUReservationOption: "true"},
|
||||||
|
stAssignments: state.ContainerCPUAssignments{},
|
||||||
|
stDefaultCPUSet: cpuset.New(0, 1),
|
||||||
|
expErr: fmt.Errorf("some of strictly reserved cpus: \"0\" are present in defaultCpuSet: \"0-1\""),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "assigned core 2 is still present in available cpuset",
|
description: "assigned core 2 is still present in available cpuset",
|
||||||
topo: topoDualSocketHT,
|
topo: topoDualSocketHT,
|
||||||
@@ -118,6 +127,18 @@ func TestStaticPolicyStart(t *testing.T) {
|
|||||||
stDefaultCPUSet: cpuset.New(2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
|
stDefaultCPUSet: cpuset.New(2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
|
||||||
expErr: fmt.Errorf("pod: fakePod, container: 0 cpuset: \"0-2\" overlaps with default cpuset \"2-11\""),
|
expErr: fmt.Errorf("pod: fakePod, container: 0 cpuset: \"0-2\" overlaps with default cpuset \"2-11\""),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "assigned core 2 is still present in available cpuset (StrictCPUReservationOption)",
|
||||||
|
topo: topoDualSocketHT,
|
||||||
|
options: map[string]string{StrictCPUReservationOption: "true"},
|
||||||
|
stAssignments: state.ContainerCPUAssignments{
|
||||||
|
"fakePod": map[string]cpuset.CPUSet{
|
||||||
|
"0": cpuset.New(0, 1, 2),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
stDefaultCPUSet: cpuset.New(2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
|
||||||
|
expErr: fmt.Errorf("pod: fakePod, container: 0 cpuset: \"0-2\" overlaps with default cpuset \"2-11\""),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "core 12 is not present in topology but is in state cpuset",
|
description: "core 12 is not present in topology but is in state cpuset",
|
||||||
topo: topoDualSocketHT,
|
topo: topoDualSocketHT,
|
||||||
@@ -145,7 +166,8 @@ func TestStaticPolicyStart(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, testCase := range testCases {
|
for _, testCase := range testCases {
|
||||||
t.Run(testCase.description, func(t *testing.T) {
|
t.Run(testCase.description, func(t *testing.T) {
|
||||||
p, _ := NewStaticPolicy(testCase.topo, testCase.numReservedCPUs, cpuset.New(), topologymanager.NewFakeManager(), nil)
|
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, pkgfeatures.CPUManagerPolicyAlphaOptions, true)
|
||||||
|
p, _ := NewStaticPolicy(testCase.topo, testCase.numReservedCPUs, cpuset.New(), topologymanager.NewFakeManager(), testCase.options)
|
||||||
policy := p.(*staticPolicy)
|
policy := p.(*staticPolicy)
|
||||||
st := &mockState{
|
st := &mockState{
|
||||||
assignments: testCase.stAssignments,
|
assignments: testCase.stAssignments,
|
||||||
@@ -939,17 +961,18 @@ func TestTopologyAwareAllocateCPUs(t *testing.T) {
|
|||||||
// above test cases are without kubelet --reserved-cpus cmd option
|
// above test cases are without kubelet --reserved-cpus cmd option
|
||||||
// the following tests are with --reserved-cpus configured
|
// the following tests are with --reserved-cpus configured
|
||||||
type staticPolicyTestWithResvList struct {
|
type staticPolicyTestWithResvList struct {
|
||||||
description string
|
description string
|
||||||
topo *topology.CPUTopology
|
topo *topology.CPUTopology
|
||||||
numReservedCPUs int
|
numReservedCPUs int
|
||||||
reserved cpuset.CPUSet
|
reserved cpuset.CPUSet
|
||||||
stAssignments state.ContainerCPUAssignments
|
cpuPolicyOptions map[string]string
|
||||||
stDefaultCPUSet cpuset.CPUSet
|
stAssignments state.ContainerCPUAssignments
|
||||||
pod *v1.Pod
|
stDefaultCPUSet cpuset.CPUSet
|
||||||
expErr error
|
pod *v1.Pod
|
||||||
expNewErr error
|
expErr error
|
||||||
expCPUAlloc bool
|
expNewErr error
|
||||||
expCSet cpuset.CPUSet
|
expCPUAlloc bool
|
||||||
|
expCSet cpuset.CPUSet
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStaticPolicyStartWithResvList(t *testing.T) {
|
func TestStaticPolicyStartWithResvList(t *testing.T) {
|
||||||
@@ -963,6 +986,16 @@ func TestStaticPolicyStartWithResvList(t *testing.T) {
|
|||||||
stDefaultCPUSet: cpuset.New(),
|
stDefaultCPUSet: cpuset.New(),
|
||||||
expCSet: cpuset.New(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
|
expCSet: cpuset.New(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "empty cpuset with StrictCPUReservationOption enabled",
|
||||||
|
topo: topoDualSocketHT,
|
||||||
|
numReservedCPUs: 2,
|
||||||
|
reserved: cpuset.New(0, 1),
|
||||||
|
cpuPolicyOptions: map[string]string{StrictCPUReservationOption: "true"},
|
||||||
|
stAssignments: state.ContainerCPUAssignments{},
|
||||||
|
stDefaultCPUSet: cpuset.New(),
|
||||||
|
expCSet: cpuset.New(2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "reserved cores 0 & 1 are not present in available cpuset",
|
description: "reserved cores 0 & 1 are not present in available cpuset",
|
||||||
topo: topoDualSocketHT,
|
topo: topoDualSocketHT,
|
||||||
@@ -972,6 +1005,16 @@ func TestStaticPolicyStartWithResvList(t *testing.T) {
|
|||||||
stDefaultCPUSet: cpuset.New(2, 3, 4, 5),
|
stDefaultCPUSet: cpuset.New(2, 3, 4, 5),
|
||||||
expErr: fmt.Errorf("not all reserved cpus: \"0-1\" are present in defaultCpuSet: \"2-5\""),
|
expErr: fmt.Errorf("not all reserved cpus: \"0-1\" are present in defaultCpuSet: \"2-5\""),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "reserved cores 0 & 1 are present in available cpuset with StrictCPUReservationOption enabled",
|
||||||
|
topo: topoDualSocketHT,
|
||||||
|
numReservedCPUs: 2,
|
||||||
|
reserved: cpuset.New(0, 1),
|
||||||
|
cpuPolicyOptions: map[string]string{StrictCPUReservationOption: "true"},
|
||||||
|
stAssignments: state.ContainerCPUAssignments{},
|
||||||
|
stDefaultCPUSet: cpuset.New(0, 1, 2, 3, 4, 5),
|
||||||
|
expErr: fmt.Errorf("some of strictly reserved cpus: \"0-1\" are present in defaultCpuSet: \"0-5\""),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "inconsistency between numReservedCPUs and reserved",
|
description: "inconsistency between numReservedCPUs and reserved",
|
||||||
topo: topoDualSocketHT,
|
topo: topoDualSocketHT,
|
||||||
@@ -984,7 +1027,8 @@ func TestStaticPolicyStartWithResvList(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, testCase := range testCases {
|
for _, testCase := range testCases {
|
||||||
t.Run(testCase.description, func(t *testing.T) {
|
t.Run(testCase.description, func(t *testing.T) {
|
||||||
p, err := NewStaticPolicy(testCase.topo, testCase.numReservedCPUs, testCase.reserved, topologymanager.NewFakeManager(), nil)
|
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, pkgfeatures.CPUManagerPolicyAlphaOptions, true)
|
||||||
|
p, err := NewStaticPolicy(testCase.topo, testCase.numReservedCPUs, testCase.reserved, topologymanager.NewFakeManager(), testCase.cpuPolicyOptions)
|
||||||
if !reflect.DeepEqual(err, testCase.expNewErr) {
|
if !reflect.DeepEqual(err, testCase.expNewErr) {
|
||||||
t.Errorf("StaticPolicy Start() error (%v). expected error: %v but got: %v",
|
t.Errorf("StaticPolicy Start() error (%v). expected error: %v but got: %v",
|
||||||
testCase.description, testCase.expNewErr, err)
|
testCase.description, testCase.expNewErr, err)
|
||||||
|
Reference in New Issue
Block a user