@@ -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}
0 commit comments