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-2762] Improve util funtion's test coverage #879

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion pkg/common/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package utils

import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
Expand All @@ -44,6 +45,10 @@ const userInfoKey = siCommon.DomainYuniKorn + "user.info"
const uniqueAutogenSuffix = "-uniqueautogen"

var pluginMode bool
var (
// ErrorTimeout returned if waiting for a condition times out
ErrorTimeout = errors.New("timeout waiting for condition")
SP12893678 marked this conversation as resolved.
Show resolved Hide resolved
)

func SetPluginMode(value bool) {
pluginMode = value
Expand Down Expand Up @@ -250,7 +255,7 @@ func WaitForCondition(eval func() bool, interval time.Duration, timeout time.Dur
}

if time.Now().After(deadline) {
return fmt.Errorf("timeout waiting for condition")
return ErrorTimeout
}

time.Sleep(interval)
Expand Down
98 changes: 98 additions & 0 deletions pkg/common/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"bytes"
"compress/gzip"
"fmt"
"reflect"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -49,6 +50,21 @@ func TestConvert2Pod(t *testing.T) {
assert.Assert(t, pod != nil)
}

func TestConvert2ConfigMap(t *testing.T) {
configMap := &v1.ConfigMap{}
result := Convert2ConfigMap(configMap)
assert.Equal(t, result != nil, true)
assert.Equal(t, reflect.DeepEqual(result, configMap), true)

obj := struct{}{}
result = Convert2ConfigMap(obj)
assert.Equal(t, result == nil, true)

pod := &v1.Pod{}
result = Convert2ConfigMap(pod)
assert.Equal(t, result == nil, true)
}

func TestIsAssignedPod(t *testing.T) {
assigned := IsAssignedPod(&v1.Pod{
Spec: v1.PodSpec{
Expand Down Expand Up @@ -212,6 +228,15 @@ func TestGetNamespaceQuotaFromAnnotationUsingNewAnnotations(t *testing.T) {
},
},
}, nil},
{&v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
Annotations: map[string]string{
constants.DomainYuniKorn + "namespace.quota": "expecting JSON object",
},
},
}, nil},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("namespace: %v", tc.namespace), func(t *testing.T) {
Expand Down Expand Up @@ -624,9 +649,11 @@ func TestGetApplicationIDFromPod(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
conf.GetSchedulerConf().GenerateUniqueAppIds = tc.generateUniqueAppIds
SetPluginMode(false)
assert.Equal(t, IsPluginMode(), false)
appID := GetApplicationIDFromPod(tc.pod)
assert.Equal(t, appID, tc.expectedAppID, "Wrong appID (standard mode)")
SetPluginMode(true)
assert.Equal(t, IsPluginMode(), true)
appID2 := GetApplicationIDFromPod(tc.pod)
assert.Equal(t, appID2, tc.expectedAppIDPluginMode, "Wrong appID (plugin mode)")
})
Expand Down Expand Up @@ -946,6 +973,43 @@ func TestPodAlreadyBound(t *testing.T) {
}
}

func TestIsPodRunning(t *testing.T) {
pod := &v1.Pod{
Status: v1.PodStatus{
Phase: v1.PodRunning,
},
}
assert.Equal(t, IsPodRunning(pod), true)

pod = &v1.Pod{
Status: v1.PodStatus{
Phase: v1.PodFailed,
},
}
assert.Equal(t, IsPodRunning(pod), false)
SP12893678 marked this conversation as resolved.
Show resolved Hide resolved

pod = &v1.Pod{
Status: v1.PodStatus{
Phase: v1.PodPending,
},
}
assert.Equal(t, IsPodRunning(pod), false)

pod = &v1.Pod{
Status: v1.PodStatus{
Phase: v1.PodSucceeded,
},
}
assert.Equal(t, IsPodRunning(pod), false)

pod = &v1.Pod{
Status: v1.PodStatus{
Phase: v1.PodUnknown,
},
}
assert.Equal(t, IsPodRunning(pod), false)
}

func TestGetTaskGroupFromPodSpec(t *testing.T) {
pod := &v1.Pod{
TypeMeta: metav1.TypeMeta{
Expand Down Expand Up @@ -1075,6 +1139,13 @@ func TestGetCoreSchedulerConfigFromConfigMap(t *testing.T) {
assert.Equal(t, "test", GetCoreSchedulerConfigFromConfigMap(cm))
}

func TestGetCoreSchedulerConfigNotMapping(t *testing.T) {
cm := map[string]string{
"unknow.yaml": "test",
}
assert.Equal(t, "", GetCoreSchedulerConfigFromConfigMap(cm))
}

SP12893678 marked this conversation as resolved.
Show resolved Hide resolved
func TestGzipCompressedConfigMap(t *testing.T) {
var b bytes.Buffer
gzWriter := gzip.NewWriter(&b)
Expand Down Expand Up @@ -1140,3 +1211,30 @@ func TestConvert2PriorityClass(t *testing.T) {
assert.Assert(t, result != nil)
assert.Equal(t, result.PreemptionPolicy, &preemptLower)
}

func TestWaitForCondition(t *testing.T) {
target := false
eval := func() bool {
return target
}
tests := []struct {
input bool
interval time.Duration
timeout time.Duration
output error
}{
{true, time.Duration(1) * time.Second, time.Duration(2) * time.Second, nil},
{false, time.Duration(1) * time.Second, time.Duration(2) * time.Second, ErrorTimeout},
{true, time.Duration(3) * time.Second, time.Duration(2) * time.Second, nil},
{false, time.Duration(3) * time.Second, time.Duration(2) * time.Second, ErrorTimeout},
}
for _, test := range tests {
target = test.input
get := WaitForCondition(eval, test.timeout, test.interval)
if test.output == nil {
assert.NilError(t, get)
} else {
assert.Equal(t, get.Error(), test.output.Error())
}
}
}
chenyulin0719 marked this conversation as resolved.
Show resolved Hide resolved
Loading