1 package v3alpha1_test
2
3 import (
4 "encoding/json"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 "sigs.k8s.io/yaml"
9
10 crds "github.com/datawire/ambassador/v2/pkg/api/getambassador.io/v3alpha1"
11 )
12
13 func TestHostState(t *testing.T) {
14 t.Parallel()
15 type TestResource struct {
16 Field crds.HostState `json:"field,omitempty"`
17 }
18 type subtest struct {
19 inputYAML string
20 expectedStruct TestResource
21 expectedJSON string
22 expectedErr string
23 }
24 subtests := map[string]subtest{
25 "empty": {`{}`, TestResource{}, `{}`, ``},
26 "explicitEmpty": {`field:`, TestResource{}, `{}`, ``},
27 "explicitnull": {`field: null`, TestResource{}, `{}`, ``},
28 "explicitNull": {`field: Null`, TestResource{}, `{}`, ``},
29 "explicitNULL": {`field: NULL`, TestResource{}, `{}`, ``},
30 "explicitTilde": {`field: ~`, TestResource{}, `{}`, ``},
31
32 "Initial": {`field: "Initial"`, TestResource{crds.HostState_Initial}, `{}`, ``},
33 "Pending": {`field: "Pending"`, TestResource{crds.HostState_Pending}, `{"field":"Pending"}`, ``},
34 "Ready": {`field: "Ready"`, TestResource{crds.HostState_Ready}, `{"field":"Ready"}`, ``},
35 "Error": {`field: "Error"`, TestResource{crds.HostState_Error}, `{"field":"Error"}`, ``},
36
37 "invalid-string": {`field: "invalid"`, TestResource{crds.HostState_Initial}, `{}`, ``},
38 "invalid-type": {`field: {}`, TestResource{crds.HostState_Initial}, `{}`, `error unmarshaling JSON: while decoding JSON: json: cannot unmarshal object into Go struct field TestResource.field of type string`},
39 }
40 for name, info := range subtests {
41 info := info
42 t.Run(name, func(t *testing.T) {
43 t.Parallel()
44 var parsed TestResource
45 err := yaml.Unmarshal([]byte(info.inputYAML), &parsed)
46 if info.expectedErr == `` {
47 assert.NoError(t, err)
48 } else {
49 assert.EqualError(t, err, info.expectedErr)
50 }
51 assert.Equal(t, info.expectedStruct, parsed)
52 jsonbytes, err := json.Marshal(parsed)
53 assert.NoError(t, err)
54 assert.Equal(t, info.expectedJSON, string(jsonbytes))
55 })
56 }
57 }
58
59 func TestHostPhase(t *testing.T) {
60 t.Parallel()
61 type TestResource struct {
62 Field crds.HostPhase `json:"field,omitempty"`
63 }
64 type subtest struct {
65 inputYAML string
66 expectedStruct TestResource
67 expectedJSON string
68 expectedErr string
69 }
70 subtests := map[string]subtest{
71 "empty": {`{}`, TestResource{}, `{}`, ``},
72 "explicitEmpty": {`field:`, TestResource{}, `{}`, ``},
73 "explicitnull": {`field: null`, TestResource{}, `{}`, ``},
74 "explicitNull": {`field: Null`, TestResource{}, `{}`, ``},
75 "explicitNULL": {`field: NULL`, TestResource{}, `{}`, ``},
76 "explicitTilde": {`field: ~`, TestResource{}, `{}`, ``},
77
78 "NA": {`field: "NA"`, TestResource{crds.HostPhase_NA}, `{}`, ``},
79 "DefaultsFilled": {`field: "DefaultsFilled"`, TestResource{crds.HostPhase_DefaultsFilled}, `{"field":"DefaultsFilled"}`, ``},
80 "ACMEUserPrivateKeyCreated": {`field: "ACMEUserPrivateKeyCreated"`, TestResource{crds.HostPhase_ACMEUserPrivateKeyCreated}, `{"field":"ACMEUserPrivateKeyCreated"}`, ``},
81 "ACMEUserRegistered": {`field: "ACMEUserRegistered"`, TestResource{crds.HostPhase_ACMEUserRegistered}, `{"field":"ACMEUserRegistered"}`, ``},
82 "ACMECertificateChallenge": {`field: "ACMECertificateChallenge"`, TestResource{crds.HostPhase_ACMECertificateChallenge}, `{"field":"ACMECertificateChallenge"}`, ``},
83
84 "invalid-string": {`field: "invalid"`, TestResource{crds.HostPhase_NA}, `{}`, ``},
85 "invalid-type": {`field: {}`, TestResource{crds.HostPhase_NA}, `{}`, `error unmarshaling JSON: while decoding JSON: json: cannot unmarshal object into Go struct field TestResource.field of type string`},
86 }
87 for name, info := range subtests {
88 info := info
89 t.Run(name, func(t *testing.T) {
90 t.Parallel()
91 var parsed TestResource
92 err := yaml.Unmarshal([]byte(info.inputYAML), &parsed)
93 if info.expectedErr == `` {
94 assert.NoError(t, err)
95 } else {
96 assert.EqualError(t, err, info.expectedErr)
97 }
98 assert.Equal(t, info.expectedStruct, parsed)
99 jsonbytes, err := json.Marshal(parsed)
100 assert.NoError(t, err)
101 assert.Equal(t, info.expectedJSON, string(jsonbytes))
102 })
103 }
104 }
105
View as plain text