2024-05-28 12:30:04 +00:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"k8s.io/utils/ptr"
|
|
|
|
gatev1 "sigs.k8s.io/gateway-api/apis/v1"
|
|
|
|
)
|
|
|
|
|
2024-05-30 07:14:04 +00:00
|
|
|
func Test_buildHostRule(t *testing.T) {
|
2024-05-28 12:30:04 +00:00
|
|
|
testCases := []struct {
|
2024-05-30 07:14:04 +00:00
|
|
|
desc string
|
|
|
|
hostnames []gatev1.Hostname
|
|
|
|
expectedRule string
|
|
|
|
expectedPriority int
|
|
|
|
expectErr bool
|
2024-05-28 12:30:04 +00:00
|
|
|
}{
|
|
|
|
{
|
2024-05-30 07:14:04 +00:00
|
|
|
desc: "Empty (should not happen)",
|
2024-05-28 12:30:04 +00:00
|
|
|
expectedRule: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "One Host",
|
|
|
|
hostnames: []gatev1.Hostname{
|
|
|
|
"Foo",
|
|
|
|
},
|
2024-05-30 07:14:04 +00:00
|
|
|
expectedRule: "Host(`Foo`)",
|
|
|
|
expectedPriority: 3,
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Multiple Hosts",
|
|
|
|
hostnames: []gatev1.Hostname{
|
|
|
|
"Foo",
|
|
|
|
"Bar",
|
|
|
|
"Bir",
|
|
|
|
},
|
2024-05-30 07:14:04 +00:00
|
|
|
expectedRule: "(Host(`Foo`) || Host(`Bar`) || Host(`Bir`))",
|
|
|
|
expectedPriority: 3,
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Several Host and wildcard",
|
|
|
|
hostnames: []gatev1.Hostname{
|
|
|
|
"*.bar.foo",
|
|
|
|
"bar.foo",
|
|
|
|
"foo.foo",
|
|
|
|
},
|
2024-05-30 07:14:04 +00:00
|
|
|
expectedRule: "(HostRegexp(`^[a-z0-9-\\.]+\\.bar\\.foo$`) || Host(`bar.foo`) || Host(`foo.foo`))",
|
|
|
|
expectedPriority: 9,
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Host with wildcard",
|
|
|
|
hostnames: []gatev1.Hostname{
|
|
|
|
"*.bar.foo",
|
|
|
|
},
|
2024-05-30 07:14:04 +00:00
|
|
|
expectedRule: "HostRegexp(`^[a-z0-9-\\.]+\\.bar\\.foo$`)",
|
|
|
|
expectedPriority: 9,
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-05-30 07:14:04 +00:00
|
|
|
rule, priority := buildHostRule(test.hostnames)
|
2024-05-28 12:30:04 +00:00
|
|
|
assert.Equal(t, test.expectedRule, rule)
|
2024-05-30 07:14:04 +00:00
|
|
|
assert.Equal(t, test.expectedPriority, priority)
|
2024-05-28 12:30:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-13 09:16:04 +00:00
|
|
|
func Test_buildMatchRule(t *testing.T) {
|
2024-05-28 12:30:04 +00:00
|
|
|
testCases := []struct {
|
2024-05-30 07:14:04 +00:00
|
|
|
desc string
|
2024-08-30 08:36:06 +00:00
|
|
|
match gatev1.HTTPRouteMatch
|
2024-05-30 07:14:04 +00:00
|
|
|
hostnames []gatev1.Hostname
|
|
|
|
expectedRule string
|
|
|
|
expectedPriority int
|
|
|
|
expectedError bool
|
2024-05-28 12:30:04 +00:00
|
|
|
}{
|
|
|
|
{
|
2024-08-30 08:36:06 +00:00
|
|
|
desc: "Empty rule and matches",
|
2024-05-30 07:14:04 +00:00
|
|
|
expectedRule: "PathPrefix(`/`)",
|
|
|
|
expectedPriority: 1,
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-13 09:16:04 +00:00
|
|
|
desc: "One Host rule without match",
|
2024-05-30 07:14:04 +00:00
|
|
|
hostnames: []gatev1.Hostname{"foo.com"},
|
2024-06-13 09:16:04 +00:00
|
|
|
expectedRule: "Host(`foo.com`) && PathPrefix(`/`)",
|
|
|
|
expectedPriority: 8,
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "One HTTPRouteMatch with nil HTTPHeaderMatch",
|
2024-08-30 08:36:06 +00:00
|
|
|
match: gatev1.HTTPRouteMatch{
|
2024-06-13 09:16:04 +00:00
|
|
|
Path: ptr.To(gatev1.HTTPPathMatch{
|
|
|
|
Type: ptr.To(gatev1.PathMatchPathPrefix),
|
|
|
|
Value: ptr.To("/"),
|
|
|
|
}),
|
|
|
|
Headers: nil,
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
2024-05-30 07:14:04 +00:00
|
|
|
expectedRule: "PathPrefix(`/`)",
|
|
|
|
expectedPriority: 1,
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "One HTTPRouteMatch with nil HTTPHeaderMatch Type",
|
2024-08-30 08:36:06 +00:00
|
|
|
match: gatev1.HTTPRouteMatch{
|
2024-06-13 09:16:04 +00:00
|
|
|
Path: ptr.To(gatev1.HTTPPathMatch{
|
|
|
|
Type: ptr.To(gatev1.PathMatchPathPrefix),
|
|
|
|
Value: ptr.To("/"),
|
|
|
|
}),
|
|
|
|
Headers: []gatev1.HTTPHeaderMatch{
|
|
|
|
{Name: "foo", Value: "bar"},
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
},
|
2024-05-30 07:14:04 +00:00
|
|
|
expectedRule: "PathPrefix(`/`) && Header(`foo`,`bar`)",
|
2024-06-13 09:16:04 +00:00
|
|
|
expectedPriority: 101,
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-13 09:16:04 +00:00
|
|
|
desc: "One HTTPRouteMatch with nil HTTPPathMatch",
|
2024-08-30 08:36:06 +00:00
|
|
|
match: gatev1.HTTPRouteMatch{Path: nil},
|
2024-05-30 07:14:04 +00:00
|
|
|
expectedRule: "PathPrefix(`/`)",
|
|
|
|
expectedPriority: 1,
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "One HTTPRouteMatch with nil HTTPPathMatch Type",
|
2024-08-30 08:36:06 +00:00
|
|
|
match: gatev1.HTTPRouteMatch{
|
2024-06-13 09:16:04 +00:00
|
|
|
Path: &gatev1.HTTPPathMatch{
|
|
|
|
Type: nil,
|
|
|
|
Value: ptr.To("/foo/"),
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
},
|
2024-05-30 07:14:04 +00:00
|
|
|
expectedRule: "(Path(`/foo`) || PathPrefix(`/foo/`))",
|
2024-06-13 09:16:04 +00:00
|
|
|
expectedPriority: 10500,
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "One HTTPRouteMatch with nil HTTPPathMatch Values",
|
2024-08-30 08:36:06 +00:00
|
|
|
match: gatev1.HTTPRouteMatch{
|
2024-06-13 09:16:04 +00:00
|
|
|
Path: &gatev1.HTTPPathMatch{
|
|
|
|
Type: ptr.To(gatev1.PathMatchExact),
|
|
|
|
Value: nil,
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
},
|
2024-05-30 07:14:04 +00:00
|
|
|
expectedRule: "Path(`/`)",
|
2024-06-13 09:16:04 +00:00
|
|
|
expectedPriority: 100000,
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-13 09:16:04 +00:00
|
|
|
desc: "One Path",
|
2024-08-30 08:36:06 +00:00
|
|
|
match: gatev1.HTTPRouteMatch{
|
2024-06-13 09:16:04 +00:00
|
|
|
Path: &gatev1.HTTPPathMatch{
|
|
|
|
Type: ptr.To(gatev1.PathMatchExact),
|
|
|
|
Value: ptr.To("/foo/"),
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
},
|
2024-05-30 07:14:04 +00:00
|
|
|
expectedRule: "Path(`/foo/`)",
|
2024-06-13 09:16:04 +00:00
|
|
|
expectedPriority: 100000,
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-13 09:16:04 +00:00
|
|
|
desc: "Path && Header",
|
2024-08-30 08:36:06 +00:00
|
|
|
match: gatev1.HTTPRouteMatch{
|
2024-06-13 09:16:04 +00:00
|
|
|
Path: &gatev1.HTTPPathMatch{
|
|
|
|
Type: ptr.To(gatev1.PathMatchExact),
|
|
|
|
Value: ptr.To("/foo/"),
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
2024-06-13 09:16:04 +00:00
|
|
|
Headers: []gatev1.HTTPHeaderMatch{
|
|
|
|
{
|
|
|
|
Type: ptr.To(gatev1.HeaderMatchExact),
|
|
|
|
Name: "my-header",
|
|
|
|
Value: "foo",
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-30 07:14:04 +00:00
|
|
|
expectedRule: "Path(`/foo/`) && Header(`my-header`,`foo`)",
|
2024-06-13 09:16:04 +00:00
|
|
|
expectedPriority: 100100,
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-13 09:16:04 +00:00
|
|
|
desc: "Host && Path && Header",
|
2024-05-30 07:14:04 +00:00
|
|
|
hostnames: []gatev1.Hostname{"foo.com"},
|
2024-08-30 08:36:06 +00:00
|
|
|
match: gatev1.HTTPRouteMatch{
|
2024-06-13 09:16:04 +00:00
|
|
|
Path: &gatev1.HTTPPathMatch{
|
|
|
|
Type: ptr.To(gatev1.PathMatchExact),
|
|
|
|
Value: ptr.To("/foo/"),
|
2024-05-30 07:14:04 +00:00
|
|
|
},
|
2024-06-13 09:16:04 +00:00
|
|
|
Headers: []gatev1.HTTPHeaderMatch{
|
|
|
|
{
|
|
|
|
Type: ptr.To(gatev1.HeaderMatchExact),
|
|
|
|
Name: "my-header",
|
|
|
|
Value: "foo",
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-06-13 09:16:04 +00:00
|
|
|
expectedRule: "Host(`foo.com`) && Path(`/foo/`) && Header(`my-header`,`foo`)",
|
|
|
|
expectedPriority: 100107,
|
2024-05-28 12:30:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-08-30 08:36:06 +00:00
|
|
|
rule, priority := buildMatchRule(test.hostnames, test.match)
|
2024-05-28 12:30:04 +00:00
|
|
|
assert.Equal(t, test.expectedRule, rule)
|
2024-05-30 07:14:04 +00:00
|
|
|
assert.Equal(t, test.expectedPriority, priority)
|
2024-05-28 12:30:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|