Update pkg/api/testapi because namespace is always in the path since we remove v1beta1/2

This commit is contained in:
Chao Xu 2015-06-15 17:06:34 -07:00
parent b65c321a87
commit a309d3e652
8 changed files with 28 additions and 84 deletions

View File

@ -22,7 +22,6 @@ import (
"os" "os"
"strings" "strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
@ -79,20 +78,17 @@ func SelfLink(resource, name string) string {
// Returns the appropriate path for the given prefix (watch, proxy, redirect, etc), resource, namespace and name. // Returns the appropriate path for the given prefix (watch, proxy, redirect, etc), resource, namespace and name.
// For ex, this is of the form: // For ex, this is of the form:
// /api/v1beta1/watch/pods/pod0 for v1beta1 and // /api/v1/watch/namespaces/foo/pods/pod0 for v1.
// /api/v1beta3/watch/namespaces/foo/pods/pod0 for v1beta3.
func ResourcePathWithPrefix(prefix, resource, namespace, name string) string { func ResourcePathWithPrefix(prefix, resource, namespace, name string) string {
path := "/api/" + Version() path := "/api/" + Version()
if prefix != "" { if prefix != "" {
path = path + "/" + prefix path = path + "/" + prefix
} }
if !api.PreV1Beta3(Version()) {
if namespace != "" { if namespace != "" {
path = path + "/namespaces/" + namespace path = path + "/namespaces/" + namespace
} }
// Resource names in v1beta3 are lower case. // Resource names are lower case.
resource = strings.ToLower(resource) resource = strings.ToLower(resource)
}
if resource != "" { if resource != "" {
path = path + "/" + resource path = path + "/" + resource
} }
@ -109,20 +105,3 @@ func ResourcePathWithPrefix(prefix, resource, namespace, name string) string {
func ResourcePath(resource, namespace, name string) string { func ResourcePath(resource, namespace, name string) string {
return ResourcePathWithPrefix("", resource, namespace, name) return ResourcePathWithPrefix("", resource, namespace, name)
} }
// 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 ResourcePathWithNamespaceQuery(resource, namespace, name string) string {
return ResourcePathWithPrefixAndNamespaceQuery("", resource, namespace, name)
}
func ResourcePathWithPrefixAndNamespaceQuery(prefix, resource, namespace, name string) string {
path := ResourcePathWithPrefix(prefix, resource, namespace, name)
// Add namespace as query param for pre v1beta3.
if api.PreV1Beta3(Version()) && namespace != "" {
path = path + "?namespace=" + namespace
}
return path
}

View File

@ -59,22 +59,3 @@ func TestResourcePath(t *testing.T) {
} }
} }
} }
func TestResourcePathWithNamespaceQuery(t *testing.T) {
testCases := []struct {
resource string
namespace string
name string
expected string
}{
{"resource", "mynamespace", "myresource", "/api/" + Version() + "/namespaces/mynamespace/resource/myresource"},
{"resource", "", "myresource", "/api/" + Version() + "/resource/myresource"},
{"resource", "mynamespace", "", "/api/" + Version() + "/namespaces/mynamespace/resource"},
{"resource", "", "", "/api/" + Version() + "/resource"},
}
for _, item := range testCases {
if actual := ResourcePathWithNamespaceQuery(item.resource, item.namespace, item.name); actual != item.expected {
t.Errorf("Expected: %s, got: %s for resource: %s, namespace: %s and name: %s", item.expected, actual, item.resource, item.namespace, item.name)
}
}
}

View File

