Skip to content

Commit 205cd90

Browse files
committed
apimachinery+apiserver: extract test types to work w/ deepcopy-gen
1 parent 954c356 commit 205cd90

File tree

16 files changed

+726
-539
lines changed

16 files changed

+726
-539
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// +k8s:deepcopy-gen=package
18+
package v1

staging/src/k8s.io/apimachinery/pkg/runtime/conversion_test.go

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,17 @@ import (
2222

2323
"k8s.io/apimachinery/pkg/runtime"
2424
"k8s.io/apimachinery/pkg/runtime/schema"
25+
runtimetesting "k8s.io/apimachinery/pkg/runtime/testing"
2526
)
2627

27-
type InternalComplex struct {
28-
runtime.TypeMeta
29-
String string
30-
Integer int
31-
Integer64 int64
32-
Int64 int64
33-
Bool bool
34-
}
35-
36-
type ExternalComplex struct {
37-
runtime.TypeMeta `json:",inline"`
38-
String string `json:"string" description:"testing"`
39-
Integer int `json:"int"`
40-
Integer64 int64 `json:",omitempty"`
41-
Int64 int64
42-
Bool bool `json:"bool"`
43-
}
44-
45-
func (obj *InternalComplex) GetObjectKind() schema.ObjectKind { return &obj.TypeMeta }
46-
func (obj *ExternalComplex) GetObjectKind() schema.ObjectKind { return &obj.TypeMeta }
47-
4828
func TestStringMapConversion(t *testing.T) {
4929
internalGV := schema.GroupVersion{Group: "test.group", Version: runtime.APIVersionInternal}
5030
externalGV := schema.GroupVersion{Group: "test.group", Version: "external"}
5131

5232
scheme := runtime.NewScheme()
5333
scheme.Log(t)
54-
scheme.AddKnownTypeWithName(internalGV.WithKind("Complex"), &InternalComplex{})
55-
scheme.AddKnownTypeWithName(externalGV.WithKind("Complex"), &ExternalComplex{})
34+
scheme.AddKnownTypeWithName(internalGV.WithKind("Complex"), &runtimetesting.InternalComplex{})
35+
scheme.AddKnownTypeWithName(externalGV.WithKind("Complex"), &runtimetesting.ExternalComplex{})
5636

5737
testCases := map[string]struct {
5838
input map[string][]string
@@ -66,62 +46,62 @@ func TestStringMapConversion(t *testing.T) {
6646
"int": {"1"},
6747
"Integer64": {"2"},
6848
},
69-
expected: &ExternalComplex{String: "value", Integer: 1},
49+
expected: &runtimetesting.ExternalComplex{String: "value", Integer: 1},
7050
},
7151
"returns error on bad int": {
7252
input: map[string][]string{
7353
"int": {"a"},
7454
},
7555
errFn: func(err error) bool { return err != nil },
76-
expected: &ExternalComplex{},
56+
expected: &runtimetesting.ExternalComplex{},
7757
},
7858
"parses int64": {
7959
input: map[string][]string{
8060
"Int64": {"-1"},
8161
},
82-
expected: &ExternalComplex{Int64: -1},
62+
expected: &runtimetesting.ExternalComplex{Int64: -1},
8363
},
8464
"returns error on bad int64": {
8565
input: map[string][]string{
8666
"Int64": {"a"},
8767
},
8868
errFn: func(err error) bool { return err != nil },
89-
expected: &ExternalComplex{},
69+
expected: &runtimetesting.ExternalComplex{},
9070
},
9171
"parses boolean true": {
9272
input: map[string][]string{
9373
"bool": {"true"},
9474
},
95-
expected: &ExternalComplex{Bool: true},
75+
expected: &runtimetesting.ExternalComplex{Bool: true},
9676
},
9777
"parses boolean any value": {
9878
input: map[string][]string{
9979
"bool": {"foo"},
10080
},
101-
expected: &ExternalComplex{Bool: true},
81+
expected: &runtimetesting.ExternalComplex{Bool: true},
10282
},
10383
"parses boolean false": {
10484
input: map[string][]string{
10585
"bool": {"false"},
10686
},
107-
expected: &ExternalComplex{Bool: false},
87+
expected: &runtimetesting.ExternalComplex{Bool: false},
10888
},
10989
"parses boolean empty value": {
11090
input: map[string][]string{
11191
"bool": {""},
11292
},
113-
expected: &ExternalComplex{Bool: true},
93+
expected: &runtimetesting.ExternalComplex{Bool: true},
11494
},
11595
"parses boolean no value": {
11696
input: map[string][]string{
11797
"bool": {},
11898
},
119-
expected: &ExternalComplex{Bool: false},
99+
expected: &runtimetesting.ExternalComplex{Bool: false},
120100
},
121101
}
122102

