From 93dbc059f561340a14aeb96ea2925b9a669c5673 Mon Sep 17 00:00:00 2001 From: Easwar Swaminathan Date: Wed, 4 Oct 2023 09:09:57 -0700 Subject: [PATCH] xds: move virtual host matcher test to the xdsresource package (#6680) --- xds/internal/resolver/watch_service_test.go | 51 ------------------- .../xdsclient/xdsresource/matcher_test.go | 40 +++++++++++++++ 2 files changed, 40 insertions(+), 51 deletions(-) diff --git a/xds/internal/resolver/watch_service_test.go b/xds/internal/resolver/watch_service_test.go index 1a4b45bc8ad2..fdc23e712708 100644 --- a/xds/internal/resolver/watch_service_test.go +++ b/xds/internal/resolver/watch_service_test.go @@ -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 diff --git a/xds/internal/xdsclient/xdsresource/matcher_test.go b/xds/internal/xdsclient/xdsresource/matcher_test.go index 2746e58e6c77..9931a63b6d76 100644 --- a/xds/internal/xdsclient/xdsresource/matcher_test.go +++ b/xds/internal/xdsclient/xdsresource/matcher_test.go @@ -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) { @@ -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) + } + }) + } +}