From f8e9c1161a79e469af6397d569008f375b216643 Mon Sep 17 00:00:00 2001 From: SP12893678 <36910625+SP12893678@users.noreply.github.com> Date: Wed, 28 Aug 2024 20:07:42 +0200 Subject: [PATCH] [YUNIKORN-2842] Improve metadata & gang_utils funtion's test coverage (#903) Closes: #903 Signed-off-by: Peter Bacsko --- pkg/cache/gang_utils_test.go | 1 + pkg/cache/metadata_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/pkg/cache/gang_utils_test.go b/pkg/cache/gang_utils_test.go index 4c25b982d..480dcf590 100644 --- a/pkg/cache/gang_utils_test.go +++ b/pkg/cache/gang_utils_test.go @@ -150,6 +150,7 @@ func Test_GetPlaceholderResourceRequest(t *testing.T) { {"base", map[string]resource.Quantity{"pods": resource.MustParse("1")}, v1.ResourceList{"pods": resource.MustParse("1")}}, {"hugepages", map[string]resource.Quantity{"hugepages-huge": resource.MustParse("2")}, v1.ResourceList{"hugepages-huge": resource.MustParse("2")}}, {"mixed", map[string]resource.Quantity{"pods": resource.MustParse("4"), "nvidia.com/gpu": resource.MustParse("5")}, v1.ResourceList{"pods": resource.MustParse("4"), "nvidia.com/gpu": resource.MustParse("5")}}, + {"empty key", map[string]resource.Quantity{"": resource.MustParse("1")}, v1.ResourceList{}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/cache/metadata_test.go b/pkg/cache/metadata_test.go index 755ff65d3..d4760e2d1 100644 --- a/pkg/cache/metadata_test.go +++ b/pkg/cache/metadata_test.go @@ -99,9 +99,16 @@ func TestGetTaskMetadata(t *testing.T) { assert.Equal(t, task.ApplicationID, "yunikorn-default-autogen") assert.Equal(t, task.TaskID, "UID-POD-00001") assert.Equal(t, task.TaskGroupName, "") + + // case: empty pod + task, ok = getTaskMetadata(&v1.Pod{}) + assert.Equal(t, ok, false) + assert.DeepEqual(t, task, TaskMetadata{}) } func TestGetAppMetadata(t *testing.T) { //nolint:funlen + conf.GetSchedulerConf().SetTestMode(true) + defer utils.SetPluginMode(false) defer func() { conf.GetSchedulerConf().GenerateUniqueAppIds = false }() utils.SetPluginMode(false) @@ -301,6 +308,33 @@ func TestGetAppMetadata(t *testing.T) { //nolint:funlen utils.SetPluginMode(true) app, ok = getAppMetadata(&pod) assert.Equal(t, ok, false) + + // case: invalid annotation task groups + pod = v1.Pod{ + TypeMeta: apis.TypeMeta{ + Kind: "Pod", + APIVersion: "v1", + }, + ObjectMeta: apis.ObjectMeta{ + Name: "pod00001", + Namespace: "default", + UID: "UID-POD-00001", + Labels: map[string]string{ + "applicationId": "app00001", + "queue": "root.a", + }, + Annotations: map[string]string{ + constants.AnnotationTaskGroups: "{ name:\"\" }", + }, + }, + Spec: v1.PodSpec{SchedulerName: constants.SchedulerName}, + Status: v1.PodStatus{Phase: v1.PodPending}, + } + app, ok = getAppMetadata(&pod) + assert.Equal(t, ok, true) + assert.Equal(t, app.SchedulingPolicyParameters.GetGangSchedulingStyle(), "Soft") + assert.Equal(t, app.Tags[common.AppTagCreateForce], "false") + assert.Equal(t, len(app.TaskGroups), 0) } func TestGetOwnerReferences(t *testing.T) {