1
16
17 package v1beta1
18
19 import (
20 "encoding/json"
21 "reflect"
22 "testing"
23
24 "k8s.io/apimachinery/pkg/runtime"
25
26 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
27 )
28
29 func TestJSONConversion(t *testing.T) {
30 nilJSON := apiextensions.JSON(nil)
31 nullJSON := apiextensions.JSON("null")
32 stringJSON := apiextensions.JSON("foo")
33 boolJSON := apiextensions.JSON(true)
34 sliceJSON := apiextensions.JSON([]string{"foo", "bar", "baz"})
35
36 testCases := map[string]struct {
37 input *apiextensions.JSONSchemaProps
38 expected *JSONSchemaProps
39 }{
40 "nil": {
41 input: &apiextensions.JSONSchemaProps{
42 Default: nil,
43 },
44 expected: &JSONSchemaProps{},
45 },
46 "aliased nil": {
47 input: &apiextensions.JSONSchemaProps{
48 Default: &nilJSON,
49 },
50 expected: &JSONSchemaProps{},
51 },
52 "null": {
53 input: &apiextensions.JSONSchemaProps{
54 Default: &nullJSON,
55 },
56 expected: &JSONSchemaProps{
57 Default: &JSON{
58 Raw: []byte(`"null"`),
59 },
60 },
61 },
62 "string": {
63 input: &apiextensions.JSONSchemaProps{
64 Default: &stringJSON,
65 },
66 expected: &JSONSchemaProps{
67 Default: &JSON{
68 Raw: []byte(`"foo"`),
69 },
70 },
71 },
72 "bool": {
73 input: &apiextensions.JSONSchemaProps{
74 Default: &boolJSON,
75 },
76 expected: &JSONSchemaProps{
77 Default: &JSON{
78 Raw: []byte(`true`),
79 },
80 },
81 },
82 "slice": {
83 input: &apiextensions.JSONSchemaProps{
84 Default: &sliceJSON,
85 },
86 expected: &JSONSchemaProps{
87 Default: &JSON{
88 Raw: []byte(`["foo","bar","baz"]`),
89 },
90 },
91 },
92 }
93
94 scheme := runtime.NewScheme()
95
96
97 if err := apiextensions.AddToScheme(scheme); err != nil {
98 t.Fatal(err)
99 }
100 if err := AddToScheme(scheme); err != nil {
101 t.Fatal(err)
102 }
103
104 for k, tc := range testCases {
105 external := &JSONSchemaProps{}
106 if err := scheme.Convert(tc.input, external, nil); err != nil {
107 t.Errorf("%s: unexpected error: %v", k, err)
108 }
109
110 if !reflect.DeepEqual(external, tc.expected) {
111 t.Errorf("%s: expected\n\t%#v, got \n\t%#v", k, tc.expected, external)
112 }
113 }
114 }
115
116 func TestJSONRoundTrip(t *testing.T) {
117 testcases := []struct {
118 name string
119 in string
120 out string
121 }{
122 {
123 name: "nulls",
124 in: `{"default":null,"enum":null,"example":null}`,
125 out: `{}`,
126 },
127 {
128 name: "null values",
129 in: `{"default":{"test":null},"enum":[null],"example":{"test":null}}`,
130 out: `{"default":{"test":null},"enum":[null],"example":{"test":null}}`,
131 },
132 }
133
134 scheme := runtime.NewScheme()
135
136 if err := apiextensions.AddToScheme(scheme); err != nil {
137 t.Fatal(err)
138 }
139 if err := AddToScheme(scheme); err != nil {
140 t.Fatal(err)
141 }
142
143 for _, tc := range testcases {
144 t.Run(tc.name, func(t *testing.T) {
145 external := &JSONSchemaProps{}
146 if err := json.Unmarshal([]byte(tc.in), external); err != nil {
147 t.Fatal(err)
148 }
149
150 internal := &apiextensions.JSONSchemaProps{}
151 if err := scheme.Convert(external, internal, nil); err != nil {
152 t.Fatalf("unexpected error: %v", err)
153 }
154 roundtripped := &JSONSchemaProps{}
155 if err := scheme.Convert(internal, roundtripped, nil); err != nil {
156 t.Fatalf("unexpected error: %v", err)
157 }
158
159 out, err := json.Marshal(roundtripped)
160 if err != nil {
161 t.Fatal(err)
162 }
163 if string(out) != string(tc.out) {
164 t.Fatalf("expected\n%s\ngot\n%s", string(tc.out), string(out))
165 }
166 })
167 }
168 }
169
View as plain text