@ -53,18 +53,10 @@ func getPath(resource, namespace, name string) string {
return testapi.ResourcePath(resource, namespace, name) return testapi.ResourcePath(resource, namespace, name)
} }
func pathWithNamespaceQuery(resource, namespace, name string) string {
return testapi.ResourcePathWithNamespaceQuery(resource, namespace, name)
}
func pathWithPrefix(prefix, resource, namespace, name string) string { func pathWithPrefix(prefix, resource, namespace, name string) string {
return testapi.ResourcePathWithPrefix(prefix, resource, namespace, name) return testapi.ResourcePathWithPrefix(prefix, resource, namespace, name)
} }
func pathWithPrefixAndNamespaceQuery(prefix, resource, namespace, name string) string {
return testapi.ResourcePathWithPrefixAndNamespaceQuery(prefix, resource, namespace, name)
}
func TestMaxInFlight(t *testing.T) { func TestMaxInFlight(t *testing.T) {
const Iterations = 3 const Iterations = 3
block := sync.WaitGroup{} block := sync.WaitGroup{}
@ -185,15 +177,15 @@ func TestGetAPIRequestInfo(t *testing.T) {
{"GET", "/watch/namespaces/other/pods", "watch", "", "other", "pods", "", "Pod", "", []string{"pods"}}, {"GET", "/watch/namespaces/other/pods", "watch", "", "other", "pods", "", "Pod", "", []string{"pods"}},
// fully-qualified paths // fully-qualified paths
{"GET", pathWithNamespaceQuery("pods", "other", ""), "list", testapi.Version(), "other", "pods", "", "Pod", "", []string{"pods"}}, {"GET", getPath("pods", "other", ""), "list", testapi.Version(), "other", "pods", "", "Pod", "", []string{"pods"}},
{"GET", pathWithNamespaceQuery("pods", "other", "foo"), "get", testapi.Version(), "other", "pods", "", "Pod", "foo", []string{"pods", "foo"}}, {"GET", getPath("pods", "other", "foo"), "get", testapi.Version(), "other", "pods", "", "Pod", "foo", []string{"pods", "foo"}},
{"GET", getPath("pods", "", ""), "list", testapi.Version(), api.NamespaceAll, "pods", "", "Pod", "", []string{"pods"}}, {"GET", getPath("pods", "", ""), "list", testapi.Version(), api.NamespaceAll, "pods", "", "Pod", "", []string{"pods"}},
{"POST", getPath("pods", "", ""), "create", testapi.Version(), api.NamespaceDefault, "pods", "", "Pod", "", []string{"pods"}}, {"POST", getPath("pods", "", ""), "create", testapi.Version(), api.NamespaceDefault, "pods", "", "Pod", "", []string{"pods"}},
{"GET", getPath("pods", "", "foo"), "get", testapi.Version(), api.NamespaceDefault, "pods", "", "Pod", "foo", []string{"pods", "foo"}}, {"GET", getPath("pods", "", "foo"), "get", testapi.Version(), api.NamespaceDefault, "pods", "", "Pod", "foo", []string{"pods", "foo"}},
{"GET", pathWithPrefix("proxy", "pods", "", "foo"), "proxy", testapi.Version(), api.NamespaceDefault, "pods", "", "Pod", "foo", []string{"pods", "foo"}}, {"GET", pathWithPrefix("proxy", "pods", "", "foo"), "proxy", testapi.Version(), api.NamespaceDefault, "pods", "", "Pod", "foo", []string{"pods", "foo"}},
{"GET", pathWithPrefix("watch", "pods", "", ""), "watch", testapi.Version(), api.NamespaceAll, "pods", "", "Pod", "", []string{"pods"}}, {"GET", pathWithPrefix("watch", "pods", "", ""), "watch", testapi.Version(), api.NamespaceAll, "pods", "", "Pod", "", []string{"pods"}},
{"GET", pathWithPrefixAndNamespaceQuery("redirect", "pods", "", ""), "redirect", testapi.Version(), api.NamespaceAll, "pods", "", "Pod", "", []string{"pods"}}, {"GET", pathWithPrefix("redirect", "pods", "", ""), "redirect", testapi.Version(), api.NamespaceAll, "pods", "", "Pod", "", []string{"pods"}},
{"GET", pathWithPrefixAndNamespaceQuery("watch", "pods", "other", ""), "watch", testapi.Version(), "other", "pods", "", "Pod", "", []string{"pods"}}, {"GET", pathWithPrefix("watch", "pods", "other", ""), "watch", testapi.Version(), "other", "pods", "", "Pod", "", []string{"pods"}},
// subresource identification // subresource identification
{"GET", "/namespaces/other/pods/foo/status", "get", "", "other", "pods", "status", "Pod", "foo", []string{"pods", "foo", "status"}}, {"GET", "/namespaces/other/pods/foo/status", "get", "", "other", "pods", "status", "Pod", "foo", []string{"pods", "foo", "status"}},

View File

@ -67,8 +67,8 @@ func TestCreateObjects(t *testing.T) {
typer, mapper := getTyperAndMapper() typer, mapper := getTyperAndMapper()
client, s := getFakeClient(t, []string{ client, s := getFakeClient(t, []string{
testapi.ResourcePathWithNamespaceQuery("pods", api.NamespaceDefault, ""), testapi.ResourcePath("pods", api.NamespaceDefault, ""),
testapi.ResourcePathWithNamespaceQuery("services", api.NamespaceDefault, ""), testapi.ResourcePath("services", api.NamespaceDefault, ""),
}) })
errs := CreateObjects(typer, mapper, client, items) errs := CreateObjects(typer, mapper, client, items)

View File

@ -328,7 +328,7 @@ func TestCreateReplica(t *testing.T) {
}, },
Spec: controllerSpec.Spec.Template.Spec, Spec: controllerSpec.Spec.Template.Spec,
} }
fakeHandler.ValidateRequest(t, testapi.ResourcePathWithNamespaceQuery("pods", api.NamespaceDefault, ""), "POST", nil) fakeHandler.ValidateRequest(t, testapi.ResourcePath("pods", api.NamespaceDefault, ""), "POST", nil)
actualPod, err := client.Codec.Decode([]byte(fakeHandler.RequestBody)) actualPod, err := client.Codec.Decode([]byte(fakeHandler.RequestBody))
if err != nil { if err != nil {
t.Errorf("Unexpected error: %#v", err) t.Errorf("Unexpected error: %#v", err)
@ -397,7 +397,7 @@ func TestControllerUpdateReplicas(t *testing.T) {
rc.Status = api.ReplicationControllerStatus{Replicas: 4} rc.Status = api.ReplicationControllerStatus{Replicas: 4}
decRc := runtime.EncodeOrDie(testapi.Codec(), rc) decRc := runtime.EncodeOrDie(testapi.Codec(), rc)
fakeHandler.ValidateRequest(t, testapi.ResourcePathWithNamespaceQuery(replicationControllerResourceName(), rc.Namespace, rc.Name), "PUT", &decRc) fakeHandler.ValidateRequest(t, testapi.ResourcePath(replicationControllerResourceName(), rc.Namespace, rc.Name), "PUT", &decRc)
validateSyncReplication(t, &fakePodControl, 1, 0) validateSyncReplication(t, &fakePodControl, 1, 0)
} }

View File

@ -318,7 +318,7 @@ func TestSyncEndpointsItemsEmptySelectorSelectsAll(t *testing.T) {
Ports: []api.EndpointPort{{Port: 8080, Protocol: "TCP"}}, Ports: []api.EndpointPort{{Port: 8080, Protocol: "TCP"}},
}}, }},
}) })
endpointsHandler.ValidateRequest(t, testapi.ResourcePathWithNamespaceQuery("endpoints", ns, "foo"), "PUT", &data) endpointsHandler.ValidateRequest(t, testapi.ResourcePath("endpoints", ns, "foo"), "PUT", &data)
} }
func TestSyncEndpointsItemsPreexisting(t *testing.T) { func TestSyncEndpointsItemsPreexisting(t *testing.T) {
@ -358,7 +358,7 @@ func TestSyncEndpointsItemsPreexisting(t *testing.T) {
Ports: []api.EndpointPort{{Port: 8080, Protocol: "TCP"}}, Ports: []api.EndpointPort{{Port: 8080, Protocol: "TCP"}},
}}, }},
}) })
endpointsHandler.ValidateRequest(t, testapi.ResourcePathWithNamespaceQuery("endpoints", ns, "foo"), "PUT", &data) endpointsHandler.ValidateRequest(t, testapi.ResourcePath("endpoints", ns, "foo"), "PUT", &data)
} }
func TestSyncEndpointsItemsPreexistingIdentical(t *testing.T) { func TestSyncEndpointsItemsPreexistingIdentical(t *testing.T) {
@ -387,7 +387,7 @@ func TestSyncEndpointsItemsPreexistingIdentical(t *testing.T) {
}, },
}) })
endpoints.syncService(ns + "/foo") endpoints.syncService(ns + "/foo")
endpointsHandler.ValidateRequest(t, testapi.ResourcePathWithNamespaceQuery("endpoints", api.NamespaceDefault, "foo"), "GET", nil) endpointsHandler.ValidateRequest(t, testapi.ResourcePath("endpoints", api.NamespaceDefault, "foo"), "GET", nil)
} }
func TestSyncEndpointsItems(t *testing.T) { func TestSyncEndpointsItems(t *testing.T) {
@ -429,7 +429,7 @@ func TestSyncEndpointsItems(t *testing.T) {
}) })
// endpointsHandler should get 2 requests - one for "GET" and the next for "POST". // endpointsHandler should get 2 requests - one for "GET" and the next for "POST".
endpointsHandler.ValidateRequestCount(t, 2) endpointsHandler.ValidateRequestCount(t, 2)
endpointsHandler.ValidateRequest(t, testapi.ResourcePathWithNamespaceQuery("endpoints", ns, ""), "POST", &data) endpointsHandler.ValidateRequest(t, testapi.ResourcePath("endpoints", ns, ""), "POST", &data)
} }
func TestSyncEndpointsItemsWithLabels(t *testing.T) { func TestSyncEndpointsItemsWithLabels(t *testing.T) {
@ -476,7 +476,7 @@ func TestSyncEndpointsItemsWithLabels(t *testing.T) {
}) })
// endpointsHandler should get 2 requests - one for "GET" and the next for "POST". // endpointsHandler should get 2 requests - one for "GET" and the next for "POST".
endpointsHandler.ValidateRequestCount(t, 2) endpointsHandler.ValidateRequestCount(t, 2)
endpointsHandler.ValidateRequest(t, testapi.ResourcePathWithNamespaceQuery("endpoints", ns, ""), "POST", &data) endpointsHandler.ValidateRequest(t, testapi.ResourcePath("endpoints", ns, ""), "POST", &data)
} }
func TestSyncEndpointsItemsPreexistingLabelsChange(t *testing.T) { func TestSyncEndpointsItemsPreexistingLabelsChange(t *testing.T) {
@ -525,5 +525,5 @@ func TestSyncEndpointsItemsPreexistingLabelsChange(t *testing.T) {
Ports: []api.EndpointPort{{Port: 8080, Protocol: "TCP"}}, Ports: []api.EndpointPort{{Port: 8080, Protocol: "TCP"}},
}}, }},
}) })
endpointsHandler.ValidateRequest(t, testapi.ResourcePathWithNamespaceQuery("endpoints", ns, "foo"), "PUT", &data) endpointsHandler.ValidateRequest(t, testapi.ResourcePath("endpoints", ns, "foo"), "PUT", &data)
} }

