2014-08-20 21:34:55 +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 factory
|
|
|
|
|
|
|
|
import (
|
2014-08-29 03:18:27 +00:00
|
|
|
"net/http"
|
2014-08-20 21:34:55 +00:00
|
|
|
"net/http/httptest"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-08-20 22:03:32 +00:00
|
|
|
"time"
|
2014-08-20 21:34:55 +00:00
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-09-11 17:02:53 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
2014-09-30 00:15:00 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
|
2014-08-20 21:34:55 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-09-02 17:55:27 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-08-20 21:34:55 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreate(t *testing.T) {
|
|
|
|
handler := util.FakeHandler{
|
|
|
|
StatusCode: 500,
|
|
|
|
ResponseBody: "",
|
|
|
|
T: t,
|
|
|
|
}
|
|
|
|
server := httptest.NewServer(&handler)
|
2014-09-30 00:15:00 +00:00
|
|
|
client := client.NewOrDie(&client.Config{Host: server.URL, Version: testapi.Version()})
|
2014-08-28 13:56:38 +00:00
|
|
|
factory := ConfigFactory{client}
|
2014-08-20 21:34:55 +00:00
|
|
|
factory.Create()
|
|
|
|
}
|
|
|
|
|
2014-09-16 21:08:57 +00:00
|
|
|
func TestCreateLists(t *testing.T) {
|
|
|
|
factory := ConfigFactory{nil}
|
|
|
|
table := []struct {
|
|
|
|
location string
|
|
|
|
factory func() *listWatch
|
|
|
|
}{
|
|
|
|
// Minion
|
|
|
|
{
|
2014-09-30 00:15:00 +00:00
|
|
|
location: "/api/" + testapi.Version() + "/minions?fields=",
|
2014-09-16 21:08:57 +00:00
|
|
|
factory: factory.createMinionLW,
|
|
|
|
},
|
|
|
|
// Assigned pod
|
|
|
|
{
|
2014-09-30 00:15:00 +00:00
|
|
|
location: "/api/" + testapi.Version() + "/pods?fields=DesiredState.Host!%3D",
|
2014-09-16 21:08:57 +00:00
|
|
|
factory: factory.createAssignedPodLW,
|
|
|
|
},
|
|
|
|
// Unassigned pod
|
|
|
|
{
|
2014-09-30 00:15:00 +00:00
|
|
|
location: "/api/" + testapi.Version() + "/pods?fields=DesiredState.Host%3D",
|
2014-09-16 21:08:57 +00:00
|
|
|
factory: factory.createUnassignedPodLW,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range table {
|
|
|
|
handler := util.FakeHandler{
|
|
|
|
StatusCode: 500,
|
|
|
|
ResponseBody: "",
|
|
|
|
T: t,
|
|
|
|
}
|
|
|
|
server := httptest.NewServer(&handler)
|
2014-09-30 00:15:00 +00:00
|
|
|
factory.Client = client.NewOrDie(&client.Config{Host: server.URL, Version: testapi.Version()})
|
2014-09-16 21:08:57 +00:00
|
|
|
// This test merely tests that the correct request is made.
|
|
|
|
item.factory().List()
|
|
|
|
handler.ValidateRequest(t, item.location, "GET", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 21:34:55 +00:00
|
|
|
func TestCreateWatches(t *testing.T) {
|
|
|
|
factory := ConfigFactory{nil}
|
|
|
|
table := []struct {
|
2014-10-07 20:51:28 +00:00
|
|
|
rv string
|
2014-09-16 21:08:57 +00:00
|
|
|
location string
|
|
|
|
factory func() *listWatch
|
2014-08-20 21:34:55 +00:00
|
|
|
}{
|
|
|
|
// Minion watch
|
|
|
|
{
|
2014-10-07 20:51:28 +00:00
|
|
|
rv: "",
|
|
|
|
location: "/api/" + testapi.Version() + "/watch/minions?fields=&resourceVersion=",
|
|
|
|
factory: factory.createMinionLW,
|
|
|
|
}, {
|
|
|
|
rv: "0",
|
2014-09-30 00:15:00 +00:00
|
|
|
location: "/api/" + testapi.Version() + "/watch/minions?fields=&resourceVersion=0",
|
2014-09-16 21:08:57 +00:00
|
|
|
factory: factory.createMinionLW,
|
2014-08-20 21:34:55 +00:00
|
|
|
}, {
|
2014-10-07 20:51:28 +00:00
|
|
|
rv: "42",
|
2014-09-30 00:15:00 +00:00
|
|
|
location: "/api/" + testapi.Version() + "/watch/minions?fields=&resourceVersion=42",
|
2014-09-16 21:08:57 +00:00
|
|
|
factory: factory.createMinionLW,
|
2014-08-20 21:34:55 +00:00
|
|
|
},
|
|
|
|
// Assigned pod watches
|
|
|
|
{
|
2014-10-07 20:51:28 +00:00
|
|
|
rv: "",
|
|
|
|
location: "/api/" + testapi.Version() + "/watch/pods?fields=DesiredState.Host!%3D&resourceVersion=",
|
2014-09-16 21:08:57 +00:00
|
|
|
factory: factory.createAssignedPodLW,
|
2014-08-20 21:34:55 +00:00
|
|
|
}, {
|
2014-10-07 20:51:28 +00:00
|
|
|
rv: "42",
|
2014-09-30 00:15:00 +00:00
|
|
|
location: "/api/" + testapi.Version() + "/watch/pods?fields=DesiredState.Host!%3D&resourceVersion=42",
|
2014-09-16 21:08:57 +00:00
|
|
|
factory: factory.createAssignedPodLW,
|
2014-08-20 21:34:55 +00:00
|
|
|
},
|
|
|
|
// Unassigned pod watches
|
|
|
|
{
|
2014-10-07 20:51:28 +00:00
|
|
|
rv: "",
|
|
|
|
location: "/api/" + testapi.Version() + "/watch/pods?fields=DesiredState.Host%3D&resourceVersion=",
|
2014-09-16 21:08:57 +00:00
|
|
|
factory: factory.createUnassignedPodLW,
|
2014-08-20 21:34:55 +00:00
|
|
|
}, {
|
2014-10-07 20:51:28 +00:00
|
|
|
rv: "42",
|
2014-09-30 00:15:00 +00:00
|
|
|
location: "/api/" + testapi.Version() + "/watch/pods?fields=DesiredState.Host%3D&resourceVersion=42",
|
2014-09-16 21:08:57 +00:00
|
|
|
factory: factory.createUnassignedPodLW,
|
2014-08-20 21:34:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range table {
|
|
|
|
handler := util.FakeHandler{
|
|
|
|
StatusCode: 500,
|
|
|
|
ResponseBody: "",
|
|
|
|
T: t,
|
|
|
|
}
|
|
|
|
server := httptest.NewServer(&handler)
|
2014-09-30 00:15:00 +00:00
|
|
|
factory.Client = client.NewOrDie(&client.Config{Host: server.URL, Version: testapi.Version()})
|
2014-08-20 21:34:55 +00:00
|
|
|
// This test merely tests that the correct request is made.
|
2014-09-16 21:08:57 +00:00
|
|
|
item.factory().Watch(item.rv)
|
2014-08-20 21:34:55 +00:00
|
|
|
handler.ValidateRequest(t, item.location, "GET", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPollMinions(t *testing.T) {
|
|
|
|
table := []struct {
|
|
|
|
minions []api.Minion
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
minions: []api.Minion{
|
2014-10-22 17:02:02 +00:00
|
|
|
{TypeMeta: api.TypeMeta{Name: "foo"}},
|
|
|
|
{TypeMeta: api.TypeMeta{Name: "bar"}},
|
2014-08-20 21:34:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range table {
|
|
|
|
ml := &api.MinionList{Items: item.minions}
|
|
|
|
handler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
2014-09-11 17:02:53 +00:00
|
|
|
ResponseBody: runtime.EncodeOrDie(latest.Codec, ml),
|
2014-08-20 21:34:55 +00:00
|
|
|
T: t,
|
|
|
|
}
|
2014-08-29 03:18:27 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
// FakeHandler musn't be sent requests other than the one you want to test.
|
2014-09-30 00:15:00 +00:00
|
|
|
mux.Handle("/api/"+testapi.Version()+"/minions", &handler)
|
2014-08-29 03:18:27 +00:00
|
|
|
server := httptest.NewServer(mux)
|
2014-09-30 00:15:00 +00:00
|
|
|
client := client.NewOrDie(&client.Config{Host: server.URL, Version: testapi.Version()})
|
2014-08-28 13:56:38 +00:00
|
|
|
cf := ConfigFactory{client}
|
2014-08-20 21:34:55 +00:00
|
|
|
|
|
|
|
ce, err := cf.pollMinions()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2014-09-30 00:15:00 +00:00
|
|
|
handler.ValidateRequest(t, "/api/"+testapi.Version()+"/minions", "GET", nil)
|
2014-08-20 21:34:55 +00:00
|
|
|
|
|
|
|
if e, a := len(item.minions), ce.Len(); e != a {
|
|
|
|
t.Errorf("Expected %v, got %v", e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 22:03:32 +00:00
|
|
|
func TestDefaultErrorFunc(t *testing.T) {
|
2014-10-22 17:02:02 +00:00
|
|
|
testPod := &api.Pod{TypeMeta: api.TypeMeta{Name: "foo"}}
|
2014-08-20 22:03:32 +00:00
|
|
|
handler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
2014-09-11 17:02:53 +00:00
|
|
|
ResponseBody: runtime.EncodeOrDie(latest.Codec, testPod),
|
2014-08-20 22:03:32 +00:00
|
|
|
T: t,
|
|
|
|
}
|
2014-08-29 03:18:27 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
// FakeHandler musn't be sent requests other than the one you want to test.
|
2014-09-30 00:15:00 +00:00
|
|
|
mux.Handle("/api/"+testapi.Version()+"/pods/foo", &handler)
|
2014-08-29 03:18:27 +00:00
|
|
|
server := httptest.NewServer(mux)
|
2014-09-30 00:15:00 +00:00
|
|
|
factory := ConfigFactory{client.NewOrDie(&client.Config{Host: server.URL, Version: testapi.Version()})}
|
2014-08-20 22:03:32 +00:00
|
|
|
queue := cache.NewFIFO()
|
2014-10-03 16:59:39 +00:00
|
|
|
podBackoff := podBackoff{
|
|
|
|
perPodBackoff: map[string]*backoffEntry{},
|
|
|
|
clock: &fakeClock{},
|
|
|
|
}
|
|
|
|
errFunc := factory.makeDefaultErrorFunc(&podBackoff, queue)
|
2014-08-20 22:03:32 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
// out if something doesn't work.
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
got, exists := queue.Get("foo")
|
|
|
|
if !exists {
|
|
|
|
continue
|
|
|
|
}
|
2014-09-30 00:15:00 +00:00
|
|
|
handler.ValidateRequest(t, "/api/"+testapi.Version()+"/pods/foo", "GET", nil)
|
2014-08-20 22:03:32 +00:00
|
|
|
if e, a := testPod, got; !reflect.DeepEqual(e, a) {
|
|
|
|
t.Errorf("Expected %v, got %v", e, a)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 21:34:55 +00:00
|
|
|
func TestStoreToMinionLister(t *testing.T) {
|
|
|
|
store := cache.NewStore()
|
|
|
|
ids := util.NewStringSet("foo", "bar", "baz")
|
|
|
|
for id := range ids {
|
2014-10-22 17:02:02 +00:00
|
|
|
store.Add(id, &api.Minion{TypeMeta: api.TypeMeta{Name: id}})
|
2014-08-20 21:34:55 +00:00
|
|
|
}
|
|
|
|
sml := storeToMinionLister{store}
|
|
|
|
|
2014-10-06 22:58:18 +00:00
|
|
|
gotNodes, err := sml.List()
|
2014-08-20 21:34:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
2014-10-06 22:58:18 +00:00
|
|
|
got := make([]string, len(gotNodes.Items))
|
|
|
|
for ix := range gotNodes.Items {
|
2014-10-22 17:02:02 +00:00
|
|
|
got[ix] = gotNodes.Items[ix].Name
|
2014-10-06 22:58:18 +00:00
|
|
|
}
|
2014-08-20 21:34:55 +00:00
|
|
|
if !ids.HasAll(got...) || len(got) != len(ids) {
|
|
|
|
t.Errorf("Expected %v, got %v", ids, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStoreToPodLister(t *testing.T) {
|
|
|
|
store := cache.NewStore()
|
|
|
|
ids := []string{"foo", "bar", "baz"}
|
|
|
|
for _, id := range ids {
|
|
|
|
store.Add(id, &api.Pod{
|
2014-10-22 17:02:02 +00:00
|
|
|
TypeMeta: api.TypeMeta{Name: id},
|
2014-08-20 21:34:55 +00:00
|
|
|
Labels: map[string]string{"name": id},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
spl := storeToPodLister{store}
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
got, err := spl.ListPods(labels.Set{"name": id}.AsSelector())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if e, a := 1, len(got); e != a {
|
|
|
|
t.Errorf("Expected %v, got %v", e, a)
|
|
|
|
continue
|
|
|
|
}
|
2014-10-22 17:02:02 +00:00
|
|
|
if e, a := id, got[0].Name; e != a {
|
2014-08-20 21:34:55 +00:00
|
|
|
t.Errorf("Expected %v, got %v", e, a)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMinionEnumerator(t *testing.T) {
|
|
|
|
testList := &api.MinionList{
|
|
|
|
Items: []api.Minion{
|
2014-10-22 17:02:02 +00:00
|
|
|
{TypeMeta: api.TypeMeta{Name: "foo"}},
|
|
|
|
{TypeMeta: api.TypeMeta{Name: "bar"}},
|
|
|
|
{TypeMeta: api.TypeMeta{Name: "baz"}},
|
2014-08-20 21:34:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
me := minionEnumerator{testList}
|
|
|
|
|
|
|
|
if e, a := 3, me.Len(); e != a {
|
|
|
|
t.Fatalf("expected %v, got %v", e, a)
|
|
|
|
}
|
|
|
|
for i := range testList.Items {
|
|
|
|
gotID, gotObj := me.Get(i)
|
2014-10-22 17:02:02 +00:00
|
|
|
if e, a := testList.Items[i].Name, gotID; e != a {
|
2014-08-20 21:34:55 +00:00
|
|
|
t.Errorf("Expected %v, got %v", e, a)
|
|
|
|
}
|
|
|
|
if e, a := &testList.Items[i], gotObj; !reflect.DeepEqual(e, a) {
|
|
|
|
t.Errorf("Expected %#v, got %v#", e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-03 16:59:39 +00:00
|
|
|
type fakeClock struct {
|
|
|
|
t time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeClock) Now() time.Time {
|
|
|
|
return f.t
|
|
|
|
}
|
|
|
|
|
2014-08-20 21:34:55 +00:00
|
|
|
func TestBind(t *testing.T) {
|
|
|
|
table := []struct {
|
|
|
|
binding *api.Binding
|
|
|
|
}{
|
|
|
|
{binding: &api.Binding{PodID: "foo", Host: "foohost.kubernetes.mydomain.com"}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range table {
|
|
|
|
handler := util.FakeHandler{
|
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: "",
|
|
|
|
T: t,
|
|
|
|
}
|
|
|
|
server := httptest.NewServer(&handler)
|
2014-09-30 00:15:00 +00:00
|
|
|
client := client.NewOrDie(&client.Config{Host: server.URL, Version: testapi.Version()})
|
2014-08-28 13:56:38 +00:00
|
|
|
b := binder{client}
|
2014-08-20 21:34:55 +00:00
|
|
|
|
2014-08-28 13:56:38 +00:00
|
|
|
if err := b.Bind(item.binding); err != nil {
|
2014-08-20 21:34:55 +00:00
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2014-10-09 20:13:08 +00:00
|
|
|
expectedBody := runtime.EncodeOrDie(testapi.Codec(), item.binding)
|
2014-09-30 00:15:00 +00:00
|
|
|
handler.ValidateRequest(t, "/api/"+testapi.Version()+"/bindings", "POST", &expectedBody)
|
2014-08-20 21:34:55 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-03 16:59:39 +00:00
|
|
|
|
|
|
|
func TestBackoff(t *testing.T) {
|
|
|
|
clock := fakeClock{}
|
|
|
|
backoff := podBackoff{
|
|
|
|
perPodBackoff: map[string]*backoffEntry{},
|
|
|
|
clock: &clock,
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
podID string
|
|
|
|
expectedDuration time.Duration
|
|
|
|
advanceClock time.Duration
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
podID: "foo",
|
|
|
|
expectedDuration: 1 * time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
podID: "foo",
|
|
|
|
expectedDuration: 2 * time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
podID: "foo",
|
|
|
|
expectedDuration: 4 * time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
podID: "bar",
|
|
|
|
expectedDuration: 1 * time.Second,
|
|
|
|
advanceClock: 120 * time.Second,
|
|
|
|
},
|
|
|
|
// 'foo' should have been gc'd here.
|
|
|
|
{
|
|
|
|
podID: "foo",
|
|
|
|
expectedDuration: 1 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
duration := backoff.getBackoff(test.podID)
|
|
|
|
if duration != test.expectedDuration {
|
|
|
|
t.Errorf("expected: %s, got %s for %s", test.expectedDuration.String(), duration.String(), test.podID)
|
|
|
|
}
|
|
|
|
clock.t = clock.t.Add(test.advanceClock)
|
|
|
|
backoff.gc()
|
|
|
|
}
|
|
|
|
|
|
|
|
backoff.perPodBackoff["foo"].backoff = 60 * time.Second
|
|
|
|
duration := backoff.getBackoff("foo")
|
|
|
|
if duration != 60*time.Second {
|
|
|
|
t.Errorf("expected: 60, got %s", duration.String())
|
|
|
|
}
|
|
|
|
}
|