k3s/plugin/pkg/scheduler/scheduler_test.go

150 lines
4.3 KiB
Go
Raw Normal View History

2014-08-03 07:01:28 +00:00
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scheduler
import (
"errors"
"reflect"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
2014-10-14 22:38:31 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/record"
2014-08-03 07:01:28 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/scheduler"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
2014-08-03 07:01:28 +00:00
)
type fakeBinder struct {
b func(binding *api.Binding) error
}
func (fb fakeBinder) Bind(binding *api.Binding) error { return fb.b(binding) }
func podWithID(id, desiredHost string) *api.Pod {
return &api.Pod{
ObjectMeta: api.ObjectMeta{Name: id, SelfLink: testapi.SelfLink("pods", id)},
Spec: api.PodSpec{
Host: desiredHost,
},
Status: api.PodStatus{
Host: desiredHost,
},
}
2014-08-03 07:01:28 +00:00
}
type mockScheduler struct {
machine string
err error
}
func (es mockScheduler) Schedule(pod api.Pod, ml scheduler.MinionLister) (string, error) {
return es.machine, es.err
}
func TestScheduler(t *testing.T) {
eventBroadcaster := record.NewBroadcaster()
defer eventBroadcaster.StartLogging(t.Logf).Stop()
2014-08-03 07:01:28 +00:00
errS := errors.New("scheduler")
errB := errors.New("binder")
table := []struct {
injectBindError error
sendPod *api.Pod
algo scheduler.Scheduler
expectErrorPod *api.Pod
expectAssumedPod *api.Pod
expectError error
expectBind *api.Binding
eventReason string
2014-08-03 07:01:28 +00:00
}{
{
sendPod: podWithID("foo", ""),
algo: mockScheduler{"machine1", nil},
expectBind: &api.Binding{ObjectMeta: api.ObjectMeta{Name: "foo"}, Target: api.ObjectReference{Kind: "Node", Name: "machine1"}},
expectAssumedPod: podWithID("foo", "machine1"),
eventReason: "scheduled",
2014-08-03 07:01:28 +00:00
}, {
sendPod: podWithID("foo", ""),
2014-08-03 07:01:28 +00:00
algo: mockScheduler{"machine1", errS},
expectError: errS,
expectErrorPod: podWithID("foo", ""),
2014-10-14 22:38:31 +00:00
eventReason: "failedScheduling",
2014-08-03 07:01:28 +00:00
}, {
sendPod: podWithID("foo", ""),
2014-08-03 07:01:28 +00:00
algo: mockScheduler{"machine1", nil},
expectBind: &api.Binding{ObjectMeta: api.ObjectMeta{Name: "foo"}, Target: api.ObjectReference{Kind: "Node", Name: "machine1"}},
2014-08-03 07:01:28 +00:00
injectBindError: errB,
expectError: errB,
expectErrorPod: podWithID("foo", ""),
2014-10-14 22:38:31 +00:00
eventReason: "failedScheduling",
2014-08-03 07:01:28 +00:00
},
}
for i, item := range table {
var gotError error
var gotPod *api.Pod
var gotAssumedPod *api.Pod
2014-08-03 07:01:28 +00:00
var gotBinding *api.Binding
c := &Config{
Modeler: &FakeModeler{
AssumePodFunc: func(pod *api.Pod) {
gotAssumedPod = pod
},
},
MinionLister: scheduler.FakeMinionLister(
2014-12-08 03:44:27 +00:00
api.NodeList{Items: []api.Node{{ObjectMeta: api.ObjectMeta{Name: "machine1"}}}},
),
Algorithm: item.algo,
2014-08-03 07:01:28 +00:00
Binder: fakeBinder{func(b *api.Binding) error {
gotBinding = b
return item.injectBindError
}},
Error: func(p *api.Pod, err error) {
gotPod = p
gotError = err
},
NextPod: func() *api.Pod {
return item.sendPod
},
Recorder: eventBroadcaster.NewRecorder(api.EventSource{Component: "scheduler"}),
2014-08-03 07:01:28 +00:00
}
s := New(c)
2014-10-14 22:38:31 +00:00
called := make(chan struct{})
events := eventBroadcaster.StartEventWatcher(func(e *api.Event) {
2014-10-14 22:38:31 +00:00
if e, a := item.eventReason, e.Reason; e != a {
t.Errorf("%v: expected %v, got %v", i, e, a)
}
close(called)
})
2014-08-03 07:01:28 +00:00
s.scheduleOne()
if e, a := item.expectAssumedPod, gotAssumedPod; !reflect.DeepEqual(e, a) {
t.Errorf("%v: assumed pod: wanted %v, got %v", i, e, a)
}
2014-08-03 07:01:28 +00:00
if e, a := item.expectErrorPod, gotPod; !reflect.DeepEqual(e, a) {
t.Errorf("%v: error pod: wanted %v, got %v", i, e, a)
}
if e, a := item.expectError, gotError; !reflect.DeepEqual(e, a) {
t.Errorf("%v: error: wanted %v, got %v", i, e, a)
}
if e, a := item.expectBind, gotBinding; !reflect.DeepEqual(e, a) {
t.Errorf("%v: error: %s", i, util.ObjectDiff(e, a))
2014-08-03 07:01:28 +00:00
}
2014-10-14 22:38:31 +00:00
<-called
events.Stop()
2014-08-03 07:01:28 +00:00
}
}