mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 10:51:29 +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 {
|
||||
nodes := makeNodeList([]string{"1"})
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
nodes := makeNodeList([]string{"1"})
|
||||
|
||||
plugin := st.FakeFilterPlugin{}
|
||||
registerFakeFilterFunc := st.RegisterFilterPlugin(
|
||||
"FakeFilter",
|
||||
func(_ runtime.Object, fh framework.Handle) (framework.Plugin, error) {
|
||||
return &plugin, nil
|
||||
},
|
||||
)
|
||||
registerPlugins := []st.RegisterPluginFunc{
|
||||
st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
|
||||
registerFakeFilterFunc,
|
||||
st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
|
||||
}
|
||||
fwk, err := st.NewFramework(
|
||||
registerPlugins,
|
||||
frameworkruntime.WithPodNominator(internalqueue.NewPodNominator()),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
plugin := st.FakeFilterPlugin{}
|
||||
registerFakeFilterFunc := st.RegisterFilterPlugin(
|
||||
"FakeFilter",
|
||||
func(_ runtime.Object, fh framework.Handle) (framework.Plugin, error) {
|
||||
return &plugin, nil
|
||||
},
|
||||
)
|
||||
registerPlugins := []st.RegisterPluginFunc{
|
||||
st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
|
||||
registerFakeFilterFunc,
|
||||
st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
|
||||
}
|
||||
fwk, err := st.NewFramework(
|
||||
registerPlugins,
|
||||
frameworkruntime.WithPodNominator(internalqueue.NewPodNominator()),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
scheduler := makeScheduler(nodes)
|
||||
if err := scheduler.cache.UpdateSnapshot(scheduler.nodeInfoSnapshot); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fwk.PreemptHandle().AddNominatedPod(&v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: "nominated"}, Spec: v1.PodSpec{Priority: &midPriority}}, "1")
|
||||
scheduler := makeScheduler(nodes)
|
||||
if err := scheduler.cache.UpdateSnapshot(scheduler.nodeInfoSnapshot); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
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 {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if test.expectedCount != plugin.NumFilterCalled {
|
||||
t.Errorf("predicate was called %d times, expected is %d", plugin.NumFilterCalled, test.expectedCount)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if test.expectedCount != plugin.NumFilterCalled {
|
||||
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]},
|
||||
}}
|
||||
|
||||
for _, tt := range tests {
|
||||
cache := newSchedulerCache(ttl, time.Second, nil)
|
||||
for _, podToAssume := range tt.podsToAssume {
|
||||
if err := assumeAndFinishBinding(cache, podToAssume, now); err != nil {
|
||||
t.Errorf("assumePod failed: %v", err)
|
||||
for i, tt := range tests {
|
||||
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
|
||||
cache := newSchedulerCache(ttl, time.Second, nil)
|
||||
for _, podToAssume := range tt.podsToAssume {
|
||||
if err := assumeAndFinishBinding(cache, podToAssume, now); err != nil {
|
||||
t.Errorf("assumePod failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, podToAdd := range tt.podsToAdd {
|
||||
if err := cache.AddPod(podToAdd); err != nil {
|
||||
t.Errorf("AddPod failed: %v", err)
|
||||
for _, podToAdd := range tt.podsToAdd {
|
||||
if err := cache.AddPod(podToAdd); err != nil {
|
||||
t.Errorf("AddPod failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
snapshot := cache.Dump()
|
||||
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))
|
||||
}
|
||||
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)
|
||||
snapshot := cache.Dump()
|
||||
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))
|
||||
}
|
||||
}
|
||||
if !reflect.DeepEqual(snapshot.AssumedPods, cache.assumedPods) {
|
||||
t.Errorf("expect \n%+v; got \n%+v", cache.assumedPods, snapshot.AssumedPods)
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
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 {
|
||||
cache := newSchedulerCache(ttl, time.Second, nil)
|
||||
for i, tt := range tests {
|
||||
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 {
|
||||
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)
|
||||
if err := tt.handler(cache, tt.pod); err != nil {
|
||||
t.Fatalf("unexpected err: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
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 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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
@ -82,11 +83,13 @@ func TestGetNodeImageStates(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
imageStates := getNodeImageStates(test.node, test.imageExistenceMap)
|
||||
if !reflect.DeepEqual(test.expected, imageStates) {
|
||||
t.Errorf("expected: %#v, got: %#v", test.expected, imageStates)
|
||||
}
|
||||
for i, test := range tests {
|
||||
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
|
||||
imageStates := getNodeImageStates(test.node, test.imageExistenceMap)
|
||||
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 {
|
||||
imageMap := createImageExistenceMap(test.nodes)
|
||||
if !reflect.DeepEqual(test.expected, imageMap) {
|
||||
t.Errorf("expected: %#v, got: %#v", test.expected, imageMap)
|
||||
}
|
||||
for i, test := range tests {
|
||||
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
|
||||
imageMap := createImageExistenceMap(test.nodes)
|
||||
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 {
|
||||
got := MoreImportantPod(v.p1, v.p2)
|
||||
if got != v.expected {
|
||||
t.Errorf("%s failed, expected %t but got %t", k, v.expected, got)
|
||||
}
|
||||
t.Run(k, func(t *testing.T) {
|
||||
got := MoreImportantPod(v.p1, v.p2)
|
||||
if got != v.expected {
|
||||
t.Errorf("expected %t but got %t", v.expected, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user