Skip to content

Commit acb8c90

Browse files
committed
chore: add unit tests
chore: add unit tests
1 parent 2b20f65 commit acb8c90

4 files changed

Lines changed: 133 additions & 12 deletions

File tree

pkg/agent/qrm-plugins/cpu/dynamicpolicy/cpuburst/manager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import (
2020
"context"
2121
"fmt"
2222

23+
v1 "k8s.io/api/core/v1"
24+
utilerrors "k8s.io/apimachinery/pkg/util/errors"
25+
2326
"github.com/kubewharf/katalyst-api/pkg/consts"
2427
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/util"
2528
"github.com/kubewharf/katalyst-core/pkg/config/generic"
@@ -29,8 +32,6 @@ import (
2932
"github.com/kubewharf/katalyst-core/pkg/util/general"
3033
"github.com/kubewharf/katalyst-core/pkg/util/native"
3134
qosutil "github.com/kubewharf/katalyst-core/pkg/util/qos"
32-
v1 "k8s.io/api/core/v1"
33-
utilerrors "k8s.io/apimachinery/pkg/util/errors"
3435
)
3536

3637
type Manager interface {
@@ -104,7 +105,6 @@ func (m *managerImpl) updateCPUBurstForStaticPolicy(percent float64, pod *v1.Pod
104105
continue
105106
} else if !exist {
106107
general.Infof("container cgroup does not exist, pod: %s, container: %s(%s)", podUID, containerName, containerID)
107-
errList = append(errList, err)
108108
continue
109109
}
110110

pkg/agent/qrm-plugins/cpu/dynamicpolicy/cpuburst/manager_test.go

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package cpuburst
1818

1919
import (
20+
"fmt"
2021
"testing"
2122

2223
"github.com/bytedance/mockey"
@@ -242,12 +243,128 @@ func TestManagerImpl_UpdateCPUBurst(t *testing.T) {
242243
"/sys/fs/cgroup/cpu/test-pod-2/test-container-2-id": 50,
243244
},
244245
},
246+
{
247+
name: "get container ID fails returns error",
248+
pods: []*v1.Pod{
249+
{
250+
ObjectMeta: metav1.ObjectMeta{
251+
UID: "test-pod",
252+
Annotations: map[string]string{
253+
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelDedicatedCores,
254+
consts.PodAnnotationCPUEnhancementKey: `{"cpu_burst_policy":"static", "cpu_burst_percent":"50"}`,
255+
},
256+
},
257+
Spec: v1.PodSpec{
258+
Containers: []v1.Container{
259+
{
260+
Name: "test-container-1",
261+
},
262+
{
263+
Name: "test-container-2",
264+
},
265+
},
266+
},
267+
},
268+
},
269+
wantErr: true,
270+
},
271+
{
272+
name: "Checking container cgroup exists fails, returns error",
273+
pods: []*v1.Pod{
274+
{
275+
ObjectMeta: metav1.ObjectMeta{
276+
UID: "test-pod-1",
277+
Annotations: map[string]string{
278+
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelDedicatedCores,
279+
consts.PodAnnotationCPUEnhancementKey: `{"cpu_burst_policy":"static", "cpu_burst_percent":"50"}`,
280+
},
281+
},
282+
Spec: v1.PodSpec{
283+
Containers: []v1.Container{
284+
{
285+
Name: "test-container-1",
286+
},
287+
{
288+
Name: "test-container-2",
289+
},
290+
},
291+
},
292+
Status: v1.PodStatus{
293+
ContainerStatuses: []v1.ContainerStatus{
294+
{
295+
Name: "test-container-1",
296+
ContainerID: "test-container-1-id",
297+
},
298+
{
299+
Name: "test-container-2",
300+
ContainerID: "test-container-2-id",
301+
},
302+
},
303+
},
304+
},
305+
},
306+
mocks: func(s resultState) {
307+
mockey.Mock(common.IsContainerCgroupExist).Return(false, fmt.Errorf("test error")).Build()
308+
},
309+
wantErr: true,
310+
},
311+
{
312+
name: "Container cgroup path does not exist, just skip",
313+
pods: []*v1.Pod{
314+
{
315+
ObjectMeta: metav1.ObjectMeta{
316+
UID: "test-pod-1",
317+
Annotations: map[string]string{
318+
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelDedicatedCores,
319+
consts.PodAnnotationCPUEnhancementKey: `{"cpu_burst_policy":"static", "cpu_burst_percent":"50"}`,
320+
},
321+
},
322+
Spec: v1.PodSpec{
323+
Containers: []v1.Container{
324+
{
325+
Name: "test-container-1",
326+
},
327+
{
328+
Name: "test-container-2",
329+
},
330+
},
331+
},
332+
Status: v1.PodStatus{
333+
ContainerStatuses: []v1.ContainerStatus{
334+
{
335+
Name: "test-container-1",
336+
ContainerID: "test-container-1-id",
337+
},
338+
{
339+
Name: "test-container-2",
340+
ContainerID: "test-container-2-id",
341+
},
342+
},
343+
},
344+
},
345+
},
346+
mocks: func(s resultState) {
347+
mockey.Mock(common.IsContainerCgroupExist).Return(false, nil).Build()
348+
mockey.Mock(common.GetContainerAbsCgroupPath).To(func(_, podUID, containerID string) (string, error) {
349+
return "/sys/fs/cgroup/cpu/" + podUID + "/" + containerID, nil
350+
}).Build()
351+
mockey.Mock(manager.GetCPUWithAbsolutePath).Return(&common.CPUStats{CpuQuota: 200}, nil).Build()
352+
mockey.Mock(manager.ApplyCPUWithAbsolutePath).
353+
To(func(absPath string, cpuData *common.CPUData) error {
354+
s[absPath] = cpuData.CpuBurst
355+
return nil
356+
}).Build()
357+
},
358+
wantResults: make(resultState),
359+
},
245360
}
246361

247362
for _, tt := range tests {
248363
mockey.PatchConvey(tt.name, t, func() {
249364
results := make(resultState)
250-
tt.mocks(results)
365+
if tt.mocks != nil {
366+
tt.mocks(results)
367+
}
251368

252369
cpuBurstManager := NewManager(generateTestMetaServer(tt.pods))
253370

@@ -257,7 +374,9 @@ func TestManagerImpl_UpdateCPUBurst(t *testing.T) {
257374
return
258375
}
259376

260-
assert.Equal(t, tt.wantResults, results)
377+
if !tt.wantErr {
378+
assert.Equal(t, tt.wantResults, results)
379+
}
261380
})
262381
}
263382
}

