Skip to content

Commit f4036eb

Browse files
committed
Add another test case
1 parent 4a606af commit f4036eb

File tree

1 file changed

+281
-0
lines changed

1 file changed

+281
-0
lines changed

pkg/reconciler/ingress/resources/httproute_test.go

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,287 @@ func TestMakeHTTPRoute(t *testing.T) {
627627
}
628628
}
629629

630+
func TestMakeHTTPRouteWithSectionName(t *testing.T) {
631+
for _, tc := range []struct {
632+
name string
633+
ing *v1alpha1.Ingress
634+
expected []*gatewayapi.HTTPRoute
635+
changeConfig func(gw *config.Config)
636+
}{
637+
{
638+
name: "single external domain with split and cluster local",
639+
ing: &v1alpha1.Ingress{
640+
ObjectMeta: metav1.ObjectMeta{
641+
Name: testIngressName,
642+
Namespace: testNamespace,
643+
Labels: map[string]string{
644+
networking.IngressLabelKey: testIngressName,
645+
},
646+
},
647+
Spec: v1alpha1.IngressSpec{
648+
Rules: []v1alpha1.IngressRule{
649+
{
650+
Hosts: testHosts,
651+
Visibility: v1alpha1.IngressVisibilityExternalIP,
652+
HTTP: &v1alpha1.HTTPIngressRuleValue{
653+
Paths: []v1alpha1.HTTPIngressPath{{
654+
AppendHeaders: map[string]string{
655+
"Foo": "bar",
656+
},
657+
Splits: []v1alpha1.IngressBackendSplit{{
658+
IngressBackend: v1alpha1.IngressBackend{
659+
ServiceName: "goo",
660+
ServicePort: intstr.FromInt(123),
661+
},
662+
Percent: 12,
663+
AppendHeaders: map[string]string{
664+
"Baz": "blah",
665+
"Bleep": "bloop",
666+
},
667+
}, {
668+
IngressBackend: v1alpha1.IngressBackend{
669+
ServiceName: "doo",
670+
ServicePort: intstr.FromInt(124),
671+
},
672+
Percent: 88,
673+
AppendHeaders: map[string]string{
674+
"Baz": "blurg",
675+
},
676+
}},
677+
}},
678+
},
679+
}, {
680+
Hosts: testLocalHosts,
681+
Visibility: v1alpha1.IngressVisibilityClusterLocal,
682+
HTTP: &v1alpha1.HTTPIngressRuleValue{
683+
Paths: []v1alpha1.HTTPIngressPath{{
684+
AppendHeaders: map[string]string{
685+
"Foo": "bar",
686+
},
687+
Splits: []v1alpha1.IngressBackendSplit{{
688+
IngressBackend: v1alpha1.IngressBackend{
689+
ServiceName: "goo",
690+
ServicePort: intstr.FromInt(123),
691+
},
692+
Percent: 12,
693+
AppendHeaders: map[string]string{
694+
"Bleep": "bloop",
695+
"Baz": "blah",
696+
},
697+
}, {
698+
IngressBackend: v1alpha1.IngressBackend{
699+
ServiceName: "doo",
700+
ServicePort: intstr.FromInt(124),
701+
},
702+
Percent: 88,
703+
AppendHeaders: map[string]string{
704+
"Baz": "blurg",
705+
},
706+
}},
707+
}},
708+
},
709+
},
710+
}},
711+
},
712+
expected: []*gatewayapi.HTTPRoute{
713+
{
714+
ObjectMeta: metav1.ObjectMeta{
715+
Name: LongestHost(testHosts),
716+
Namespace: testNamespace,
717+
Labels: map[string]string{
718+
networking.IngressLabelKey: testIngressName,
719+
"networking.knative.dev/visibility": "",
720+
},
721+
Annotations: map[string]string{},
722+
},
723+
Spec: gatewayapi.HTTPRouteSpec{
724+
Hostnames: []gatewayapi.Hostname{externalHost},
725+
Rules: []gatewayapi.HTTPRouteRule{{
726+
BackendRefs: []gatewayapi.HTTPBackendRef{{
727+
BackendRef: gatewayapi.BackendRef{
728+
BackendObjectReference: gatewayapi.BackendObjectReference{
729+
Group: (*gatewayapi.Group)(ptr.To("")),
730+
Kind: (*gatewayapi.Kind)(ptr.To("Service")),
731+
Name: gatewayapi.ObjectName("goo"),
732+
Port: ptr.To[gatewayapi.PortNumber](123),
733+
},
734+
Weight: ptr.To(int32(12)),
735+
},
736+
Filters: []gatewayapi.HTTPRouteFilter{{
737+
Type: gatewayapi.HTTPRouteFilterRequestHeaderModifier,
738+
RequestHeaderModifier: &gatewayapi.HTTPHeaderFilter{
739+
Set: []gatewayapi.HTTPHeader{
740+
{
741+
Name: "Baz",
742+
Value: "blah",
743+
},
744+
{
745+
Name: "Bleep",
746+
Value: "bloop",
747+
},
748+
}}}},
749+
}, {
750+
BackendRef: gatewayapi.BackendRef{
751+
BackendObjectReference: gatewayapi.BackendObjectReference{
752+
Group: (*gatewayapi.Group)(ptr.To("")),
753+
Kind: (*gatewayapi.Kind)(ptr.To("Service")),
754+
Port: ptr.To[gatewayapi.PortNumber](124),
755+
Name: gatewayapi.ObjectName("doo"),
756+
},
757+
Weight: ptr.To(int32(88)),
758+
},
759+
Filters: []gatewayapi.HTTPRouteFilter{{
760+
Type: gatewayapi.HTTPRouteFilterRequestHeaderModifier,
761+
RequestHeaderModifier: &gatewayapi.HTTPHeaderFilter{
762+
Set: []gatewayapi.HTTPHeader{
763+
{
764+
Name: "Baz",
765+
Value: "blurg",
766+
},
767+
},
768+
}}},
769+
}},
770+
Filters: []gatewayapi.HTTPRouteFilter{{
771+
Type: gatewayapi.HTTPRouteFilterRequestHeaderModifier,
772+
RequestHeaderModifier: &gatewayapi.HTTPHeaderFilter{
773+
Set: []gatewayapi.HTTPHeader{
774+
{
775+
Name: "Foo",
776+
Value: "bar",
777+
},
778+
},
779+
}}},
780+
Matches: []gatewayapi.HTTPRouteMatch{
781+
{
782+
Path: &gatewayapi.HTTPPathMatch{
783+
Type: ptr.To(gatewayapi.PathMatchPathPrefix),
784+
Value: ptr.To("/"),
785+
},
786+
},
787+
},
788+
}},
789+
CommonRouteSpec: gatewayapi.CommonRouteSpec{
790+
ParentRefs: []gatewayapi.ParentReference{{
791+
Group: (*gatewayapi.Group)(ptr.To("gateway.networking.k8s.io")),
792+
Kind: (*gatewayapi.Kind)(ptr.To("Gateway")),
793+
Namespace: ptr.To[gatewayapi.Namespace]("test-ns"),
794+
Name: gatewayapi.ObjectName("foo"),
795+
SectionName: ptr.To[gatewayapi.SectionName]("http"),
796+
}},
797+
},
798+
},
799+
}, {
800+
ObjectMeta: metav1.ObjectMeta{
801+
Name: LongestHost(testLocalHosts),
802+
Namespace: testNamespace,
803+
Labels: map[string]string{
804+
networking.IngressLabelKey: testIngressName,
805+
"networking.knative.dev/visibility": "cluster-local",
806+
},
807+
Annotations: map[string]string{},
808+
},
809+
Spec: gatewayapi.HTTPRouteSpec{
810+
Hostnames: []gatewayapi.Hostname{localHostShortest, localHostShort, localHostFull},
811+
Rules: []gatewayapi.HTTPRouteRule{{
812+
BackendRefs: []gatewayapi.HTTPBackendRef{{
813+
BackendRef: gatewayapi.BackendRef{
814+
BackendObjectReference: gatewayapi.BackendObjectReference{
815+
Group: (*gatewayapi.Group)(ptr.To("")),
816+
Kind: (*gatewayapi.Kind)(ptr.To("Service")),
817+
Port: ptr.To[gatewayapi.PortNumber](123),
818+
Name: gatewayapi.ObjectName("goo"),
819+
},
820+
Weight: ptr.To(int32(12)),
821+
},
822+
Filters: []gatewayapi.HTTPRouteFilter{{
823+
Type: gatewayapi.HTTPRouteFilterRequestHeaderModifier,
824+
RequestHeaderModifier: &gatewayapi.HTTPHeaderFilter{
825+
Set: []gatewayapi.HTTPHeader{
826+
{
827+
Name: "Baz",
828+
Value: "blah",
829+
},
830+
{
831+
Name: "Bleep",
832+
Value: "bloop",
833+
},
834+
}}}},
835+
}, {
836+
BackendRef: gatewayapi.BackendRef{
837+
BackendObjectReference: gatewayapi.BackendObjectReference{
838+
Group: (*gatewayapi.Group)(ptr.To("")),
839+
Kind: (*gatewayapi.Kind)(ptr.To("Service")),
840+
Port: ptr.To[gatewayapi.PortNumber](124),
841+
Name: gatewayapi.ObjectName("doo"),
842+
},
843+
Weight: ptr.To(int32(88)),
844+
},
845+
Filters: []gatewayapi.HTTPRouteFilter{{
846+
Type: gatewayapi.HTTPRouteFilterRequestHeaderModifier,
847+
RequestHeaderModifier: &gatewayapi.HTTPHeaderFilter{
848+
Set: []gatewayapi.HTTPHeader{
849+
{
850+
Name: "Baz",
851+
Value: "blurg",
852+
},
853+
},
854+
}}},
855+
}},
856+
Filters: []gatewayapi.HTTPRouteFilter{{
857+
Type: gatewayapi.HTTPRouteFilterRequestHeaderModifier,
858+
RequestHeaderModifier: &gatewayapi.HTTPHeaderFilter{
859+
Set: []gatewayapi.HTTPHeader{
860+
{
861+
Name: "Foo",
862+
Value: "bar",
863+
},
864+
},
865+
}}},
866+
Matches: []gatewayapi.HTTPRouteMatch{{
867+
Path: &gatewayapi.HTTPPathMatch{
868+
Type: ptr.To(gatewayapi.PathMatchPathPrefix),
869+
Value: ptr.To("/"),
870+
},
871+
}},
872+
}},
873+
CommonRouteSpec: gatewayapi.CommonRouteSpec{
874+
ParentRefs: []gatewayapi.ParentReference{{
875+
Group: (*gatewayapi.Group)(ptr.To("gateway.networking.k8s.io")),
876+
Kind: (*gatewayapi.Kind)(ptr.To("Gateway")),
877+
Namespace: ptr.To[gatewayapi.Namespace]("test-ns"),
878+
Name: gatewayapi.ObjectName("foo-local"),
879+
SectionName: ptr.To[gatewayapi.SectionName]("http"),
880+
}},
881+
},
882+
},
883+
},
884+
},
885+
}} {
886+
t.Run(tc.name, func(t *testing.T) {
887+
for i, rule := range tc.ing.Spec.Rules {
888+
rule := rule
889+
cfg := testConfig.DeepCopy()
890+
if tc.changeConfig != nil {
891+
tc.changeConfig(cfg)
892+
893+
fmt.Printf("%#v", cfg.GatewayPlugin.ExternalGateways)
894+
}
895+
tcs := &testConfigStore{config: cfg}
896+
ctx := tcs.ToContext(context.Background())
897+
898+
route, err := MakeHTTPRoute(ctx, tc.ing, &rule, ptr.To[gatewayapi.SectionName]("http"))
899+
if err != nil {
900+
t.Fatal("MakeHTTPRoute failed:", err)
901+
}
902+
tc.expected[i].OwnerReferences = []metav1.OwnerReference{*kmeta.NewControllerRef(tc.ing)}
903+
if diff := cmp.Diff(tc.expected[i], route); diff != "" {
904+
t.Error("Unexpected HTTPRoute (-want +got):", diff)
905+
}
906+
}
907+
})
908+
}
909+
}
910+
630911
func TestAddEndpointProbes(t *testing.T) {
631912
tcs := &testConfigStore{config: testConfig}
632913
ctx := tcs.ToContext(context.Background())

0 commit comments

Comments
 (0)