@@ -41,11 +41,10 @@ type staticPolicyTest struct {
41
41
expErr error
42
42
expCPUAlloc bool
43
43
expCSet cpuset.CPUSet
44
- expPanic bool
45
44
}
46
45
47
46
func TestStaticPolicyName (t * testing.T ) {
48
- policy := NewStaticPolicy (topoSingleSocketHT , 1 , cpuset .NewCPUSet (), topologymanager .NewFakeManager ())
47
+ policy , _ := NewStaticPolicy (topoSingleSocketHT , 1 , cpuset .NewCPUSet (), topologymanager .NewFakeManager ())
49
48
50
49
policyName := policy .Name ()
51
50
if policyName != "static" {
@@ -81,7 +80,7 @@ func TestStaticPolicyStart(t *testing.T) {
81
80
numReservedCPUs : 2 ,
82
81
stAssignments : state.ContainerCPUAssignments {},
83
82
stDefaultCPUSet : cpuset .NewCPUSet (0 , 1 ),
84
- expPanic : true ,
83
+ expErr : fmt . Errorf ( "not all reserved cpus: \" 0,6 \" are present in defaultCpuSet: \" 0-1 \" " ) ,
85
84
},
86
85
{
87
86
description : "assigned core 2 is still present in available cpuset" ,
@@ -92,7 +91,7 @@ func TestStaticPolicyStart(t *testing.T) {
92
91
},
93
92
},
94
93
stDefaultCPUSet : cpuset .NewCPUSet (2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ),
95
- expPanic : true ,
94
+ expErr : fmt . Errorf ( "pod: fakePod, container: 0 cpuset: \" 0-2 \" overlaps with default cpuset \" 2-11 \" " ) ,
96
95
},
97
96
{
98
97
description : "core 12 is not present in topology but is in state cpuset" ,
@@ -104,7 +103,7 @@ func TestStaticPolicyStart(t *testing.T) {
104
103
},
105
104
},
106
105
stDefaultCPUSet : cpuset .NewCPUSet (5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ),
107
- expPanic : true ,
106
+ expErr : fmt . Errorf ( "current set of available CPUs \" 0-11 \" doesn't match with CPUs in state \" 0-12 \" " ) ,
108
107
},
109
108
{
110
109
description : "core 11 is present in topology but is not in state cpuset" ,
@@ -116,26 +115,25 @@ func TestStaticPolicyStart(t *testing.T) {
116
115
},
117
116
},
118
117
stDefaultCPUSet : cpuset .NewCPUSet (5 , 6 , 7 , 8 , 9 , 10 ),
119
- expPanic : true ,
118
+ expErr : fmt . Errorf ( "current set of available CPUs \" 0-11 \" doesn't match with CPUs in state \" 0-10 \" " ) ,
120
119
},
121
120
}
122
121
for _ , testCase := range testCases {
123
122
t .Run (testCase .description , func (t * testing.T ) {
124
- defer func () {
125
- if err := recover (); err != nil {
126
- if ! testCase .expPanic {
127
- t .Errorf ("unexpected panic occurred: %q" , err )
128
- }
129
- } else if testCase .expPanic {
130
- t .Error ("expected panic doesn't occurred" )
131
- }
132
- }()
133
- policy := NewStaticPolicy (testCase .topo , testCase .numReservedCPUs , cpuset .NewCPUSet (), topologymanager .NewFakeManager ()).(* staticPolicy )
123
+ p , _ := NewStaticPolicy (testCase .topo , testCase .numReservedCPUs , cpuset .NewCPUSet (), topologymanager .NewFakeManager ())
124
+ policy := p .(* staticPolicy )
134
125
st := & mockState {
135
126
assignments : testCase .stAssignments ,
136
127
defaultCPUSet : testCase .stDefaultCPUSet ,
137
128
}
138
- policy .Start (st )
129
+ err := policy .Start (st )
130
+ if ! reflect .DeepEqual (err , testCase .expErr ) {
131
+ t .Errorf ("StaticPolicy Start() error (%v). expected error: %v but got: %v" ,
132
+ testCase .description , testCase .expErr , err )
133
+ }
134
+ if err != nil {
135
+ return
136
+ }
139
137
140
138
if ! testCase .stDefaultCPUSet .IsEmpty () {
141
139
for cpuid := 1 ; cpuid < policy .topology .NumCPUs ; cpuid ++ {
@@ -438,7 +436,7 @@ func TestStaticPolicyAdd(t *testing.T) {
438
436
}
439
437
440
438
for _ , testCase := range testCases {
441
- policy := NewStaticPolicy (testCase .topo , testCase .numReservedCPUs , cpuset .NewCPUSet (), topologymanager .NewFakeManager ())
439
+ policy , _ := NewStaticPolicy (testCase .topo , testCase .numReservedCPUs , cpuset .NewCPUSet (), topologymanager .NewFakeManager ())
442
440
443
441
st := & mockState {
444
442
assignments : testCase .stAssignments ,
@@ -539,7 +537,7 @@ func TestStaticPolicyRemove(t *testing.T) {
539
537
}
540
538
541
539
for _ , testCase := range testCases {
542
- policy := NewStaticPolicy (testCase .topo , testCase .numReservedCPUs , cpuset .NewCPUSet (), topologymanager .NewFakeManager ())
540
+ policy , _ := NewStaticPolicy (testCase .topo , testCase .numReservedCPUs , cpuset .NewCPUSet (), topologymanager .NewFakeManager ())
543
541
544
542
st := & mockState {
545
543
assignments : testCase .stAssignments ,
@@ -629,12 +627,17 @@ func TestTopologyAwareAllocateCPUs(t *testing.T) {
629
627
},
630
628
}
631
629
for _ , tc := range testCases {
632
- policy := NewStaticPolicy (tc .topo , 0 , cpuset .NewCPUSet (), topologymanager .NewFakeManager ()).(* staticPolicy )
630
+ p , _ := NewStaticPolicy (tc .topo , 0 , cpuset .NewCPUSet (), topologymanager .NewFakeManager ())
631
+ policy := p .(* staticPolicy )
633
632
st := & mockState {
634
633
assignments : tc .stAssignments ,
635
634
defaultCPUSet : tc .stDefaultCPUSet ,
636
635
}
637
- policy .Start (st )
636
+ err := policy .Start (st )
637
+ if err != nil {
638
+ t .Errorf ("StaticPolicy Start() error (%v)" , err )
639
+ continue
640
+ }
638
641
639
642
cset , err := policy .allocateCPUs (st , tc .numRequested , tc .socketMask )
640
643
if err != nil {
@@ -661,9 +664,9 @@ type staticPolicyTestWithResvList struct {
661
664
stDefaultCPUSet cpuset.CPUSet
662
665
pod * v1.Pod
663
666
expErr error
667
+ expNewErr error
664
668
expCPUAlloc bool
665
669
expCSet cpuset.CPUSet
666
- expPanic bool
667
670
}
668
671
669
672
func TestStaticPolicyStartWithResvList (t * testing.T ) {
@@ -684,7 +687,7 @@ func TestStaticPolicyStartWithResvList(t *testing.T) {
684
687
reserved : cpuset .NewCPUSet (0 , 1 ),
685
688
stAssignments : state.ContainerCPUAssignments {},
686
689
stDefaultCPUSet : cpuset .NewCPUSet (2 , 3 , 4 , 5 ),
687
- expPanic : true ,
690
+ expErr : fmt . Errorf ( "not all reserved cpus: \" 0-1 \" are present in defaultCpuSet: \" 2-5 \" " ) ,
688
691
},
689
692
{
690
693
description : "inconsistency between numReservedCPUs and reserved" ,
@@ -693,26 +696,32 @@ func TestStaticPolicyStartWithResvList(t *testing.T) {
693
696
reserved : cpuset .NewCPUSet (0 , 1 ),
694
697
stAssignments : state.ContainerCPUAssignments {},
695
698
stDefaultCPUSet : cpuset .NewCPUSet (0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ),
696
- expPanic : true ,
699
+ expNewErr : fmt . Errorf ( "[cpumanager] unable to reserve the required amount of CPUs (size of 0-1 did not equal 1)" ) ,
697
700
},
698
701
}
699
702
for _ , testCase := range testCases {
700
703
t .Run (testCase .description , func (t * testing.T ) {
701
- defer func () {
702
- if err := recover (); err != nil {
703
- if ! testCase .expPanic {
704
- t .Errorf ("unexpected panic occurred: %q" , err )
705
- }
706
- } else if testCase .expPanic {
707
- t .Error ("expected panic doesn't occurred" )
708
- }
709
- }()
710
- policy := NewStaticPolicy (testCase .topo , testCase .numReservedCPUs , testCase .reserved , topologymanager .NewFakeManager ()).(* staticPolicy )
704
+ p , err := NewStaticPolicy (testCase .topo , testCase .numReservedCPUs , testCase .reserved , topologymanager .NewFakeManager ())
705
+ if ! reflect .DeepEqual (err , testCase .expNewErr ) {
706
+ t .Errorf ("StaticPolicy Start() error (%v). expected error: %v but got: %v" ,
707
+ testCase .description , testCase .expNewErr , err )
708
+ }
709
+ if err != nil {
710
+ return
711
+ }
712
+ policy := p .(* staticPolicy )
711
713
st := & mockState {
712
714
assignments : testCase .stAssignments ,
713
715
defaultCPUSet : testCase .stDefaultCPUSet ,
714
716
}
715
- policy .Start (st )
717
+ err = policy .Start (st )
718
+ if ! reflect .DeepEqual (err , testCase .expErr ) {
719
+ t .Errorf ("StaticPolicy Start() error (%v). expected error: %v but got: %v" ,
720
+ testCase .description , testCase .expErr , err )
721
+ }
722
+ if err != nil {
723
+ return
724
+ }
716
725
717
726
if ! st .GetDefaultCPUSet ().Equals (testCase .expCSet ) {
718
727
t .Errorf ("State CPUSet is different than expected. Have %q wants: %q" , st .GetDefaultCPUSet (),
@@ -769,7 +778,7 @@ func TestStaticPolicyAddWithResvList(t *testing.T) {
769
778
}
770
779
771
780
for _ , testCase := range testCases {
772
- policy := NewStaticPolicy (testCase .topo , testCase .numReservedCPUs , testCase .reserved , topologymanager .NewFakeManager ())
781
+ policy , _ := NewStaticPolicy (testCase .topo , testCase .numReservedCPUs , testCase .reserved , topologymanager .NewFakeManager ())
773
782
774
783
st := & mockState {
775
784
assignments : testCase .stAssignments ,
0 commit comments