-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathroute_test.go
More file actions
236 lines (207 loc) · 5.71 KB
/
route_test.go
File metadata and controls
236 lines (207 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package coder
import (
"strings"
"testing"
"github.com/drycc/controller-sdk-go/api"
)
func TestRouteCoderDecode(t *testing.T) {
t.Parallel()
input := `{
"apiVersion": "gateway.networking.k8s.io/v1",
"kind": "HTTPRoute",
"metadata": {"name": "my-route"},
"spec": {
"parents": [
{"name": "my-gateway", "port": 80}
],
"rules": [
{
"backends": [
{"kind": "Service", "name": "my-svc", "port": 5000, "weight": 100}
]
}
]
}
}`
c := &RouteCoder{}
if err := c.Decode([]byte(input)); err != nil {
t.Fatalf("Decode failed: %v", err)
}
if c.Request.Name != "my-route" {
t.Errorf("expected Name=my-route, got %s", c.Request.Name)
}
if c.Request.Kind != "HTTPRoute" {
t.Errorf("expected Kind=HTTPRoute, got %s", c.Request.Kind)
}
if len(c.Request.ParentRefs) != 1 {
t.Fatalf("expected 1 ParentRef, got %d", len(c.Request.ParentRefs))
}
if c.Request.ParentRefs[0].Name != "my-gateway" {
t.Errorf("expected ParentRef name=my-gateway, got %s", c.Request.ParentRefs[0].Name)
}
if c.Request.ParentRefs[0].Port != 80 {
t.Errorf("expected ParentRef port=80, got %d", c.Request.ParentRefs[0].Port)
}
if len(c.Request.Rules) != 1 {
t.Fatalf("expected 1 Rule, got %d", len(c.Request.Rules))
}
// backends should be mapped to backendRefs
if _, ok := c.Request.Rules[0]["backendRefs"]; !ok {
t.Error("expected backendRefs key in rule, got none")
}
if _, ok := c.Request.Rules[0]["backends"]; ok {
t.Error("backends key should not exist in rule after decode")
}
}
func TestRouteCoderDecodeMinimal(t *testing.T) {
t.Parallel()
input := `{"kind": "HTTPRoute", "metadata": {"name": "simple"}}`
c := &RouteCoder{}
if err := c.Decode([]byte(input)); err != nil {
t.Fatalf("Decode failed: %v", err)
}
if c.Request.Name != "simple" {
t.Errorf("expected Name=simple, got %s", c.Request.Name)
}
if c.Request.Kind != "HTTPRoute" {
t.Errorf("expected Kind=HTTPRoute, got %s", c.Request.Kind)
}
if len(c.Request.ParentRefs) != 0 {
t.Errorf("expected 0 ParentRefs, got %d", len(c.Request.ParentRefs))
}
if len(c.Request.Rules) != 0 {
t.Errorf("expected 0 Rules, got %d", len(c.Request.Rules))
}
}
func TestRouteCoderDecodeInvalidJSON(t *testing.T) {
t.Parallel()
c := &RouteCoder{}
if err := c.Decode([]byte(`{invalid`)); err == nil {
t.Error("expected error for invalid JSON, got nil")
}
}
func TestRouteCoderEncode(t *testing.T) {
t.Parallel()
routable := true
c := &RouteCoder{
Info: api.RouteInfo{
Name: "my-route",
Kind: "HTTPRoute",
ParentRefs: []api.RouteParentRef{
{Name: "my-gateway", Port: 80},
},
Rules: []api.RouteRule{
{
"backendRefs": []map[string]any{
{"kind": "Service", "name": "my-svc", "port": 5000},
},
},
},
Routable: &routable,
},
}
data, err := c.Encode()
if err != nil {
t.Fatalf("Encode failed: %v", err)
}
yaml := string(data)
// Check field order: apiVersion should come first
if !strings.HasPrefix(yaml, "apiVersion:") {
t.Errorf("expected YAML to start with apiVersion:, got:\n%s", yaml)
}
// Check key fields are present
if !strings.Contains(yaml, "kind: HTTPRoute") {
t.Error("expected kind: HTTPRoute in output")
}
if !strings.Contains(yaml, "name: my-route") {
t.Error("expected name: my-route in output")
}
if !strings.Contains(yaml, "parents:") {
t.Error("expected parents: in output")
}
if !strings.Contains(yaml, "backends:") {
t.Error("expected backends: in output (backendRefs should be mapped to backends)")
}
if !strings.Contains(yaml, "routable: true") {
t.Error("expected routable: true in status")
}
// backendRefs should NOT appear in output
if strings.Contains(yaml, "backendRefs:") {
t.Error("backendRefs should not appear in output, should be backends")
}
}
func TestRouteCoderEncodeNoRoutable(t *testing.T) {
t.Parallel()
c := &RouteCoder{
Info: api.RouteInfo{
Name: "my-route",
Kind: "HTTPRoute",
},
}
data, err := c.Encode()
if err != nil {
t.Fatalf("Encode failed: %v", err)
}
yaml := string(data)
if strings.Contains(yaml, "routable") {
t.Error("routable should not appear when nil")
}
}
func TestRouteCoderRoundTrip(t *testing.T) {
t.Parallel()
// Encode
routable := false
original := &RouteCoder{
Info: api.RouteInfo{
Name: "round-trip",
Kind: "HTTPRoute",
ParentRefs: []api.RouteParentRef{
{Name: "gw", Port: 443},
},
Rules: []api.RouteRule{
{
"backendRefs": []map[string]any{
{"name": "svc", "port": 8080},
},
},
},
Routable: &routable,
},
}
yamlData, err := original.Encode()
if err != nil {
t.Fatalf("Encode failed: %v", err)
}
// Decode the YAML back (first convert YAML to JSON)
// Since Decode expects JSON, we test the field mapping logic directly
input := `{
"apiVersion": "controller.drycc.cc/v2.3",
"kind": "HTTPRoute",
"metadata": {"name": "round-trip"},
"spec": {
"parents": [{"name": "gw", "port": 443}],
"rules": [{"backends": [{"name": "svc", "port": 8080}]}]
},
"status": {"routable": false}
}`
decoded := &RouteCoder{}
if err := decoded.Decode([]byte(input)); err != nil {
t.Fatalf("Decode failed: %v", err)
}
if decoded.Request.Name != "round-trip" {
t.Errorf("expected Name=round-trip, got %s", decoded.Request.Name)
}
if decoded.Request.Kind != "HTTPRoute" {
t.Errorf("expected Kind=HTTPRoute, got %s", decoded.Request.Kind)
}
if len(decoded.Request.ParentRefs) != 1 || decoded.Request.ParentRefs[0].Port != 443 {
t.Error("ParentRefs mismatch after round trip")
}
if len(decoded.Request.Rules) != 1 {
t.Fatal("expected 1 Rule after round trip")
}
if _, ok := decoded.Request.Rules[0]["backendRefs"]; !ok {
t.Error("expected backendRefs in rule after round trip")
}
_ = yamlData // yamlData is valid YAML output, verified above
}