Use file-backed state for all cpumanager policies

- Add unit test to verify policy name mismatch behavior.
This commit is contained in:
Connor Doyle 2017-11-15 20:30:19 -08:00
parent d6325933e1
commit c95ee34234
2 changed files with 31 additions and 6 deletions

View File

@ -106,13 +106,11 @@ func NewManager(
stateFileDirecory string,
) (Manager, error) {
var policy Policy
var stateHandle state.State
switch policyName(cpuPolicyName) {
case PolicyNone:
policy = NewNonePolicy()
stateHandle = state.NewMemoryState()
case PolicyStatic:
topo, err := topology.Discover(machineInfo)
@ -141,18 +139,20 @@ func NewManager(
reservedCPUsFloat := float64(reservedCPUs.MilliValue()) / 1000
numReservedCPUs := int(math.Ceil(reservedCPUsFloat))
policy = NewStaticPolicy(topo, numReservedCPUs)
stateHandle = state.NewFileState(path.Join(stateFileDirecory, CPUManagerStateFileName), policy.Name())
default:
glog.Errorf("[cpumanager] Unknown policy \"%s\", falling back to default policy \"%s\"", cpuPolicyName, PolicyNone)
policy = NewNonePolicy()
stateHandle = state.NewMemoryState()
}
stateImpl := state.NewFileState(
path.Join(stateFileDirecory, CPUManagerStateFileName),
policy.Name())
manager := &manager{
policy: policy,
reconcilePeriod: reconcilePeriod,
state: stateHandle,
state: stateImpl,
machineInfo: machineInfo,
nodeAllocatableReservation: nodeAllocatableReservation,
}

View File

@ -75,12 +75,14 @@ func TestFileStateTryRestore(t *testing.T) {
testCases := []struct {
description string
stateFileContent string
policyName string
expErr string
expectedState *stateMemory
}{
{
"Invalid JSON - empty file",
"\n",
"none",
"state file: could not unmarshal, corrupted state file",
&stateMemory{
assignments: ContainerCPUAssignments{},
@ -90,6 +92,7 @@ func TestFileStateTryRestore(t *testing.T) {
{
"Invalid JSON - invalid content",
"{",
"none",
"state file: could not unmarshal, corrupted state file",
&stateMemory{
assignments: ContainerCPUAssignments{},
@ -99,6 +102,7 @@ func TestFileStateTryRestore(t *testing.T) {
{
"Try restore defaultCPUSet only",
`{"policyName": "none", "defaultCpuSet": "4-6"}`,
"none",
"",
&stateMemory{
assignments: ContainerCPUAssignments{},
@ -108,6 +112,7 @@ func TestFileStateTryRestore(t *testing.T) {
{
"Try restore defaultCPUSet only - invalid name",
`{"policyName": "none", "defaultCpuSet" "4-6"}`,
"none",
"",
&stateMemory{
assignments: ContainerCPUAssignments{},
@ -123,6 +128,7 @@ func TestFileStateTryRestore(t *testing.T) {
"container2": "1-3"
}
}`,
"none",
"",
&stateMemory{
assignments: ContainerCPUAssignments{
@ -132,9 +138,24 @@ func TestFileStateTryRestore(t *testing.T) {
defaultCPUSet: cpuset.NewCPUSet(),
},
},
{
"Try restore invalid policy name",
`{
"policyName": "A",
"defaultCpuSet": "0-7",
"entries": {}
}`,
"B",
"policy configured \"B\" != policy from state file \"A\"",
&stateMemory{
assignments: ContainerCPUAssignments{},
defaultCPUSet: cpuset.NewCPUSet(),
},
},
{
"Try restore invalid assignments",
`{"entries": }`,
"none",
"state file: could not unmarshal, corrupted state file",
&stateMemory{
assignments: ContainerCPUAssignments{},
@ -151,6 +172,7 @@ func TestFileStateTryRestore(t *testing.T) {
"container2": "1-3"
}
}`,
"none",
"",
&stateMemory{
assignments: ContainerCPUAssignments{
@ -166,6 +188,7 @@ func TestFileStateTryRestore(t *testing.T) {
"policyName": "none",
"defaultCpuSet": "2-sd"
}`,
"none",
"state file: could not parse state file",
&stateMemory{
assignments: ContainerCPUAssignments{},
@ -182,6 +205,7 @@ func TestFileStateTryRestore(t *testing.T) {
"container2": "1-3"
}
}`,
"none",
"state file: could not parse state file",
&stateMemory{
assignments: ContainerCPUAssignments{},
@ -191,6 +215,7 @@ func TestFileStateTryRestore(t *testing.T) {
{
"TryRestoreState creates empty state file",
"",
"none",
"",
&stateMemory{
assignments: ContainerCPUAssignments{},
@ -214,7 +239,7 @@ func TestFileStateTryRestore(t *testing.T) {
defer os.Remove(sfilePath.Name())
logData, fileState := stderrCapture(t, func() State {
return NewFileState(sfilePath.Name(), "none")
return NewFileState(sfilePath.Name(), tc.policyName)
})
if tc.expErr != "" {