Skip to content

Commit

Permalink
Removing more references to v1beta1 from pkg/
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhiljindal committed Apr 21, 2015
1 parent b9904f6 commit 9a98ea0
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 238 deletions.
6 changes: 5 additions & 1 deletion pkg/api/testapi/testapi.go
Expand Up @@ -115,7 +115,11 @@ func ResourcePath(resource, namespace, name string) string {
// /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)
return ResourcePathWithPrefixAndQueryParams("", resource, namespace, name)
}

func ResourcePathWithPrefixAndQueryParams(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
Expand Down
40 changes: 27 additions & 13 deletions pkg/apiserver/handlers_test.go
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)

Expand All @@ -48,6 +49,22 @@ func expectHTTP(url string, code int, t *testing.T) {
}
}

func getPath(resource, namespace, name string) string {
return testapi.ResourcePath(resource, namespace, name)
}

func pathWithQuery(resource, namespace, name string) string {
return testapi.ResourcePathWithQueryParams(resource, namespace, name)
}

func pathWithPrefix(prefix, resource, namespace, name string) string {
return testapi.ResourcePathWithPrefix(prefix, resource, namespace, name)
}

func pathWithPrefixAndQuery(prefix, resource, namespace, name string) string {
return testapi.ResourcePathWithPrefixAndQueryParams(prefix, resource, namespace, name)
}

func TestMaxInFlight(t *testing.T) {
const Iterations = 3
block := sync.WaitGroup{}
Expand Down Expand Up @@ -168,17 +185,14 @@ func TestGetAPIRequestInfo(t *testing.T) {
{"GET", "/watch/namespaces/other/pods", "watch", "", "other", "pods", "", "Pod", "", []string{"pods"}},

// fully-qualified paths
{"GET", "/api/v1beta1/namespaces/other/pods", "list", "v1beta1", "other", "pods", "", "Pod", "", []string{"pods"}},
{"GET", "/api/v1beta1/namespaces/other/pods/foo", "get", "v1beta1", "other", "pods", "", "Pod", "foo", []string{"pods", "foo"}},
{"GET", "/api/v1beta1/pods", "list", "v1beta1", api.NamespaceAll, "pods", "", "Pod", "", []string{"pods"}},
{"POST", "/api/v1beta1/pods", "create", "v1beta1", api.NamespaceDefault, "pods", "", "Pod", "", []string{"pods"}},
{"GET", "/api/v1beta1/pods/foo", "get", "v1beta1", api.NamespaceDefault, "pods", "", "Pod", "foo", []string{"pods", "foo"}},
{"GET", "/api/v1beta1/pods/foo?namespace=other", "get", "v1beta1", "other", "pods", "", "Pod", "foo", []string{"pods", "foo"}},
{"GET", "/api/v1beta1/pods?namespace=other", "list", "v1beta1", "other", "pods", "", "Pod", "", []string{"pods"}},
{"GET", "/api/v1beta1/proxy/pods/foo", "proxy", "v1beta1", api.NamespaceDefault, "pods", "", "Pod", "foo", []string{"pods", "foo"}},
{"GET", "/api/v1beta1/redirect/pods/foo", "redirect", "v1beta1", api.NamespaceDefault, "pods", "", "Pod", "foo", []string{"pods", "foo"}},
{"GET", "/api/v1beta1/watch/pods", "watch", "v1beta1", api.NamespaceAll, "pods", "", "Pod", "", []string{"pods"}},
{"GET", "/api/v1beta1/watch/namespaces/other/pods", "watch", "v1beta1", "other", "pods", "", "Pod", "", []string{"pods"}},
{"GET", pathWithQuery("pods", "other", ""), "list", testapi.Version(), "other", "pods", "", "Pod", "", []string{"pods"}},
{"GET", pathWithQuery("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", pathWithPrefixAndQuery("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 Expand Up @@ -222,8 +236,8 @@ func TestGetAPIRequestInfo(t *testing.T) {

errorCases := map[string]string{
"no resource path": "/",
"just apiversion": "/api/v1beta1/",
"apiversion with no resource": "/api/v1beta1/",
"just apiversion": "/api/version/",
"apiversion with no resource": "/api/version/",
}
for k, v := range errorCases {
req, err := http.NewRequest("GET", v, nil)
Expand Down
58 changes: 33 additions & 25 deletions pkg/client/request_test.go
Expand Up @@ -36,8 +36,6 @@ import (
apierrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta2"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
Expand Down Expand Up @@ -712,15 +710,15 @@ func TestDoRequestNewWay(t *testing.T) {
Port: 12345,
TargetPort: util.NewIntOrStringFromInt(12345),
}}}}
expectedBody, _ := v1beta2.Codec.Encode(expectedObj)
expectedBody, _ := testapi.Codec().Encode(expectedObj)
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(expectedBody),
T: t,
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta2", Username: "user", Password: "pass"})
c := NewOrDie(&Config{Host: testServer.URL, Version: testapi.Version(), Username: "user", Password: "pass"})
obj, err := c.Verb("POST").
Prefix("foo", "bar").
Suffix("baz").
Expand All @@ -736,7 +734,9 @@ func TestDoRequestNewWay(t *testing.T) {
} else if !api.Semantic.DeepDerivative(expectedObj, obj) {
t.Errorf("Expected: %#v, got %#v", expectedObj, obj)
}
fakeHandler.ValidateRequest(t, "/api/v1beta2/foo/bar/baz?timeout=1s", "POST", &reqBody)
requestURL := testapi.ResourcePathWithPrefix("foo/bar", "", "", "baz")
requestURL += "?timeout=1s"
fakeHandler.ValidateRequest(t, requestURL, "POST", &reqBody)
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
}
Expand All @@ -758,7 +758,7 @@ func TestCheckRetryClosesBody(t *testing.T) {
}))
defer testServer.Close()

c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta2", Username: "user", Password: "pass"})
c := NewOrDie(&Config{Host: testServer.URL, Version: testapi.Version(), Username: "user", Password: "pass"})
_, err := c.Verb("POST").
Prefix("foo", "bar").
Suffix("baz").
Expand Down Expand Up @@ -787,7 +787,7 @@ func BenchmarkCheckRetryClosesBody(t *testing.B) {
}))
defer testServer.Close()

