Skip to content

Commit

Permalink
xds: move virtual host matcher test to the xdsresource package (#6680)
Browse files Browse the repository at this point in the history
  • Loading branch information
easwars authored Oct 4, 2023
1 parent 2c00469 commit 93dbc05
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 51 deletions.
51 changes: 0 additions & 51 deletions xds/internal/resolver/watch_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,59 +29,8 @@ import (
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/xds/internal/testutils/fakeclient"
"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
"google.golang.org/protobuf/proto"
)

func (s) TestFindBestMatchingVirtualHost(t *testing.T) {
var (
oneExactMatch = &xdsresource.VirtualHost{
Domains: []string{"foo.bar.com"},
}
oneSuffixMatch = &xdsresource.VirtualHost{
Domains: []string{"*.bar.com"},
}
onePrefixMatch = &xdsresource.VirtualHost{
Domains: []string{"foo.bar.*"},
}
oneUniversalMatch = &xdsresource.VirtualHost{
Domains: []string{"*"},
}
longExactMatch = &xdsresource.VirtualHost{
Domains: []string{"v2.foo.bar.com"},
}
multipleMatch = &xdsresource.VirtualHost{
Domains: []string{"pi.foo.bar.com", "314.*", "*.159"},
}
vhs = []*xdsresource.VirtualHost{oneExactMatch, oneSuffixMatch, onePrefixMatch, oneUniversalMatch, longExactMatch, multipleMatch}
)

tests := []struct {
name string
host string
vHosts []*xdsresource.VirtualHost
want *xdsresource.VirtualHost
}{
{name: "exact-match", host: "foo.bar.com", vHosts: vhs, want: oneExactMatch},
{name: "suffix-match", host: "123.bar.com", vHosts: vhs, want: oneSuffixMatch},
{name: "prefix-match", host: "foo.bar.org", vHosts: vhs, want: onePrefixMatch},
{name: "universal-match", host: "abc.123", vHosts: vhs, want: oneUniversalMatch},
{name: "long-exact-match", host: "v2.foo.bar.com", vHosts: vhs, want: longExactMatch},
// Matches suffix "*.bar.com" and exact "pi.foo.bar.com". Takes exact.
{name: "multiple-match-exact", host: "pi.foo.bar.com", vHosts: vhs, want: multipleMatch},
// Matches suffix "*.159" and prefix "foo.bar.*". Takes suffix.
{name: "multiple-match-suffix", host: "foo.bar.159", vHosts: vhs, want: multipleMatch},
// Matches suffix "*.bar.com" and prefix "314.*". Takes suffix.
{name: "multiple-match-prefix", host: "314.bar.com", vHosts: vhs, want: oneSuffixMatch},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := xdsresource.FindBestMatchingVirtualHost(tt.host, tt.vHosts); !cmp.Equal(got, tt.want, cmp.Comparer(proto.Equal)) {
t.Errorf("findBestMatchingxdsclient.VirtualHost() = %v, want %v", got, tt.want)
}
})
}
}

type serviceUpdateErr struct {
u serviceUpdate
err error
Expand Down
40 changes: 40 additions & 0 deletions xds/internal/xdsclient/xdsresource/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"google.golang.org/grpc/internal/grpcrand"
"google.golang.org/grpc/internal/grpcutil"
iresolver "google.golang.org/grpc/internal/resolver"
"google.golang.org/grpc/internal/xds/matcher"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/proto"
)

func (s) TestAndMatcherMatch(t *testing.T) {
Expand Down Expand Up @@ -190,3 +192,41 @@ func (s) TestMatch(t *testing.T) {
})
}
}

func (s) TestFindBestMatchingVirtualHost(t *testing.T) {
var (
oneExactMatch = &VirtualHost{Domains: []string{"foo.bar.com"}}
oneSuffixMatch = &VirtualHost{Domains: []string{"*.bar.com"}}
onePrefixMatch = &VirtualHost{Domains: []string{"foo.bar.*"}}
oneUniversalMatch = &VirtualHost{Domains: []string{"*"}}
longExactMatch = &VirtualHost{Domains: []string{"v2.foo.bar.com"}}
multipleMatch = &VirtualHost{Domains: []string{"pi.foo.bar.com", "314.*", "*.159"}}
vhs = []*VirtualHost{oneExactMatch, oneSuffixMatch, onePrefixMatch, oneUniversalMatch, longExactMatch, multipleMatch}
)

tests := []struct {
name string
host string
vHosts []*VirtualHost
want *VirtualHost
}{
{name: "exact-match", host: "foo.bar.com", vHosts: vhs, want: oneExactMatch},
{name: "suffix-match", host: "123.bar.com", vHosts: vhs, want: oneSuffixMatch},
{name: "prefix-match", host: "foo.bar.org", vHosts: vhs, want: onePrefixMatch},
{name: "universal-match", host: "abc.123", vHosts: vhs, want: oneUniversalMatch},
{name: "long-exact-match", host: "v2.foo.bar.com", vHosts: vhs, want: longExactMatch},
// Matches suffix "*.bar.com" and exact "pi.foo.bar.com". Takes exact.
{name: "multiple-match-exact", host: "pi.foo.bar.com", vHosts: vhs, want: multipleMatch},
// Matches suffix "*.159" and prefix "foo.bar.*". Takes suffix.
{name: "multiple-match-suffix", host: "foo.bar.159", vHosts: vhs, want: multipleMatch},
// Matches suffix "*.bar.com" and prefix "314.*". Takes suffix.
{name: "multiple-match-prefix", host: "314.bar.com", vHosts: vhs, want: oneSuffixMatch},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FindBestMatchingVirtualHost(tt.host, tt.vHosts); !cmp.Equal(got, tt.want, cmp.Comparer(proto.Equal)) {
t.Errorf("FindBestMatchingxdsclient.VirtualHost() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 93dbc05

Please sign in to comment.