Skip to content

Commit

Permalink
[YUNIKORN-2851] Improve placeholder / manager test coverage (#905)
Browse files Browse the repository at this point in the history
Closes: #905

Signed-off-by: Craig Condit <ccondit@apache.org>
  • Loading branch information
SP12893678 authored and craigcondit committed Sep 4, 2024
1 parent f8e9c11 commit c8493d9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
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

0 comments on commit c8493d9

Please sign in to comment.