Skip to content

Commit 6ec2875

Browse files
committed
complete unit tests
1 parent 14c1151 commit 6ec2875

File tree

7 files changed

+347
-18
lines changed

7 files changed

+347
-18
lines changed

internal/mode/static/nginx/config/generator_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,13 @@ func TestGenerate(t *testing.T) {
6363
},
6464
StreamUpstreams: []dataplane.Upstream{
6565
{
66-
Name: "stream_up",
67-
Endpoints: make([]resolver.Endpoint, 1),
66+
Name: "stream_up",
67+
Endpoints: []resolver.Endpoint{
68+
{
69+
Address: "1.1.1.1",
70+
Port: 80,
71+
},
72+
},
6873
},
6974
},
7075
BackendGroups: []dataplane.BackendGroup{bg},

internal/mode/static/nginx/config/maps_test.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,22 @@ func TestExecuteStreamMaps(t *testing.T) {
217217
},
218218
StreamUpstreams: []dataplane.Upstream{
219219
{
220-
Name: "backend1",
221-
Endpoints: make([]resolver.Endpoint, 1),
220+
Name: "backend1",
221+
Endpoints: []resolver.Endpoint{
222+
{
223+
Address: "1.1.1.1",
224+
Port: 80,
225+
},
226+
},
222227
},
223228
{
224-
Name: "backend2",
225-
Endpoints: make([]resolver.Endpoint, 1),
229+
Name: "backend2",
230+
Endpoints: []resolver.Endpoint{
231+
{
232+
Address: "1.1.1.1",
233+
Port: 80,
234+
},
235+
},
226236
},
227237
},
228238
}
@@ -278,6 +288,11 @@ func TestCreateStreamMaps(t *testing.T) {
278288
Port: 8080,
279289
IsDefault: true,
280290
},
291+
{
292+
Hostname: "wrong2.example.com",
293+
Port: 8080,
294+
UpstreamName: "backend3",
295+
},
281296
},
282297
SSLServers: []dataplane.VirtualServer{
283298
{
@@ -291,12 +306,26 @@ func TestCreateStreamMaps(t *testing.T) {
291306
},
292307
StreamUpstreams: []dataplane.Upstream{
293308
{
294-
Name: "backend1",
295-
Endpoints: make([]resolver.Endpoint, 1),
309+
Name: "backend1",
310+
Endpoints: []resolver.Endpoint{
311+
{
312+
Address: "1.1.1.1",
313+
Port: 80,
314+
},
315+
},
316+
},
317+
{
318+
Name: "backend2",
319+
Endpoints: []resolver.Endpoint{
320+
{
321+
Address: "1.1.1.1",
322+
Port: 80,
323+
},
324+
},
296325
},
297326
{
298-
Name: "backend2",
299-
Endpoints: make([]resolver.Endpoint, 1),
327+
Name: "backend3",
328+
Endpoints: nil,
300329
},
301330
},
302331
}
@@ -329,6 +358,7 @@ func TestCreateStreamMaps(t *testing.T) {
329358
{Value: "cafe.example.com", Result: getSocketNameTLS(8080, "cafe.example.com")},
330359
{Value: "wrong.example.com", Result: `""`},
331360
{Value: "*.example.com", Result: connectionClosedStreamServerSocket},
361+
{Value: "wrong2.example.com", Result: `""`},
332362
{Value: "app.example.com", Result: getSocketNameHTTPS(8080)},
333363
{Value: "default", Result: getSocketNameHTTPS(8080)},
334364
},

internal/mode/static/nginx/config/stream_servers_test.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,22 @@ func TestExecuteStreamServers(t *testing.T) {
3333
},
3434
StreamUpstreams: []dataplane.Upstream{
3535
{
36-
Name: "backend1",
37-
Endpoints: make([]resolver.Endpoint, 1),
36+
Name: "backend1",
37+
Endpoints: []resolver.Endpoint{
38+
{
39+
Address: "1.1.1.1",
40+
Port: 80,
41+
},
42+
},
3843
},
3944
{
40-
Name: "backend2",
41-
Endpoints: make([]resolver.Endpoint, 1),
45+
Name: "backend2",
46+
Endpoints: []resolver.Endpoint{
47+
{
48+
Address: "1.1.1.1",
49+
Port: 80,
50+
},
51+
},
4252
},
4353
},
4454
}
@@ -87,12 +97,22 @@ func TestCreateStreamServers(t *testing.T) {
8797
},
8898
StreamUpstreams: []dataplane.Upstream{
8999
{
90-
Name: "backend1",
91-
Endpoints: make([]resolver.Endpoint, 1),
100+
Name: "backend1",
101+
Endpoints: []resolver.Endpoint{
102+
{
103+
Address: "1.1.1.1",
104+
Port: 80,
105+
},
106+
},
92107
},
93108
{
94-
Name: "backend2",
95-
Endpoints: make([]resolver.Endpoint, 1),
109+
Name: "backend2",
110+
Endpoints: []resolver.Endpoint{
111+
{
112+
Address: "1.1.1.1",
113+
Port: 80,
114+
},
115+
},
96116
},
97117
},
98118
}

