diff --git a/pkg/api/testapi/testapi.go b/pkg/api/testapi/testapi.go index bd7f6e1e420..b65c612bd5f 100644 --- a/pkg/api/testapi/testapi.go +++ b/pkg/api/testapi/testapi.go @@ -21,6 +21,7 @@ import ( "fmt" "os" + "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" @@ -74,3 +75,34 @@ func SelfLink(resource, name string) string { } return fmt.Sprintf("/api/%s/%s/%s", Version(), resource, name) } + +// Returns the appropriate path for the given resource, namespace and name. +// For ex, this is of the form: +// /api/v1beta1/pods/pod0 for v1beta1 and +// /api/v1beta3/namespaces/foo/pods/pod0 for v1beta3. +func ResourcePath(resource, namespace, name string) string { + path := "/api/" + Version() + if !api.PreV1Beta3(Version()) && namespace != "" { + path = path + "/namespaces/" + namespace + } + if resource != "" { + path = path + "/" + resource + } + if name != "" { + path = path + "/" + name + } + return path +} + +// Returns the appropriate path along with the query params for the given resource, namespace and name. +// For ex, this is of the form: +// /api/v1beta1/pods/pod0?namespace=foo for v1beta1 and +// /api/v1beta3/namespaces/foo/pods/pod0 for v1beta3. +func ResourcePathWithQueryParams(resource, namespace, name string) string { + path := ResourcePath(resource, namespace, name) + // Add namespace as query param for pre v1beta3. + if api.PreV1Beta3(Version()) { + path = path + "?namespace=" + namespace + } + return path +} diff --git a/pkg/client/cache/listwatch_test.go b/pkg/client/cache/listwatch_test.go index 6ddc9b31078..cf39864655e 100644 --- a/pkg/client/cache/listwatch_test.go +++ b/pkg/client/cache/listwatch_test.go @@ -78,6 +78,7 @@ func buildLocation(resourcePath string, query url.Values) string { } func TestListWatchesCanList(t *testing.T) { + fieldSelectorQueryParamName := api.FieldSelectorQueryParam(testapi.Version()) table := []struct { location string resource string @@ -93,14 +94,18 @@ func TestListWatchesCanList(t *testing.T) { }, // pod with "assigned" field selector. { - location: buildLocation(buildResourcePath("", api.NamespaceAll, "pods"), buildQueryValues(api.NamespaceAll, url.Values{"fields": []string{getHostFieldLabel() + "="}})), + location: buildLocation( + buildResourcePath("", api.NamespaceAll, "pods"), + buildQueryValues(api.NamespaceAll, url.Values{fieldSelectorQueryParamName: []string{getHostFieldLabel() + "="}})), resource: "pods", namespace: api.NamespaceAll, fieldSelector: fields.Set{getHostFieldLabel(): ""}.AsSelector(), }, // pod in namespace "foo" { - location: buildLocation(buildResourcePath("", "foo", "pods"), buildQueryValues("foo", url.Values{"fields": []string{getHostFieldLabel() + "="}})), + location: buildLocation( + buildResourcePath("", "foo", "pods"), + buildQueryValues("foo", url.Values{fieldSelectorQueryParamName: []string{getHostFieldLabel() + "="}})), resource: "pods", namespace: "foo", fieldSelector: fields.Set{getHostFieldLabel(): ""}.AsSelector(), @@ -123,6 +128,7 @@ func TestListWatchesCanList(t *testing.T) { } func TestListWatchesCanWatch(t *testing.T) { + fieldSelectorQueryParamName := api.FieldSelectorQueryParam(testapi.Version()) table := []struct { rv string location string @@ -132,14 +138,18 @@ func TestListWatchesCanWatch(t *testing.T) { }{ // Minion { - location: buildLocation(buildResourcePath("watch", api.NamespaceAll, "minions"), buildQueryValues(api.NamespaceAll, url.Values{"resourceVersion": []string{""}})), + location: buildLocation( + buildResourcePath("watch", api.NamespaceAll, "minions"), + buildQueryValues(api.NamespaceAll, url.Values{"resourceVersion": []string{""}})), rv: "", resource: "minions", namespace: api.NamespaceAll, fieldSelector: parseSelectorOrDie(""), }, { - location: buildLocation(buildResourcePath("watch", api.NamespaceAll, "minions"), buildQueryValues(api.NamespaceAll, url.Values{"resourceVersion": []string{"42"}})), + location: buildLocation( + buildResourcePath("watch", api.NamespaceAll, "minions"), + buildQueryValues(api.NamespaceAll, url.Values{"resourceVersion": []string{"42"}})), rv: "42", resource: "minions", namespace: api.NamespaceAll, @@ -147,7 +157,9 @@ func TestListWatchesCanWatch(t *testing.T) { }, // pod with "assigned" field selector. { - location: buildLocation(buildResourcePath("watch", api.NamespaceAll, "pods"), buildQueryValues(api.NamespaceAll, url.Values{"fields": []string{getHostFieldLabel() + "="}, "resourceVersion": []string{"0"}})), + location: buildLocation( + buildResourcePath("watch", api.NamespaceAll, "pods"), + buildQueryValues(api.NamespaceAll, url.Values{fieldSelectorQueryParamName: []string{getHostFieldLabel() + "="}, "resourceVersion": []string{"0"}})), rv: "0", resource: "pods", namespace: api.NamespaceAll, @@ -155,7 +167,9 @@ func TestListWatchesCanWatch(t *testing.T) { }, // pod with namespace foo and assigned field selector { - location: buildLocation(buildResourcePath("watch", "foo", "pods"), buildQueryValues("foo", url.Values{"fields": []string{getHostFieldLabel() + "="}, "resourceVersion": []string{"0"}})), + location: buildLocation( + buildResourcePath("watch", "foo", "pods"), + buildQueryValues("foo", url.Values{fieldSelectorQueryParamName: []string{getHostFieldLabel() + "="}, "resourceVersion": []string{"0"}})), rv: "0", resource: "pods", namespace: "foo", diff --git a/pkg/controller/replication_controller_test.go b/pkg/controller/replication_controller_test.go index 481239d8c19..dbba1358f3d 100644 --- a/pkg/controller/replication_controller_test.go +++ b/pkg/controller/replication_controller_test.go @@ -20,7 +20,6 @@ import ( "fmt" "net/http" "net/http/httptest" - "path" "sync" "testing" "time" @@ -34,17 +33,6 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/watch" ) -func makeNamespaceURL(namespace, suffix string) string { - if !(testapi.Version() == "v1beta1" || testapi.Version() == "v1beta2") { - return makeURL("/namespaces/" + namespace + suffix) - } - return makeURL(suffix + "?namespace=" + namespace) -} - -func makeURL(suffix string) string { - return path.Join("/api", testapi.Version(), suffix) -} - type FakePodControl struct { controllerSpec []api.ReplicationController deletePodName []string @@ -67,7 +55,7 @@ func (f *FakePodControl) deletePod(namespace string, podName string) error { func newReplicationController(replicas int) api.ReplicationController { return api.ReplicationController{ TypeMeta: api.TypeMeta{APIVersion: testapi.Version()}, - ObjectMeta: api.ObjectMeta{Name: "foobar", Namespace: "default", ResourceVersion: "18"}, + ObjectMeta: api.ObjectMeta{Name: "foobar", Namespace: api.NamespaceDefault, ResourceVersion: "18"}, Spec: api.ReplicationControllerSpec{ Replicas: replicas, Template: &api.PodTemplateSpec{ @@ -119,6 +107,47 @@ func validateSyncReplication(t *testing.T, fakePodControl *FakePodControl, expec } } +func replicationControllerResourceName() string { + if api.PreV1Beta3(testapi.Version()) { + return "replicationControllers" + } + return "replicationcontrollers" +} + +type serverResponse struct { + statusCode int + obj interface{} +} + +func makeTestServer(t *testing.T, namespace, name string, podResponse, controllerResponse, updateResponse serverResponse) (*httptest.Server, *util.FakeHandler) { + fakePodHandler := util.FakeHandler{ + StatusCode: podResponse.statusCode, + ResponseBody: runtime.EncodeOrDie(testapi.Codec(), podResponse.obj.(runtime.Object)), + } + fakeControllerHandler := util.FakeHandler{ + StatusCode: controllerResponse.statusCode, + ResponseBody: runtime.EncodeOrDie(testapi.Codec(), controllerResponse.obj.(runtime.Object)), + } + fakeUpdateHandler := util.FakeHandler{ + StatusCode: updateResponse.statusCode, + ResponseBody: runtime.EncodeOrDie(testapi.Codec(), updateResponse.obj.(runtime.Object)), + } + mux := http.NewServeMux() + mux.Handle(testapi.ResourcePath("pods", namespace, ""), &fakePodHandler) + mux.Handle(testapi.ResourcePath(replicationControllerResourceName(), "", ""), &fakeControllerHandler) + if !api.PreV1Beta3(testapi.Version()) && namespace != "" { + mux.Handle(testapi.ResourcePath(replicationControllerResourceName(), namespace, ""), &fakeControllerHandler) + } + if name != "" { + mux.Handle(testapi.ResourcePath(replicationControllerResourceName(), namespace, name), &fakeUpdateHandler) + } + mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) { + t.Errorf("unexpected request: %v", req.RequestURI) + res.WriteHeader(http.StatusNotFound) + }) + return httptest.NewServer(mux), &fakeUpdateHandler +} + func TestSyncReplicationControllerDoesNothing(t *testing.T) { body, _ := latest.Codec.Encode(newPodList(2)) fakeHandler := util.FakeHandler{ @@ -162,28 +191,16 @@ func TestSyncReplicationControllerDeletes(t *testing.T) { } func TestSyncReplicationControllerCreates(t *testing.T) { - body := runtime.EncodeOrDie(testapi.Codec(), newPodList(0)) - fakePodHandler := util.FakeHandler{ - StatusCode: 200, - ResponseBody: string(body), - } - fakePodControl := FakePodControl{} - controller := newReplicationController(2) - fakeUpdateHandler := util.FakeHandler{ - StatusCode: 200, - ResponseBody: runtime.EncodeOrDie(testapi.Codec(), &controller), - T: t, - } - - testServerMux := http.NewServeMux() - testServerMux.Handle("/api/"+testapi.Version()+"/pods/", &fakePodHandler) - testServerMux.Handle(fmt.Sprintf("/api/"+testapi.Version()+"/replicationControllers/%s", controller.Name), &fakeUpdateHandler) - testServer := httptest.NewServer(testServerMux) + testServer, fakeUpdateHandler := makeTestServer(t, api.NamespaceDefault, controller.Name, + serverResponse{http.StatusOK, newPodList(0)}, + serverResponse{http.StatusInternalServerError, &api.ReplicationControllerList{}}, + serverResponse{http.StatusOK, &controller}) defer testServer.Close() client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()}) manager := NewReplicationManager(client) + fakePodControl := FakePodControl{} manager.podControl = &fakePodControl manager.syncReplicationController(controller) validateSyncReplication(t, &fakePodControl, 2, 0) @@ -226,7 +243,7 @@ func TestCreateReplica(t *testing.T) { }, Spec: controllerSpec.Spec.Template.Spec, } - fakeHandler.ValidateRequest(t, makeNamespaceURL("default", "/pods"), "POST", nil) + fakeHandler.ValidateRequest(t, testapi.ResourcePathWithQueryParams("pods", api.NamespaceDefault, ""), "POST", nil) actualPod, err := client.Codec.Decode([]byte(fakeHandler.RequestBody)) if err != nil { t.Errorf("Unexpected error: %#v", err) @@ -246,29 +263,14 @@ func TestSynchronize(t *testing.T) { "type": "production", } - fakePodHandler := util.FakeHandler{ - StatusCode: 200, - ResponseBody: "{\"apiVersion\": \"" + testapi.Version() + "\", \"kind\": \"PodList\"}", - T: t, - } - fakeControllerHandler := util.FakeHandler{ - StatusCode: 200, - ResponseBody: runtime.EncodeOrDie(latest.Codec, &api.ReplicationControllerList{ + testServer, _ := makeTestServer(t, api.NamespaceDefault, "", + serverResponse{http.StatusOK, newPodList(0)}, + serverResponse{http.StatusOK, &api.ReplicationControllerList{ Items: []api.ReplicationController{ controllerSpec1, controllerSpec2, - }, - }), - T: t, - } - mux := http.NewServeMux() - mux.Handle("/api/"+testapi.Version()+"/pods/", &fakePodHandler) - mux.Handle("/api/"+testapi.Version()+"/replicationControllers/", &fakeControllerHandler) - mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { - w.WriteHeader(http.StatusNotFound) - t.Errorf("Unexpected request for %v", req.RequestURI) - }) - testServer := httptest.NewServer(mux) + }}}, + serverResponse{http.StatusInternalServerError, &api.ReplicationController{}}) defer testServer.Close() client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()}) manager := NewReplicationManager(client) @@ -286,34 +288,12 @@ func TestControllerNoReplicaUpdate(t *testing.T) { rc.Status = api.ReplicationControllerStatus{Replicas: 5} activePods := 5 - body, _ := latest.Codec.Encode(newPodList(activePods)) - fakePodHandler := util.FakeHandler{ - StatusCode: 200, - ResponseBody: string(body), - T: t, - } - fakeControllerHandler := util.FakeHandler{ - StatusCode: 200, - ResponseBody: runtime.EncodeOrDie(latest.Codec, &api.ReplicationControllerList{ + testServer, fakeUpdateHandler := makeTestServer(t, api.NamespaceDefault, rc.Name, + serverResponse{http.StatusOK, newPodList(activePods)}, + serverResponse{http.StatusOK, &api.ReplicationControllerList{ Items: []api.ReplicationController{rc}, - }), - T: t, - } - fakeUpdateHandler := util.FakeHandler{ - StatusCode: 200, - ResponseBody: runtime.EncodeOrDie(testapi.Codec(), &rc), - T: t, - } - - mux := http.NewServeMux() - mux.Handle("/api/"+testapi.Version()+"/pods/", &fakePodHandler) - mux.Handle("/api/"+testapi.Version()+"/replicationControllers/", &fakeControllerHandler) - mux.Handle(fmt.Sprintf("/api/"+testapi.Version()+"/replicationControllers/%s", rc.Name), &fakeUpdateHandler) - mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { - w.WriteHeader(http.StatusNotFound) - t.Errorf("Unexpected request for %v", req.RequestURI) - }) - testServer := httptest.NewServer(mux) + }}, + serverResponse{http.StatusOK, &rc}) defer testServer.Close() client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()}) manager := NewReplicationManager(client) @@ -336,35 +316,12 @@ func TestControllerUpdateReplicas(t *testing.T) { rc.Status = api.ReplicationControllerStatus{Replicas: 2} activePods := 4 - body, _ := latest.Codec.Encode(newPodList(activePods)) - fakePodHandler := util.FakeHandler{ - StatusCode: 200, - ResponseBody: string(body), - T: t, - } - fakeControllerHandler := util.FakeHandler{ - StatusCode: 200, - ResponseBody: runtime.EncodeOrDie(latest.Codec, &api.ReplicationControllerList{ + testServer, fakeUpdateHandler := makeTestServer(t, api.NamespaceDefault, rc.Name, + serverResponse{http.StatusOK, newPodList(activePods)}, + serverResponse{http.StatusOK, &api.ReplicationControllerList{ Items: []api.ReplicationController{rc}, - }), - T: t, - } - fakeUpdateHandler := util.FakeHandler{ - StatusCode: 200, - ResponseBody: runtime.EncodeOrDie(testapi.Codec(), &rc), - T: t, - } - - mux := http.NewServeMux() - - mux.Handle("/api/"+testapi.Version()+"/pods/", &fakePodHandler) - mux.Handle("/api/"+testapi.Version()+"/replicationControllers/", &fakeControllerHandler) - mux.Handle(fmt.Sprintf("/api/"+testapi.Version()+"/replicationControllers/%s", rc.Name), &fakeUpdateHandler) - mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { - w.WriteHeader(http.StatusNotFound) - t.Errorf("Unexpected request for %v", req.RequestURI) - }) - testServer := httptest.NewServer(mux) + }}, + serverResponse{http.StatusOK, &rc}) defer testServer.Close() client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()}) manager := NewReplicationManager(client) @@ -376,7 +333,7 @@ func TestControllerUpdateReplicas(t *testing.T) { // Status.Replicas should go up from 2->4 even though we created 5-4=1 pod rc.Status = api.ReplicationControllerStatus{Replicas: 4} decRc := runtime.EncodeOrDie(testapi.Codec(), &rc) - fakeUpdateHandler.ValidateRequest(t, fmt.Sprintf("/api/"+testapi.Version()+"/replicationControllers/%s?namespace=%s", rc.Name, rc.Namespace), "PUT", &decRc) + fakeUpdateHandler.ValidateRequest(t, testapi.ResourcePathWithQueryParams(replicationControllerResourceName(), rc.Namespace, rc.Name), "PUT", &decRc) validateSyncReplication(t, &fakePodControl, 1, 0) } diff --git a/pkg/kubectl/resource/helper_test.go b/pkg/kubectl/resource/helper_test.go index a56b31b8774..c9c049a5531 100644 --- a/pkg/kubectl/resource/helper_test.go +++ b/pkg/kubectl/resource/helper_test.go @@ -329,7 +329,7 @@ func TestHelperList(t *testing.T) { t.Errorf("url doesn't contain name: %#v", req.URL) return false } - if req.URL.Query().Get("labels") != labels.SelectorFromSet(labels.Set{"foo": "baz"}).String() { + if req.URL.Query().Get(api.LabelSelectorQueryParam(testapi.Version())) != labels.SelectorFromSet(labels.Set{"foo": "baz"}).String() { t.Errorf("url doesn't contain query parameters: %#v", req.URL) return false } diff --git a/pkg/service/endpoints_controller_test.go b/pkg/service/endpoints_controller_test.go index ba9eef5c0a3..8c637c524b9 100644 --- a/pkg/service/endpoints_controller_test.go +++ b/pkg/service/endpoints_controller_test.go @@ -231,7 +231,7 @@ type serverResponse struct { obj interface{} } -func makeTestServer(t *testing.T, podResponse serverResponse, serviceResponse serverResponse, endpointsResponse serverResponse) (*httptest.Server, *util.FakeHandler) { +func makeTestServer(t *testing.T, namespace string, podResponse, serviceResponse, endpointsResponse serverResponse) (*httptest.Server, *util.FakeHandler) { fakePodHandler := util.FakeHandler{ StatusCode: podResponse.statusCode, ResponseBody: runtime.EncodeOrDie(testapi.Codec(), podResponse.obj.(runtime.Object)), @@ -245,10 +245,10 @@ func makeTestServer(t *testing.T, podResponse serverResponse, serviceResponse se 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.Handle(testapi.ResourcePath("pods", namespace, ""), &fakePodHandler) + mux.Handle(testapi.ResourcePath("services", "", ""), &fakeServiceHandler) + mux.Handle(testapi.ResourcePath("endpoints", namespace, ""), &fakeEndpointsHandler) + mux.Handle(testapi.ResourcePath("endpoints/", namespace, ""), &fakeEndpointsHandler) mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) { t.Errorf("unexpected request: %v", req.RequestURI) res.WriteHeader(http.StatusNotFound) @@ -257,7 +257,7 @@ func makeTestServer(t *testing.T, podResponse serverResponse, serviceResponse se } func TestSyncEndpointsEmpty(t *testing.T) { - testServer, _ := makeTestServer(t, + testServer, _ := makeTestServer(t, api.NamespaceDefault, serverResponse{http.StatusOK, newPodList(0)}, serverResponse{http.StatusOK, &api.ServiceList{}}, serverResponse{http.StatusOK, &api.Endpoints{}}) @@ -270,7 +270,7 @@ func TestSyncEndpointsEmpty(t *testing.T) { } func TestSyncEndpointsError(t *testing.T) { - testServer, _ := makeTestServer(t, + testServer, _ := makeTestServer(t, api.NamespaceDefault, serverResponse{http.StatusOK, newPodList(0)}, serverResponse{http.StatusInternalServerError, &api.ServiceList{}}, serverResponse{http.StatusOK, &api.Endpoints{}}) @@ -291,7 +291,7 @@ func TestSyncEndpointsItemsPreserveNoSelector(t *testing.T) { }, }, } - testServer, endpointsHandler := makeTestServer(t, + testServer, endpointsHandler := makeTestServer(t, api.NamespaceDefault, serverResponse{http.StatusOK, newPodList(0)}, serverResponse{http.StatusOK, &serviceList}, serverResponse{http.StatusOK, &api.Endpoints{ @@ -323,7 +323,7 @@ func TestSyncEndpointsProtocolTCP(t *testing.T) { }, }, } - testServer, endpointsHandler := makeTestServer(t, + testServer, endpointsHandler := makeTestServer(t, "other", serverResponse{http.StatusOK, newPodList(0)}, serverResponse{http.StatusOK, &serviceList}, serverResponse{http.StatusOK, &api.Endpoints{ @@ -355,7 +355,7 @@ func TestSyncEndpointsProtocolUDP(t *testing.T) { }, }, } - testServer, endpointsHandler := makeTestServer(t, + testServer, endpointsHandler := makeTestServer(t, "other", serverResponse{http.StatusOK, newPodList(0)}, serverResponse{http.StatusOK, &serviceList}, serverResponse{http.StatusOK, &api.Endpoints{ @@ -386,7 +386,7 @@ func TestSyncEndpointsItemsEmptySelectorSelectsAll(t *testing.T) { }, }, } - testServer, endpointsHandler := makeTestServer(t, + testServer, endpointsHandler := makeTestServer(t, "other", serverResponse{http.StatusOK, newPodList(1)}, serverResponse{http.StatusOK, &serviceList}, serverResponse{http.StatusOK, &api.Endpoints{ @@ -418,7 +418,7 @@ func TestSyncEndpointsItemsEmptySelectorSelectsAll(t *testing.T) { }, }}, }) - endpointsHandler.ValidateRequest(t, "/api/"+testapi.Version()+"/endpoints/foo?namespace=other", "PUT", &data) + endpointsHandler.ValidateRequest(t, testapi.ResourcePathWithQueryParams("endpoints", "other", "foo"), "PUT", &data) } func TestSyncEndpointsItemsPreexisting(t *testing.T) { @@ -434,7 +434,7 @@ func TestSyncEndpointsItemsPreexisting(t *testing.T) { }, }, } - testServer, endpointsHandler := makeTestServer(t, + testServer, endpointsHandler := makeTestServer(t, "bar", serverResponse{http.StatusOK, newPodList(1)}, serverResponse{http.StatusOK, &serviceList}, serverResponse{http.StatusOK, &api.Endpoints{ @@ -466,7 +466,7 @@ func TestSyncEndpointsItemsPreexisting(t *testing.T) { }, }}, }) - endpointsHandler.ValidateRequest(t, "/api/"+testapi.Version()+"/endpoints/foo?namespace=bar", "PUT", &data) + endpointsHandler.ValidateRequest(t, testapi.ResourcePathWithQueryParams("endpoints", "bar", "foo"), "PUT", &data) } func TestSyncEndpointsItemsPreexistingIdentical(t *testing.T) { @@ -482,7 +482,7 @@ func TestSyncEndpointsItemsPreexistingIdentical(t *testing.T) { }, }, } - testServer, endpointsHandler := makeTestServer(t, + testServer, endpointsHandler := makeTestServer(t, api.NamespaceDefault, serverResponse{http.StatusOK, newPodList(1)}, serverResponse{http.StatusOK, &serviceList}, serverResponse{http.StatusOK, &api.Endpoints{ @@ -505,7 +505,7 @@ func TestSyncEndpointsItemsPreexistingIdentical(t *testing.T) { if err := endpoints.SyncServiceEndpoints(); err != nil { t.Errorf("unexpected error: %v", err) } - endpointsHandler.ValidateRequest(t, "/api/"+testapi.Version()+"/endpoints/foo?namespace=default", "GET", nil) + endpointsHandler.ValidateRequest(t, testapi.ResourcePathWithQueryParams("endpoints", api.NamespaceDefault, "foo"), "GET", nil) } func TestSyncEndpointsItems(t *testing.T) { @@ -521,7 +521,7 @@ func TestSyncEndpointsItems(t *testing.T) { }, }, } - testServer, endpointsHandler := makeTestServer(t, + testServer, endpointsHandler := makeTestServer(t, "other", serverResponse{http.StatusOK, newPodList(1)}, serverResponse{http.StatusOK, &serviceList}, serverResponse{http.StatusOK, &api.Endpoints{}}) @@ -545,13 +545,16 @@ func TestSyncEndpointsItems(t *testing.T) { }, }}, }) - endpointsHandler.ValidateRequest(t, "/api/"+testapi.Version()+"/endpoints?namespace=other", "POST", &data) + // endpointsHandler should get 2 requests - one for "GET" and the next for "POST". + endpointsHandler.ValidateRequestCount(t, 2) + endpointsHandler.ValidateRequest(t, testapi.ResourcePathWithQueryParams("endpoints", "other", ""), "POST", &data) } func TestSyncEndpointsPodError(t *testing.T) { serviceList := api.ServiceList{ Items: []api.Service{ { + ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: api.NamespaceDefault}, Spec: api.ServiceSpec{ Selector: map[string]string{ "foo": "bar", @@ -560,7 +563,7 @@ func TestSyncEndpointsPodError(t *testing.T) { }, }, } - testServer, _ := makeTestServer(t, + testServer, _ := makeTestServer(t, api.NamespaceDefault, serverResponse{http.StatusInternalServerError, &api.PodList{}}, serverResponse{http.StatusOK, &serviceList}, serverResponse{http.StatusOK, &api.Endpoints{}}) diff --git a/plugin/pkg/scheduler/factory/factory_test.go b/plugin/pkg/scheduler/factory/factory_test.go index 4da60254090..ef634fd096d 100644 --- a/plugin/pkg/scheduler/factory/factory_test.go +++ b/plugin/pkg/scheduler/factory/factory_test.go @@ -19,7 +19,6 @@ package factory import ( "net/http" "net/http/httptest" - "path" "reflect" "testing" "time" @@ -313,7 +312,11 @@ func TestPollMinions(t *testing.T) { } mux := http.NewServeMux() // FakeHandler musn't be sent requests other than the one you want to test. - mux.Handle("/api/"+testapi.Version()+"/minions", &handler) + resource := "nodes" + if api.PreV1Beta3(testapi.Version()) { + resource = "minions" + } + mux.Handle(testapi.ResourcePath(resource, api.NamespaceAll, ""), &handler) server := httptest.NewServer(mux) defer server.Close() client := client.NewOrDie(&client.Config{Host: server.URL, Version: testapi.Version()}) @@ -324,7 +327,7 @@ func TestPollMinions(t *testing.T) { t.Errorf("Unexpected error: %v", err) continue } - handler.ValidateRequest(t, "/api/"+testapi.Version()+"/minions", "GET", nil) + handler.ValidateRequest(t, testapi.ResourcePath(resource, api.NamespaceAll, ""), "GET", nil) if a := ce.Len(); item.expectedCount != a { t.Errorf("Expected %v, got %v", item.expectedCount, a) @@ -332,22 +335,6 @@ func TestPollMinions(t *testing.T) { } } -func makeNamespaceURL(namespace, suffix string, isClient bool) string { - if !(testapi.Version() == "v1beta1" || testapi.Version() == "v1beta2") { - return makeURL("/namespaces/" + namespace + suffix) - } - // if this is a url the client should call, encode the url - if isClient { - return makeURL(suffix + "?namespace=" + namespace) - } - // its not a client url, so its what the server needs to listen on - return makeURL(suffix) -} - -func makeURL(suffix string) string { - return path.Join("/api", testapi.Version(), suffix) -} - func TestDefaultErrorFunc(t *testing.T) { testPod := &api.Pod{ ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "bar"}, @@ -364,7 +351,7 @@ func TestDefaultErrorFunc(t *testing.T) { mux := http.NewServeMux() // FakeHandler musn't be sent requests other than the one you want to test. - mux.Handle(makeNamespaceURL("bar", "/pods/foo", false), &handler) + mux.Handle(testapi.ResourcePath("pods", "bar", "foo"), &handler) server := httptest.NewServer(mux) defer server.Close() factory := NewConfigFactory(client.NewOrDie(&client.Config{Host: server.URL, Version: testapi.Version()})) @@ -387,7 +374,7 @@ func TestDefaultErrorFunc(t *testing.T) { if !exists { continue } - handler.ValidateRequest(t, makeNamespaceURL("bar", "/pods/foo", true), "GET", nil) + handler.ValidateRequest(t, testapi.ResourcePathWithQueryParams("pods", "bar", "foo"), "GET", nil) if e, a := testPod, got; !reflect.DeepEqual(e, a) { t.Errorf("Expected %v, got %v", e, a) } @@ -458,7 +445,7 @@ func TestBind(t *testing.T) { continue } expectedBody := runtime.EncodeOrDie(testapi.Codec(), item.binding) - handler.ValidateRequest(t, "/api/"+testapi.Version()+"/bindings?namespace=default", "POST", &expectedBody) + handler.ValidateRequest(t, testapi.ResourcePathWithQueryParams("bindings", api.NamespaceDefault, ""), "POST", &expectedBody) } }