c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta2", Username: "user", Password: "pass"})
c := NewOrDie(&Config{Host: testServer.URL, Version: testapi.Version(), Username: "user", Password: "pass"})
r := c.Verb("POST").
Prefix("foo", "bar").
Suffix("baz").
Expand All @@ -802,20 +802,20 @@ func BenchmarkCheckRetryClosesBody(t *testing.B) {
}
func TestDoRequestNewWayReader(t *testing.T) {
reqObj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
reqBodyExpected, _ := v1beta1.Codec.Encode(reqObj)
reqBodyExpected, _ := testapi.Codec().Encode(reqObj)
expectedObj := &api.Service{Spec: api.ServiceSpec{Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 12345,
TargetPort: util.NewIntOrStringFromInt(12345),
}}}}
expectedBody, _ := v1beta1.Codec.Encode(expectedObj)
expectedBody, _ := testapi.Codec().Encode(expectedObj)
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(expectedBody),
T: t,
}
testServer := httptest.NewServer(&fakeHandler)
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta1", Username: "user", Password: "pass"})
c := NewOrDie(&Config{Host: testServer.URL, Version: testapi.Version(), Username: "user", Password: "pass"})
obj, err := c.Verb("POST").
Resource("bar").
Name("baz").
Expand All @@ -834,28 +834,30 @@ func TestDoRequestNewWayReader(t *testing.T) {
t.Errorf("Expected: %#v, got %#v", expectedObj, obj)
}
tmpStr := string(reqBodyExpected)
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?labels=name%3Dfoo&timeout=1s", "POST", &tmpStr)
requestURL := testapi.ResourcePathWithPrefix("foo", "bar", "", "baz")
requestURL += "?" + api.LabelSelectorQueryParam(testapi.Version()) + "=name%3Dfoo&timeout=1s"
fakeHandler.ValidateRequest(t, requestURL, "POST", &tmpStr)
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
}
}

