@@ -332,3 +332,133 @@ func newCommonCols(t test.Failer, initObjs ...client.Object) *collections.Common
332332 gateways .Gateways .WaitUntilSynced (ctx .Done ())
333333 return commonCols
334334}
335+
336+ func TestExtractLoadBalancerIP (t * testing.T ) {
337+ tests := []struct {
338+ name string
339+ addresses []gwv1.GatewaySpecAddress
340+ want * string
341+ }{
342+ {
343+ name : "single valid IPv4 address" ,
344+ addresses : []gwv1.GatewaySpecAddress {
345+ {Type : ptr .To (gwv1 .IPAddressType ), Value : "203.0.113.10" },
346+ },
347+ want : ptr .To ("203.0.113.10" ),
348+ },
349+ {
350+ name : "single valid IPv6 address" ,
351+ addresses : []gwv1.GatewaySpecAddress {
352+ {Type : ptr .To (gwv1 .IPAddressType ), Value : "2001:db8::1" },
353+ },
354+ want : ptr .To ("2001:db8::1" ),
355+ },
356+ {
357+ name : "nil address type defaults to IPAddressType" ,
358+ addresses : []gwv1.GatewaySpecAddress {
359+ {Type : nil , Value : "192.0.2.1" },
360+ },
361+ want : ptr .To ("192.0.2.1" ),
362+ },
363+ {
364+ name : "empty addresses array" ,
365+ addresses : []gwv1.GatewaySpecAddress {},
366+ want : nil ,
367+ },
368+ {
369+ name : "multiple valid IP addresses uses first one" ,
370+ addresses : []gwv1.GatewaySpecAddress {
371+ {Type : ptr .To (gwv1 .IPAddressType ), Value : "203.0.113.10" },
372+ {Type : ptr .To (gwv1 .IPAddressType ), Value : "203.0.113.11" },
373+ },
374+ want : ptr .To ("203.0.113.10" ),
375+ },
376+ {
377+ name : "multiple IP addresses with invalid format skips invalid and uses first valid" ,
378+ addresses : []gwv1.GatewaySpecAddress {
379+ {Type : ptr .To (gwv1 .IPAddressType ), Value : "invalid-ip" },
380+ {Type : ptr .To (gwv1 .IPAddressType ), Value : "203.0.113.10" },
381+ },
382+ want : ptr .To ("203.0.113.10" ),
383+ },
384+ {
385+ name : "mixed address types uses first IP address" ,
386+ addresses : []gwv1.GatewaySpecAddress {
387+ {Type : ptr .To (gwv1 .HostnameAddressType ), Value : "example.com" },
388+ {Type : ptr .To (gwv1 .IPAddressType ), Value : "203.0.113.10" },
389+ },
390+ want : ptr .To ("203.0.113.10" ),
391+ },
392+ {
393+ name : "all hostname addresses returns nil" ,
394+ addresses : []gwv1.GatewaySpecAddress {
395+ {Type : ptr .To (gwv1 .HostnameAddressType ), Value : "example.com" },
396+ {Type : ptr .To (gwv1 .HostnameAddressType ), Value : "test.example.com" },
397+ },
398+ want : nil ,
399+ },
400+ {
401+ name : "all invalid IP addresses returns nil" ,
402+ addresses : []gwv1.GatewaySpecAddress {
403+ {Type : ptr .To (gwv1 .IPAddressType ), Value : "not-an-ip" },
404+ {Type : ptr .To (gwv1 .IPAddressType ), Value : "256.256.256.256" },
405+ },
406+ want : nil ,
407+ },
408+ {
409+ name : "nil type with invalid IP skips and continues" ,
410+ addresses : []gwv1.GatewaySpecAddress {
411+ {Type : nil , Value : "invalid" },
412+ {Type : nil , Value : "203.0.113.10" },
413+ },
414+ want : ptr .To ("203.0.113.10" ),
415+ },
416+ {
417+ name : "IPv4 and IPv6 mixed uses first valid" ,
418+ addresses : []gwv1.GatewaySpecAddress {
419+ {Type : ptr .To (gwv1 .IPAddressType ), Value : "203.0.113.10" },
420+ {Type : ptr .To (gwv1 .IPAddressType ), Value : "2001:db8::1" },
421+ },
422+ want : ptr .To ("203.0.113.10" ),
423+ },
424+ {
425+ name : "IPv6 first in multiple addresses" ,
426+ addresses : []gwv1.GatewaySpecAddress {
427+ {Type : ptr .To (gwv1 .IPAddressType ), Value : "2001:db8::1" },
428+ {Type : ptr .To (gwv1 .IPAddressType ), Value : "203.0.113.10" },
429+ },
430+ want : ptr .To ("2001:db8::1" ),
431+ },
432+ {
433+ name : "hostname before IP address skips hostname" ,
434+ addresses : []gwv1.GatewaySpecAddress {
435+ {Type : ptr .To (gwv1 .HostnameAddressType ), Value : "example.com" },
436+ {Type : ptr .To (gwv1 .IPAddressType ), Value : "203.0.113.10" },
437+ {Type : ptr .To (gwv1 .IPAddressType ), Value : "203.0.113.11" },
438+ },
439+ want : ptr .To ("203.0.113.10" ),
440+ },
441+ }
442+
443+ for _ , tt := range tests {
444+ t .Run (tt .name , func (t * testing.T ) {
445+ gw := & gwv1.Gateway {
446+ ObjectMeta : metav1.ObjectMeta {
447+ Name : "test-gateway" ,
448+ Namespace : "default" ,
449+ },
450+ Spec : gwv1.GatewaySpec {
451+ Addresses : tt .addresses ,
452+ },
453+ }
454+
455+ got := extractLoadBalancerIP (gw )
456+ if tt .want == nil {
457+ assert .Nil (t , got , "expected nil but got %v" , got )
458+ } else {
459+ assert .NotNil (t , got , "expected non-nil result" )
460+ assert .Equal (t , * tt .want , * got , "IP address mismatch" )
461+ }
462+ })
463+ }
464+ }
0 commit comments