From 2aa28c65538393ecacf56ffb126361fca457a588 Mon Sep 17 00:00:00 2001 From: nikhiljindal Date: Tue, 2 Feb 2016 13:03:49 -0800 Subject: [PATCH] Adding test for apiserver example --- examples/apiserver/README.md | 6 + .../apiserver/{server.go => apiserver.go} | 8 +- examples/apiserver/apiserver_test.go | 104 ++++++++++++++++++ examples/apiserver/server/main.go | 25 +++++ pkg/genericapiserver/genericapiserver.go | 3 + pkg/genericapiserver/genericapiserver_test.go | 1 + 6 files changed, 145 insertions(+), 2 deletions(-) rename examples/apiserver/{server.go => apiserver.go} (94%) create mode 100644 examples/apiserver/apiserver_test.go create mode 100644 examples/apiserver/server/main.go diff --git a/examples/apiserver/README.md b/examples/apiserver/README.md index 522544984f8..d493f7fbdea 100644 --- a/examples/apiserver/README.md +++ b/examples/apiserver/README.md @@ -39,6 +39,12 @@ API objects. Some relevant issues: This code here is to examplify what it takes to write your own API server. +To start this example api server, run: + +``` +$ go run examples/apiserver/server/main.go +``` + [![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/examples/apiserver/README.md?pixel)]() diff --git a/examples/apiserver/server.go b/examples/apiserver/apiserver.go similarity index 94% rename from examples/apiserver/server.go rename to examples/apiserver/apiserver.go index cd565e43a3c..9a61528f75f 100644 --- a/examples/apiserver/server.go +++ b/examples/apiserver/apiserver.go @@ -14,12 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package apiserver import ( "github.com/golang/glog" "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testdata/apis/testgroup/v1" testgroupetcd "k8s.io/kubernetes/examples/apiserver/rest" + "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/rest" "k8s.io/kubernetes/pkg/apimachinery" "k8s.io/kubernetes/pkg/apimachinery/registered" @@ -44,11 +45,12 @@ func newStorageDestinations(groupName string, groupMeta *apimachinery.GroupMeta) return &storageDestinations, nil } -func main() { +func Run() { config := genericapiserver.Config{ EnableIndex: true, APIPrefix: "/api", APIGroupPrefix: "/apis", + Serializer: api.Codecs, } s := genericapiserver.New(&config) @@ -70,6 +72,8 @@ func main() { VersionedResourcesStorageMap: map[string]map[string]rest.Storage{ groupVersion.Version: restStorageMap, }, + Scheme: api.Scheme, + NegotiatedSerializer: api.Codecs, } if err := s.InstallAPIGroups([]genericapiserver.APIGroupInfo{apiGroupInfo}); err != nil { glog.Fatalf("Error in installing API: %v", err) diff --git a/examples/apiserver/apiserver_test.go b/examples/apiserver/apiserver_test.go new file mode 100644 index 00000000000..134e356987e --- /dev/null +++ b/examples/apiserver/apiserver_test.go @@ -0,0 +1,104 @@ +/* +Copyright 2016 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package apiserver + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "testing" + "time" + + "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testdata/apis/testgroup/v1" + + "github.com/stretchr/testify/assert" + "k8s.io/kubernetes/pkg/api/unversioned" +) + +var serverIP = "http://localhost:8080" + +var groupVersion = v1.SchemeGroupVersion + +func TestRun(t *testing.T) { + go Run() + if err := waitForApiserverUp(); err != nil { + t.Fatalf("%v", err) + } + testAPIGroup(t) + testAPIResourceList(t) +} + +func waitForApiserverUp() error { + for start := time.Now(); time.Since(start) < time.Minute; time.Sleep(5 * time.Second) { + _, err := http.Get(serverIP) + if err == nil { + return nil + } + } + return fmt.Errorf("waiting for apiserver timed out") +} + +func readResponse(serverURL string) ([]byte, error) { + response, err := http.Get(serverURL) + if err != nil { + return nil, fmt.Errorf("Error in fetching %s: %v", serverURL, err) + } + defer response.Body.Close() + contents, err := ioutil.ReadAll(response.Body) + if err != nil { + return nil, fmt.Errorf("Error reading response from %s: %v", serverURL, err) + } + return contents, nil +} + +func testAPIGroup(t *testing.T) { + serverURL := serverIP + "/apis/testgroup" + contents, err := readResponse(serverURL) + if err != nil { + t.Fatalf("%v", err) + } + var apiGroup unversioned.APIGroup + err = json.Unmarshal(contents, &apiGroup) + if err != nil { + t.Fatalf("Error in unmarshalling response from server %s: %v", serverURL, err) + } + assert.Equal(t, apiGroup.APIVersion, groupVersion.Version) + assert.Equal(t, apiGroup.Name, groupVersion.Group) + assert.Equal(t, 1, len(apiGroup.Versions)) + assert.Equal(t, apiGroup.Versions[0].GroupVersion, groupVersion.String()) + assert.Equal(t, apiGroup.Versions[0].Version, groupVersion.Version) + assert.Equal(t, apiGroup.Versions[0], apiGroup.PreferredVersion) +} + +func testAPIResourceList(t *testing.T) { + serverURL := serverIP + "/apis/testgroup/v1" + contents, err := readResponse(serverURL) + if err != nil { + t.Fatalf("%v", err) + } + var apiResourceList unversioned.APIResourceList + err = json.Unmarshal(contents, &apiResourceList) + if err != nil { + t.Fatalf("Error in unmarshalling response from server %s: %v", serverURL, err) + } + assert.Equal(t, apiResourceList.APIVersion, groupVersion.Version) + assert.Equal(t, apiResourceList.GroupVersion, groupVersion.String()) + assert.Equal(t, 1, len(apiResourceList.APIResources)) + assert.Equal(t, apiResourceList.APIResources[0].Name, "testtypes") + assert.True(t, apiResourceList.APIResources[0].Namespaced) +} diff --git a/examples/apiserver/server/main.go b/examples/apiserver/server/main.go new file mode 100644 index 00000000000..19961442fc5 --- /dev/null +++ b/examples/apiserver/server/main.go @@ -0,0 +1,25 @@ +/* +Copyright 2016 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "k8s.io/kubernetes/examples/apiserver" +) + +func main() { + apiserver.Run() +} diff --git a/pkg/genericapiserver/genericapiserver.go b/pkg/genericapiserver/genericapiserver.go index 538094b2153..d80893b49c7 100644 --- a/pkg/genericapiserver/genericapiserver.go +++ b/pkg/genericapiserver/genericapiserver.go @@ -379,6 +379,9 @@ func setDefaults(c *Config) { // auth, then the caller should create a handler for those endpoints, which delegates the // any unhandled paths to "Handler". func New(c *Config) *GenericAPIServer { + if c.Serializer == nil { + glog.Fatalf("Genericapiserver.New() called with config.Serializer == nil") + } setDefaults(c) s := &GenericAPIServer{ diff --git a/pkg/genericapiserver/genericapiserver_test.go b/pkg/genericapiserver/genericapiserver_test.go index be22c99e342..e21ddcc95a4 100644 --- a/pkg/genericapiserver/genericapiserver_test.go +++ b/pkg/genericapiserver/genericapiserver_test.go @@ -54,6 +54,7 @@ func TestNew(t *testing.T) { config.ProxyDialer = func(network, addr string) (net.Conn, error) { return nil, nil } config.ProxyTLSClientConfig = &tls.Config{} + config.Serializer = api.Codecs s := New(&config)