Skip to content

Commit 7e5cbbe

Browse files
committed
Add unit tests for HTTPRoute
Signed-off-by: Sheng Lin <shelin@nvidia.com>
1 parent c6b9347 commit 7e5cbbe

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

api/apps/v1alpha1/nimservice_types_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"testing"
2222

2323
corev1 "k8s.io/api/core/v1"
24+
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
2425
)
2526

2627
// TestGetVolumes tests the GetVolumes function.
@@ -113,5 +114,91 @@ func TestGetVolumes(t *testing.T) {
113114
}
114115
})
115116
}
117+
}
118+
119+
func TestHTTpRoute(t *testing.T) {
120+
enabled := true
121+
prefixTypeMatch := gatewayv1.PathMatchPathPrefix
122+
root := "/"
123+
var port gatewayv1.PortNumber = DefaultAPIPort
116124

125+
tests := []struct {
126+
name string
127+
nimSpec *HTTPRoute
128+
desiredGWSpec gatewayv1.HTTPRouteSpec
129+
}{
130+
{
131+
name: "should return empty gatewayv1.HTTPRouteSpec if HTTPRouteSpec is nil",
132+
nimSpec: &HTTPRoute{
133+
Enabled: &enabled,
134+
Spec: nil,
135+
},
136+
desiredGWSpec: gatewayv1.HTTPRouteSpec{},
137+
},
138+
{
139+
name: "should correctly translate to gatewayv1.HTTPRouteSpec",
140+
nimSpec: &HTTPRoute{
141+
Enabled: &enabled,
142+
Spec: &HTTPRouteSpec{
143+
CommonRouteSpec: gatewayv1.CommonRouteSpec{
144+
ParentRefs: []gatewayv1.ParentReference{
145+
{
146+
Name: "istio-gateway",
147+
},
148+
},
149+
},
150+
Host: "foobar.nim",
151+
Paths: []HTTPPathMatch{
152+
{
153+
Type: &prefixTypeMatch,
154+
Value: &root,
155+
},
156+
},
157+
},
158+
},
159+
desiredGWSpec: gatewayv1.HTTPRouteSpec{
160+
CommonRouteSpec: gatewayv1.CommonRouteSpec{
161+
ParentRefs: []gatewayv1.ParentReference{
162+
{
163+
Name: "istio-gateway",
164+
},
165+
},
166+
},
167+
Hostnames: []gatewayv1.Hostname{
168+
"foobar.nim",
169+
},
170+
Rules: []gatewayv1.HTTPRouteRule{
171+
{
172+
Matches: []gatewayv1.HTTPRouteMatch{
173+
{
174+
Path: &gatewayv1.HTTPPathMatch{
175+
Type: &prefixTypeMatch,
176+
Value: &root,
177+
},
178+
},
179+
},
180+
BackendRefs: []gatewayv1.HTTPBackendRef{
181+
{
182+
BackendRef: gatewayv1.BackendRef{
183+
BackendObjectReference: gatewayv1.BackendObjectReference{
184+
Name: "test",
185+
Port: &port,
186+
},
187+
},
188+
},
189+
},
190+
},
191+
},
192+
},
193+
},
194+
}
195+
196+
for _, tt := range tests {
197+
t.Run(tt.name, func(t *testing.T) {
198+
gwSpec := tt.nimSpec.GenerateGatewayHTTPRouteSpec("test")
199+
if !reflect.DeepEqual(gwSpec, tt.desiredGWSpec) {
200+
t.Errorf("GenerateGatewayHTTPRouteSpec() = %+v, want %+v", gwSpec, tt.desiredGWSpec)
201+
}
202+
})
203+
}
117204
}

internal/render/render_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333

3434
corev1 "k8s.io/api/core/v1"
3535

36+
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
37+
3638
"github.com/NVIDIA/k8s-nim-operator/internal/render"
3739
"github.com/NVIDIA/k8s-nim-operator/internal/render/types"
3840
)
@@ -500,6 +502,45 @@ var _ = Describe("K8s Resources Rendering", func() {
500502
Expect(ingress.Namespace).To(Equal("default"))
501503
})
502504

505+
It("should render HTTPRoute template correctly", func() {
506+
params := types.HTTPRouteParams{
507+
Enabled: true,
508+
Name: "test-httproute",
509+
Namespace: "default",
510+
Spec: gatewayv1.HTTPRouteSpec{
511+
CommonRouteSpec: gatewayv1.CommonRouteSpec{
512+
ParentRefs: []gatewayv1.ParentReference{},
513+
},
514+
Hostnames: []gatewayv1.Hostname{
515+
"host.name",
516+
},
517+
Rules: []gatewayv1.HTTPRouteRule{
518+
{
519+
Matches: []gatewayv1.HTTPRouteMatch{
520+
{
521+
Path: &gatewayv1.HTTPPathMatch{},
522+
},
523+
},
524+
BackendRefs: []gatewayv1.HTTPBackendRef{
525+
{
526+
BackendRef: gatewayv1.BackendRef{
527+
BackendObjectReference: gatewayv1.BackendObjectReference{
528+
Name: "foobar-service",
529+
},
530+
},
531+
},
532+
},
533+
},
534+
},
535+
},
536+
}
537+
r := render.NewRenderer(templatesDir)
538+
httpRoute, err := r.HTTPRoute(&params)
539+
Expect(err).NotTo(HaveOccurred())
540+
Expect(httpRoute.Name).To(Equal("test-httproute"))
541+
Expect(httpRoute.Namespace).To(Equal("default"))
542+
})
543+
503544
It("should render HPA template correctly", func() {
504545
minRep := int32(1)
505546
params := types.HPAParams{

0 commit comments

Comments
 (0)