@@ -2,14 +2,15 @@ package routeutils
22
33import (
44 "context"
5+ "testing"
6+
57 "github.com/go-logr/logr"
68 "github.com/pkg/errors"
79 "github.com/stretchr/testify/assert"
810 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
911 "k8s.io/apimachinery/pkg/types"
1012 "k8s.io/apimachinery/pkg/util/sets"
1113 gwv1 "sigs.k8s.io/gateway-api/apis/v1"
12- "testing"
1314)
1415
1516type mockNamespaceSelector struct {
@@ -547,6 +548,112 @@ func Test_hostnameCheck(t *testing.T) {
547548 }
548549}
549550
551+ func Test_hostnameIntersection (t * testing.T ) {
552+ tests := []struct {
553+ name string
554+ listenerHostname * gwv1.Hostname
555+ routeHostnames []gwv1.Hostname
556+ expectedAttachment bool
557+ expectedCompatibleHostnames []gwv1.Hostname
558+ expectEmpty bool
559+ }{
560+ {
561+ name : "Scenario 1: Route has NO hostnames - inherits listener hostname" ,
562+ listenerHostname : ptr (gwv1 .Hostname ("bar.com" )),
563+ routeHostnames : []gwv1.Hostname {},
564+ expectedAttachment : true ,
565+ expectedCompatibleHostnames : []gwv1.Hostname {"bar.com" },
566+ },
567+ {
568+ name : "Scenario 2: Listener has NO hostname" ,
569+ listenerHostname : nil ,
570+ routeHostnames : []gwv1.Hostname {"foo.com" },
571+ expectedAttachment : true ,
572+ expectEmpty : true ,
573+ },
574+ {
575+ name : "Scenario 3: Both have NO hostnames" ,
576+ listenerHostname : nil ,
577+ routeHostnames : []gwv1.Hostname {},
578+ expectedAttachment : true ,
579+ expectEmpty : true ,
580+ },
581+ {
582+ name : "Scenario 4: Exact match" ,
583+ listenerHostname : ptr (gwv1 .Hostname ("bar.com" )),
584+ routeHostnames : []gwv1.Hostname {"bar.com" },
585+ expectedAttachment : true ,
586+ expectedCompatibleHostnames : []gwv1.Hostname {"bar.com" },
587+ },
588+ {
589+ name : "Scenario 5: Listener wildcard matches route" ,
590+ listenerHostname : ptr (gwv1 .Hostname ("*.bar.com" )),
591+ routeHostnames : []gwv1.Hostname {"foo.bar.com" },
592+ expectedAttachment : true ,
593+ expectedCompatibleHostnames : []gwv1.Hostname {"foo.bar.com" },
594+ },
595+ {
596+ name : "Scenario 6: Route wildcard matches listener" ,
597+ listenerHostname : ptr (gwv1 .Hostname ("foo.bar.com" )),
598+ routeHostnames : []gwv1.Hostname {"*.bar.com" },
599+ expectedAttachment : true ,
600+ expectedCompatibleHostnames : []gwv1.Hostname {"foo.bar.com" },
601+ },
602+ {
603+ name : "Scenario 7: Both wildcards, compatible" ,
604+ listenerHostname : ptr (gwv1 .Hostname ("*.bar.com" )),
605+ routeHostnames : []gwv1.Hostname {"*.bar.com" },
606+ expectedAttachment : true ,
607+ expectedCompatibleHostnames : []gwv1.Hostname {"*.bar.com" },
608+ },
609+ {
610+ name : "Scenario 8: No overlap - rejected" ,
611+ listenerHostname : ptr (gwv1 .Hostname ("bar.com" )),
612+ routeHostnames : []gwv1.Hostname {"foo.com" },
613+ expectedAttachment : false ,
614+ expectEmpty : true ,
615+ },
616+ {
617+ name : "Scenario 9: Multiple route hostnames, partial match" ,
618+ listenerHostname : ptr (gwv1 .Hostname ("*.bar.com" )),
619+ routeHostnames : []gwv1.Hostname {"foo.bar.com" , "baz.bar.com" , "unrelated.com" },
620+ expectedAttachment : true ,
621+ expectedCompatibleHostnames : []gwv1.Hostname {"foo.bar.com" , "baz.bar.com" },
622+ },
623+ }
624+
625+ for _ , tt := range tests {
626+ t .Run (tt .name , func (t * testing.T ) {
627+ helper := & listenerAttachmentHelperImpl {
628+ logger : logr .Discard (),
629+ }
630+
631+ listener := gwv1.Listener {
632+ Hostname : tt .listenerHostname ,
633+ }
634+
635+ route := & mockRoute {
636+ hostnames : tt .routeHostnames ,
637+ }
638+
639+ result , err := helper .hostnameCheck (listener , route )
640+
641+ assert .NoError (t , err )
642+ assert .Equal (t , tt .expectedAttachment , result )
643+
644+ if tt .expectEmpty {
645+ assert .Empty (t , route .GetCompatibleHostnames ())
646+ } else {
647+ assert .Equal (t , tt .expectedCompatibleHostnames , route .GetCompatibleHostnames ())
648+ }
649+ })
650+ }
651+ }
652+
653+ func ptr [T any ](v T ) * T {
654+ return & v
655+ }
656+
550657func Test_crossServingHostnameUniquenessCheck (t * testing.T ) {
551658 hostnames := []gwv1.Hostname {"example.com" }
552659 namespace := "test-namespace"
0 commit comments