Skip to content

Commit

Permalink
Add compatibility test for (shudder) string->int and {}->[] coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
liggitt committed Dec 14, 2017
1 parent d9c9d9b commit a8b819c
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions pkg/api/compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
"strings"
"testing"

"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
jsonserializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kubernetes/pkg/api/legacyscheme"
api "k8s.io/kubernetes/pkg/apis/core"
Expand Down Expand Up @@ -111,6 +114,60 @@ func TestAllowedGrouplessVersion(t *testing.T) {
}
}

func TestAllowedTypeCoercion(t *testing.T) {
ten := int64(10)

testcases := []struct {
name string
input []byte
into runtime.Object
expected runtime.Object
}{
{
name: "string to number",
input: []byte(`{
"kind":"Pod",
"apiVersion":"v1",
"spec":{"activeDeadlineSeconds":"10"}
}`),
expected: &v1.Pod{
TypeMeta: metav1.TypeMeta{Kind: "Pod", APIVersion: "v1"},
Spec: v1.PodSpec{ActiveDeadlineSeconds: &ten},
},
},
{
name: "empty object to array",
input: []byte(`{
"kind":"Pod",
"apiVersion":"v1",
"spec":{"containers":{}}
}`),
expected: &v1.Pod{
TypeMeta: metav1.TypeMeta{Kind: "Pod", APIVersion: "v1"},
Spec: v1.PodSpec{Containers: []v1.Container{}},
},
},
}

for i := range testcases {
func(i int) {
tc := testcases[i]
t.Run(tc.name, func(t *testing.T) {
s := jsonserializer.NewSerializer(jsonserializer.DefaultMetaFactory, legacyscheme.Scheme, legacyscheme.Scheme, false)
obj, _, err := s.Decode(tc.input, nil, tc.into)
if err != nil {
t.Error(err)
return
}
if !reflect.DeepEqual(obj, tc.expected) {
t.Errorf("Expected\n%#v\ngot\n%#v", tc.expected, obj)
return
}
})
}(i)
}
}

func getJSONValue(data map[string]interface{}, keys ...string) (interface{}, bool, error) {
// No keys, current value is it
if len(keys) == 0 {
Expand Down

0 comments on commit a8b819c

Please sign in to comment.