Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[YUNIKORN-2851] Improve placeholder & placeholder_manger funtion's test coverage #905

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions pkg/cache/placeholder_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,16 @@ func TestCleanUp(t *testing.T) {
UID: "UID-03",
},
}
pod4 := &v1.Pod{
TypeMeta: apis.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: apis.ObjectMeta{
Name: "pod-04",
UID: "UID-04",
},
}
taskID1 := "task01"
task1 := NewTask(taskID1, app, mockedContext, pod1)
task1.placeholder = true
Expand All @@ -268,12 +278,19 @@ func TestCleanUp(t *testing.T) {
task3 := NewTask(taskID3, app, mockedContext, pod3)
task3.placeholder = false
app.taskMap[taskID3] = task3
taskID4 := "task04"
task4 := NewTask(taskID4, app, mockedContext, pod4)
task4.placeholder = true
app.taskMap[taskID4] = task4
res = app.getNonTerminatedTaskAlias()
assert.Equal(t, len(res), 3)
assert.Equal(t, len(res), 4)

deletePod := make([]string, 0)
mockedAPIProvider := client.NewMockedAPIProvider(false)
mockedAPIProvider.MockDeleteFn(func(pod *v1.Pod) error {
if pod.Name == "pod-04" {
return fmt.Errorf("error")
}
deletePod = append(deletePod, pod.Name)
return nil
})
Expand All @@ -290,11 +307,17 @@ func TestCleanUp(t *testing.T) {
}
}
assert.Equal(t, exist, false)
assert.Equal(t, len(placeholderMgr.orphanPods), 0)
assert.Equal(t, len(placeholderMgr.orphanPods), 1)
}

func TestCleanOrphanPlaceholders(t *testing.T) {
mockedAPIProvider := client.NewMockedAPIProvider(false)
mockedAPIProvider.MockDeleteFn(func(pod *v1.Pod) error {
if pod.Name == "pod-02" {
return fmt.Errorf("error")
}
return nil
})
placeholderMgr := NewPlaceholderManager(mockedAPIProvider.GetAPIs())
pod1 := &v1.Pod{
TypeMeta: apis.TypeMeta{
Expand All @@ -306,10 +329,21 @@ func TestCleanOrphanPlaceholders(t *testing.T) {
UID: "UID-01",
},
}
pod2 := &v1.Pod{
TypeMeta: apis.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: apis.ObjectMeta{
Name: "pod-02",
UID: "UID-02",
},
}
placeholderMgr.orphanPods["task01"] = pod1
assert.Equal(t, len(placeholderMgr.orphanPods), 1)
placeholderMgr.orphanPods["task02"] = pod2
assert.Equal(t, len(placeholderMgr.orphanPods), 2)
placeholderMgr.cleanOrphanPlaceholders()
assert.Equal(t, len(placeholderMgr.orphanPods), 0)
assert.Equal(t, len(placeholderMgr.orphanPods), 1)
}

func TestPlaceholderManagerStartStop(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/cache/placeholder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func TestNewPlaceholder(t *testing.T) {
testGroups, map[string]string{constants.AppTagNamespace: namespace, constants.AppTagImagePullSecrets: "secret1,secret2"},
mockedSchedulerAPI)
app.setTaskGroups(taskGroups)
app.setSchedulingParamsDefinition("gangSchedulingStyle=Soft")
marshalledTaskGroups, err := json.Marshal(taskGroups)
assert.NilError(t, err, "taskGroups marshalling failed")
app.setTaskGroupsDefinition(string(marshalledTaskGroups))
Expand All @@ -130,12 +131,13 @@ func TestNewPlaceholder(t *testing.T) {
"labelKey0": "labelKeyValue0",
"labelKey1": "labelKeyValue1",
})
assert.Equal(t, len(holder.pod.Annotations), 6, "unexpected number of annotations")
assert.Equal(t, len(holder.pod.Annotations), 7, "unexpected number of annotations")
assert.Equal(t, holder.pod.Annotations[constants.AnnotationTaskGroupName], app.taskGroups[0].Name)
assert.Equal(t, holder.pod.Annotations[constants.AnnotationPlaceholderFlag], constants.True)
assert.Equal(t, holder.pod.Annotations["annotationKey0"], "annotationValue0")
assert.Equal(t, holder.pod.Annotations["annotationKey1"], "annotationValue1")
assert.Equal(t, holder.pod.Annotations["annotationKey2"], "annotationValue2")
assert.Equal(t, holder.pod.Annotations[constants.AnnotationSchedulingPolicyParam], "gangSchedulingStyle=Soft")
var taskGroupsDef []TaskGroup
err = json.Unmarshal([]byte(holder.pod.Annotations[siCommon.DomainYuniKorn+"task-groups"]), &taskGroupsDef)
assert.NilError(t, err, "taskGroupsDef unmarshal failed")
Expand Down