1 package v3alpha1_test
2
3 import (
4 "encoding/json"
5 "testing"
6 "time"
7
8 "github.com/stretchr/testify/assert"
9 "sigs.k8s.io/yaml"
10
11 crds "github.com/datawire/ambassador/v2/pkg/api/getambassador.io/v3alpha1"
12 )
13
14 func TestAmbassadorID(t *testing.T) {
15 t.Parallel()
16 type subtest struct {
17 inputResource crds.AmbassadorID
18 inputEnvVar string
19 expected bool
20 }
21 subtests := map[string]subtest{
22 "nil-d": {crds.AmbassadorID(nil), "default", true},
23 "nil-c": {crds.AmbassadorID(nil), "custom", false},
24 "empty-d": {crds.AmbassadorID{}, "default", true},
25 "empty-c": {crds.AmbassadorID{}, "custom", false},
26 "one-d": {crds.AmbassadorID{"default"}, "default", true},
27 "one-c": {crds.AmbassadorID{"default"}, "custom", false},
28 "one-c2": {crds.AmbassadorID{"custom"}, "custom", true},
29 "multi": {crds.AmbassadorID{"default", "custom"}, "custom", true},
30 }
31 for name, info := range subtests {
32 info := info
33 t.Run(name, func(t *testing.T) {
34 t.Parallel()
35 assert.Equal(t, info.expected, info.inputResource.Matches(info.inputEnvVar))
36 })
37 }
38 }
39
40 func TestMillisecondDuration(t *testing.T) {
41 t.Parallel()
42 type TestResource struct {
43 Field crds.MillisecondDuration `json:"field,omitempty"`
44 }
45 type subtest struct {
46 inputYAML string
47 expectedStruct TestResource
48 expectedJSON string
49 expectedErr string
50 }
51 subtests := map[string]subtest{
52 "empty": {`{}`, TestResource{}, `{"field":0}`, ``},
53 "explicitEmpty": {`field:`, TestResource{}, `{"field":0}`, ``},
54 "explicitnull": {`field: null`, TestResource{}, `{"field":0}`, ``},
55 "explicitNull": {`field: Null`, TestResource{}, `{"field":0}`, ``},
56 "explicitNULL": {`field: NULL`, TestResource{}, `{"field":0}`, ``},
57 "explicitTilde": {`field: ~`, TestResource{}, `{"field":0}`, ``},
58 "3000": {`field: 3000`, TestResource{crds.MillisecondDuration{3 * time.Second}}, `{"field":3000}`, ``},
59 "overflow32": {`field: 4320000000`, TestResource{crds.MillisecondDuration{50 * 24 * time.Hour}}, `{"field":4320000000}`, ``},
60 "string": {`field: "30s"`, TestResource{crds.MillisecondDuration{0}}, `{"field":0}`, "error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go struct field TestResource.field of type int64"},
61 }
62 for name, info := range subtests {
63 info := info
64 t.Run(name, func(t *testing.T) {
65 t.Parallel()
66 var parsed TestResource
67 err := yaml.Unmarshal([]byte(info.inputYAML), &parsed)
68 if info.expectedErr == `` {
69 assert.NoError(t, err)
70 } else {
71 assert.EqualError(t, err, info.expectedErr)
72 }
73 assert.Equal(t, info.expectedStruct, parsed)
74 jsonbytes, err := json.Marshal(parsed)
75 assert.NoError(t, err)
76 assert.Equal(t, info.expectedJSON, string(jsonbytes))
77 })
78 }
79 }
80
81 func TestMillisecondDurationPtr(t *testing.T) {
82 t.Parallel()
83 type TestResource struct {
84 Field *crds.MillisecondDuration `json:"field,omitempty"`
85 }
86 type subtest struct {
87 inputYAML string
88 expectedStruct TestResource
89 expectedJSON string
90 expectedErr string
91 }
92 subtests := map[string]subtest{
93 "empty": {`{}`, TestResource{}, `{}`, ``},
94 "explicitEmpty": {`field:`, TestResource{}, `{}`, ``},
95 "explicitnull": {`field: null`, TestResource{}, `{}`, ``},
96 "explicitNull": {`field: Null`, TestResource{}, `{}`, ``},
97 "explicitNULL": {`field: NULL`, TestResource{}, `{}`, ``},
98 "explicitTilde": {`field: ~`, TestResource{}, `{}`, ``},
99 "3000": {`field: 3000`, TestResource{&crds.MillisecondDuration{3 * time.Second}}, `{"field":3000}`, ``},
100 "overflow32": {`field: 4320000000`, TestResource{&crds.MillisecondDuration{50 * 24 * time.Hour}}, `{"field":4320000000}`, ``},
101 "string": {`field: "30s"`, TestResource{&crds.MillisecondDuration{0}}, `{"field":0}`, "error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go struct field TestResource.field of type int64"},
102 }
103 for name, info := range subtests {
104 info := info
105 t.Run(name, func(t *testing.T) {
106 t.Parallel()
107 var parsed TestResource
108 err := yaml.Unmarshal([]byte(info.inputYAML), &parsed)
109 if info.expectedErr == `` {
110 assert.NoError(t, err)
111 } else {
112 assert.EqualError(t, err, info.expectedErr)
113 }
114 assert.Equal(t, info.expectedStruct, parsed)
115 jsonbytes, err := json.Marshal(parsed)
116 assert.NoError(t, err)
117 assert.Equal(t, info.expectedJSON, string(jsonbytes))
118 })
119 }
120 }
121
122 func TestUntypedDict(t *testing.T) {
123 t.Parallel()
124 type TestResource struct {
125 Field crds.UntypedDict `json:"field,omitempty"`
126 }
127 type subtest struct {
128 inputYAML string
129 expectedStruct TestResource
130 expectedJSON string
131 expectedErr string
132 }
133 subtests := map[string]subtest{
134 "empty": {`{}`, TestResource{}, `{"field":null}`, ``},
135 "explicitEmpty": {`field:`, TestResource{}, `{"field":null}`, ``},
136 "explicitnull": {`field: null`, TestResource{}, `{"field":null}`, ``},
137 "explicitNull": {`field: Null`, TestResource{}, `{"field":null}`, ``},
138 "explicitNULL": {`field: NULL`, TestResource{}, `{"field":null}`, ``},
139 "explicitTilde": {`field: ~`, TestResource{}, `{"field":null}`, ``},
140 "basic": {`field: {foo: "bar"}`, TestResource{crds.UntypedDict{map[string]json.RawMessage{"foo": json.RawMessage(`"bar"`)}}}, `{"field":{"foo":"bar"}}`, ``},
141 "string": {`field: "str"`, TestResource{}, `{"field":null}`, "error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go struct field TestResource.field of type map[string]json.RawMessage"},
142 "badkey": {`
143 badkey: &anchor
144 baz: qux
145 field: { *anchor: "bar"}`, TestResource{}, `{"field":null}`, "error converting YAML to JSON: yaml: invalid map key: map[interface {}]interface {}{\"baz\":\"qux\"}"},
146 }
147 for name, info := range subtests {
148 info := info
149 t.Run(name, func(t *testing.T) {
150 t.Parallel()
151 var parsed TestResource
152 err := yaml.Unmarshal([]byte(info.inputYAML), &parsed)
153 if info.expectedErr == `` {
154 assert.NoError(t, err)
155 } else {
156 assert.EqualError(t, err, info.expectedErr)
157 }
158 assert.Equal(t, info.expectedStruct, parsed)
159 jsonbytes, err := json.Marshal(parsed)
160 assert.NoError(t, err)
161 assert.Equal(t, info.expectedJSON, string(jsonbytes))
162 })
163 }
164 }
165
166 func TestUntypedDictPtr(t *testing.T) {
167 t.Parallel()
168 type TestResource struct {
169 Field *crds.UntypedDict `json:"field,omitempty"`
170 }
171 type subtest struct {
172 inputYAML string
173 expectedStruct TestResource
174 expectedJSON string
175 expectedErr string
176 }
177 subtests := map[string]subtest{
178 "empty": {`{}`, TestResource{}, `{}`, ``},
179 "explicitEmpty": {`field:`, TestResource{}, `{}`, ``},
180 "explicitnull": {`field: null`, TestResource{}, `{}`, ``},
181 "explicitNull": {`field: Null`, TestResource{}, `{}`, ``},
182 "explicitNULL": {`field: NULL`, TestResource{}, `{}`, ``},
183 "explicitTilde": {`field: ~`, TestResource{}, `{}`, ``},
184 "basic": {`field: {foo: "bar"}`, TestResource{&crds.UntypedDict{map[string]json.RawMessage{"foo": json.RawMessage(`"bar"`)}}}, `{"field":{"foo":"bar"}}`, ``},
185 "string": {`field: "str"`, TestResource{&crds.UntypedDict{}}, `{"field":null}`, "error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go struct field TestResource.field of type map[string]json.RawMessage"},
186 "badkey": {`
187 badkey: &anchor
188 baz: qux
189 field: { *anchor: "bar"}`, TestResource{}, `{}`, "error converting YAML to JSON: yaml: invalid map key: map[interface {}]interface {}{\"baz\":\"qux\"}"},
190 }
191 for name, info := range subtests {
192 info := info
193 t.Run(name, func(t *testing.T) {
194 t.Parallel()
195 var parsed TestResource
196 err := yaml.Unmarshal([]byte(info.inputYAML), &parsed)
197 if info.expectedErr == `` {
198 assert.NoError(t, err)
199 } else {
200 assert.EqualError(t, err, info.expectedErr)
201 }
202 assert.Equal(t, info.expectedStruct, parsed)
203 jsonbytes, err := json.Marshal(parsed)
204 assert.NoError(t, err)
205 assert.Equal(t, info.expectedJSON, string(jsonbytes))
206 })
207 }
208 }
209
View as plain text