adding a mock.testType to test the generated client code

This commit is contained in:
Chao Xu
2015-12-03 17:01:33 -08:00
parent 775369a8f1
commit 05e3cb8bfb
17 changed files with 2357 additions and 47 deletions

View File

@@ -0,0 +1,18 @@
/*
Copyright 2015 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 unversioned has the automatically generated clients for unversioned resources.
package testoutput

View File

@@ -0,0 +1,130 @@
/*
Copyright 2015 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 testoutput
import (
testgroup "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testdata/apis/testgroup"
api "k8s.io/kubernetes/pkg/api"
unversioned "k8s.io/kubernetes/pkg/api/unversioned"
watch "k8s.io/kubernetes/pkg/watch"
)
// TestTypeNamespacer has methods to work with TestType resources in a namespace
type TestTypeNamespacer interface {
TestTypes(namespace string) TestTypeInterface
}
// TestTypeInterface has methods to work with TestType resources.
type TestTypeInterface interface {
Create(*testgroup.TestType) (*testgroup.TestType, error)
Update(*testgroup.TestType) (*testgroup.TestType, error)
Delete(name string, options *api.DeleteOptions) error
Get(name string) (*testgroup.TestType, error)
List(opts unversioned.ListOptions) (*testgroup.TestTypeList, error)
Watch(opts unversioned.ListOptions) (watch.Interface, error)
}
// testTypes implements TestTypeInterface
type testTypes struct {
client *TestgroupClient
ns string
}
// newTestTypes returns a TestTypes
func newTestTypes(c *TestgroupClient, namespace string) *testTypes {
return &testTypes{
client: c,
ns: namespace,
}
}
// Create takes the representation of a testType and creates it. Returns the server's representation of the testType, and an error, if there is any.
func (c *testTypes) Create(testType *testgroup.TestType) (result *testgroup.TestType, err error) {
result = &testgroup.TestType{}
err = c.client.Post().
Namespace(c.ns).
Resource("testTypes").
Body(testType).
Do().
Into(result)
return
}
// Update takes the representation of a testType and updates it. Returns the server's representation of the testType, and an error, if there is any.
func (c *testTypes) Update(testType *testgroup.TestType) (result *testgroup.TestType, err error) {
result = &testgroup.TestType{}
err = c.client.Put().
Namespace(c.ns).
Resource("testTypes").
Name(testType.Name).
Body(testType).
Do().
Into(result)
return
}
// Delete takes name of the testType and deletes it. Returns an error if one occurs.
func (c *testTypes) Delete(name string, options *api.DeleteOptions) error {
if options == nil {
return c.client.Delete().Namespace(c.ns).Resource("testTypes").Name(name).Do().Error()
}
body, err := api.Scheme.EncodeToVersion(options, c.client.APIVersion().String())
if err != nil {
return err
}
return c.client.Delete().
Namespace(c.ns).
Resource("testTypes").
Name(name).
Body(body).
Do().
Error()
}
// Get takes name of the testType, and returns the corresponding testType object, and an error if there is any.
func (c *testTypes) Get(name string) (result *testgroup.TestType, err error) {
result = &testgroup.TestType{}
err = c.client.Get().
Namespace(c.ns).
Resource("testTypes").
Name(name).
Do().
Into(result)
return
}
// List takes label and field selectors, and returns the list of TestTypes that match those selectors.
func (c *testTypes) List(opts unversioned.ListOptions) (result *testgroup.TestTypeList, err error) {
result = &testgroup.TestTypeList{}
err = c.client.Get().
Namespace(c.ns).
Resource("testTypes").
VersionedParams(&opts, api.Scheme).
Do().
Into(result)
return
}
// Watch returns a watch.Interface that watches the requested testTypes.
func (c *testTypes) Watch(opts unversioned.ListOptions) (watch.Interface, error) {
return c.client.Get().
Prefix("watch").
Namespace(c.ns).
Resource("testTypes").
VersionedParams(&opts, api.Scheme).
Watch()
}

View File

@@ -0,0 +1,90 @@
/*
Copyright 2015 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 testoutput
import (
"fmt"
latest "k8s.io/kubernetes/pkg/api/latest"
unversioned "k8s.io/kubernetes/pkg/client/unversioned"
)
type TestgroupInterface interface {
TestTypeNamespacer
}
// TestgroupClient is used to interact with features provided by the Testgroup group.
type TestgroupClient struct {
*unversioned.RESTClient
}
func (c *TestgroupClient) TestTypes(namespace string) TestTypeInterface {
return newTestTypes(c, namespace)
}
// NewTestgroup creates a new TestgroupClient for the given config.
func NewTestgroup(c *unversioned.Config) (*TestgroupClient, error) {
config := *c
if err := setTestgroupDefaults(&config); err != nil {
return nil, err
}
client, err := unversioned.RESTClientFor(&config)
if err != nil {
return nil, err
}
return &TestgroupClient{client}, nil
}
// NewTestgroupOrDie creates a new TestgroupClient for the given config and
// panics if there is an error in the config.
func NewTestgroupOrDie(c *unversioned.Config) *TestgroupClient {
client, err := NewTestgroup(c)
if err != nil {
panic(err)
}
return client
}
func setTestgroupDefaults(config *unversioned.Config) error {
// if testgroup group is not registered, return an error
g, err := latest.Group("testgroup")
if err != nil {
return err
}
config.Prefix = "/apis"
if config.UserAgent == "" {
config.UserAgent = unversioned.DefaultKubernetesUserAgent()
}
// TODO: Unconditionally set the config.Version, until we fix the config.
//if config.Version == "" {
copyGroupVersion := g.GroupVersion
config.GroupVersion = &copyGroupVersion
//}
versionInterfaces, err := g.InterfacesFor(*config.GroupVersion)
if err != nil {
return fmt.Errorf("Testgroup API version '%s' is not recognized (valid values: %s)",
config.GroupVersion, g.GroupVersions)
}
config.Codec = versionInterfaces.Codec
if config.QPS == 0 {
config.QPS = 5
}
if config.Burst == 0 {
config.Burst = 10
}
return nil
}

View File

@@ -0,0 +1,216 @@
/*
Copyright 2015 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 testoutput_test
import (
"net/http"
"net/url"
"testing"
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testdata/apis/testgroup"
_ "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testdata/apis/testgroup/install"
. "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testoutput"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/latest"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/api/unversioned"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/client/unversioned/testclient/simple"
"k8s.io/kubernetes/pkg/labels"
)
var testHelper testapi.TestGroup
func init() {
if _, found := testapi.Groups[testgroup.SchemeGroupVersion.Group]; found {
return
}
testapi.Groups[testgroup.SchemeGroupVersion.Group] = testapi.NewTestGroup(
unversioned.GroupVersion{Group: testgroup.SchemeGroupVersion.Group, Version: latest.GroupOrDie(testgroup.SchemeGroupVersion.Group).GroupVersion.Version},
testgroup.SchemeGroupVersion)
testHelper = testapi.Groups[testgroup.SchemeGroupVersion.Group]
}
type DecoratedSimpleClient struct {
*TestgroupClient
simpleClient simple.Client
}
func (c *DecoratedSimpleClient) Setup(t *testing.T) *DecoratedSimpleClient {
c.simpleClient.Setup(t)
url := c.simpleClient.ServerURL()
c.TestgroupClient = NewTestgroupOrDie(&client.Config{
Host: url,
})
return c
}
func TestCreateTestTypes(t *testing.T) {
ns := api.NamespaceDefault
requestTestType := &testgroup.TestType{}
c := DecoratedSimpleClient{
simpleClient: simple.Client{
Request: simple.Request{Method: "POST", Path: testHelper.ResourcePath("testtypes", ns, ""), Query: simple.BuildQueryValues(nil), Body: requestTestType},
Response: simple.Response{
StatusCode: http.StatusOK,
Body: requestTestType,
},
},
}
receivedTestType, err := c.Setup(t).TestTypes(ns).Create(requestTestType)
c.simpleClient.Validate(t, receivedTestType, err)
}
func TestUpdateTestType(t *testing.T) {
ns := api.NamespaceDefault
requestTestType := &testgroup.TestType{
ObjectMeta: api.ObjectMeta{
Name: "foo",
ResourceVersion: "1",
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
}
c := DecoratedSimpleClient{
simpleClient: simple.Client{
Request: simple.Request{Method: "PUT", Path: testHelper.ResourcePath("testtypes", ns, "foo"), Query: simple.BuildQueryValues(nil)},
Response: simple.Response{StatusCode: http.StatusOK, Body: requestTestType},
},
}
receivedTestType, err := c.Setup(t).TestTypes(ns).Update(requestTestType)
c.simpleClient.Validate(t, receivedTestType, err)
}
func TestDeleteTestType(t *testing.T) {
ns := api.NamespaceDefault
c := DecoratedSimpleClient{
simpleClient: simple.Client{
Request: simple.Request{Method: "DELETE", Path: testHelper.ResourcePath("testtypes", ns, "foo"), Query: simple.BuildQueryValues(nil)},
Response: simple.Response{StatusCode: http.StatusOK},
},
}
err := c.Setup(t).TestTypes(ns).Delete("foo", nil)
c.simpleClient.Validate(t, nil, err)
}
func TestGetTestType(t *testing.T) {
ns := api.NamespaceDefault
c := DecoratedSimpleClient{
simpleClient: simple.Client{
Request: simple.Request{Method: "GET", Path: testHelper.ResourcePath("testtypes", ns, "foo"), Query: simple.BuildQueryValues(nil)},
Response: simple.Response{
StatusCode: http.StatusOK,
Body: &testgroup.TestType{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
},
},
},
}
receivedTestType, err := c.Setup(t).TestTypes(ns).Get("foo")
c.simpleClient.Validate(t, receivedTestType, err)
}
func TestGetTestTypeWithNoName(t *testing.T) {
ns := api.NamespaceDefault
c := DecoratedSimpleClient{
simpleClient: simple.Client{Error: true},
}
receivedTestType, err := c.Setup(t).TestTypes(ns).Get("")
if (err != nil) && (err.Error() != simple.NameRequiredError) {
t.Errorf("Expected error: %v, but got %v", simple.NameRequiredError, err)
}
c.simpleClient.Validate(t, receivedTestType, err)
}
func TestListEmptyTestTypes(t *testing.T) {
ns := api.NamespaceDefault
c := DecoratedSimpleClient{
simpleClient: simple.Client{
Request: simple.Request{Method: "GET", Path: testHelper.ResourcePath("testtypes", ns, ""), Query: simple.BuildQueryValues(nil)},
Response: simple.Response{StatusCode: http.StatusOK, Body: &testgroup.TestTypeList{}},
},
}
podList, err := c.Setup(t).TestTypes(ns).List(unversioned.ListOptions{})
c.simpleClient.Validate(t, podList, err)
}
func TestListTestTypes(t *testing.T) {
ns := api.NamespaceDefault
c := DecoratedSimpleClient{
simpleClient: simple.Client{
Request: simple.Request{Method: "GET", Path: testHelper.ResourcePath("testtypes", ns, ""), Query: simple.BuildQueryValues(nil)},
Response: simple.Response{StatusCode: http.StatusOK,
Body: &testgroup.TestTypeList{
Items: []testgroup.TestType{
{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
},
},
},
},
},
}
receivedTestTypeList, err := c.Setup(t).TestTypes(ns).List(unversioned.ListOptions{})
c.simpleClient.Validate(t, receivedTestTypeList, err)
}
func TestListTestTypesLabels(t *testing.T) {
ns := api.NamespaceDefault
labelSelectorQueryParamName := unversioned.LabelSelectorQueryParam(testHelper.GroupVersion().String())
c := DecoratedSimpleClient{
simpleClient: simple.Client{
Request: simple.Request{
Method: "GET",
Path: testHelper.ResourcePath("testtypes", ns, ""),
Query: simple.BuildQueryValues(url.Values{labelSelectorQueryParamName: []string{"foo=bar,name=baz"}})},
Response: simple.Response{
StatusCode: http.StatusOK,
Body: &testgroup.TestTypeList{
Items: []testgroup.TestType{
{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
},
},
},
},
},
}
c.Setup(t)
c.simpleClient.QueryValidator[labelSelectorQueryParamName] = simple.ValidateLabels
selector := labels.Set{"foo": "bar", "name": "baz"}.AsSelector()
options := unversioned.ListOptions{LabelSelector: unversioned.LabelSelector{selector}}
receivedTestTypeList, err := c.TestTypes(ns).List(options)
c.simpleClient.Validate(t, receivedTestTypeList, err)
}