pkg/agent/qrm-plugins/cpu/dynamicpolicy/policy.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ const (
7373

7474
reservedReclaimedCPUsSize = 4
7575

76-
cpusetCheckPeriod = 10 * time.Second
77-
stateCheckPeriod = 30 * time.Second
78-
maxResidualTime = 5 * time.Minute
79-
syncCPUIdlePeriod = 30 * time.Second
76+
cpusetCheckPeriod = 10 * time.Second
77+
stateCheckPeriod = 30 * time.Second
78+
maxResidualTime = 5 * time.Minute
79+
syncCPUIdlePeriod = 30 * time.Second
80+
syncCPUBurstPeriod = 10 * time.Second
8081

8182
healthCheckTolerationTimes = 3
8283
)
@@ -370,7 +371,7 @@ func (p *DynamicPolicy) Start() (err error) {
370371
general.Infof("cpu burst is enabled")
371372

372373
err = periodicalhandler.RegisterPeriodicalHandlerWithHealthz(cpuconsts.SyncCPUBurst, general.HealthzCheckStateNotReady,
373-
qrm.QRMCPUPluginPeriodicalHandlerGroupName, p.syncCPUBurst, syncCPUIdlePeriod, healthCheckTolerationTimes)
374+
qrm.QRMCPUPluginPeriodicalHandlerGroupName, p.syncCPUBurst, syncCPUBurstPeriod, healthCheckTolerationTimes)
374375
if err != nil {
375376
general.Errorf("start %v failed,err:%v", cpuconsts.SyncCPUBurst, err)
376377
}

pkg/util/cgroup/common/cgroup_linux.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ import (
3232
"time"
3333

3434
"github.com/google/cadvisor/container/libcontainer"
35-
"github.com/kubewharf/katalyst-core/pkg/consts"
36-
"github.com/kubewharf/katalyst-core/pkg/util/eventbus"
3735
"github.com/opencontainers/runc/libcontainer/cgroups"
3836
"github.com/opencontainers/runc/libcontainer/configs"
37+
38+
"github.com/kubewharf/katalyst-core/pkg/consts"
39+
"github.com/kubewharf/katalyst-core/pkg/util/eventbus"
3940
)
4041

4142
// CheckCgroup2UnifiedMode return whether it is in cgroupv2 env

0 commit comments

Comments
 (0)