From 15712b92c39fab0e881f94fff40ee67a6123fdcc Mon Sep 17 00:00:00 2001 From: "Dr. Stefan Schimanski" Date: Thu, 6 Jul 2017 10:46:04 +0200 Subject: [PATCH] apimachinery+apiserver: extract test types to work w/ deepcopy-gen Kubernetes-commit: 205cd90d465b7287fdad5f77d1dc4ac13624b067 --- pkg/endpoints/apiserver_test.go | 42 +++++------------- pkg/endpoints/openapi/openapi_test.go | 30 +++---------- pkg/endpoints/openapi/testing/types.go | 40 +++++++++++++++++ .../openapi/testing/zz_generated.deepcopy.go | 43 +++++++++++++++++++ pkg/endpoints/testing/doc.go | 19 ++++++++ pkg/endpoints/testing/types.go | 11 +++++ pkg/storage/testing/doc.go | 19 ++++++++ 7 files changed, 149 insertions(+), 55 deletions(-) create mode 100644 pkg/endpoints/openapi/testing/types.go create mode 100644 pkg/endpoints/openapi/testing/zz_generated.deepcopy.go create mode 100644 pkg/endpoints/testing/doc.go create mode 100644 pkg/storage/testing/doc.go diff --git a/pkg/endpoints/apiserver_test.go b/pkg/endpoints/apiserver_test.go index 2667b3483..b17ba4791 100644 --- a/pkg/endpoints/apiserver_test.go +++ b/pkg/endpoints/apiserver_test.go @@ -174,30 +174,21 @@ func addGrouplessTypes() { } func addTestTypes() { - type ListOptions struct { - Object runtime.Object - metav1.TypeMeta `json:",inline"` - LabelSelector string `json:"labelSelector,omitempty"` - FieldSelector string `json:"fieldSelector,omitempty"` - Watch bool `json:"watch,omitempty"` - ResourceVersion string `json:"resourceVersion,omitempty"` - TimeoutSeconds *int64 `json:"timeoutSeconds,omitempty"` - } scheme.AddKnownTypes(testGroupVersion, &genericapitesting.Simple{}, &genericapitesting.SimpleList{}, &metav1.ExportOptions{}, &metav1.DeleteOptions{}, &genericapitesting.SimpleGetOptions{}, &genericapitesting.SimpleRoot{}, - &SimpleXGSubresource{}) + &genericapitesting.SimpleXGSubresource{}) scheme.AddKnownTypes(testGroupVersion, &examplev1.Pod{}) scheme.AddKnownTypes(testInternalGroupVersion, &genericapitesting.Simple{}, &genericapitesting.SimpleList{}, &metav1.ExportOptions{}, &genericapitesting.SimpleGetOptions{}, &genericapitesting.SimpleRoot{}, - &SimpleXGSubresource{}) + &genericapitesting.SimpleXGSubresource{}) scheme.AddKnownTypes(testInternalGroupVersion, &example.Pod{}) // Register SimpleXGSubresource in both testGroupVersion and testGroup2Version, and also their // their corresponding internal versions, to verify that the desired group version object is // served in the tests. - scheme.AddKnownTypes(testGroup2Version, &SimpleXGSubresource{}, &metav1.ExportOptions{}) - scheme.AddKnownTypes(testInternalGroup2Version, &SimpleXGSubresource{}, &metav1.ExportOptions{}) + scheme.AddKnownTypes(testGroup2Version, &genericapitesting.SimpleXGSubresource{}, &metav1.ExportOptions{}) + scheme.AddKnownTypes(testInternalGroup2Version, &genericapitesting.SimpleXGSubresource{}, &metav1.ExportOptions{}) metav1.AddToGroupVersion(scheme, testGroupVersion) } @@ -1153,10 +1144,10 @@ func TestList(t *testing.T) { t.Errorf("%d: %q unexpected resource namespace: %s", i, testCase.url, simpleStorage.actualNamespace) } if simpleStorage.requestedLabelSelector == nil || simpleStorage.requestedLabelSelector.String() != testCase.label { - t.Errorf("%d: unexpected label selector: %v", i, simpleStorage.requestedLabelSelector) + t.Errorf("%d: unexpected label selector: expected=%v got=%v", i, testCase.label, simpleStorage.requestedLabelSelector) } if simpleStorage.requestedFieldSelector == nil || simpleStorage.requestedFieldSelector.String() != testCase.field { - t.Errorf("%d: unexpected field selector: %v", i, simpleStorage.requestedFieldSelector) + t.Errorf("%d: unexpected field selector: expected=%v got=%v", i, testCase.field, simpleStorage.requestedFieldSelector) } } } @@ -3935,23 +3926,12 @@ func TestUpdateChecksAPIVersion(t *testing.T) { } } -// SimpleXGSubresource is a cross group subresource, i.e. the subresource does not belong to the -// same group as its parent resource. -type SimpleXGSubresource struct { - metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata"` - SubresourceInfo string `json:"subresourceInfo,omitempty"` - Labels map[string]string `json:"labels,omitempty"` -} - -func (obj *SimpleXGSubresource) GetObjectKind() schema.ObjectKind { return &obj.TypeMeta } - type SimpleXGSubresourceRESTStorage struct { - item SimpleXGSubresource + item genericapitesting.SimpleXGSubresource } func (storage *SimpleXGSubresourceRESTStorage) New() runtime.Object { - return &SimpleXGSubresource{} + return &genericapitesting.SimpleXGSubresource{} } func (storage *SimpleXGSubresourceRESTStorage) Get(ctx request.Context, id string, options *metav1.GetOptions) (runtime.Object, error) { @@ -3969,7 +3949,7 @@ func TestXGSubresource(t *testing.T) { itemID := "theID" subresourceStorage := &SimpleXGSubresourceRESTStorage{ - item: SimpleXGSubresource{ + item: genericapitesting.SimpleXGSubresource{ SubresourceInfo: "foo", }, } @@ -4018,7 +3998,7 @@ func TestXGSubresource(t *testing.T) { if resp.StatusCode != http.StatusOK { t.Fatalf("unexpected response: %#v", resp) } - var itemOut SimpleXGSubresource + var itemOut genericapitesting.SimpleXGSubresource body, err := extractBody(resp, &itemOut) if err != nil { t.Errorf("unexpected error: %v", err) @@ -4030,7 +4010,7 @@ func TestXGSubresource(t *testing.T) { // conversion type list in API scheme and hence cannot be converted from input type object // to output type object. So it's values don't appear in the decoded output object. decoder := json.NewDecoder(strings.NewReader(body)) - var itemFromBody SimpleXGSubresource + var itemFromBody genericapitesting.SimpleXGSubresource err = decoder.Decode(&itemFromBody) if err != nil { t.Errorf("unexpected JSON decoding error: %v", err) diff --git a/pkg/endpoints/openapi/openapi_test.go b/pkg/endpoints/openapi/openapi_test.go index 6f6ac6f29..fb188fed0 100644 --- a/pkg/endpoints/openapi/openapi_test.go +++ b/pkg/endpoints/openapi/openapi_test.go @@ -25,27 +25,9 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/runtime/schema" + openapitesting "k8s.io/apiserver/pkg/endpoints/openapi/testing" ) -type TestType struct { -} - -func (t TestType) GetObjectKind() schema.ObjectKind { - return t -} - -func (t TestType) SetGroupVersionKind(kind schema.GroupVersionKind) { -} - -func (t TestType) GroupVersionKind() schema.GroupVersionKind { - return schema.GroupVersionKind{ - Group: "test", - Version: "v1", - Kind: "TestType", - } -} - func assertEqual(t *testing.T, expected, actual interface{}) { var equal bool if expected == nil || actual == nil { @@ -59,17 +41,17 @@ func assertEqual(t *testing.T, expected, actual interface{}) { } func TestGetDefinitionName(t *testing.T) { - testType := TestType{} + testType := openapitesting.TestType{} // in production, the name is stripped of ".*vendor/" prefix before passed // to GetDefinitionName, so here typePkgName does not have the // "k8s.io/kubernetes/vendor" prefix. - typePkgName := "k8s.io/apiserver/pkg/endpoints/openapi.TestType" - typeFriendlyName := "io.k8s.apiserver.pkg.endpoints.openapi.TestType" + typePkgName := "k8s.io/apiserver/pkg/endpoints/openapi/testing.TestType" + typeFriendlyName := "io.k8s.apiserver.pkg.endpoints.openapi.testing.TestType" if strings.HasSuffix(reflect.TypeOf(testType).PkgPath(), "go_default_test") { // the test is running inside bazel where the package name is changed and // "go_default_test" will add to package path. - typePkgName = "k8s.io/apiserver/pkg/endpoints/openapi/go_default_test.TestType" - typeFriendlyName = "io.k8s.apiserver.pkg.endpoints.openapi.go_default_test.TestType" + typePkgName = "k8s.io/apiserver/pkg/endpoints/openapi/testing/go_default_test.TestType" + typeFriendlyName = "io.k8s.apiserver.pkg.endpoints.openapi.testing.go_default_test.TestType" } s := runtime.NewScheme() s.AddKnownTypeWithName(testType.GroupVersionKind(), &testType) diff --git a/pkg/endpoints/openapi/testing/types.go b/pkg/endpoints/openapi/testing/types.go new file mode 100644 index 000000000..9b408f5f9 --- /dev/null +++ b/pkg/endpoints/openapi/testing/types.go @@ -0,0 +1,40 @@ +/* +Copyright 2016 The Kubernetes Authors. + +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 testing + +import ( + "k8s.io/apimachinery/pkg/runtime/schema" +) + +// +k8s:deepcopy-gen=true +type TestType struct { +} + +func (t TestType) GetObjectKind() schema.ObjectKind { + return t +} + +func (t TestType) SetGroupVersionKind(kind schema.GroupVersionKind) { +} + +func (t TestType) GroupVersionKind() schema.GroupVersionKind { + return schema.GroupVersionKind{ + Group: "test", + Version: "v1", + Kind: "TestType", + } +} diff --git a/pkg/endpoints/openapi/testing/zz_generated.deepcopy.go b/pkg/endpoints/openapi/testing/zz_generated.deepcopy.go new file mode 100644 index 000000000..867af4361 --- /dev/null +++ b/pkg/endpoints/openapi/testing/zz_generated.deepcopy.go @@ -0,0 +1,43 @@ +// +build !ignore_autogenerated + +/* +Copyright 2017 The Kubernetes Authors. + +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 file was autogenerated by deepcopy-gen. Do not edit it manually! + +package testing + +import ( + conversion "k8s.io/apimachinery/pkg/conversion" + reflect "reflect" +) + +// GetGeneratedDeepCopyFuncs returns the generated funcs, since we aren't registering them. +func GetGeneratedDeepCopyFuncs() []conversion.GeneratedDeepCopyFunc { + return []conversion.GeneratedDeepCopyFunc{ + {Fn: DeepCopy_openapi_TestType, InType: reflect.TypeOf(&TestType{})}, + } +} + +// DeepCopy_openapi_TestType is an autogenerated deepcopy function. +func DeepCopy_openapi_TestType(in interface{}, out interface{}, c *conversion.Cloner) error { + { + in := in.(*TestType) + out := out.(*TestType) + *out = *in + return nil + } +} diff --git a/pkg/endpoints/testing/doc.go b/pkg/endpoints/testing/doc.go new file mode 100644 index 000000000..2e801ab5f --- /dev/null +++ b/pkg/endpoints/testing/doc.go @@ -0,0 +1,19 @@ +/* +Copyright 2017 The Kubernetes Authors. + +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. +*/ + +// +k8s:deepcopy-gen=package + +package testing // import "k8s.io/apiserver/pkg/endpoints/testing" diff --git a/pkg/endpoints/testing/types.go b/pkg/endpoints/testing/types.go index c5d367caf..c14103261 100644 --- a/pkg/endpoints/testing/types.go +++ b/pkg/endpoints/testing/types.go @@ -67,3 +67,14 @@ type SimpleList struct { } func (obj *SimpleList) GetObjectKind() schema.ObjectKind { return &obj.TypeMeta } + +// SimpleXGSubresource is a cross group subresource, i.e. the subresource does not belong to the +// same group as its parent resource. +type SimpleXGSubresource struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata"` + SubresourceInfo string `json:"subresourceInfo,omitempty"` + Labels map[string]string `json:"labels,omitempty"` +} + +func (obj *SimpleXGSubresource) GetObjectKind() schema.ObjectKind { return &obj.TypeMeta } diff --git a/pkg/storage/testing/doc.go b/pkg/storage/testing/doc.go new file mode 100644 index 000000000..f6870edfb --- /dev/null +++ b/pkg/storage/testing/doc.go @@ -0,0 +1,19 @@ +/* +Copyright 2015 The Kubernetes Authors. + +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. +*/ + +// +k8s:deepcopy-gen=package + +package testing