@@ -25,58 +25,26 @@ import (
25
25
"k8s.io/apimachinery/pkg/runtime"
26
26
"k8s.io/apimachinery/pkg/runtime/schema"
27
27
"k8s.io/apimachinery/pkg/runtime/serializer"
28
+ runtimetesting "k8s.io/apimachinery/pkg/runtime/testing"
28
29
"k8s.io/apimachinery/pkg/util/diff"
29
30
)
30
31
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
-
64
32
func TestDecodeEmptyRawExtensionAsObject (t * testing.T ) {
65
33
internalGV := schema.GroupVersion {Group : "test.group" , Version : runtime .APIVersionInternal }
66
34
externalGV := schema.GroupVersion {Group : "test.group" , Version : "v1test" }
67
35
externalGVK := externalGV .WithKind ("ObjectTest" )
68
36
69
37
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 {})
72
40
73
41
codec := serializer .NewCodecFactory (s ).LegacyCodec (externalGV )
74
42
75
43
obj , gvk , err := codec .Decode ([]byte (`{"kind":"` + externalGVK .Kind + `","apiVersion":"` + externalGV .String ()+ `","items":[{}]}` ), nil , nil )
76
44
if err != nil {
77
45
t .Fatalf ("unexpected error: %v" , err )
78
46
}
79
- test := obj .(* ObjectTest )
47
+ test := obj .(* runtimetesting. ObjectTest )
80
48
if unk , ok := test .Items [0 ].(* runtime.Unknown ); ! ok || unk .Kind != "" || unk .APIVersion != "" || string (unk .Raw ) != "{}" || unk .ContentType != runtime .ContentTypeJSON {
81
49
t .Fatalf ("unexpected object: %#v" , test .Items [0 ])
82
50
}
@@ -88,7 +56,7 @@ func TestDecodeEmptyRawExtensionAsObject(t *testing.T) {
88
56
if err != nil {
89
57
t .Fatalf ("unexpected error: %v" , err )
90
58
}
91
- test = obj .(* ObjectTest )
59
+ test = obj .(* runtimetesting. ObjectTest )
92
60
if unk , ok := test .Items [0 ].(* runtime.Unknown ); ! ok || unk .Kind != "" || unk .APIVersion != "" || string (unk .Raw ) != `{"kind":"Other","apiVersion":"v1"}` || unk .ContentType != runtime .ContentTypeJSON {
93
61
t .Fatalf ("unexpected object: %#v" , test .Items [0 ])
94
62
}
@@ -102,29 +70,29 @@ func TestArrayOfRuntimeObject(t *testing.T) {
102
70
externalGV := schema.GroupVersion {Group : "test.group" , Version : "v1test" }
103
71
104
72
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 {})
109
77
110
78
codec := serializer .NewCodecFactory (s ).LegacyCodec (externalGV )
111
79
112
80
innerItems := []runtime.Object {
113
- & EmbeddedTest {ID : "baz" },
81
+ & runtimetesting. EmbeddedTest {ID : "baz" },
114
82
}
115
83
items := []runtime.Object {
116
- & EmbeddedTest {ID : "foo" },
117
- & EmbeddedTest {ID : "bar" },
84
+ & runtimetesting. EmbeddedTest {ID : "foo" },
85
+ & runtimetesting. EmbeddedTest {ID : "bar" },
118
86
// TODO: until YAML is removed, this JSON must be in ascending key order to ensure consistent roundtrip serialization
119
87
& runtime.Unknown {
120
88
Raw : []byte (`{"apiVersion":"unknown.group/unknown","foo":"bar","kind":"OtherTest"}` ),
121
89
ContentType : runtime .ContentTypeJSON ,
122
90
},
123
- & ObjectTest {
91
+ & runtimetesting. ObjectTest {
124
92
Items : runtime .NewEncodableList (codec , innerItems ),
125
93
},
126
94
}
127
- internal := & ObjectTest {
95
+ internal := & runtimetesting. ObjectTest {
128
96
Items : runtime .NewEncodableList (codec , items ),
129
97
}
130
98
wire , err := runtime .Encode (codec , internal )
@@ -133,13 +101,13 @@ func TestArrayOfRuntimeObject(t *testing.T) {
133
101
}
134
102
t .Logf ("Wire format is:\n %s\n " , string (wire ))
135
103
136
- obj := & ObjectTestExternal {}
104
+ obj := & runtimetesting. ObjectTestExternal {}
137
105
if err := json .Unmarshal (wire , obj ); err != nil {
138
106
t .Fatalf ("unexpected error: %v" , err )
139
107
}
140
108
t .Logf ("exact wire is: %s" , string (obj .Items [0 ].Raw ))
141
109
142
- items [3 ] = & ObjectTest {Items : innerItems }
110
+ items [3 ] = & runtimetesting. ObjectTest {Items : innerItems }
143
111
internal .Items = items
144
112
145
113
decoded , err := runtime .Decode (codec , wire )
@@ -178,15 +146,15 @@ func TestNestedObject(t *testing.T) {
178
146
embeddedTestExternalGVK := externalGV .WithKind ("EmbeddedTest" )
179
147
180
148
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 {})
183
151
184
152
codec := serializer .NewCodecFactory (s ).LegacyCodec (externalGV )
185
153
186
- inner := & EmbeddedTest {
154
+ inner := & runtimetesting. EmbeddedTest {
187
155
ID : "inner" ,
188
156
}
189
- outer := & EmbeddedTest {
157
+ outer := & runtimetesting. EmbeddedTest {
190
158
ID : "outer" ,
191
159
Object : runtime .NewEncodable (codec , inner ),
192
160
}
@@ -210,18 +178,18 @@ func TestNestedObject(t *testing.T) {
210
178
t .Errorf ("Expected unequal %#v %#v" , e , a )
211
179
}
212
180
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 )
214
182
if err != nil {
215
183
t .Fatal (err )
216
184
}
217
- decoded .(* EmbeddedTest ).Object = obj
185
+ decoded .(* runtimetesting. EmbeddedTest ).Object = obj
218
186
if e , a := outer , decoded ; ! reflect .DeepEqual (e , a ) {
219
187
t .Errorf ("Expected equal %#v %#v" , e , a )
220
188
}
221
189
222
190
// test JSON decoding of the external object, which should preserve
223
191
// raw bytes
224
- var externalViaJSON EmbeddedTestExternal
192
+ var externalViaJSON runtimetesting. EmbeddedTestExternal
225
193
err = json .Unmarshal (wire , & externalViaJSON )
226
194
if err != nil {
227
195
t .Fatalf ("Unexpected decode error %v" , err )
@@ -237,7 +205,7 @@ func TestNestedObject(t *testing.T) {
237
205
// Generic Unmarshalling of JSON cannot load the nested objects because there is
238
206
// no default schema set. Consumers wishing to get direct JSON decoding must use
239
207
// the external representation
240
- var decodedViaJSON EmbeddedTest
208
+ var decodedViaJSON runtimetesting. EmbeddedTest
241
209
err = json .Unmarshal (wire , & decodedViaJSON )
242
210
if err == nil {
243
211
t .Fatal ("Expeceted decode error" )
@@ -257,12 +225,12 @@ func TestDeepCopyOfRuntimeObject(t *testing.T) {
257
225
embeddedTestExternalGVK := externalGV .WithKind ("EmbeddedTest" )
258
226
259
227
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 {})
262
230
263
- original := & EmbeddedTest {
231
+ original := & runtimetesting. EmbeddedTest {
264
232
ID : "outer" ,
265
- Object : & EmbeddedTest {
233
+ Object : & runtimetesting. EmbeddedTest {
266
234
ID : "inner" ,
267
235
},
268
236
}
0 commit comments