func TestDoRequestNewWayObj(t *testing.T) {
reqObj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
reqBodyExpected, _ := v1beta2.Codec.Encode(reqObj)
reqBodyExpected, _ := testapi.Codec().Encode(reqObj)
expectedObj := &api.Service{Spec: api.ServiceSpec{Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 12345,
TargetPort: util.NewIntOrStringFromInt(12345),
}}}}
expectedBody, _ := v1beta2.Codec.Encode(expectedObj)
expectedBody, _ := testapi.Codec().Encode(expectedObj)
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(expectedBody),
T: t,
}
testServer := httptest.NewServer(&fakeHandler)
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta2", Username: "user", Password: "pass"})
c := NewOrDie(&Config{Host: testServer.URL, Version: testapi.Version(), Username: "user", Password: "pass"})
obj, err := c.Verb("POST").
Suffix("baz").
Name("bar").
Expand All @@ -874,15 +876,17 @@ func TestDoRequestNewWayObj(t *testing.T) {
t.Errorf("Expected: %#v, got %#v", expectedObj, obj)
}
tmpStr := string(reqBodyExpected)
fakeHandler.ValidateRequest(t, "/api/v1beta2/foo/bar/baz?labels=name%3Dfoo&timeout=1s", "POST", &tmpStr)
requestURL := testapi.ResourcePath("foo", "", "bar/baz")
requestURL += "?" + api.LabelSelectorQueryParam(testapi.Version()) + "=name%3Dfoo&timeout=1s"
fakeHandler.ValidateRequest(t, requestURL, "POST", &tmpStr)
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
}
}

func TestDoRequestNewWayFile(t *testing.T) {
reqObj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
reqBodyExpected, err := v1beta1.Codec.Encode(reqObj)
reqBodyExpected, err := testapi.Codec().Encode(reqObj)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand All @@ -902,14 +906,14 @@ func TestDoRequestNewWayFile(t *testing.T) {
Port: 12345,
TargetPort: util.NewIntOrStringFromInt(12345),
}}}}
expectedBody, _ := v1beta1.Codec.Encode(expectedObj)
expectedBody, _ := testapi.Codec().Encode(expectedObj)
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(expectedBody),
T: t,
}
testServer := httptest.NewServer(&fakeHandler)
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta1", Username: "user", Password: "pass"})
c := NewOrDie(&Config{Host: testServer.URL, Version: testapi.Version(), Username: "user", Password: "pass"})
wasCreated := true
obj, err := c.Verb("POST").
Prefix("foo/bar", "baz").
Expand All @@ -929,15 +933,17 @@ func TestDoRequestNewWayFile(t *testing.T) {
t.Errorf("expected object was not created")
}
tmpStr := string(reqBodyExpected)
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?timeout=1s", "POST", &tmpStr)
requestURL := testapi.ResourcePathWithPrefix("foo/bar/baz", "", "", "")
requestURL += "?timeout=1s"
fakeHandler.ValidateRequest(t, requestURL, "POST", &tmpStr)
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
}
}

func TestWasCreated(t *testing.T) {
reqObj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
reqBodyExpected, err := v1beta1.Codec.Encode(reqObj)
reqBodyExpected, err := testapi.Codec().Encode(reqObj)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand All @@ -947,14 +953,14 @@ func TestWasCreated(t *testing.T) {
Port: 12345,
TargetPort: util.NewIntOrStringFromInt(12345),
}}}}
expectedBody, _ := v1beta1.Codec.Encode(expectedObj)
expectedBody, _ := testapi.Codec().Encode(expectedObj)
fakeHandler := util.FakeHandler{
StatusCode: 201,
ResponseBody: string(expectedBody),
T: t,
}
testServer := httptest.NewServer(&fakeHandler)
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta1", Username: "user", Password: "pass"})
c := NewOrDie(&Config{Host: testServer.URL, Version: testapi.Version(), Username: "user", Password: "pass"})
wasCreated := false
obj, err := c.Verb("PUT").
Prefix("foo/bar", "baz").
Expand All @@ -975,7 +981,9 @@ func TestWasCreated(t *testing.T) {
}

