From f6bef07f39dbf2434ed481f091c69d3578fb60b6 Mon Sep 17 00:00:00 2001 From: luomingmeng Date: Tue, 27 Aug 2024 12:05:25 +0800 Subject: [PATCH] add some unit-test --- .../cpu/dynamicpolicy/policy_test.go | 109 ++++++++++++++++++ .../cpu/dynamicpolicy/state/util_test.go | 8 ++ 2 files changed, 117 insertions(+) diff --git a/pkg/agent/qrm-plugins/cpu/dynamicpolicy/policy_test.go b/pkg/agent/qrm-plugins/cpu/dynamicpolicy/policy_test.go index da68acf5e..8dd7c0249 100644 --- a/pkg/agent/qrm-plugins/cpu/dynamicpolicy/policy_test.go +++ b/pkg/agent/qrm-plugins/cpu/dynamicpolicy/policy_test.go @@ -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 @@ -119,6 +124,7 @@ 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 @@ -126,6 +132,7 @@ func getTestDynamicPolicyWithoutInitialization(topology *machine.CPUTopology, st consts.PodAnnotationQoSLevelSharedCores: policyImplement.sharedCoresHintHandler, consts.PodAnnotationQoSLevelDedicatedCores: policyImplement.dedicatedCoresHintHandler, consts.PodAnnotationQoSLevelReclaimedCores: policyImplement.reclaimedCoresHintHandler, + consts.PodAnnotationQoSLevelSystemCores: policyImplement.systemCoresHintHandler, } policyImplement.metaServer = &metaserver.MetaServer{ @@ -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 { diff --git a/pkg/agent/qrm-plugins/cpu/dynamicpolicy/state/util_test.go b/pkg/agent/qrm-plugins/cpu/dynamicpolicy/state/util_test.go index 1cc9b265f..8f731225c 100644 --- a/pkg/agent/qrm-plugins/cpu/dynamicpolicy/state/util_test.go +++ b/pkg/agent/qrm-plugins/cpu/dynamicpolicy/state/util_test.go @@ -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