Skip to content

Commit

Permalink
add some unit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
luomingmeng committed Aug 27, 2024
1 parent 207a437 commit f6bef07
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
109 changes: 109 additions & 0 deletions pkg/agent/qrm-plugins/cpu/dynamicpolicy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ func getTestDynamicPolicyWithInitialization(topology *machine.CPUTopology, state
return nil, err
}

err = dynamicPolicy.initReservePool()
if err != nil {
return nil, err
}

err = dynamicPolicy.initReclaimPool()
if err != nil {
return nil, err
Expand Down Expand Up @@ -119,13 +124,15 @@ func getTestDynamicPolicyWithoutInitialization(topology *machine.CPUTopology, st
consts.PodAnnotationQoSLevelSharedCores: policyImplement.sharedCoresAllocationHandler,
consts.PodAnnotationQoSLevelDedicatedCores: policyImplement.dedicatedCoresAllocationHandler,
consts.PodAnnotationQoSLevelReclaimedCores: policyImplement.reclaimedCoresAllocationHandler,
consts.PodAnnotationQoSLevelSystemCores: policyImplement.systemCoresAllocationHandler,
}

// register hint providers for pods with different QoS level
policyImplement.hintHandlers = map[string]util.HintHandler{
consts.PodAnnotationQoSLevelSharedCores: policyImplement.sharedCoresHintHandler,
consts.PodAnnotationQoSLevelDedicatedCores: policyImplement.dedicatedCoresHintHandler,
consts.PodAnnotationQoSLevelReclaimedCores: policyImplement.reclaimedCoresHintHandler,
consts.PodAnnotationQoSLevelSystemCores: policyImplement.systemCoresHintHandler,
}

policyImplement.metaServer = &metaserver.MetaServer{
Expand Down Expand Up @@ -894,6 +901,108 @@ func TestAllocate(t *testing.T) {
},
cpuTopology: cpuTopology,
},
{
description: "req for system_cores with specified cpuset pool",
req: &pluginapi.ResourceRequest{
PodUid: string(uuid.NewUUID()),
PodNamespace: testName,
PodName: testName,
ContainerName: testName,
ContainerType: pluginapi.ContainerType_MAIN,
ContainerIndex: 0,
ResourceName: string(v1.ResourceCPU),
ResourceRequests: map[string]float64{
string(v1.ResourceCPU): 0,
},
Labels: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSystemCores,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSystemCores,
consts.PodAnnotationCPUEnhancementKey: `{"cpuset_pool": "reserve"}`,
},
},
expectedResp: &pluginapi.ResourceAllocationResponse{
PodNamespace: testName,
PodName: testName,
ContainerName: testName,
ContainerType: pluginapi.ContainerType_MAIN,
ContainerIndex: 0,
ResourceName: string(v1.ResourceCPU),
AllocationResult: &pluginapi.ResourceAllocation{
ResourceAllocation: map[string]*pluginapi.ResourceAllocationInfo{
string(v1.ResourceCPU): {
OciPropertyName: util.OCIPropertyNameCPUSetCPUs,
IsNodeResource: false,
IsScalarResource: true,
AllocatedQuantity: 2, // reserve pool
AllocationResult: machine.NewCPUSet(0, 2).String(),
ResourceHints: &pluginapi.ListOfTopologyHints{
Hints: []*pluginapi.TopologyHint{nil},
},
},
},
},
Labels: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSystemCores,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSystemCores,
consts.PodAnnotationCPUEnhancementCPUSet: "reserve",
},
},
cpuTopology: cpuTopology,
},
{
description: "req for system_cores without specified cpuset pool",
req: &pluginapi.ResourceRequest{
PodUid: string(uuid.NewUUID()),
PodNamespace: testName,
PodName: testName,
ContainerName: testName,
ContainerType: pluginapi.ContainerType_MAIN,
ContainerIndex: 0,
ResourceName: string(v1.ResourceCPU),
ResourceRequests: map[string]float64{
string(v1.ResourceCPU): 0,
},
Labels: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSystemCores,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSystemCores,
},
},
expectedResp: &pluginapi.ResourceAllocationResponse{
PodNamespace: testName,
PodName: testName,
ContainerName: testName,
ContainerType: pluginapi.ContainerType_MAIN,
ContainerIndex: 0,
ResourceName: string(v1.ResourceCPU),
AllocationResult: &pluginapi.ResourceAllocation{
ResourceAllocation: map[string]*pluginapi.ResourceAllocationInfo{
string(v1.ResourceCPU): {
OciPropertyName: util.OCIPropertyNameCPUSetCPUs,
IsNodeResource: false,
IsScalarResource: true,
AllocatedQuantity: float64(cpuTopology.CPUDetails.CPUs().Size()), // default for all cpuset
AllocationResult: cpuTopology.CPUDetails.CPUs().String(),
ResourceHints: &pluginapi.ListOfTopologyHints{
Hints: []*pluginapi.TopologyHint{nil},
},
},
},
},
Labels: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSystemCores,
},
Annotations: map[string]string{
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSystemCores,
},
},
cpuTopology: cpuTopology,
},
}

for _, tc := range testCases {
Expand Down
8 changes: 8 additions & 0 deletions pkg/agent/qrm-plugins/cpu/dynamicpolicy/state/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,14 @@ func TestGetSpecifiedPoolName(t *testing.T) {
},
want: PoolNameReclaim,
},
{
name: "system_cores with empty cpusetEnhancementValue",
args: args{
qosLevel: consts.PodAnnotationQoSLevelSystemCores,
cpusetEnhancementValue: "reserve",
},
want: "reserve",
},
}
for _, tt := range tests {
tt := tt
Expand Down

0 comments on commit f6bef07

Please sign in to comment.