tmpStr := string(reqBodyExpected)
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?timeout=1s", "PUT", &tmpStr)
requestURL := testapi.ResourcePathWithPrefix("foo/bar/baz", "", "", "")
requestURL += "?timeout=1s"
fakeHandler.ValidateRequest(t, requestURL, "PUT", &tmpStr)
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
}
Expand Down Expand Up @@ -1142,7 +1150,7 @@ func TestWatch(t *testing.T) {

s, err := New(&Config{
Host: testServer.URL,
Version: "v1beta1",
Version: testapi.Version(),
Username: "user",
Password: "pass",
})
Expand Down Expand Up @@ -1192,7 +1200,7 @@ func TestStream(t *testing.T) {

s, err := New(&Config{
Host: testServer.URL,
Version: "v1beta1",
Version: testapi.Version(),
Username: "user",
Password: "pass",
})
Expand Down
26 changes: 18 additions & 8 deletions pkg/config/config_test.go
Expand Up @@ -27,6 +27,7 @@ import (
"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/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
)
Expand All @@ -46,9 +47,10 @@ func getFakeClient(t *testing.T, validURLs []string) (ClientPosterFunc, *httptes
}
server := httptest.NewServer(http.HandlerFunc(handlerFunc))
return func(mapping *meta.RESTMapping) (RESTClientPoster, error) {
fakeCodec := runtime.CodecFor(api.Scheme, "v1beta1")
fakeUri, _ := url.Parse(server.URL + "/api/v1beta1")
return client.NewRESTClient(fakeUri, "v1beta1", fakeCodec, true, 5, 10), nil
fakeCodec := testapi.Codec()
fakeUri, _ := url.Parse(server.URL + "/api/" + testapi.Version())
legacyBehavior := api.PreV1Beta3(testapi.Version())
return client.NewRESTClient(fakeUri, testapi.Version(), fakeCodec, legacyBehavior, 5, 10), nil
}, server
}

Expand All @@ -64,7 +66,10 @@ func TestCreateObjects(t *testing.T) {
})

typer, mapper := getTyperAndMapper()
client, s := getFakeClient(t, []string{"/api/v1beta1/pods?namespace=default", "/api/v1beta1/services?namespace=default"})
client, s := getFakeClient(t, []string{
testapi.ResourcePathWithQueryParams("pods", api.NamespaceDefault, ""),
testapi.ResourcePathWithQueryParams("services", api.NamespaceDefault, ""),
})

errs := CreateObjects(typer, mapper, client, items)
s.Close()
Expand All @@ -77,11 +82,13 @@ func TestCreateNoNameItem(t *testing.T) {
items := []runtime.Object{}

items = append(items, &api.Service{
TypeMeta: api.TypeMeta{APIVersion: "v1beta1", Kind: "Service"},
TypeMeta: api.TypeMeta{APIVersion: testapi.Version(), Kind: "Service"},
})

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

errs := CreateObjects(typer, mapper, client, items)
s.Close()
Expand Down Expand Up @@ -125,12 +132,15 @@ func TestCreateNoClientItems(t *testing.T) {
items := []runtime.Object{}

items = append(items, &api.Pod{
TypeMeta: api.TypeMeta{APIVersion: "v1beta1", Kind: "Pod"},
TypeMeta: api.TypeMeta{APIVersion: testapi.Version(), Kind: "Pod"},
ObjectMeta: api.ObjectMeta{Name: "test-pod"},
})

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

noClientFunc := func(mapping *meta.RESTMapping) (RESTClientPoster, error) {
return nil, fmt.Errorf("no client")
Expand Down

0 comments on commit 9a98ea0

Please sign in to comment.