View File

@ -170,7 +170,7 @@ func TestDefaultErrorFunc(t *testing.T) {
if !exists { if !exists {
continue continue
} }
handler.ValidateRequest(t, testapi.ResourcePathWithNamespaceQuery("pods", "bar", "foo"), "GET", nil) handler.ValidateRequest(t, testapi.ResourcePath("pods", "bar", "foo"), "GET", nil)
if e, a := testPod, got; !reflect.DeepEqual(e, a) { if e, a := testPod, got; !reflect.DeepEqual(e, a) {
t.Errorf("Expected %v, got %v", e, a) t.Errorf("Expected %v, got %v", e, a)
} }
@ -241,7 +241,7 @@ func TestBind(t *testing.T) {
continue continue
} }
expectedBody := runtime.EncodeOrDie(testapi.Codec(), item.binding) expectedBody := runtime.EncodeOrDie(testapi.Codec(), item.binding)
handler.ValidateRequest(t, testapi.ResourcePathWithNamespaceQuery("bindings", api.NamespaceDefault, ""), "POST", &expectedBody) handler.ValidateRequest(t, testapi.ResourcePath("bindings", api.NamespaceDefault, ""), "POST", &expectedBody)
} }
} }

View File

@ -78,10 +78,6 @@ func path(resource, namespace, name string) string {
return testapi.ResourcePath(resource, namespace, name) return testapi.ResourcePath(resource, namespace, name)
} }
func pathWithNamespaceQuery(resource, namespace, name string) string {
return testapi.ResourcePathWithNamespaceQuery(resource, namespace, name)
}
func pathWithPrefix(prefix, resource, namespace, name string) string { func pathWithPrefix(prefix, resource, namespace, name string) string {
return testapi.ResourcePathWithPrefix(prefix, resource, namespace, name) return testapi.ResourcePathWithPrefix(prefix, resource, namespace, name)
} }
@ -90,10 +86,6 @@ func timeoutPath(resource, namespace, name string) string {
return addTimeoutFlag(testapi.ResourcePath(resource, namespace, name)) return addTimeoutFlag(testapi.ResourcePath(resource, namespace, name))
} }
func timeoutPathWithNamespaceQuery(resource, namespace, name string) string {
return addTimeoutFlag(testapi.ResourcePathWithNamespaceQuery(resource, namespace, name))
}
// Bodies for requests used in subsequent tests. // Bodies for requests used in subsequent tests.
var aPod string = ` var aPod string = `
{ {
@ -846,15 +838,15 @@ func TestNamespaceAuthorization(t *testing.T) {
statusCodes map[int]bool // allowed status codes. statusCodes map[int]bool // allowed status codes.
}{ }{
{"POST", timeoutPathWithNamespaceQuery("pods", "foo", ""), "foo", aPod, code201}, {"POST", timeoutPath("pods", "foo", ""), "foo", aPod, code201},
{"GET", pathWithNamespaceQuery("pods", "foo", ""), "foo", "", code200}, {"GET", path("pods", "foo", ""), "foo", "", code200},
{"GET", pathWithNamespaceQuery("pods", "foo", "a"), "foo", "", code200}, {"GET", path("pods", "foo", "a"), "foo", "", code200},
{"DELETE", timeoutPathWithNamespaceQuery("pods", "foo", "a"), "foo", "", code200}, {"DELETE", timeoutPath("pods", "foo", "a"), "foo", "", code200},
{"POST", timeoutPath("pods", "bar", ""), "bar", aPod, code403}, {"POST", timeoutPath("pods", "bar", ""), "bar", aPod, code403},
{"GET", pathWithNamespaceQuery("pods", "bar", ""), "bar", "", code403}, {"GET", path("pods", "bar", ""), "bar", "", code403},
{"GET", pathWithNamespaceQuery("pods", "bar", "a"), "bar", "", code403}, {"GET", path("pods", "bar", "a"), "bar", "", code403},
{"DELETE", timeoutPathWithNamespaceQuery("pods", "bar", "a"), "bar", "", code403}, {"DELETE", timeoutPath("pods", "bar", "a"), "bar", "", code403},
{"POST", timeoutPath("pods", api.NamespaceDefault, ""), "", aPod, code403}, {"POST", timeoutPath("pods", api.NamespaceDefault, ""), "", aPod, code403},
{"GET", path("pods", "", ""), "", "", code403}, {"GET", path("pods", "", ""), "", "", code403},