k3s/pkg/service/endpoints_controller_test.go

415 lines
11 KiB
Go
Raw Normal View History

2014-06-06 23:40:48 +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.
*/
2014-06-23 18:32:11 +00:00
package service
2014-06-06 23:40:48 +00:00
import (
"fmt"
"net/http"
"net/http/httptest"
2014-06-06 23:40:48 +00:00
"testing"
2014-06-12 20:17:34 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
2014-06-06 23:40:48 +00:00
)
func newPodList(count int) *api.PodList {
pods := []api.Pod{}
for i := 0; i < count; i++ {
pods = append(pods, api.Pod{
TypeMeta: api.TypeMeta{APIVersion: testapi.Version()},
ObjectMeta: api.ObjectMeta{Name: fmt.Sprintf("pod%d", i)},
2014-11-13 15:52:13 +00:00
Spec: api.PodSpec{
Containers: []api.Container{
{
Ports: []api.Port{
{
ContainerPort: 8080,
},
},
},
},
},
2014-11-13 15:52:13 +00:00
Status: api.PodStatus{
PodIP: "1.2.3.4",
},
})
}
return &api.PodList{
TypeMeta: api.TypeMeta{APIVersion: testapi.Version(), Kind: "PodList"},
2014-07-16 21:30:28 +00:00
Items: pods,
}
}
func TestFindPort(t *testing.T) {
2014-11-13 15:52:13 +00:00
pod := api.Pod{
Spec: api.PodSpec{
Containers: []api.Container{
{
Ports: []api.Port{
{
Name: "foo",
ContainerPort: 8080,
HostPort: 9090,
},
{
Name: "bar",
ContainerPort: 8000,
HostPort: 9000,
},
},
},
},
},
}
2014-11-13 15:52:13 +00:00
emptyPortsPod := api.Pod{
Spec: api.PodSpec{
Containers: []api.Container{
{
Ports: []api.Port{},
},
},
},
}
2014-11-13 15:52:13 +00:00
tests := []struct {
2014-11-13 15:52:13 +00:00
pod api.Pod
portName util.IntOrString
wport int
werr bool
}{
{
2014-11-13 15:52:13 +00:00
pod,
util.IntOrString{Kind: util.IntstrString, StrVal: "foo"},
8080,
false,
},
{
2014-11-13 15:52:13 +00:00
pod,
util.IntOrString{Kind: util.IntstrString, StrVal: "bar"},
8000,
false,
},
{
2014-11-13 15:52:13 +00:00
pod,
util.IntOrString{Kind: util.IntstrInt, IntVal: 8000},
8000,
false,
},
{
2014-11-13 15:52:13 +00:00
pod,
util.IntOrString{Kind: util.IntstrInt, IntVal: 7000},
7000,
false,
},
{
2014-11-13 15:52:13 +00:00
pod,
util.IntOrString{Kind: util.IntstrString, StrVal: "baz"},
0,
true,
},
{
2014-11-13 15:52:13 +00:00
pod,
util.IntOrString{Kind: util.IntstrString, StrVal: ""},
8080,
false,
},
{
2014-11-13 15:52:13 +00:00
pod,
util.IntOrString{Kind: util.IntstrInt, IntVal: 0},
8080,
false,
},
{
2014-11-13 15:52:13 +00:00
emptyPortsPod,
util.IntOrString{Kind: util.IntstrString, StrVal: ""},
0,
true,
},
{
2014-11-13 15:52:13 +00:00
emptyPortsPod,
util.IntOrString{Kind: util.IntstrInt, IntVal: 0},
0,
true,
},
2014-08-03 23:51:34 +00:00
}
for _, test := range tests {
2014-11-13 15:52:13 +00:00
port, err := findPort(&test.pod, test.portName)
if port != test.wport {
t.Errorf("Expected port %d, Got %d", test.wport, port)
}
if err == nil && test.werr {
t.Errorf("unexpected non-error")
}
if err != nil && test.werr == false {
t.Errorf("unexpected error: %v", err)
}
}
}
type serverResponse struct {
statusCode int
obj interface{}
}
func makeTestServer(t *testing.T, podResponse serverResponse, serviceResponse serverResponse, endpointsResponse serverResponse) (*httptest.Server, *util.FakeHandler) {
fakePodHandler := util.FakeHandler{
StatusCode: podResponse.statusCode,
ResponseBody: runtime.EncodeOrDie(testapi.Codec(), podResponse.obj.(runtime.Object)),
}
fakeServiceHandler := util.FakeHandler{
StatusCode: serviceResponse.statusCode,
ResponseBody: runtime.EncodeOrDie(testapi.Codec(), serviceResponse.obj.(runtime.Object)),
}
fakeEndpointsHandler := util.FakeHandler{
StatusCode: endpointsResponse.statusCode,
ResponseBody: runtime.EncodeOrDie(testapi.Codec(), endpointsResponse.obj.(runtime.Object)),
}
mux := http.NewServeMux()
mux.Handle("/api/"+testapi.Version()+"/pods", &fakePodHandler)
mux.Handle("/api/"+testapi.Version()+"/services", &fakeServiceHandler)
mux.Handle("/api/"+testapi.Version()+"/endpoints", &fakeEndpointsHandler)
mux.Handle("/api/"+testapi.Version()+"/endpoints/", &fakeEndpointsHandler)
mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
t.Errorf("unexpected request: %v", req.RequestURI)
res.WriteHeader(http.StatusNotFound)
})
return httptest.NewServer(mux), &fakeEndpointsHandler
}
2014-06-06 23:40:48 +00:00
func TestSyncEndpointsEmpty(t *testing.T) {
testServer, _ := makeTestServer(t,
serverResponse{http.StatusOK, newPodList(0)},
serverResponse{http.StatusOK, &api.ServiceList{}},
serverResponse{http.StatusOK, &api.Endpoints{}})
2014-10-23 00:14:39 +00:00
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})
endpoints := NewEndpointController(client)
if err := endpoints.SyncServiceEndpoints(); err != nil {
2014-08-03 23:51:34 +00:00
t.Errorf("unexpected error: %v", err)
}
2014-06-06 23:40:48 +00:00
}
func TestSyncEndpointsError(t *testing.T) {
testServer, _ := makeTestServer(t,
serverResponse{http.StatusOK, newPodList(0)},
serverResponse{http.StatusInternalServerError, &api.ServiceList{}},
serverResponse{http.StatusOK, &api.Endpoints{}})
2014-10-23 00:14:39 +00:00
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})
endpoints := NewEndpointController(client)
if err := endpoints.SyncServiceEndpoints(); err == nil {
t.Errorf("unexpected non-error")
2014-06-06 23:40:48 +00:00
}
}
2014-11-18 17:49:00 +00:00
func TestSyncEndpointsItemsPreserveNoSelector(t *testing.T) {
serviceList := api.ServiceList{
Items: []api.Service{
{
ObjectMeta: api.ObjectMeta{Name: "foo"},
Spec: api.ServiceSpec{},
},
},
}
testServer, endpointsHandler := makeTestServer(t,
serverResponse{http.StatusOK, newPodList(0)},
serverResponse{http.StatusOK, &serviceList},
serverResponse{http.StatusOK, &api.Endpoints{
ObjectMeta: api.ObjectMeta{
Name: "foo",
ResourceVersion: "1",
},
Endpoints: []string{"6.7.8.9:1000"},
}})
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})
endpoints := NewEndpointController(client)
if err := endpoints.SyncServiceEndpoints(); err != nil {
t.Errorf("unexpected error: %v", err)
}
endpointsHandler.ValidateRequestCount(t, 0)
}
func TestSyncEndpointsItemsEmptySelectorSelectsAll(t *testing.T) {
serviceList := api.ServiceList{
Items: []api.Service{
{
ObjectMeta: api.ObjectMeta{Name: "foo"},
Spec: api.ServiceSpec{
Selector: map[string]string{},
},
},
},
}
testServer, endpointsHandler := makeTestServer(t,
serverResponse{http.StatusOK, newPodList(1)},
serverResponse{http.StatusOK, &serviceList},
serverResponse{http.StatusOK, &api.Endpoints{
ObjectMeta: api.ObjectMeta{
Name: "foo",
ResourceVersion: "1",
},
Endpoints: []string{},
}})
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})
endpoints := NewEndpointController(client)
if err := endpoints.SyncServiceEndpoints(); err != nil {
t.Errorf("unexpected error: %v", err)
}
data := runtime.EncodeOrDie(testapi.Codec(), &api.Endpoints{
ObjectMeta: api.ObjectMeta{
Name: "foo",
ResourceVersion: "1",
},
Endpoints: []string{"1.2.3.4:8080"},
})
endpointsHandler.ValidateRequest(t, "/api/"+testapi.Version()+"/endpoints/foo", "PUT", &data)
}
func TestSyncEndpointsItemsPreexisting(t *testing.T) {
serviceList := api.ServiceList{
Items: []api.Service{
{
ObjectMeta: api.ObjectMeta{Name: "foo"},
2014-10-30 13:29:11 +00:00
Spec: api.ServiceSpec{
Selector: map[string]string{
"foo": "bar",
},
},
},
},
}
testServer, endpointsHandler := makeTestServer(t,
serverResponse{http.StatusOK, newPodList(1)},
serverResponse{http.StatusOK, &serviceList},
serverResponse{http.StatusOK, &api.Endpoints{
ObjectMeta: api.ObjectMeta{
2014-10-22 17:02:02 +00:00
Name: "foo",
ResourceVersion: "1",
},
Endpoints: []string{"6.7.8.9:1000"},
}})
2014-10-23 00:14:39 +00:00
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})
endpoints := NewEndpointController(client)
if err := endpoints.SyncServiceEndpoints(); err != nil {
t.Errorf("unexpected error: %v", err)
}
data := runtime.EncodeOrDie(testapi.Codec(), &api.Endpoints{
ObjectMeta: api.ObjectMeta{
2014-10-22 17:02:02 +00:00
Name: "foo",
ResourceVersion: "1",
},
Endpoints: []string{"1.2.3.4:8080"},
})
endpointsHandler.ValidateRequest(t, "/api/"+testapi.Version()+"/endpoints/foo", "PUT", &data)
}
func TestSyncEndpointsItemsPreexistingIdentical(t *testing.T) {
serviceList := api.ServiceList{
Items: []api.Service{
{
ObjectMeta: api.ObjectMeta{Name: "foo"},
2014-10-30 13:29:11 +00:00
Spec: api.ServiceSpec{
Selector: map[string]string{
"foo": "bar",
},
2014-06-06 23:40:48 +00:00
},
},
},
}
testServer, endpointsHandler := makeTestServer(t,
serverResponse{http.StatusOK, newPodList(1)},
serverResponse{http.StatusOK, &serviceList},
serverResponse{http.StatusOK, &api.Endpoints{
ObjectMeta: api.ObjectMeta{
ResourceVersion: "1",
},
Endpoints: []string{"1.2.3.4:8080"},
}})
2014-10-23 00:14:39 +00:00
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})
endpoints := NewEndpointController(client)
if err := endpoints.SyncServiceEndpoints(); err != nil {
2014-08-03 23:51:34 +00:00
t.Errorf("unexpected error: %v", err)
}
endpointsHandler.ValidateRequest(t, "/api/"+testapi.Version()+"/endpoints/foo", "GET", nil)
}
func TestSyncEndpointsItems(t *testing.T) {
serviceList := api.ServiceList{
Items: []api.Service{
{
ObjectMeta: api.ObjectMeta{Name: "foo"},
2014-10-30 13:29:11 +00:00
Spec: api.ServiceSpec{
Selector: map[string]string{
"foo": "bar",
},
},
},
},
2014-06-06 23:40:48 +00:00
}
testServer, endpointsHandler := makeTestServer(t,
serverResponse{http.StatusOK, newPodList(1)},
serverResponse{http.StatusOK, &serviceList},
serverResponse{http.StatusOK, &api.Endpoints{}})
2014-10-23 00:14:39 +00:00
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})
endpoints := NewEndpointController(client)
if err := endpoints.SyncServiceEndpoints(); err != nil {
t.Errorf("unexpected error: %v", err)
}
data := runtime.EncodeOrDie(testapi.Codec(), &api.Endpoints{
ObjectMeta: api.ObjectMeta{
ResourceVersion: "",
},
Endpoints: []string{"1.2.3.4:8080"},
})
endpointsHandler.ValidateRequest(t, "/api/"+testapi.Version()+"/endpoints", "POST", &data)
2014-06-06 23:40:48 +00:00
}
2014-06-09 05:38:45 +00:00
func TestSyncEndpointsPodError(t *testing.T) {
serviceList := api.ServiceList{
Items: []api.Service{
{
2014-10-30 13:29:11 +00:00
Spec: api.ServiceSpec{
Selector: map[string]string{
"foo": "bar",
},
},
},
},
}
testServer, _ := makeTestServer(t,
serverResponse{http.StatusInternalServerError, &api.PodList{}},
serverResponse{http.StatusOK, &serviceList},
serverResponse{http.StatusOK, &api.Endpoints{}})
2014-10-23 00:14:39 +00:00
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})
endpoints := NewEndpointController(client)
if err := endpoints.SyncServiceEndpoints(); err == nil {
2014-06-06 23:40:48 +00:00
t.Error("Unexpected non-error")
}
}