Skip to content

Commit

Permalink
Merge pull request #9835 from caesarxuchao/remove-testapi-nsQuery
Browse files Browse the repository at this point in the history
Update pkg/api/testapi because namespace is always in the path now
  • Loading branch information
saad-ali committed Jun 16, 2015
2 parents 9fd02fc + a309d3e commit 0aae358
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 84 deletions.
31 changes: 5 additions & 26 deletions pkg/api/testapi/testapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"os"
"strings"

"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"
Expand Down Expand Up @@ -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.
// For ex, this is of the form:
// /api/v1beta1/watch/pods/pod0 for v1beta1 and
// /api/v1beta3/watch/namespaces/foo/pods/pod0 for v1beta3.
// /api/v1/watch/namespaces/foo/pods/pod0 for v1.
func ResourcePathWithPrefix(prefix, resource, namespace, name string) string {
path := "/api/" + Version()
if prefix != "" {
path = path + "/" + prefix
}
if !api.PreV1Beta3(Version()) {
if namespace != "" {
path = path + "/namespaces/" + namespace
}
// Resource names in v1beta3 are lower case.
resource = strings.ToLower(resource)
if namespace != "" {
path = path + "/namespaces/" + namespace
}
// Resource names are lower case.
resource = strings.ToLower(resource)
if resource != "" {
path = path + "/" + resource
}
Expand All @@ -109,20 +105,3 @@ func ResourcePathWithPrefix(prefix, resource, namespace, name string) string {
func ResourcePath(resource, namespace, name string) string {
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
}
19 changes: 0 additions & 19 deletions pkg/api/testapi/testapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
16 changes: 4 additions & 12 deletions pkg/apiserver/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,10 @@ func getPath(resource, namespace, name string) string {
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 {
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) {
const Iterations = 3
block := sync.WaitGroup{}
Expand Down Expand Up @@ -185,15 +177,15 @@ func TestGetAPIRequestInfo(t *testing.T) {
{"GET", "/watch/namespaces/other/pods", "watch", "", "other", "pods", "", "Pod", "", []string{"pods"}},

// fully-qualified paths
{"GET", pathWithNamespaceQuery("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", ""), "list", testapi.Version(), "other", "pods", "", "Pod", "", []string{"pods"}},
{"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"}},
{"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", 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", pathWithPrefixAndNamespaceQuery("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("redirect", "pods", "", ""), "redirect", testapi.Version(), api.NamespaceAll, "pods", "", "Pod", "", []string{"pods"}},
{"GET", pathWithPrefix("watch", "pods", "other", ""), "watch", testapi.Version(), "other", "pods", "", "Pod", "", []string{"pods"}},

// subresource identification
{"GET", "/namespaces/other/pods/foo/status", "get", "", "other", "pods", "status", "Pod", "foo", []string{"pods", "foo", "status"}},
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func TestCreateObjects(t *testing.T) {

typer, mapper := getTyperAndMapper()
client, s := getFakeClient(t, []string{
testapi.ResourcePathWithNamespaceQuery("pods", api.NamespaceDefault, ""),
testapi.ResourcePathWithNamespaceQuery("services", api.NamespaceDefault, ""),
testapi.ResourcePath("pods", api.NamespaceDefault, ""),
testapi.ResourcePath("services", api.NamespaceDefault, ""),
})

errs := CreateObjects(typer, mapper, client, items)
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/replication_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func TestCreateReplica(t *testing.T) {
},
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))
if err != nil {
t.Errorf("Unexpected error: %#v", err)
Expand Down Expand Up @@ -397,7 +397,7 @@ func TestControllerUpdateReplicas(t *testing.T) {
rc.Status = api.ReplicationControllerStatus{Replicas: 4}

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)
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/service/endpoints_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func TestSyncEndpointsItemsEmptySelectorSelectsAll(t *testing.T) {
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) {
Expand Down Expand Up @@ -358,7 +358,7 @@ func TestSyncEndpointsItemsPreexisting(t *testing.T) {
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) {
Expand Down Expand Up @@ -387,7 +387,7 @@ func TestSyncEndpointsItemsPreexistingIdentical(t *testing.T) {
},
})
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) {
Expand Down Expand Up @@ -429,7 +429,7 @@ func TestSyncEndpointsItems(t *testing.T) {
})
// endpointsHandler should get 2 requests - one for "GET" and the next for "POST".
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) {
Expand Down Expand Up @@ -476,7 +476,7 @@ func TestSyncEndpointsItemsWithLabels(t *testing.T) {
})
// endpointsHandler should get 2 requests - one for "GET" and the next for "POST".
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) {
Expand Down Expand Up @@ -525,5 +525,5 @@ func TestSyncEndpointsItemsPreexistingLabelsChange(t *testing.T) {
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)
}
4 changes: 2 additions & 2 deletions plugin/pkg/scheduler/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestDefaultErrorFunc(t *testing.T) {
if !exists {
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) {
t.Errorf("Expected %v, got %v", e, a)
}
Expand Down Expand Up @@ -241,7 +241,7 @@ func TestBind(t *testing.T) {
continue
}
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)
}
}

Expand Down
22 changes: 7 additions & 15 deletions test/integration/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ func path(resource, namespace, name string) string {
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 {
return testapi.ResourcePathWithPrefix(prefix, resource, namespace, name)
}
Expand All @@ -90,10 +86,6 @@ func timeoutPath(resource, namespace, name string) string {
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.
var aPod string = `
{
Expand Down Expand Up @@ -846,15 +838,15 @@ func TestNamespaceAuthorization(t *testing.T) {
statusCodes map[int]bool // allowed status codes.
}{

{"POST", timeoutPathWithNamespaceQuery("pods", "foo", ""), "foo", aPod, code201},
{"GET", pathWithNamespaceQuery("pods", "foo", ""), "foo", "", code200},
{"GET", pathWithNamespaceQuery("pods", "foo", "a"), "foo", "", code200},
{"DELETE", timeoutPathWithNamespaceQuery("pods", "foo", "a"), "foo", "", code200},
{"POST", timeoutPath("pods", "foo", ""), "foo", aPod, code201},
{"GET", path("pods", "foo", ""), "foo", "", code200},
{"GET", path("pods", "foo", "a"), "foo", "", code200},
{"DELETE", timeoutPath("pods", "foo", "a"), "foo", "", code200},

{"POST", timeoutPath("pods", "bar", ""), "bar", aPod, code403},
{"GET", pathWithNamespaceQuery("pods", "bar", ""), "bar", "", code403},
{"GET", pathWithNamespaceQuery("pods", "bar", "a"), "bar", "", code403},
{"DELETE", timeoutPathWithNamespaceQuery("pods", "bar", "a"), "bar", "", code403},
{"GET", path("pods", "bar", ""), "bar", "", code403},
{"GET", path("pods", "bar", "a"), "bar", "", code403},
{"DELETE", timeoutPath("pods", "bar", "a"), "bar", "", code403},

{"POST", timeoutPath("pods", api.NamespaceDefault, ""), "", aPod, code403},
{"GET", path("pods", "", ""), "", "", code403},
Expand Down

0 comments on commit 0aae358

Please sign in to comment.