internal/mode/static/nginx/config/upstreams_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,10 @@ func TestCreateStreamUpstreams(t *testing.T) {
372372
Address: "10.0.0.2",
373373
Port: 80,
374374
},
375+
{
376+
Address: "2001:db8::1",
377+
IPv6: true,
378+
},
375379
},
376380
},
377381
{
@@ -403,6 +407,9 @@ func TestCreateStreamUpstreams(t *testing.T) {
403407
{
404408
Address: "10.0.0.2:80",
405409
},
410+
{
411+
Address: "[2001:db8::1]:0",
412+
},
406413
},
407414
},
408415
{

internal/mode/static/state/graph/route_common_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,3 +2065,18 @@ func TestBuildL4RoutesForGateways(t *testing.T) {
20652065

20662066
g.Expect(buildL4RoutesForGateways(nil, nil, nil, nil)).To(BeNil())
20672067
}
2068+
2069+
func TestTryToAttachL4RouteToListeners(t *testing.T) {
2070+
g := NewWithT(t)
2071+
2072+
cond, attachable := tryToAttachL4RouteToListeners(
2073+
nil,
2074+
nil,
2075+
nil,
2076+
nil,
2077+
nil,
2078+
nil,
2079+
)
2080+
g.Expect(cond).To(Equal(staticConds.NewRouteInvalidListener()))
2081+
g.Expect(attachable).To(BeFalse())
2082+
}

internal/mode/static/status/prepare_requests_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ func TestBuildHTTPRouteStatuses(t *testing.T) {
222222
CommonRouteSpec: commonRouteSpecValid,
223223
},
224224
}
225+
225226
hrInvalid := &v1.HTTPRoute{
226227
ObjectMeta: metav1.ObjectMeta{
227228
Namespace: "test",
@@ -367,6 +368,82 @@ func TestBuildGRPCRouteStatuses(t *testing.T) {
367368
}
368369
}
369370

371+
func TestBuildTLSRouteStatuses(t *testing.T) {
372+
trValid := &v1alpha2.TLSRoute{
373+
ObjectMeta: metav1.ObjectMeta{
374+
Namespace: "test",
375+
Name: "tr-valid",
376+
Generation: 3,
377+
},
378+
Spec: v1alpha2.TLSRouteSpec{
379+
CommonRouteSpec: commonRouteSpecValid,
380+
},
381+
}
382+
trInvalid := &v1alpha2.TLSRoute{
383+
ObjectMeta: metav1.ObjectMeta{
384+
Namespace: "test",
385+
Name: "tr-invalid",
386+
Generation: 3,
387+
},
388+
Spec: v1alpha2.TLSRouteSpec{
389+
CommonRouteSpec: commonRouteSpecInvalid,
390+
},
391+
}
392+
routes := map[graph.L4RouteKey]*graph.L4Route{
393+
graph.CreateRouteKeyL4(trValid): {
394+
Valid: true,
395+
Source: trValid,
396+
ParentRefs: parentRefsValid,
397+
},
398+
graph.CreateRouteKeyL4(trInvalid): {
399+
Valid: false,
400+
Conditions: []conditions.Condition{invalidRouteCondition},
401+
Source: trInvalid,
402+
ParentRefs: parentRefsInvalid,
403+
},
404+
}
405+
406+
expectedStatuses := map[types.NamespacedName]v1alpha2.TLSRouteStatus{
407+
{Namespace: "test", Name: "tr-valid"}: {
408+
RouteStatus: routeStatusValid,
409+
},
410+
{Namespace: "test", Name: "tr-invalid"}: {
411+
RouteStatus: routeStatusInvalid,
412+
},
413+
}
414+
415+
g := NewWithT(t)
416+
417+
k8sClient := createK8sClientFor(&v1alpha2.TLSRoute{})
418+
419+
for _, r := range routes {
420+
err := k8sClient.Create(context.Background(), r.Source)
421+
g.Expect(err).ToNot(HaveOccurred())
422+
}
423+
424+
updater := statusFramework.NewUpdater(k8sClient, zap.New())
425+
426+
reqs := PrepareRouteRequests(
427+
routes,
428+
map[graph.RouteKey]*graph.L7Route{},
429+
transitionTime,
430+
NginxReloadResult{},
431+
gatewayCtlrName,
432+
)
433+
434+
updater.Update(context.Background(), reqs...)
435+
436+
g.Expect(reqs).To(HaveLen(len(expectedStatuses)))
437+
438+
for nsname, expected := range expectedStatuses {
439+
var hr v1alpha2.TLSRoute
440+
441+
err := k8sClient.Get(context.Background(), nsname, &hr)
442+
g.Expect(err).ToNot(HaveOccurred())
443+
g.Expect(helpers.Diff(expected, hr.Status)).To(BeEmpty())
444+
}
445+
}
446+
370447
func TestBuildRouteStatusesNginxErr(t *testing.T) {
371448
const gatewayCtlrName = "controller"
372449

0 commit comments

Comments
 (0)