Second step in decoupling the service controller, use the apiserver for writes too.
This commit is contained in:
@@ -24,8 +24,10 @@ import (
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
)
|
||||
|
||||
@@ -127,7 +129,7 @@ type serverResponse struct {
|
||||
obj interface{}
|
||||
}
|
||||
|
||||
func makeTestServer(t *testing.T, podResponse serverResponse, serviceResponse serverResponse) *httptest.Server {
|
||||
func makeTestServer(t *testing.T, podResponse serverResponse, serviceResponse serverResponse, endpointsResponse serverResponse) (*httptest.Server, *util.FakeHandler) {
|
||||
fakePodHandler := util.FakeHandler{
|
||||
StatusCode: podResponse.statusCode,
|
||||
ResponseBody: util.EncodeJSON(podResponse.obj),
|
||||
@@ -136,20 +138,27 @@ func makeTestServer(t *testing.T, podResponse serverResponse, serviceResponse se
|
||||
StatusCode: serviceResponse.statusCode,
|
||||
ResponseBody: util.EncodeJSON(serviceResponse.obj),
|
||||
}
|
||||
fakeEndpointsHandler := util.FakeHandler{
|
||||
StatusCode: endpointsResponse.statusCode,
|
||||
ResponseBody: util.EncodeJSON(endpointsResponse.obj),
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/api/v1beta1/pods", &fakePodHandler)
|
||||
mux.Handle("/api/v1beta1/services", &fakeServiceHandler)
|
||||
mux.Handle("/api/v1beta1/endpoints", &fakeEndpointsHandler)
|
||||
mux.Handle("/api/v1beta1/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)
|
||||
return httptest.NewServer(mux), &fakeEndpointsHandler
|
||||
}
|
||||
|
||||
func TestSyncEndpointsEmpty(t *testing.T) {
|
||||
testServer := makeTestServer(t,
|
||||
testServer, _ := makeTestServer(t,
|
||||
serverResponse{http.StatusOK, newPodList(0)},
|
||||
serverResponse{http.StatusOK, api.ServiceList{}})
|
||||
serverResponse{http.StatusOK, api.ServiceList{}},
|
||||
serverResponse{http.StatusOK, api.Endpoints{}})
|
||||
client := client.NewOrDie(testServer.URL, "v1beta1", nil)
|
||||
serviceRegistry := registrytest.ServiceRegistry{}
|
||||
endpoints := NewEndpointController(&serviceRegistry, client)
|
||||
@@ -159,9 +168,10 @@ func TestSyncEndpointsEmpty(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSyncEndpointsError(t *testing.T) {
|
||||
testServer := makeTestServer(t,
|
||||
testServer, _ := makeTestServer(t,
|
||||
serverResponse{http.StatusOK, newPodList(0)},
|
||||
serverResponse{http.StatusInternalServerError, api.ServiceList{}})
|
||||
serverResponse{http.StatusInternalServerError, api.ServiceList{}},
|
||||
serverResponse{http.StatusOK, api.Endpoints{}})
|
||||
client := client.NewOrDie(testServer.URL, "v1beta1", nil)
|
||||
serviceRegistry := registrytest.ServiceRegistry{
|
||||
Err: fmt.Errorf("test error"),
|
||||
@@ -172,29 +182,100 @@ func TestSyncEndpointsError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncEndpointsItems(t *testing.T) {
|
||||
func TestSyncEndpointsItemsPreexisting(t *testing.T) {
|
||||
serviceList := api.ServiceList{
|
||||
Items: []api.Service{
|
||||
{
|
||||
JSONBase: api.JSONBase{ID: "foo"},
|
||||
Selector: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
testServer := makeTestServer(t,
|
||||
testServer, endpointsHandler := makeTestServer(t,
|
||||
serverResponse{http.StatusOK, newPodList(1)},
|
||||
serverResponse{http.StatusOK, serviceList})
|
||||
serverResponse{http.StatusOK, serviceList},
|
||||
serverResponse{http.StatusOK, api.Endpoints{
|
||||
JSONBase: api.JSONBase{
|
||||
ID: "foo",
|
||||
ResourceVersion: 1,
|
||||
},
|
||||
Endpoints: []string{"6.7.8.9:1000"},
|
||||
}})
|
||||
client := client.NewOrDie(testServer.URL, "v1beta1", nil)
|
||||
serviceRegistry := registrytest.ServiceRegistry{}
|
||||
endpoints := NewEndpointController(&serviceRegistry, client)
|
||||
if err := endpoints.SyncServiceEndpoints(); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if len(serviceRegistry.Endpoints.Endpoints) != 1 ||
|
||||
serviceRegistry.Endpoints.Endpoints[0] != "1.2.3.4:8080" {
|
||||
t.Errorf("Unexpected endpoints update: %#v", serviceRegistry.Endpoints)
|
||||
data := runtime.EncodeOrDie(v1beta1.Codec, &api.Endpoints{
|
||||
JSONBase: api.JSONBase{
|
||||
ID: "foo",
|
||||
ResourceVersion: 1,
|
||||
},
|
||||
Endpoints: []string{"1.2.3.4:8080"},
|
||||
})
|
||||
endpointsHandler.ValidateRequest(t, "/api/v1beta1/endpoints/foo", "PUT", &data)
|
||||
}
|
||||
|
||||
func TestSyncEndpointsItemsPreexistingIdentical(t *testing.T) {
|
||||
serviceList := api.ServiceList{
|
||||
Items: []api.Service{
|
||||
{
|
||||
JSONBase: api.JSONBase{ID: "foo"},
|
||||
Selector: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
testServer, endpointsHandler := makeTestServer(t,
|
||||
serverResponse{http.StatusOK, newPodList(1)},
|
||||
serverResponse{http.StatusOK, serviceList},
|
||||
serverResponse{http.StatusOK, api.Endpoints{
|
||||
JSONBase: api.JSONBase{
|
||||
ResourceVersion: 1,
|
||||
},
|
||||
Endpoints: []string{"1.2.3.4:8080"},
|
||||
}})
|
||||
client := client.NewOrDie(testServer.URL, "v1beta1", nil)
|
||||
serviceRegistry := registrytest.ServiceRegistry{}
|
||||
endpoints := NewEndpointController(&serviceRegistry, client)
|
||||
if err := endpoints.SyncServiceEndpoints(); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
endpointsHandler.ValidateRequest(t, "/api/v1beta1/endpoints/foo", "GET", nil)
|
||||
}
|
||||
|
||||
func TestSyncEndpointsItems(t *testing.T) {
|
||||
serviceList := api.ServiceList{
|
||||
Items: []api.Service{
|
||||
{
|
||||
JSONBase: api.JSONBase{ID: "foo"},
|
||||
Selector: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
testServer, endpointsHandler := makeTestServer(t,
|
||||
serverResponse{http.StatusOK, newPodList(1)},
|
||||
serverResponse{http.StatusOK, serviceList},
|
||||
serverResponse{http.StatusOK, api.Endpoints{}})
|
||||
client := client.NewOrDie(testServer.URL, "v1beta1", nil)
|
||||
serviceRegistry := registrytest.ServiceRegistry{}
|
||||
endpoints := NewEndpointController(&serviceRegistry, client)
|
||||
if err := endpoints.SyncServiceEndpoints(); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
data := runtime.EncodeOrDie(v1beta1.Codec, &api.Endpoints{
|
||||
JSONBase: api.JSONBase{
|
||||
ResourceVersion: 0,
|
||||
},
|
||||
Endpoints: []string{"1.2.3.4:8080"},
|
||||
})
|
||||
endpointsHandler.ValidateRequest(t, "/api/v1beta1/endpoints", "POST", &data)
|
||||
}
|
||||
|
||||
func TestSyncEndpointsPodError(t *testing.T) {
|
||||
@@ -207,9 +288,10 @@ func TestSyncEndpointsPodError(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
testServer := makeTestServer(t,
|
||||
testServer, _ := makeTestServer(t,
|
||||
serverResponse{http.StatusInternalServerError, api.PodList{}},
|
||||
serverResponse{http.StatusOK, serviceList})
|
||||
serverResponse{http.StatusOK, serviceList},
|
||||
serverResponse{http.StatusOK, api.Endpoints{}})
|
||||
client := client.NewOrDie(testServer.URL, "v1beta1", nil)
|
||||
serviceRegistry := registrytest.ServiceRegistry{
|
||||
List: api.ServiceList{
|
||||
|
Reference in New Issue
Block a user