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