|
|
|
@@ -19,8 +19,6 @@ package factory
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"reflect"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
@@ -28,13 +26,13 @@ import (
|
|
|
|
|
"k8s.io/api/core/v1"
|
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
|
|
|
|
"k8s.io/client-go/informers"
|
|
|
|
|
clientset "k8s.io/client-go/kubernetes"
|
|
|
|
|
restclient "k8s.io/client-go/rest"
|
|
|
|
|
"k8s.io/client-go/kubernetes/fake"
|
|
|
|
|
fakeV1 "k8s.io/client-go/kubernetes/typed/core/v1/fake"
|
|
|
|
|
clienttesting "k8s.io/client-go/testing"
|
|
|
|
|
"k8s.io/client-go/tools/cache"
|
|
|
|
|
utiltesting "k8s.io/client-go/util/testing"
|
|
|
|
|
apitesting "k8s.io/kubernetes/pkg/api/testing"
|
|
|
|
|
"k8s.io/kubernetes/pkg/scheduler/algorithm"
|
|
|
|
|
schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
|
|
|
|
@@ -53,14 +51,7 @@ const (
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCreate(t *testing.T) {
|
|
|
|
|
handler := utiltesting.FakeHandler{
|
|
|
|
|
StatusCode: 500,
|
|
|
|
|
ResponseBody: "",
|
|
|
|
|
T: t,
|
|
|
|
|
}
|
|
|
|
|
server := httptest.NewServer(&handler)
|
|
|
|
|
defer server.Close()
|
|
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
|
|
|
|
|
client := fake.NewSimpleClientset()
|
|
|
|
|
factory := newConfigFactory(client, v1.DefaultHardPodAffinitySymmetricWeight)
|
|
|
|
|
factory.Create()
|
|
|
|
|
}
|
|
|
|
@@ -71,14 +62,7 @@ func TestCreateFromConfig(t *testing.T) {
|
|
|
|
|
var configData []byte
|
|
|
|
|
var policy schedulerapi.Policy
|
|
|
|
|
|
|
|
|
|
handler := utiltesting.FakeHandler{
|
|
|
|
|
StatusCode: 500,
|
|
|
|
|
ResponseBody: "",
|
|
|
|
|
T: t,
|
|
|
|
|
}
|
|
|
|
|
server := httptest.NewServer(&handler)
|
|
|
|
|
defer server.Close()
|
|
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
|
|
|
|
|
client := fake.NewSimpleClientset()
|
|
|
|
|
factory := newConfigFactory(client, v1.DefaultHardPodAffinitySymmetricWeight)
|
|
|
|
|
|
|
|
|
|
// Pre-register some predicate and priority functions
|
|
|
|
@@ -116,14 +100,7 @@ func TestCreateFromConfigWithHardPodAffinitySymmetricWeight(t *testing.T) {
|
|
|
|
|
var configData []byte
|
|
|
|
|
var policy schedulerapi.Policy
|
|
|
|
|
|
|
|
|
|
handler := utiltesting.FakeHandler{
|
|
|
|
|
StatusCode: 500,
|
|
|
|
|
ResponseBody: "",
|
|
|
|
|
T: t,
|
|
|
|
|
}
|
|
|
|
|
server := httptest.NewServer(&handler)
|
|
|
|
|
defer server.Close()
|
|
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
|
|
|
|
|
client := fake.NewSimpleClientset()
|
|
|
|
|
factory := newConfigFactory(client, v1.DefaultHardPodAffinitySymmetricWeight)
|
|
|
|
|
|
|
|
|
|
// Pre-register some predicate and priority functions
|
|
|
|
@@ -162,14 +139,7 @@ func TestCreateFromEmptyConfig(t *testing.T) {
|
|
|
|
|
var configData []byte
|
|
|
|
|
var policy schedulerapi.Policy
|
|
|
|
|
|
|
|
|
|
handler := utiltesting.FakeHandler{
|
|
|
|
|
StatusCode: 500,
|
|
|
|
|
ResponseBody: "",
|
|
|
|
|
T: t,
|
|
|
|
|
}
|
|
|
|
|
server := httptest.NewServer(&handler)
|
|
|
|
|
defer server.Close()
|
|
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
|
|
|
|
|
client := fake.NewSimpleClientset()
|
|
|
|
|
factory := newConfigFactory(client, v1.DefaultHardPodAffinitySymmetricWeight)
|
|
|
|
|
|
|
|
|
|
configData = []byte(`{}`)
|
|
|
|
@@ -184,14 +154,7 @@ func TestCreateFromEmptyConfig(t *testing.T) {
|
|
|
|
|
// predicate/priority.
|
|
|
|
|
// The predicate/priority from DefaultProvider will be used.
|
|
|
|
|
func TestCreateFromConfigWithUnspecifiedPredicatesOrPriorities(t *testing.T) {
|
|
|
|
|
handler := utiltesting.FakeHandler{
|
|
|
|
|
StatusCode: 500,
|
|
|
|
|
ResponseBody: "",
|
|
|
|
|
T: t,
|
|
|
|
|
}
|
|
|
|
|
server := httptest.NewServer(&handler)
|
|
|
|
|
defer server.Close()
|
|
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
|
|
|
|
|
client := fake.NewSimpleClientset()
|
|
|
|
|
factory := newConfigFactory(client, v1.DefaultHardPodAffinitySymmetricWeight)
|
|
|
|
|
|
|
|
|
|
RegisterFitPredicate("PredicateOne", PredicateOne)
|
|
|
|
@@ -224,14 +187,7 @@ func TestCreateFromConfigWithUnspecifiedPredicatesOrPriorities(t *testing.T) {
|
|
|
|
|
// predicate/priority.
|
|
|
|
|
// Empty predicate/priority sets will be used.
|
|
|
|
|
func TestCreateFromConfigWithEmptyPredicatesOrPriorities(t *testing.T) {
|
|
|
|
|
handler := utiltesting.FakeHandler{
|
|
|
|
|
StatusCode: 500,
|
|
|
|
|
ResponseBody: "",
|
|
|
|
|
T: t,
|
|
|
|
|
}
|
|
|
|
|
server := httptest.NewServer(&handler)
|
|
|
|
|
defer server.Close()
|
|
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
|
|
|
|
|
client := fake.NewSimpleClientset()
|
|
|
|
|
factory := newConfigFactory(client, v1.DefaultHardPodAffinitySymmetricWeight)
|
|
|
|
|
|
|
|
|
|
RegisterFitPredicate("PredicateOne", PredicateOne)
|
|
|
|
@@ -283,24 +239,14 @@ func TestDefaultErrorFunc(t *testing.T) {
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "bar"},
|
|
|
|
|
Spec: apitesting.V1DeepEqualSafePodSpec(),
|
|
|
|
|
}
|
|
|
|
|
handler := utiltesting.FakeHandler{
|
|
|
|
|
StatusCode: 200,
|
|
|
|
|
ResponseBody: runtime.EncodeOrDie(schedulertesting.Test.Codec(), testPod),
|
|
|
|
|
T: t,
|
|
|
|
|
}
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
|
|
// FakeHandler mustn't be sent requests other than the one you want to test.
|
|
|
|
|
mux.Handle(schedulertesting.Test.ResourcePath(string(v1.ResourcePods), "bar", "foo"), &handler)
|
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
|
defer server.Close()
|
|
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
|
|
|
|
|
client := fake.NewSimpleClientset(&v1.PodList{Items: []v1.Pod{*testPod}})
|
|
|
|
|
factory := newConfigFactory(client, v1.DefaultHardPodAffinitySymmetricWeight)
|
|
|
|
|
queue := &internalqueue.FIFO{FIFO: cache.NewFIFO(cache.MetaNamespaceKeyFunc)}
|
|
|
|
|
podBackoff := util.CreatePodBackoff(1*time.Millisecond, 1*time.Second)
|
|
|
|
|
errFunc := factory.MakeDefaultErrorFunc(podBackoff, queue)
|
|
|
|
|
|
|
|
|
|
errFunc(testPod, nil)
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
// This is a terrible way to do this but I plan on replacing this
|
|
|
|
|
// whole error handling system in the future. The test will time
|
|
|
|
@@ -310,7 +256,27 @@ func TestDefaultErrorFunc(t *testing.T) {
|
|
|
|
|
if !exists {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
handler.ValidateRequest(t, schedulertesting.Test.ResourcePath(string(v1.ResourcePods), "bar", "foo"), "GET", nil)
|
|
|
|
|
requestReceived := false
|
|
|
|
|
actions := client.Actions()
|
|
|
|
|
for _, a := range actions {
|
|
|
|
|
if a.GetVerb() == "get" {
|
|
|
|
|
getAction, ok := a.(clienttesting.GetAction)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Errorf("Can't cast action object to GetAction interface")
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
name := getAction.GetName()
|
|
|
|
|
ns := a.GetNamespace()
|
|
|
|
|
if name != "foo" || ns != "bar" {
|
|
|
|
|
t.Errorf("Expected name %s namespace %s, got %s %s",
|
|
|
|
|
"foo", "bar", name, ns)
|
|
|
|
|
}
|
|
|
|
|
requestReceived = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !requestReceived {
|
|
|
|
|
t.Errorf("Get pod request not received")
|
|
|
|
|
}
|
|
|
|
|
if e, a := testPod, got; !reflect.DeepEqual(e, a) {
|
|
|
|
|
t.Errorf("Expected %v, got %v", e, a)
|
|
|
|
|
}
|
|
|
|
@@ -371,35 +337,38 @@ func TestBind(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testBind(binding *v1.Binding, t *testing.T) {
|
|
|
|
|
handler := utiltesting.FakeHandler{
|
|
|
|
|
StatusCode: 200,
|
|
|
|
|
ResponseBody: "",
|
|
|
|
|
T: t,
|
|
|
|
|
testPod := &v1.Pod{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: binding.GetName(), Namespace: metav1.NamespaceDefault},
|
|
|
|
|
Spec: apitesting.V1DeepEqualSafePodSpec(),
|
|
|
|
|
}
|
|
|
|
|
server := httptest.NewServer(&handler)
|
|
|
|
|
defer server.Close()
|
|
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
|
|
|
|
|
client := fake.NewSimpleClientset(&v1.PodList{Items: []v1.Pod{*testPod}})
|
|
|
|
|
|
|
|
|
|
b := binder{client}
|
|
|
|
|
|
|
|
|
|
if err := b.Bind(binding); err != nil {
|
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pod := client.CoreV1().Pods(metav1.NamespaceDefault).(*fakeV1.FakePods)
|
|
|
|
|
|
|
|
|
|
bind, err := pod.GetBinding(binding.GetName())
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expectedBody := runtime.EncodeOrDie(schedulertesting.Test.Codec(), binding)
|
|
|
|
|
handler.ValidateRequest(t,
|
|
|
|
|
schedulertesting.Test.SubResourcePath(string(v1.ResourcePods), metav1.NamespaceDefault, "foo", "binding"),
|
|
|
|
|
"POST", &expectedBody)
|
|
|
|
|
bind.APIVersion = ""
|
|
|
|
|
bind.Kind = ""
|
|
|
|
|
body := runtime.EncodeOrDie(schedulertesting.Test.Codec(), bind)
|
|
|
|
|
if expectedBody != body {
|
|
|
|
|
t.Errorf("Expected body %s, Got %s", expectedBody, body)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInvalidHardPodAffinitySymmetricWeight(t *testing.T) {
|
|
|
|
|
handler := utiltesting.FakeHandler{
|
|
|
|
|
StatusCode: 500,
|
|
|
|
|
ResponseBody: "",
|
|
|
|
|
T: t,
|
|
|
|
|
}
|
|
|
|
|
server := httptest.NewServer(&handler)
|
|
|
|
|
defer server.Close()
|
|
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
|
|
|
|
|
client := fake.NewSimpleClientset()
|
|
|
|
|
// factory of "default-scheduler"
|
|
|
|
|
factory := newConfigFactory(client, -1)
|
|
|
|
|
_, err := factory.Create()
|
|
|
|
@@ -409,14 +378,7 @@ func TestInvalidHardPodAffinitySymmetricWeight(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInvalidFactoryArgs(t *testing.T) {
|
|
|
|
|
handler := utiltesting.FakeHandler{
|
|
|
|
|
StatusCode: 500,
|
|
|
|
|
ResponseBody: "",
|
|
|
|
|
T: t,
|
|
|
|
|
}
|
|
|
|
|
server := httptest.NewServer(&handler)
|
|
|
|
|
defer server.Close()
|
|
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
|
|
|
|
|
client := fake.NewSimpleClientset()
|
|
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
|
name string
|
|
|
|
@@ -539,7 +501,7 @@ func TestSkipPodUpdate(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newConfigFactory(client *clientset.Clientset, hardPodAffinitySymmetricWeight int32) Configurator {
|
|
|
|
|
func newConfigFactory(client clientset.Interface, hardPodAffinitySymmetricWeight int32) Configurator {
|
|
|
|
|
informerFactory := informers.NewSharedInformerFactory(client, 0)
|
|
|
|
|
return NewConfigFactory(&ConfigFactoryArgs{
|
|
|
|
|
v1.DefaultSchedulerName,
|
|
|
|
|