Merge pull request #24416 from caesarxuchao/add-fake-testoutput

Automatic merge from submit-queue

Client-gen: Add fake clients to testoutput; fix the imports in fake clients

Start generating fake client in the testoutput, so that we can check the fake client is generated correctly under extreme conditions.

Fix the problem reported https://github.com/kubernetes/kubernetes/pull/20573#issuecomment-210556618, where the import names contain dots when the group name contains dots.

cc @deads2k
This commit is contained in:
k8s-merge-robot 2016-04-19 12:08:02 -07:00
commit 9187a7aeb3
9 changed files with 275 additions and 5 deletions

View File

@ -62,7 +62,7 @@ func PackageForGroup(gv unversioned.GroupVersion, typeList []*types.Type, packag
generators = append(generators, &genFakeForGroup{
DefaultGen: generator.DefaultGen{
OptionalName: "fake_" + gv.Group + "_client",
OptionalName: "fake_" + normalization.BeforeFirstDot(gv.Group) + "_client",
},
outputPackage: outputPackagePath,
realClientPath: realClientPath,

View File

@ -60,10 +60,11 @@ func (g *genClientset) Imports(c *generator.Context) (imports []string) {
for _, gv := range g.groupVersions {
group := normalization.Group(gv.Group)
version := normalization.Version(gv.Version)
undotted_group := normalization.BeforeFirstDot(group)
typedClientPath := filepath.Join(g.typedClientPath, group, version)
imports = append(imports, fmt.Sprintf("%s%s \"%s\"", version, group, typedClientPath))
imports = append(imports, fmt.Sprintf("%s%s \"%s\"", version, undotted_group, typedClientPath))
fakeTypedClientPath := filepath.Join(typedClientPath, "fake")
imports = append(imports, fmt.Sprintf("fake%s%s \"%s\"", version, group, fakeTypedClientPath))
imports = append(imports, fmt.Sprintf("fake%s%s \"%s\"", version, undotted_group, fakeTypedClientPath))
}
// the package that has the clientset Interface
imports = append(imports, fmt.Sprintf("clientset \"%s\"", g.clientsetPath))
@ -96,7 +97,7 @@ func (g *genClientset) GenerateType(c *generator.Context, t *types.Type, w io.Wr
}
allGroups := []arg{}
for _, gv := range g.groupVersions {
group := normalization.Group(gv.Group)
group := normalization.BeforeFirstDot(normalization.Group(gv.Group))
version := normalization.Version(gv.Version)
allGroups = append(allGroups, arg{namer.IC(group), version + group})
}

View File

@ -102,7 +102,7 @@ func main() {
ClientsetName: "test_internalclientset",
ClientsetOutputPath: "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testoutput/clientset_generated/",
ClientsetOnly: false,
FakeClient: false,
FakeClient: true,
CmdArgs: cmdArgs,
}
} else {

View File

@ -0,0 +1,65 @@
/*
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 fake
import (
clientset "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testoutput/clientset_generated/test_internalclientset"
unversionedtestgroup "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testoutput/clientset_generated/test_internalclientset/typed/testgroup.k8s.io/unversioned"
fakeunversionedtestgroup "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testoutput/clientset_generated/test_internalclientset/typed/testgroup.k8s.io/unversioned/fake"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apimachinery/registered"
"k8s.io/kubernetes/pkg/client/testing/core"
"k8s.io/kubernetes/pkg/client/typed/discovery"
fakediscovery "k8s.io/kubernetes/pkg/client/typed/discovery/fake"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/watch"
)
// Clientset returns a clientset that will respond with the provided objects
func NewSimpleClientset(objects ...runtime.Object) *Clientset {
o := core.NewObjects(api.Scheme, api.Codecs.UniversalDecoder())
for _, obj := range objects {
if err := o.Add(obj); err != nil {
panic(err)
}
}
fakePtr := core.Fake{}
fakePtr.AddReactor("*", "*", core.ObjectReaction(o, registered.RESTMapper()))
fakePtr.AddWatchReactor("*", core.DefaultWatchReactor(watch.NewFake(), nil))
return &Clientset{fakePtr}
}
// Clientset implements clientset.Interface. Meant to be embedded into a
// struct to get a default implementation. This makes faking out just the method
// you want to test easier.
type Clientset struct {
core.Fake
}
func (c *Clientset) Discovery() discovery.DiscoveryInterface {
return &fakediscovery.FakeDiscovery{&c.Fake}
}
var _ clientset.Interface = &Clientset{}
// Testgroup retrieves the TestgroupClient
func (c *Clientset) Testgroup() unversionedtestgroup.TestgroupInterface {
return &fakeunversionedtestgroup.FakeTestgroup{&c.Fake}
}

View File

@ -0,0 +1,20 @@
/*
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.
*/
// This package is generated by client-gen with arguments: --test=true
// This package has the automatically generated fake clientset.
package fake

View File

@ -0,0 +1,20 @@
/*
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.
*/
// This package is generated by client-gen with arguments: --test=true
// Package fake has the automatically generated clients.
package fake

View File

@ -0,0 +1,30 @@
/*
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 fake
import (
unversioned "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testoutput/clientset_generated/test_internalclientset/typed/testgroup.k8s.io/unversioned"
core "k8s.io/kubernetes/pkg/client/testing/core"
)
type FakeTestgroup struct {
*core.Fake
}
func (c *FakeTestgroup) TestTypes(namespace string) unversioned.TestTypeInterface {
return &FakeTestTypes{c, namespace}
}

View File

@ -0,0 +1,113 @@
/*
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 fake
import (
testgroup_k8s_io "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testdata/apis/testgroup.k8s.io"
api "k8s.io/kubernetes/pkg/api"
core "k8s.io/kubernetes/pkg/client/testing/core"
labels "k8s.io/kubernetes/pkg/labels"
watch "k8s.io/kubernetes/pkg/watch"
)
// FakeTestTypes implements TestTypeInterface
type FakeTestTypes struct {
Fake *FakeTestgroup
ns string
}
func (c *FakeTestTypes) Create(testType *testgroup_k8s_io.TestType) (result *testgroup_k8s_io.TestType, err error) {
obj, err := c.Fake.
Invokes(core.NewCreateAction("testtypes", c.ns, testType), &testgroup_k8s_io.TestType{})
if obj == nil {
return nil, err
}
return obj.(*testgroup_k8s_io.TestType), err
}
func (c *FakeTestTypes) Update(testType *testgroup_k8s_io.TestType) (result *testgroup_k8s_io.TestType, err error) {
obj, err := c.Fake.
Invokes(core.NewUpdateAction("testtypes", c.ns, testType), &testgroup_k8s_io.TestType{})
if obj == nil {
return nil, err
}
return obj.(*testgroup_k8s_io.TestType), err
}
func (c *FakeTestTypes) UpdateStatus(testType *testgroup_k8s_io.TestType) (*testgroup_k8s_io.TestType, error) {
obj, err := c.Fake.
Invokes(core.NewUpdateSubresourceAction("testtypes", "status", c.ns, testType), &testgroup_k8s_io.TestType{})
if obj == nil {
return nil, err
}
return obj.(*testgroup_k8s_io.TestType), err
}
func (c *FakeTestTypes) Delete(name string, options *api.DeleteOptions) error {
_, err := c.Fake.
Invokes(core.NewDeleteAction("testtypes", c.ns, name), &testgroup_k8s_io.TestType{})
return err
}
func (c *FakeTestTypes) DeleteCollection(options *api.DeleteOptions, listOptions api.ListOptions) error {
action := core.NewDeleteCollectionAction("testtypes", c.ns, listOptions)
_, err := c.Fake.Invokes(action, &testgroup_k8s_io.TestTypeList{})
return err
}
func (c *FakeTestTypes) Get(name string) (result *testgroup_k8s_io.TestType, err error) {
obj, err := c.Fake.
Invokes(core.NewGetAction("testtypes", c.ns, name), &testgroup_k8s_io.TestType{})
if obj == nil {
return nil, err
}
return obj.(*testgroup_k8s_io.TestType), err
}
func (c *FakeTestTypes) List(opts api.ListOptions) (result *testgroup_k8s_io.TestTypeList, err error) {
obj, err := c.Fake.
Invokes(core.NewListAction("testtypes", c.ns, opts), &testgroup_k8s_io.TestTypeList{})
if obj == nil {
return nil, err
}
label := opts.LabelSelector
if label == nil {
label = labels.Everything()
}
list := &testgroup_k8s_io.TestTypeList{}
for _, item := range obj.(*testgroup_k8s_io.TestTypeList).Items {
if label.Matches(labels.Set(item.Labels)) {
list.Items = append(list.Items, item)
}
}
return list, err
}
// Watch returns a watch.Interface that watches the requested testTypes.
func (c *FakeTestTypes) Watch(opts api.ListOptions) (watch.Interface, error) {
return c.Fake.
InvokesWatch(core.NewWatchAction("testtypes", c.ns, opts))
}

View File

@ -0,0 +1,21 @@
/*
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 fake
func (c *FakeTestTypes) Hello() string {
return "hello!"
}