Skip to content

Commit

Permalink
chore: optimize test logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Okabe-Rintarou-0 committed May 6, 2024
1 parent 6a4508c commit 1c87de8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
1 change: 1 addition & 0 deletions pkg/binder/utils/binderunit.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func GetUnitType(pod *v1.Pod) framework.ScheduleUnitType {
// In this case, we won't move pods from activeQ to readyQ, caller should handle the not found issue.
func CreateScheduleUnit(pcLister schedulingv1.PriorityClassLister, pgLister v1alpha1.PodGroupLister, info *framework.QueuedPodInfo) (framework.ScheduleUnit, error) {
if len(unitutil.GetPodGroupName(info.Pod)) != 0 {
/// TODO
pgName := unitutil.GetPodGroupName(info.Pod)
podGroup, err := pgLister.PodGroups(info.Pod.Namespace).Get(pgName)
if err != nil {
Expand Down
40 changes: 20 additions & 20 deletions pkg/scheduler/core/pod_scheduler/pod_scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ func TestSelectHost(t *testing.T) {
isolatedCache: isolatedcache.NewIsolatedCache(),
}
tests := []struct {
name string
list framework.NodeScoreList
possibleHosts sets.String
expectsErr bool
pod *v1.Pod
expectedCacheSizes []int
name string
list framework.NodeScoreList
possibleHosts sets.String
expectsErr bool
pod *v1.Pod
}{
{
name: "unique properly ordered scores",
Expand All @@ -67,7 +66,6 @@ func TestSelectHost(t *testing.T) {
expectsErr: false,
pod: testinghelper.MakePod().Namespace("default").Name("pod1").UID("pod1").
ControllerRef(metav1.OwnerReference{Kind: "ReplicaSet", Name: "rs1", UID: "rs1"}).Obj(),
expectedCacheSizes: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
},
{
name: "equal scores",
Expand All @@ -81,7 +79,6 @@ func TestSelectHost(t *testing.T) {
expectsErr: false,
pod: testinghelper.MakePod().Namespace("default").Name("pod2").UID("pod2").
ControllerRef(metav1.OwnerReference{Kind: "ReplicaSet", Name: "rs2", UID: "rs2"}).Obj(),
expectedCacheSizes: []int{3, 6, 9, 12, 15, 18, 21, 24, 27, 30},
},
{
name: "out of order scores",
Expand All @@ -96,7 +93,6 @@ func TestSelectHost(t *testing.T) {
expectsErr: false,
pod: testinghelper.MakePod().Namespace("default").Name("pod3").UID("pod3").
ControllerRef(metav1.OwnerReference{Kind: "StatefulSet", Name: "sts3", UID: "sts3"}).Obj(),
expectedCacheSizes: []int{4, 8, 12, 16, 20, 24, 28, 32, 36, 40},
},
{
name: "empty priority list",
Expand All @@ -105,34 +101,38 @@ func TestSelectHost(t *testing.T) {
expectsErr: true,
pod: testinghelper.MakePod().Namespace("default").Name("pod4").UID("pod4").
ControllerRef(metav1.OwnerReference{Kind: "StatefulSet", Name: "sts4", UID: "sts4"}).Obj(),
expectedCacheSizes: []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
lastCacheSize := 0
// get mock pod owner
podOwner := podutil.GetPodOwner(test.pod)
// increase the randomness
for i := 0; i < 10; i++ {
// TODO: add unit test cases for caching node logic
// init cache
nonSelectedNodes := sets.NewString()
for _, nodeScore := range test.list {
nodeName := nodeScore.Name
scheduler.isolatedCache.DeleteNodeForPodOwner(podOwner, nodeName)
nonSelectedNodes.Insert(nodeName)
}

selectedNode, err := scheduler.selectHostAndCacheResults(test.list, &v1.Pod{}, podOwner, "", &framework.UnitSchedulingRequest{})
nodes := scheduler.isolatedCache.GetOrderedNodesForPodOwner(podOwner)

currCacheSize := len(nodes)
if currCacheSize != test.expectedCacheSizes[i] {
t.Errorf("Unexpected cache size, expected %d but got %d", test.expectedCacheSizes[i], currCacheSize)
}
lastCacheSize = currCacheSize
// exclude the selected node
nonSelectedNodes.Delete(selectedNode)

// Check newly cached nodes
newlyCachedNodes := nodes[lastCacheSize:]
for _, newlyCachedNode := range newlyCachedNodes {
if selectedNode == newlyCachedNode {
for _, node := range nodes {
if selectedNode == node {
t.Errorf("Unexpected cached node %s, selected node should not be cached", selectedNode)
}
if !nonSelectedNodes.Has(node) {
t.Errorf("Non-selected nodes should be cached")
}
nonSelectedNodes.Delete(node)
}

if test.expectsErr {
Expand Down

0 comments on commit 1c87de8

Please sign in to comment.