mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
Organize scheduler unit tests into subtests
This commit is contained in:
parent
645c40fcf6
commit
ebb6fb5b0c
@ -1146,42 +1146,44 @@ func TestFindFitPredicateCallCounts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
nodes := makeNodeList([]string{"1"})
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
nodes := makeNodeList([]string{"1"})
|
||||||
|
|
||||||
plugin := st.FakeFilterPlugin{}
|
plugin := st.FakeFilterPlugin{}
|
||||||
registerFakeFilterFunc := st.RegisterFilterPlugin(
|
registerFakeFilterFunc := st.RegisterFilterPlugin(
|
||||||
"FakeFilter",
|
"FakeFilter",
|
||||||
func(_ runtime.Object, fh framework.Handle) (framework.Plugin, error) {
|
func(_ runtime.Object, fh framework.Handle) (framework.Plugin, error) {
|
||||||
return &plugin, nil
|
return &plugin, nil
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
registerPlugins := []st.RegisterPluginFunc{
|
registerPlugins := []st.RegisterPluginFunc{
|
||||||
st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
|
st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
|
||||||
registerFakeFilterFunc,
|
registerFakeFilterFunc,
|
||||||
st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
|
st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
|
||||||
}
|
}
|
||||||
fwk, err := st.NewFramework(
|
fwk, err := st.NewFramework(
|
||||||
registerPlugins,
|
registerPlugins,
|
||||||
frameworkruntime.WithPodNominator(internalqueue.NewPodNominator()),
|
frameworkruntime.WithPodNominator(internalqueue.NewPodNominator()),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
scheduler := makeScheduler(nodes)
|
scheduler := makeScheduler(nodes)
|
||||||
if err := scheduler.cache.UpdateSnapshot(scheduler.nodeInfoSnapshot); err != nil {
|
if err := scheduler.cache.UpdateSnapshot(scheduler.nodeInfoSnapshot); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
fwk.PreemptHandle().AddNominatedPod(&v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: "nominated"}, Spec: v1.PodSpec{Priority: &midPriority}}, "1")
|
fwk.PreemptHandle().AddNominatedPod(&v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: "nominated"}, Spec: v1.PodSpec{Priority: &midPriority}}, "1")
|
||||||
|
|
||||||
_, _, err = scheduler.findNodesThatFitPod(context.Background(), fwk, framework.NewCycleState(), test.pod)
|
_, _, err = scheduler.findNodesThatFitPod(context.Background(), fwk, framework.NewCycleState(), test.pod)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
if test.expectedCount != plugin.NumFilterCalled {
|
if test.expectedCount != plugin.NumFilterCalled {
|
||||||
t.Errorf("predicate was called %d times, expected is %d", plugin.NumFilterCalled, test.expectedCount)
|
t.Errorf("predicate was called %d times, expected is %d", plugin.NumFilterCalled, test.expectedCount)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
82
pkg/scheduler/internal/cache/cache_test.go
vendored
82
pkg/scheduler/internal/cache/cache_test.go
vendored
@ -394,32 +394,34 @@ func TestDump(t *testing.T) {
|
|||||||
podsToAdd: []*v1.Pod{testPods[0]},
|
podsToAdd: []*v1.Pod{testPods[0]},
|
||||||
}}
|
}}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for i, tt := range tests {
|
||||||
cache := newSchedulerCache(ttl, time.Second, nil)
|
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
|
||||||
for _, podToAssume := range tt.podsToAssume {
|
cache := newSchedulerCache(ttl, time.Second, nil)
|
||||||
if err := assumeAndFinishBinding(cache, podToAssume, now); err != nil {
|
for _, podToAssume := range tt.podsToAssume {
|
||||||
t.Errorf("assumePod failed: %v", err)
|
if err := assumeAndFinishBinding(cache, podToAssume, now); err != nil {
|
||||||
|
t.Errorf("assumePod failed: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
for _, podToAdd := range tt.podsToAdd {
|
||||||
for _, podToAdd := range tt.podsToAdd {
|
if err := cache.AddPod(podToAdd); err != nil {
|
||||||
if err := cache.AddPod(podToAdd); err != nil {
|
t.Errorf("AddPod failed: %v", err)
|
||||||
t.Errorf("AddPod failed: %v", err)
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
snapshot := cache.Dump()
|
snapshot := cache.Dump()
|
||||||
if len(snapshot.Nodes) != len(cache.nodes) {
|
if len(snapshot.Nodes) != len(cache.nodes) {
|
||||||
t.Errorf("Unequal number of nodes in the cache and its snapshot. expected: %v, got: %v", len(cache.nodes), len(snapshot.Nodes))
|
t.Errorf("Unequal number of nodes in the cache and its snapshot. expected: %v, got: %v", len(cache.nodes), len(snapshot.Nodes))
|
||||||
}
|
|
||||||
for name, ni := range snapshot.Nodes {
|
|
||||||
nItem := cache.nodes[name]
|
|
||||||
if !reflect.DeepEqual(ni, nItem.info) {
|
|
||||||
t.Errorf("expect \n%+v; got \n%+v", nItem.info, ni)
|
|
||||||
}
|
}
|
||||||
}
|
for name, ni := range snapshot.Nodes {
|
||||||
if !reflect.DeepEqual(snapshot.AssumedPods, cache.assumedPods) {
|
nItem := cache.nodes[name]
|
||||||
t.Errorf("expect \n%+v; got \n%+v", cache.assumedPods, snapshot.AssumedPods)
|
if !reflect.DeepEqual(ni, nItem.info) {
|
||||||
}
|
t.Errorf("expect \n%+v; got \n%+v", nItem.info, ni)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(snapshot.AssumedPods, cache.assumedPods) {
|
||||||
|
t.Errorf("expect \n%+v; got \n%+v", cache.assumedPods, snapshot.AssumedPods)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -647,26 +649,28 @@ func TestUpdatePodAndGet(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for i, tt := range tests {
|
||||||
cache := newSchedulerCache(ttl, time.Second, nil)
|
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
|
||||||
|
cache := newSchedulerCache(ttl, time.Second, nil)
|
||||||
|
|
||||||
if err := tt.handler(cache, tt.pod); err != nil {
|
if err := tt.handler(cache, tt.pod); err != nil {
|
||||||
t.Fatalf("unexpected err: %v", err)
|
t.Fatalf("unexpected err: %v", err)
|
||||||
}
|
|
||||||
|
|
||||||
if !tt.assumePod {
|
|
||||||
if err := cache.UpdatePod(tt.pod, tt.podToUpdate); err != nil {
|
|
||||||
t.Fatalf("UpdatePod failed: %v", err)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
cachedPod, err := cache.GetPod(tt.pod)
|
if !tt.assumePod {
|
||||||
if err != nil {
|
if err := cache.UpdatePod(tt.pod, tt.podToUpdate); err != nil {
|
||||||
t.Fatalf("GetPod failed: %v", err)
|
t.Fatalf("UpdatePod failed: %v", err)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(tt.podToUpdate, cachedPod) {
|
}
|
||||||
t.Fatalf("pod get=%s, want=%s", cachedPod, tt.podToUpdate)
|
|
||||||
}
|
cachedPod, err := cache.GetPod(tt.pod)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetPod failed: %v", err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(tt.podToUpdate, cachedPod) {
|
||||||
|
t.Fatalf("pod get=%s, want=%s", cachedPod, tt.podToUpdate)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
25
pkg/scheduler/internal/cache/snapshot_test.go
vendored
25
pkg/scheduler/internal/cache/snapshot_test.go
vendored
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package cache
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -82,11 +83,13 @@ func TestGetNodeImageStates(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for i, test := range tests {
|
||||||
imageStates := getNodeImageStates(test.node, test.imageExistenceMap)
|
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
|
||||||
if !reflect.DeepEqual(test.expected, imageStates) {
|
imageStates := getNodeImageStates(test.node, test.imageExistenceMap)
|
||||||
t.Errorf("expected: %#v, got: %#v", test.expected, imageStates)
|
if !reflect.DeepEqual(test.expected, imageStates) {
|
||||||
}
|
t.Errorf("expected: %#v, got: %#v", test.expected, imageStates)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,10 +171,12 @@ func TestCreateImageExistenceMap(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for i, test := range tests {
|
||||||
imageMap := createImageExistenceMap(test.nodes)
|
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
|
||||||
if !reflect.DeepEqual(test.expected, imageMap) {
|
imageMap := createImageExistenceMap(test.nodes)
|
||||||
t.Errorf("expected: %#v, got: %#v", test.expected, imageMap)
|
if !reflect.DeepEqual(test.expected, imageMap) {
|
||||||
}
|
t.Errorf("expected: %#v, got: %#v", test.expected, imageMap)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -111,10 +111,12 @@ func TestMoreImportantPod(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range tests {
|
for k, v := range tests {
|
||||||
got := MoreImportantPod(v.p1, v.p2)
|
t.Run(k, func(t *testing.T) {
|
||||||
if got != v.expected {
|
got := MoreImportantPod(v.p1, v.p2)
|
||||||
t.Errorf("%s failed, expected %t but got %t", k, v.expected, got)
|
if got != v.expected {
|
||||||
}
|
t.Errorf("expected %t but got %t", v.expected, got)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user