123103
for k, tc := range testCases {
124-
out := &ExternalComplex{}
104+
out := &runtimetesting.ExternalComplex{}
125105
if err := scheme.Convert(&tc.input, out, nil); (tc.errFn == nil && err != nil) || (tc.errFn != nil && !tc.errFn(err)) {
126106
t.Errorf("%s: unexpected error: %v", k, err)
127107
continue

staging/src/k8s.io/apimachinery/pkg/runtime/embedded_test.go

Lines changed: 28 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -25,58 +25,26 @@ import (
2525
"k8s.io/apimachinery/pkg/runtime"
2626
"k8s.io/apimachinery/pkg/runtime/schema"
2727
"k8s.io/apimachinery/pkg/runtime/serializer"
28+
runtimetesting "k8s.io/apimachinery/pkg/runtime/testing"
2829
"k8s.io/apimachinery/pkg/util/diff"
2930
)
3031

31-
type EmbeddedTest struct {
32-
runtime.TypeMeta
33-
ID string
34-
Object runtime.Object
35-
EmptyObject runtime.Object
36-
}
37-
38-
type EmbeddedTestExternal struct {
39-
runtime.TypeMeta `json:",inline"`
40-
ID string `json:"id,omitempty"`
41-
Object runtime.RawExtension `json:"object,omitempty"`
42-
EmptyObject runtime.RawExtension `json:"emptyObject,omitempty"`
43-
}
44-
45-
type ObjectTest struct {
46-
runtime.TypeMeta
47-
48-
ID string
49-
Items []runtime.Object
50-
}
51-
52-
type ObjectTestExternal struct {
53-
runtime.TypeMeta `yaml:",inline" json:",inline"`
54-
55-
ID string `json:"id,omitempty"`
56-
Items []runtime.RawExtension `json:"items,omitempty"`
57-
}
58-
59-
func (obj *ObjectTest) GetObjectKind() schema.ObjectKind { return &obj.TypeMeta }
60-
func (obj *ObjectTestExternal) GetObjectKind() schema.ObjectKind { return &obj.TypeMeta }
61-
func (obj *EmbeddedTest) GetObjectKind() schema.ObjectKind { return &obj.TypeMeta }
62-
func (obj *EmbeddedTestExternal) GetObjectKind() schema.ObjectKind { return &obj.TypeMeta }
63-
6432
func TestDecodeEmptyRawExtensionAsObject(t *testing.T) {
6533
internalGV := schema.GroupVersion{Group: "test.group", Version: runtime.APIVersionInternal}
6634
externalGV := schema.GroupVersion{Group: "test.group", Version: "v1test"}
6735
externalGVK := externalGV.WithKind("ObjectTest")
6836

6937
s := runtime.NewScheme()
70-
s.AddKnownTypes(internalGV, &ObjectTest{})
71-
s.AddKnownTypeWithName(externalGVK, &ObjectTestExternal{})
38+
s.AddKnownTypes(internalGV, &runtimetesting.ObjectTest{})
39+
s.AddKnownTypeWithName(externalGVK, &runtimetesting.ObjectTestExternal{})
7240

7341
codec := serializer.NewCodecFactory(s).LegacyCodec(externalGV)
7442

7543
obj, gvk, err := codec.Decode([]byte(`{"kind":"`+externalGVK.Kind+`","apiVersion":"`+externalGV.String()+`","items":[{}]}`), nil, nil)
7644
if err != nil {
7745
t.Fatalf("unexpected error: %v", err)
7846
}
79-
test := obj.(*ObjectTest)
47+
test := obj.(*runtimetesting.ObjectTest)
8048
if unk, ok := test.Items[0].(*runtime.Unknown); !ok || unk.Kind != "" || unk.APIVersion != "" || string(unk.Raw) != "{}" || unk.ContentType != runtime.ContentTypeJSON {
8149
t.Fatalf("unexpected object: %#v", test.Items[0])
8250
}
@@ -88,7 +56,7 @@ func TestDecodeEmptyRawExtensionAsObject(t *testing.T) {
8856
if err != nil {
8957
t.Fatalf("unexpected error: %v", err)
9058
}
91-
test = obj.(*ObjectTest)
59+
test = obj.(*runtimetesting.ObjectTest)
9260
if unk, ok := test.Items[0].(*runtime.Unknown); !ok || unk.Kind != "" || unk.APIVersion != "" || string(unk.Raw) != `{"kind":"Other","apiVersion":"v1"}` || unk.ContentType != runtime.ContentTypeJSON {
9361
t.Fatalf("unexpected object: %#v", test.Items[0])
9462
}
@@ -102,29 +70,29 @@ func TestArrayOfRuntimeObject(t *testing.T) {
10270
externalGV := schema.GroupVersion{Group: "test.group", Version: "v1test"}
10371

10472
s := runtime.NewScheme()
105-
s.AddKnownTypes(internalGV, &EmbeddedTest{})
106-
s.AddKnownTypeWithName(externalGV.WithKind("EmbeddedTest"), &EmbeddedTestExternal{})
107-
s.AddKnownTypes(internalGV, &ObjectTest{})
108-
s.AddKnownTypeWithName(externalGV.WithKind("ObjectTest"), &ObjectTestExternal{})
73+
s.AddKnownTypes(internalGV, &runtimetesting.EmbeddedTest{})
74+
s.AddKnownTypeWithName(externalGV.WithKind("EmbeddedTest"), &runtimetesting.EmbeddedTestExternal{})
75+
s.AddKnownTypes(internalGV, &runtimetesting.ObjectTest{})
76+
s.AddKnownTypeWithName(externalGV.WithKind("ObjectTest"), &runtimetesting.ObjectTestExternal{})
10977

11078
codec := serializer.NewCodecFactory(s).LegacyCodec(externalGV)
11179

11280
innerItems := []runtime.Object{
113-
&EmbeddedTest{ID: "baz"},
81+
&runtimetesting.EmbeddedTest{ID: "baz"},
11482
}
11583
items := []runtime.Object{
116-
&EmbeddedTest{ID: "foo"},
117-
&EmbeddedTest{ID: "bar"},
84+
&runtimetesting.EmbeddedTest{ID: "foo"},
85+
&runtimetesting.EmbeddedTest{ID: "bar"},
11886
// TODO: until YAML is removed, this JSON must be in ascending key order to ensure consistent roundtrip serialization
11987
&runtime.Unknown{
12088
Raw: []byte(`{"apiVersion":"unknown.group/unknown","foo":"bar","kind":"OtherTest"}`),
12189
ContentType: runtime.ContentTypeJSON,
12290
},
123-
&ObjectTest{
91+
&runtimetesting.ObjectTest{
12492
Items: runtime.NewEncodableList(codec, innerItems),
12593
},
12694
}
127-
internal := &ObjectTest{
95+
internal := &runtimetesting.ObjectTest{
12896
Items: runtime.NewEncodableList(codec, items),
12997
}
13098
wire, err := runtime.Encode(codec, internal)
@@ -133,13 +101,13 @@ func TestArrayOfRuntimeObject(t *testing.T) {
133101
}
134102
t.Logf("Wire format is:\n%s\n", string(wire))
135103

136-
obj := &ObjectTestExternal{}
104+
obj := &runtimetesting.ObjectTestExternal{}
137105
if err := json.Unmarshal(wire, obj); err != nil {
138106
t.Fatalf("unexpected error: %v", err)
139107
}
140108
t.Logf("exact wire is: %s", string(obj.Items[0].Raw))
141109

142-
items[3] = &ObjectTest{Items: innerItems}
110+
items[3] = &runtimetesting.ObjectTest{Items: innerItems}
143111
internal.Items = items
144112

145113
decoded, err := runtime.Decode(codec, wire)
@@ -178,15 +146,15 @@ func TestNestedObject(t *testing.T) {
178146
embeddedTestExternalGVK := externalGV.WithKind("EmbeddedTest")
179147

180148
s := runtime.NewScheme()
181-
s.AddKnownTypes(internalGV, &EmbeddedTest{})
182-
s.AddKnownTypeWithName(embeddedTestExternalGVK, &EmbeddedTestExternal{})
149+
s.AddKnownTypes(internalGV, &runtimetesting.EmbeddedTest{})
150+
s.AddKnownTypeWithName(embeddedTestExternalGVK, &runtimetesting.EmbeddedTestExternal{})
183151

184152
codec := serializer.NewCodecFactory(s).LegacyCodec(externalGV)
185153

186-
inner := &EmbeddedTest{
154+
inner := &runtimetesting.EmbeddedTest{
187155
ID: "inner",
188156
}
189-
outer := &EmbeddedTest{
157+
outer := &runtimetesting.EmbeddedTest{
190158
ID: "outer",
191159
Object: runtime.NewEncodable(codec, inner),
192160
}
@@ -210,18 +178,18 @@ func TestNestedObject(t *testing.T) {
210178
t.Errorf("Expected unequal %#v %#v", e, a)
211179
}
212180

213-
obj, err := runtime.Decode(codec, decoded.(*EmbeddedTest).Object.(*runtime.Unknown).Raw)
181+
obj, err := runtime.Decode(codec, decoded.(*runtimetesting.EmbeddedTest).Object.(*runtime.Unknown).Raw)
214182
if err != nil {
215183
t.Fatal(err)
216184
}
217-
decoded.(*EmbeddedTest).Object = obj
185+
decoded.(*runtimetesting.EmbeddedTest).Object = obj
218186
if e, a := outer, decoded; !reflect.DeepEqual(e, a) {
219187
t.Errorf("Expected equal %#v %#v", e, a)
220188
}
221189

222190
// test JSON decoding of the external object, which should preserve
223191
// raw bytes
224-
var externalViaJSON EmbeddedTestExternal
192+
var externalViaJSON runtimetesting.EmbeddedTestExternal
225193
err = json.Unmarshal(wire, &externalViaJSON)
226194
if err != nil {
227195
t.Fatalf("Unexpected decode error %v", err)
@@ -237,7 +205,7 @@ func TestNestedObject(t *testing.T) {
237205
// Generic Unmarshalling of JSON cannot load the nested objects because there is
238206
// no default schema set. Consumers wishing to get direct JSON decoding must use
239207
// the external representation
240-
var decodedViaJSON EmbeddedTest
208+
var decodedViaJSON runtimetesting.EmbeddedTest
241209
err = json.Unmarshal(wire, &decodedViaJSON)
242210
if err == nil {
243211
t.Fatal("Expeceted decode error")
@@ -257,12 +225,12 @@ func TestDeepCopyOfRuntimeObject(t *testing.T) {
257225
embeddedTestExternalGVK := externalGV.WithKind("EmbeddedTest")
258226

259227
s := runtime.NewScheme()
260-
s.AddKnownTypes(internalGV, &EmbeddedTest{})
261-
s.AddKnownTypeWithName(embeddedTestExternalGVK, &EmbeddedTestExternal{})
228+
s.AddKnownTypes(internalGV, &runtimetesting.EmbeddedTest{})
229+
s.AddKnownTypeWithName(embeddedTestExternalGVK, &runtimetesting.EmbeddedTestExternal{})
262230

263-
original := &EmbeddedTest{
231+
original := &runtimetesting.EmbeddedTest{
264232
ID: "outer",
265-
Object: &EmbeddedTest{
233+
Object: &runtimetesting.EmbeddedTest{
266234
ID: "inner",
267235
},
268236
}

0 commit comments

Comments
 (0)