Skip to content

Commit 95a270e

Browse files
liggittdeads2k
authored andcommitted
Add tests to ensure type coercion handles errors correctly
1 parent a04b2e9 commit 95a270e

File tree

1 file changed

+55
-8
lines changed

1 file changed

+55
-8
lines changed

pkg/api/compatibility_test.go

+55-8
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,17 @@ func TestAllowedGrouplessVersion(t *testing.T) {
116116

117117
func TestAllowedTypeCoercion(t *testing.T) {
118118
ten := int64(10)
119+
twenty := int32(10)
119120

120121
testcases := []struct {
121-
name string
122-
input []byte
123-
into runtime.Object
124-
expected runtime.Object
122+
name string
123+
input []byte
124+
into runtime.Object
125+
expected runtime.Object
126+
expectedErr string
125127
}{
126128
{
127-
name: "string to number",
129+
name: "string to int64",
128130
input: []byte(`{
129131
"kind":"Pod",
130132
"apiVersion":"v1",
@@ -135,6 +137,36 @@ func TestAllowedTypeCoercion(t *testing.T) {
135137
Spec: v1.PodSpec{ActiveDeadlineSeconds: &ten},
136138
},
137139
},
140+
{
141+
name: "string to int64 malformed",
142+
input: []byte(`{
143+
"kind":"Pod",
144+
"apiVersion":"v1",
145+
"spec":{"activeDeadlineSeconds":"1.1"}
146+
}`),
147+
expectedErr: "read int64: unexpected character: \"",
148+
},
149+
{
150+
name: "string to int32",
151+
input: []byte(`{
152+
"kind":"Pod",
153+
"apiVersion":"v1",
154+
"spec":{"priority":"10"}
155+
}`),
156+
expected: &v1.Pod{
157+
TypeMeta: metav1.TypeMeta{Kind: "Pod", APIVersion: "v1"},
158+
Spec: v1.PodSpec{Priority: &twenty},
159+
},
160+
},
161+
{
162+
name: "string to int32 malformed",
163+
input: []byte(`{
164+
"kind":"Pod",
165+
"apiVersion":"v1",
166+
"spec":{"priority":"1.1"}
167+
}`),
168+
expectedErr: "read int32: unexpected character: \"",
169+
},
138170
{
139171
name: "empty object to array",
140172
input: []byte(`{
@@ -144,9 +176,18 @@ func TestAllowedTypeCoercion(t *testing.T) {
144176
}`),
145177
expected: &v1.Pod{
146178
TypeMeta: metav1.TypeMeta{Kind: "Pod", APIVersion: "v1"},
147-
Spec: v1.PodSpec{Containers: []v1.Container{}},
179+
Spec: v1.PodSpec{Containers: nil},
148180
},
149181
},
182+
{
183+
name: "non-empty object to array fails",
184+
input: []byte(`{
185+
"kind":"Pod",
186+
"apiVersion":"v1",
187+
"spec":{"containers":{"name":"somevalue"}}
188+
}`),
189+
expectedErr: "v1.PodSpec.Containers: decode slice: expect [ or n, but found {",
190+
},
150191
}
151192

152193
for i := range testcases {
@@ -155,8 +196,14 @@ func TestAllowedTypeCoercion(t *testing.T) {
155196
t.Run(tc.name, func(t *testing.T) {
156197
s := jsonserializer.NewSerializer(jsonserializer.DefaultMetaFactory, legacyscheme.Scheme, legacyscheme.Scheme, false)
157198
obj, _, err := s.Decode(tc.input, nil, tc.into)
158-
if err != nil {
159-
t.Error(err)
199+
if err != nil || len(tc.expectedErr) > 0 {
200+
if len(tc.expectedErr) == 0 {
201+
t.Error(err)
202+
} else if err == nil {
203+
t.Errorf("expected error %q, got none", tc.expectedErr)
204+
} else if !strings.Contains(err.Error(), tc.expectedErr) {
205+
t.Errorf("expected error %q, got %q", tc.expectedErr, err.Error())
206+
}
160207
return
161208
}
162209
if !reflect.DeepEqual(obj, tc.expected) {

0 commit comments

Comments
 (0)