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

Add unit tests for queuejob util functions #545

Merged
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
15 changes: 8 additions & 7 deletions pkg/controller/queuejob/heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,24 @@ import (
"container/heap"
"fmt"
"k8s.io/client-go/tools/cache"
arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
)

// Below is the implementation of the a heap. The logic is pretty much the same
// as cache.heap, however, this heap does not perform synchronization. It leaves
// synchronization to the SchedulingQueue.

type LessFunc func(interface{}, interface{}) bool
type LessFunc func(qj1, qj2 *arbv1.AppWrapper) bool
type KeyFunc func(obj interface{}) (string, error)

type heapItem struct {
obj interface{} // The object which is stored in the heap.
obj *arbv1.AppWrapper // The object which is stored in the heap.
index int // The index of the object's key in the Heap.queue.
}

type itemKeyValue struct {
key string
obj interface{}
obj *arbv1.AppWrapper
}

// heapData is an internal struct that implements the standard heap interface
Expand Down Expand Up @@ -121,7 +122,7 @@ type Heap struct {

// Add inserts an item, and puts it in the queue. The item is updated if it
// already exists.
func (h *Heap) Add(obj interface{}) error {
func (h *Heap) Add(obj *arbv1.AppWrapper) error {
key, err := h.data.keyFunc(obj)
if err != nil {
return cache.KeyError{Obj: obj, Err: err}
Expand All @@ -136,7 +137,7 @@ func (h *Heap) Add(obj interface{}) error {
}

// BulkAdd adds all the items in the list to the queue.
func (h *Heap) BulkAdd(list []interface{}) error {
func (h *Heap) BulkAdd(list []*arbv1.AppWrapper) error {
for _, obj := range list {
key, err := h.data.keyFunc(obj)
if err != nil {
Expand All @@ -154,7 +155,7 @@ func (h *Heap) BulkAdd(list []interface{}) error {

// AddIfNotPresent inserts an item, and puts it in the queue. If an item with
// the key is present in the map, no changes is made to the item.
func (h *Heap) AddIfNotPresent(obj interface{}) error {
func (h *Heap) AddIfNotPresent(obj *arbv1.AppWrapper) error {
key, err := h.data.keyFunc(obj)
if err != nil {
return cache.KeyError{Obj: obj, Err: err}
Expand All @@ -167,7 +168,7 @@ func (h *Heap) AddIfNotPresent(obj interface{}) error {

// Update is the same as Add in this implementation. When the item does not
// exist, it is added.
func (h *Heap) Update(obj interface{}) error {
func (h *Heap) Update(obj *arbv1.AppWrapper) error {
return h.Add(obj)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/queuejob/queuejob_controller_ex.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ func (qjm *XController) ScheduleNext() {
pq := qjm.qjqueue.(*PriorityQueue)
if qjm.qjqueue.Length() > 0 {
for key, element := range pq.activeQ.data.items {
qjtemp := element.obj.(*arbv1.AppWrapper)
qjtemp := element.obj
klog.V(4).Infof("[ScheduleNext] AfterCalc: qjqLength=%d Key=%s index=%d Priority=%.1f SystemPriority=%.1f QueueJobState=%s",
qjm.qjqueue.Length(), key, element.index, float64(qjtemp.Spec.Priority), qjtemp.Status.SystemPriority, qjtemp.Status.QueueJobState)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/queuejob/scheduling_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"reflect"
"sync"

arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
qjobv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -320,7 +321,7 @@ func (p *PriorityQueue) Delete(qj *qjobv1.AppWrapper) error {
func (p *PriorityQueue) MoveAllToActiveQueue() {
p.lock.Lock()
defer p.lock.Unlock()
var unschedulableQJs []interface{}
var unschedulableQJs []*arbv1.AppWrapper
for _, qj := range p.unschedulableQ.pods {
unschedulableQJs = append(unschedulableQJs, qj)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/queuejob/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func GetXQJFullName(qj *arbv1.AppWrapper) string {
return qj.Name + "_" + qj.Namespace
}

func HigherSystemPriorityQJ(qj1, qj2 interface{}) bool {
return qj1.(*arbv1.AppWrapper).Status.SystemPriority > qj2.(*arbv1.AppWrapper).Status.SystemPriority
func HigherSystemPriorityQJ(qj1, qj2 *arbv1.AppWrapper) bool {
return qj1.Status.SystemPriority > qj2.Status.SystemPriority
}

func createAppWrapperKind(config *rest.Config) error {
Expand Down
236 changes: 236 additions & 0 deletions pkg/controller/queuejob/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package queuejob

import (
"testing"

"github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
)


func TestGetXQJFullName(t *testing.T) {
g := gomega.NewGomegaWithT(t)

tests := []struct {
name string
qj *arbv1.AppWrapper
expected string
}{
{
name: "valid qj",
qj: &arbv1.AppWrapper{
ObjectMeta: metav1.ObjectMeta{
Name: "qjName",
Namespace: "qjNamespace",
},
},
expected: "qjName_qjNamespace",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := GetXQJFullName(tt.qj)
g.Expect(result).To(gomega.Equal(tt.expected))
})
}
}


func TestHigherSystemPriorityQJ(t *testing.T) {
g := gomega.NewGomegaWithT(t)

tests := []struct {
name string
qj1 *arbv1.AppWrapper
qj2 *arbv1.AppWrapper
expected bool
}{
{
name: "lower priority qj1",
qj1: &arbv1.AppWrapper{
Status: arbv1.AppWrapperStatus{
SystemPriority: 1,
},
},
qj2: &arbv1.AppWrapper{
Status: arbv1.AppWrapperStatus{
SystemPriority: 2,
},
},
expected: false,
},
{
name: "higher priority qj1",
qj1: &arbv1.AppWrapper{
Status: arbv1.AppWrapperStatus{
SystemPriority: 2,
},
},
qj2: &arbv1.AppWrapper{
Status: arbv1.AppWrapperStatus{
SystemPriority: 1,
},
},
expected: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := HigherSystemPriorityQJ(tt.qj1, tt.qj2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change the method signature to not use interface{} and use AppWrapper ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a new commit to make use of AppWrapper, it required me to change the heap.go functions as well to use the AppWrapper type. I'm not sure if this is the ideal way of doing it, hoping to get your thoughts/advice on the changes made. Thank you @z103cb

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are LGTM. @astefanutti please review this change and approve it. I think it might be conflict with your k8s upgrade PR>

g.Expect(result).To(gomega.Equal(tt.expected))
})
}
}

func TestGenerateAppWrapperCondition(t *testing.T) {
g := gomega.NewGomegaWithT(t)

tests := []struct {
name string
conditionType arbv1.AppWrapperConditionType
status corev1.ConditionStatus
reason string
message string
expected arbv1.AppWrapperCondition
}{
{
name: "generate condition",
conditionType: arbv1.AppWrapperConditionType("Queuing"),
status: corev1.ConditionTrue,
reason: "reason",
message: "message",
expected: arbv1.AppWrapperCondition{
Type: arbv1.AppWrapperConditionType("Queuing"),
Status: corev1.ConditionTrue,
Reason: "reason",
Message: "message",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := GenerateAppWrapperCondition(tt.conditionType, tt.status, tt.reason, tt.message)

g.Expect(result.Type).To(gomega.Equal(tt.expected.Type))
g.Expect(result.Status).To(gomega.Equal(tt.expected.Status))
g.Expect(result.Reason).To(gomega.Equal(tt.expected.Reason))
g.Expect(result.Message).To(gomega.Equal(tt.expected.Message))
})
}
}


func TestIsLastConditionDuplicate(t *testing.T) {
g := gomega.NewGomegaWithT(t)

aw := &arbv1.AppWrapper{
Status: arbv1.AppWrapperStatus{
Conditions: []arbv1.AppWrapperCondition{
{
Type: arbv1.AppWrapperConditionType("Queuing"),
Status: corev1.ConditionTrue,
Reason: "reason",
Message: "test",
},
},
},
}

tests := []struct {
name string
conditionType arbv1.AppWrapperConditionType
status corev1.ConditionStatus
reason string
message string
expected bool
}{
{
name: "duplicate condition",
conditionType: arbv1.AppWrapperConditionType("Queuing"),
status: corev1.ConditionTrue,
reason: "reason",
message: "test",
expected: true,
},
{
name: "different condition",
conditionType: arbv1.AppWrapperConditionType("Running"),
status: corev1.ConditionFalse,
reason: "noReason",
message: "noMessage",
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isLastConditionDuplicate(aw, tt.conditionType, tt.status, tt.reason, tt.message)
g.Expect(result).To(gomega.Equal(tt.expected))
})
}
}


func TestGetIndexOfMatchedCondition(t *testing.T) {
g := gomega.NewGomegaWithT(t)

aw := &arbv1.AppWrapper{
Status: arbv1.AppWrapperStatus{
Conditions: []arbv1.AppWrapperCondition{
{
Type: arbv1.AppWrapperConditionType("Queuing"),
Reason: "reason",
},
{
Type: arbv1.AppWrapperConditionType("Running"),
Reason: "reason1",
},
},
},
}

tests := []struct {
name string
conditionType arbv1.AppWrapperConditionType
reason string
expected int
}{
{
name: "match reason and condition",
conditionType: arbv1.AppWrapperConditionType("Queuing"),
reason: "reason",
expected: 0,
},
{
name: "match reason and condition",
conditionType: arbv1.AppWrapperConditionType("Running"),
reason: "reason1",
expected: 1,
},
{
name: "match reason but not condition",
conditionType: arbv1.AppWrapperConditionType("Running"),
reason: "reason",
expected: -1,
},
{
name: "no match",
conditionType: arbv1.AppWrapperConditionType("Queuing"),
reason: "reason1",
expected: -1,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := getIndexOfMatchedCondition(aw, tt.conditionType, tt.reason)
g.Expect(result).To(gomega.Equal(tt.expected))
})
}
}