From 28b9216b42442f24c1cd44c18832eb39ed92e59d Mon Sep 17 00:00:00 2001 From: STRRL Date: Mon, 2 May 2022 23:51:27 +0800 Subject: [PATCH 1/3] test: testcases for pod container manager GetPodContainerName Signed-off-by: STRRL --- .../cm/pod_container_manager_linux_test.go | 160 ++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/pkg/kubelet/cm/pod_container_manager_linux_test.go b/pkg/kubelet/cm/pod_container_manager_linux_test.go index 36a4c1bf581..899e44b33f0 100644 --- a/pkg/kubelet/cm/pod_container_manager_linux_test.go +++ b/pkg/kubelet/cm/pod_container_manager_linux_test.go @@ -20,6 +20,9 @@ limitations under the License. package cm import ( + "github.com/stretchr/testify/assert" + "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "strings" "testing" @@ -126,3 +129,160 @@ func TestIsCgroupPod(t *testing.T) { } } } + +func TestGetPodContainerName(t *testing.T) { + newGuaranteedPodWithUID := func(uid types.UID) *v1.Pod { + return &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + UID: uid, + }, + Spec: v1.PodSpec{ + Containers: []v1.Container{ + { + Name: "container", + Resources: v1.ResourceRequirements{ + Requests: v1.ResourceList{ + v1.ResourceCPU: resource.MustParse("1000m"), + v1.ResourceMemory: resource.MustParse("1G"), + }, + Limits: v1.ResourceList{ + v1.ResourceCPU: resource.MustParse("1000m"), + v1.ResourceMemory: resource.MustParse("1G"), + }, + }, + }, + }, + }, + } + } + newBurstablePodWithUID := func(uid types.UID) *v1.Pod { + return &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + UID: uid, + }, + Spec: v1.PodSpec{ + Containers: []v1.Container{ + { + Name: "container", + Resources: v1.ResourceRequirements{ + Requests: v1.ResourceList{ + v1.ResourceCPU: resource.MustParse("1000m"), + v1.ResourceMemory: resource.MustParse("1G"), + }, + }, + }, + }, + }, + } + } + newBestEffortPodWithUID := func(uid types.UID) *v1.Pod { + return &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + UID: uid, + }, + Spec: v1.PodSpec{ + Containers: []v1.Container{ + { + Name: "container", + }, + }, + }, + } + } + + qosContainersInfo := QOSContainersInfo{ + Guaranteed: RootCgroupName, + Burstable: NewCgroupName(RootCgroupName, strings.ToLower(string(v1.PodQOSBurstable))), + BestEffort: NewCgroupName(RootCgroupName, strings.ToLower(string(v1.PodQOSBestEffort))), + } + + type fields struct { + cgroupManager CgroupManager + } + type args struct { + pod *v1.Pod + } + + tests := []struct { + name string + fields fields + args args + wantCgroupName CgroupName + wantLiteralCgroupfs string + }{ + { + name: "pod with qos guaranteed and cgroupfs", + fields: fields{ + cgroupManager: NewCgroupManager(nil, "cgroupfs"), + }, + args: args{ + pod: newGuaranteedPodWithUID("fake-uid-1"), + }, + wantCgroupName: NewCgroupName(qosContainersInfo.Guaranteed, "podfake-uid-1"), + wantLiteralCgroupfs: NewCgroupName(qosContainersInfo.Guaranteed, "podfake-uid-1").ToCgroupfs(), + }, { + name: "pod with qos guaranteed and systemd", + fields: fields{ + cgroupManager: NewCgroupManager(nil, "systemd"), + }, + args: args{ + pod: newGuaranteedPodWithUID("fake-uid-2"), + }, + wantCgroupName: NewCgroupName(qosContainersInfo.Guaranteed, "podfake-uid-2"), + wantLiteralCgroupfs: NewCgroupName(qosContainersInfo.Guaranteed, "podfake-uid-2").ToSystemd(), + }, { + name: "pod with qos burstable and cgroupfs", + fields: fields{ + cgroupManager: NewCgroupManager(nil, "cgroupfs"), + }, + args: args{ + pod: newBurstablePodWithUID("fake-uid-3"), + }, + wantCgroupName: NewCgroupName(qosContainersInfo.Burstable, "podfake-uid-3"), + wantLiteralCgroupfs: NewCgroupName(qosContainersInfo.Burstable, "podfake-uid-3").ToCgroupfs(), + }, { + name: "pod with qos burstable and systemd", + fields: fields{ + cgroupManager: NewCgroupManager(nil, "systemd"), + }, + args: args{ + pod: newBurstablePodWithUID("fake-uid-4"), + }, + wantCgroupName: NewCgroupName(qosContainersInfo.Burstable, "podfake-uid-4"), + wantLiteralCgroupfs: NewCgroupName(qosContainersInfo.Burstable, "podfake-uid-4").ToSystemd(), + }, { + name: "pod with qos best-effort and cgroupfs", + fields: fields{ + cgroupManager: NewCgroupManager(nil, "cgroupfs"), + }, + args: args{ + pod: newBestEffortPodWithUID("fake-uid-5"), + }, + wantCgroupName: NewCgroupName(qosContainersInfo.BestEffort, "podfake-uid-5"), + wantLiteralCgroupfs: NewCgroupName(qosContainersInfo.BestEffort, "podfake-uid-5").ToCgroupfs(), + }, { + name: "pod with qos best-effort and systemd", + fields: fields{ + cgroupManager: NewCgroupManager(nil, "systemd"), + }, + args: args{ + pod: newBestEffortPodWithUID("fake-uid-6"), + }, + wantCgroupName: NewCgroupName(qosContainersInfo.BestEffort, "podfake-uid-6"), + wantLiteralCgroupfs: NewCgroupName(qosContainersInfo.BestEffort, "podfake-uid-6").ToSystemd(), + }, + } + + for _, tt := range tests { + pcm := &podContainerManagerImpl{ + cgroupManager: tt.fields.cgroupManager, + qosContainersInfo: qosContainersInfo, + } + + t.Run(tt.name, func(t *testing.T) { + actualCgroupName, actualLiteralCgroupfs := pcm.GetPodContainerName(tt.args.pod) + assert.Equalf(t, tt.wantCgroupName, actualCgroupName, "Unexpected cgroup name for pod with UID %s, container resources: %v", tt.args.pod.UID, tt.args.pod.Spec.Containers[0].Resources) + assert.Equalf(t, tt.wantLiteralCgroupfs, actualLiteralCgroupfs, "Unexpected literal cgroupfs for pod with UID %s, container resources: %v", tt.args.pod.UID, tt.args.pod.Spec.Containers[0].Resources) + }) + } +} From 297b6c2995dc93f4f85afa844f47ebedd7bb2a63 Mon Sep 17 00:00:00 2001 From: STRRL Date: Tue, 3 May 2022 00:01:09 +0800 Subject: [PATCH 2/3] test: another test case for TestIsCgroupPod contains reserved word pod Signed-off-by: STRRL --- pkg/kubelet/cm/pod_container_manager_linux_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/kubelet/cm/pod_container_manager_linux_test.go b/pkg/kubelet/cm/pod_container_manager_linux_test.go index 899e44b33f0..0321d099d89 100644 --- a/pkg/kubelet/cm/pod_container_manager_linux_test.go +++ b/pkg/kubelet/cm/pod_container_manager_linux_test.go @@ -102,6 +102,12 @@ func TestIsCgroupPod(t *testing.T) { expectedResult: false, expectedUID: types.UID(""), }, + { + // contains reserved word "pod" in cgroup name + input: NewCgroupName(RootCgroupName, GetPodCgroupNameSuffix("this-uid-contains-reserved-word-pod")), + expectedResult: false, + expectedUID: types.UID(""), + }, } for _, cgroupDriver := range []string{"cgroupfs", "systemd"} { pcm := &podContainerManagerImpl{ From 1dcd0c33487f301402bdf4e42ae9f152778ffed1 Mon Sep 17 00:00:00 2001 From: STRRL Date: Mon, 8 Aug 2022 21:06:38 +0800 Subject: [PATCH 3/3] chore: use require instead of assert Signed-off-by: STRRL --- pkg/kubelet/cm/pod_container_manager_linux_test.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/kubelet/cm/pod_container_manager_linux_test.go b/pkg/kubelet/cm/pod_container_manager_linux_test.go index 0321d099d89..6f59078d3db 100644 --- a/pkg/kubelet/cm/pod_container_manager_linux_test.go +++ b/pkg/kubelet/cm/pod_container_manager_linux_test.go @@ -20,12 +20,13 @@ limitations under the License. package cm import ( - "github.com/stretchr/testify/assert" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "strings" "testing" + "github.com/stretchr/testify/require" + "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" ) @@ -287,8 +288,8 @@ func TestGetPodContainerName(t *testing.T) { t.Run(tt.name, func(t *testing.T) { actualCgroupName, actualLiteralCgroupfs := pcm.GetPodContainerName(tt.args.pod) - assert.Equalf(t, tt.wantCgroupName, actualCgroupName, "Unexpected cgroup name for pod with UID %s, container resources: %v", tt.args.pod.UID, tt.args.pod.Spec.Containers[0].Resources) - assert.Equalf(t, tt.wantLiteralCgroupfs, actualLiteralCgroupfs, "Unexpected literal cgroupfs for pod with UID %s, container resources: %v", tt.args.pod.UID, tt.args.pod.Spec.Containers[0].Resources) + require.Equalf(t, tt.wantCgroupName, actualCgroupName, "Unexpected cgroup name for pod with UID %s, container resources: %v", tt.args.pod.UID, tt.args.pod.Spec.Containers[0].Resources) + require.Equalf(t, tt.wantLiteralCgroupfs, actualLiteralCgroupfs, "Unexpected literal cgroupfs for pod with UID %s, container resources: %v", tt.args.pod.UID, tt.args.pod.Spec.Containers[0].Resources) }) } }