Skip to content

Commit d75ab56

Browse files
committed
Add more unit tests.
Signed-off-by: Huang Xin <[email protected]>
1 parent 35c7329 commit d75ab56

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

apis/v1beta1/validation/gateway.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func ValidateListenerNames(listeners []gatewayv1b1.Listener, path *field.Path) f
136136
return errs
137137
}
138138

139-
// validateHostnameProtocolPort validates that the combination of port, protocol, and name are
139+
// validateHostnameProtocolPort validates that the combination of port, protocol, and hostname are
140140
// unique for each listener.
141141
func validateHostnameProtocolPort(listeners []gatewayv1b1.Listener, path *field.Path) field.ErrorList {
142142
var errs field.ErrorList
@@ -150,7 +150,7 @@ func validateHostnameProtocolPort(listeners []gatewayv1b1.Listener, path *field.
150150
port := listener.Port
151151
hostnameProtocolPort := fmt.Sprintf("%s:%s:%d", *hostname, protocol, port)
152152
if hostnameProtocolPortSets.Has(hostnameProtocolPort) {
153-
errs = append(errs, field.Forbidden(path.Index(i), fmt.Sprintln("combination of port, protocol, and name must be unique for each listener")))
153+
errs = append(errs, field.Forbidden(path.Index(i), fmt.Sprintln("combination of port, protocol, and hostname must be unique for each listener")))
154154
} else {
155155
hostnameProtocolPortSets.Insert(hostnameProtocolPort)
156156
}

apis/v1beta1/validation/gateway_test.go

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func TestValidateGateway(t *testing.T) {
149149
},
150150
expectErrsOnFields: []string{"spec.listeners[1].name"},
151151
},
152-
"combination of port, protocol, and name are not unique for each listener": {
152+
"combination of port, protocol, and hostname are not unique for each listener": {
153153
mutate: func(gw *gatewayv1b1.Gateway) {
154154
hostnameFoo := gatewayv1b1.Hostname("foo.com")
155155
gw.Spec.Listeners[0].Name = "foo"
@@ -167,6 +167,86 @@ func TestValidateGateway(t *testing.T) {
167167
},
168168
expectErrsOnFields: []string{"spec.listeners[1]"},
169169
},
170+
"combination of port and protocol are not unique for each listenr when hostnames not set": {
171+
mutate: func(gw *gatewayv1b1.Gateway) {
172+
gw.Spec.Listeners[0].Name = "foo"
173+
gw.Spec.Listeners[0].Protocol = gatewayv1b1.HTTPProtocolType
174+
gw.Spec.Listeners[0].Port = 80
175+
gw.Spec.Listeners = append(gw.Spec.Listeners,
176+
gatewayv1b1.Listener{
177+
Name: "bar",
178+
Protocol: gatewayv1b1.HTTPProtocolType,
179+
Port: 80,
180+
},
181+
)
182+
},
183+
expectErrsOnFields: []string{"spec.listeners[1]"},
184+
},
185+
"port is unique when protocol and hostname are the same": {
186+
mutate: func(gw *gatewayv1b1.Gateway) {
187+
hostnameFoo := gatewayv1b1.Hostname("foo.com")
188+
gw.Spec.Listeners[0].Name = "foo"
189+
gw.Spec.Listeners[0].Hostname = &hostnameFoo
190+
gw.Spec.Listeners[0].Protocol = gatewayv1b1.HTTPProtocolType
191+
gw.Spec.Listeners[0].Port = 80
192+
gw.Spec.Listeners = append(gw.Spec.Listeners,
193+
gatewayv1b1.Listener{
194+
Name: "bar",
195+
Hostname: &hostnameFoo,
196+
Protocol: gatewayv1b1.HTTPProtocolType,
197+
Port: 8080,
198+
},
199+
)
200+
},
201+
expectErrsOnFields: nil,
202+
},
203+
"hostname is unique when protoco and port are the same": {
204+
mutate: func(gw *gatewayv1b1.Gateway) {
205+
hostnameFoo := gatewayv1b1.Hostname("foo.com")
206+
hostnameBar := gatewayv1b1.Hostname("bar.com")
207+
gw.Spec.Listeners[0].Name = "foo"
208+
gw.Spec.Listeners[0].Hostname = &hostnameFoo
209+
gw.Spec.Listeners[0].Protocol = gatewayv1b1.HTTPProtocolType
210+
gw.Spec.Listeners[0].Port = 80
211+
gw.Spec.Listeners = append(gw.Spec.Listeners,
212+
gatewayv1b1.Listener{
213+
Name: "bar",
214+
Hostname: &hostnameBar,
215+
Protocol: gatewayv1b1.HTTPProtocolType,
216+
Port: 80,
217+
},
218+
)
219+
},
220+
expectErrsOnFields: nil,
221+
},
222+
"protocol is unique when port and hostname are the same": {
223+
mutate: func(gw *gatewayv1b1.Gateway) {
224+
hostnameFoo := gatewayv1b1.Hostname("foo.com")
225+
tlsConfigFoo := tlsConfig
226+
tlsModeFoo := gatewayv1b1.TLSModeType("Terminate")
227+
tlsConfigFoo.Mode = &tlsModeFoo
228+
tlsConfigFoo.CertificateRefs = []gatewayv1b1.SecretObjectReference{
229+
{
230+
Name: "FooCertificateRefs",
231+
},
232+
}
233+
gw.Spec.Listeners[0].Name = "foo"
234+
gw.Spec.Listeners[0].Hostname = &hostnameFoo
235+
gw.Spec.Listeners[0].Protocol = gatewayv1b1.HTTPSProtocolType
236+
gw.Spec.Listeners[0].Port = 8000
237+
gw.Spec.Listeners[0].TLS = &tlsConfigFoo
238+
gw.Spec.Listeners = append(gw.Spec.Listeners,
239+
gatewayv1b1.Listener{
240+
Name: "bar",
241+
Hostname: &hostnameFoo,
242+
Protocol: gatewayv1b1.TLSProtocolType,
243+
Port: 8000,
244+
TLS: &tlsConfigFoo,
245+
},
246+
)
247+
},
248+
expectErrsOnFields: nil,
249+
},
170250
}
171251

172252
for name, tc := range testCases {

0 commit comments

Comments
 (0)