Move namespace from query param to path part

This commit is contained in:
derekwaynecarr
2014-12-09 14:23:21 -05:00
parent 58ba3c7faa
commit 7cf664439f
16 changed files with 388 additions and 156 deletions

View File

@@ -19,7 +19,10 @@ package apiserver
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
)
type fakeRL bool
@@ -59,3 +62,75 @@ func TestReadOnly(t *testing.T) {
http.DefaultClient.Do(req)
}
}
func TestKindAndNamespace(t *testing.T) {
successCases := []struct {
url string
expectedNamespace string
expectedKind string
expectedParts []string
}{
// resource paths
{"/ns/other/pods", "other", "pods", []string{"pods"}},
{"/ns/other/pods/foo", "other", "pods", []string{"pods", "foo"}},
{"/pods", api.NamespaceAll, "pods", []string{"pods"}},
{"/pods/foo", api.NamespaceDefault, "pods", []string{"pods", "foo"}},
{"/pods/foo?namespace=other", "other", "pods", []string{"pods", "foo"}},
{"/pods?namespace=other", "other", "pods", []string{"pods"}},
// special verbs
{"/proxy/ns/other/pods/foo", "other", "pods", []string{"pods", "foo"}},
{"/proxy/pods/foo", api.NamespaceDefault, "pods", []string{"pods", "foo"}},
{"/redirect/ns/other/pods/foo", "other", "pods", []string{"pods", "foo"}},
{"/redirect/pods/foo", api.NamespaceDefault, "pods", []string{"pods", "foo"}},
{"/watch/pods", api.NamespaceAll, "pods", []string{"pods"}},
{"/watch/ns/other/pods", "other", "pods", []string{"pods"}},
// full-qualified paths
{"/api/v1beta1/ns/other/pods", "other", "pods", []string{"pods"}},
{"/api/v1beta1/ns/other/pods/foo", "other", "pods", []string{"pods", "foo"}},
{"/api/v1beta1/pods", api.NamespaceAll, "pods", []string{"pods"}},
{"/api/v1beta1/pods/foo", api.NamespaceDefault, "pods", []string{"pods", "foo"}},
{"/api/v1beta1/pods/foo?namespace=other", "other", "pods", []string{"pods", "foo"}},
{"/api/v1beta1/pods?namespace=other", "other", "pods", []string{"pods"}},
{"/api/v1beta1/proxy/pods/foo", api.NamespaceDefault, "pods", []string{"pods", "foo"}},
{"/api/v1beta1/redirect/pods/foo", api.NamespaceDefault, "pods", []string{"pods", "foo"}},
{"/api/v1beta1/watch/pods", api.NamespaceAll, "pods", []string{"pods"}},
{"/api/v1beta1/watch/ns/other/pods", "other", "pods", []string{"pods"}},
}
for _, successCase := range successCases {
req, _ := http.NewRequest("GET", successCase.url, nil)
namespace, kind, parts, err := KindAndNamespace(req)
if err != nil {
t.Errorf("Unexpected error for url: %s", successCase.url)
}
if successCase.expectedNamespace != namespace {
t.Errorf("Unexpected namespace for url: %s, expected: %s, actual: %s", successCase.url, successCase.expectedNamespace, namespace)
}
if successCase.expectedKind != kind {
t.Errorf("Unexpected resourceType for url: %s, expected: %s, actual: %s", successCase.url, successCase.expectedKind, kind)
}
if !reflect.DeepEqual(successCase.expectedParts, parts) {
t.Errorf("Unexpected parts for url: %s, expected: %v, actual: %v", successCase.url, successCase.expectedParts, parts)
}
}
errorCases := map[string]string{
"no resource path": "/",
"missing resource type": "/ns/other",
"just apiversion": "/api/v1beta1/",
"apiversion with no resource": "/api/v1beta1/",
"apiversion with just namespace": "/api/v1beta1/ns/other",
}
for k, v := range errorCases {
req, err := http.NewRequest("GET", v, nil)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
_, _, _, err = KindAndNamespace(req)
if err == nil {
t.Errorf("Expected error for key: %s", k)
}
}
}