Skip to content

Commit 93dbc05

Browse files
authored
xds: move virtual host matcher test to the xdsresource package (#6680)
1 parent 2c00469 commit 93dbc05

File tree

2 files changed

+40
-51
lines changed

2 files changed

+40
-51
lines changed

Diff for: xds/internal/resolver/watch_service_test.go

-51
Original file line numberDiff line numberDiff line change
@@ -29,59 +29,8 @@ import (
2929
"google.golang.org/grpc/internal/testutils"
3030
"google.golang.org/grpc/xds/internal/testutils/fakeclient"
3131
"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
32-
"google.golang.org/protobuf/proto"
3332
)
3433

35-
func (s) TestFindBestMatchingVirtualHost(t *testing.T) {
36-
var (
37-
oneExactMatch = &xdsresource.VirtualHost{
38-
Domains: []string{"foo.bar.com"},
39-
}
40-
oneSuffixMatch = &xdsresource.VirtualHost{
41-
Domains: []string{"*.bar.com"},
42-
}
43-
onePrefixMatch = &xdsresource.VirtualHost{
44-
Domains: []string{"foo.bar.*"},
45-
}
46-
oneUniversalMatch = &xdsresource.VirtualHost{
47-
Domains: []string{"*"},
48-
}
49-
longExactMatch = &xdsresource.VirtualHost{
50-
Domains: []string{"v2.foo.bar.com"},
51-
}
52-
multipleMatch = &xdsresource.VirtualHost{
53-
Domains: []string{"pi.foo.bar.com", "314.*", "*.159"},
54-
}
55-
vhs = []*xdsresource.VirtualHost{oneExactMatch, oneSuffixMatch, onePrefixMatch, oneUniversalMatch, longExactMatch, multipleMatch}
56-
)
57-
58-
tests := []struct {
59-
name string
60-
host string
61-
vHosts []*xdsresource.VirtualHost
62-
want *xdsresource.VirtualHost
63-
}{
64-
{name: "exact-match", host: "foo.bar.com", vHosts: vhs, want: oneExactMatch},
65-
{name: "suffix-match", host: "123.bar.com", vHosts: vhs, want: oneSuffixMatch},
66-
{name: "prefix-match", host: "foo.bar.org", vHosts: vhs, want: onePrefixMatch},
67-
{name: "universal-match", host: "abc.123", vHosts: vhs, want: oneUniversalMatch},
68-
{name: "long-exact-match", host: "v2.foo.bar.com", vHosts: vhs, want: longExactMatch},
69-
// Matches suffix "*.bar.com" and exact "pi.foo.bar.com". Takes exact.
70-
{name: "multiple-match-exact", host: "pi.foo.bar.com", vHosts: vhs, want: multipleMatch},
71-
// Matches suffix "*.159" and prefix "foo.bar.*". Takes suffix.
72-
{name: "multiple-match-suffix", host: "foo.bar.159", vHosts: vhs, want: multipleMatch},
73-
// Matches suffix "*.bar.com" and prefix "314.*". Takes suffix.
74-
{name: "multiple-match-prefix", host: "314.bar.com", vHosts: vhs, want: oneSuffixMatch},
75-
}
76-
for _, tt := range tests {
77-
t.Run(tt.name, func(t *testing.T) {
78-
if got := xdsresource.FindBestMatchingVirtualHost(tt.host, tt.vHosts); !cmp.Equal(got, tt.want, cmp.Comparer(proto.Equal)) {
79-
t.Errorf("findBestMatchingxdsclient.VirtualHost() = %v, want %v", got, tt.want)
80-
}
81-
})
82-
}
83-
}
84-
8534
type serviceUpdateErr struct {
8635
u serviceUpdate
8736
err error

Diff for: xds/internal/xdsclient/xdsresource/matcher_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ import (
2121
"context"
2222
"testing"
2323

24+
"github.com/google/go-cmp/cmp"
2425
"google.golang.org/grpc/internal/grpcrand"
2526
"google.golang.org/grpc/internal/grpcutil"
2627
iresolver "google.golang.org/grpc/internal/resolver"
2728
"google.golang.org/grpc/internal/xds/matcher"
2829
"google.golang.org/grpc/metadata"
30+
"google.golang.org/protobuf/proto"
2931
)
3032

3133
func (s) TestAndMatcherMatch(t *testing.T) {
@@ -190,3 +192,41 @@ func (s) TestMatch(t *testing.T) {
190192
})
191193
}
192194
}
195+
196+
func (s) TestFindBestMatchingVirtualHost(t *testing.T) {
197+
var (
198+
oneExactMatch = &VirtualHost{Domains: []string{"foo.bar.com"}}
199+
oneSuffixMatch = &VirtualHost{Domains: []string{"*.bar.com"}}
200+
onePrefixMatch = &VirtualHost{Domains: []string{"foo.bar.*"}}
201+
oneUniversalMatch = &VirtualHost{Domains: []string{"*"}}
202+
longExactMatch = &VirtualHost{Domains: []string{"v2.foo.bar.com"}}
203+
multipleMatch = &VirtualHost{Domains: []string{"pi.foo.bar.com", "314.*", "*.159"}}
204+
vhs = []*VirtualHost{oneExactMatch, oneSuffixMatch, onePrefixMatch, oneUniversalMatch, longExactMatch, multipleMatch}
205+
)
206+
207+
tests := []struct {
208+
name string
209+
host string
210+
vHosts []*VirtualHost
211+
want *VirtualHost
212+
}{
213+
{name: "exact-match", host: "foo.bar.com", vHosts: vhs, want: oneExactMatch},
214+
{name: "suffix-match", host: "123.bar.com", vHosts: vhs, want: oneSuffixMatch},
215+
{name: "prefix-match", host: "foo.bar.org", vHosts: vhs, want: onePrefixMatch},
216+
{name: "universal-match", host: "abc.123", vHosts: vhs, want: oneUniversalMatch},
217+
{name: "long-exact-match", host: "v2.foo.bar.com", vHosts: vhs, want: longExactMatch},
218+
// Matches suffix "*.bar.com" and exact "pi.foo.bar.com". Takes exact.
219+
{name: "multiple-match-exact", host: "pi.foo.bar.com", vHosts: vhs, want: multipleMatch},
220+
// Matches suffix "*.159" and prefix "foo.bar.*". Takes suffix.
221+
{name: "multiple-match-suffix", host: "foo.bar.159", vHosts: vhs, want: multipleMatch},
222+
// Matches suffix "*.bar.com" and prefix "314.*". Takes suffix.
223+
{name: "multiple-match-prefix", host: "314.bar.com", vHosts: vhs, want: oneSuffixMatch},
224+
}
225+
for _, tt := range tests {
226+
t.Run(tt.name, func(t *testing.T) {
227+
if got := FindBestMatchingVirtualHost(tt.host, tt.vHosts); !cmp.Equal(got, tt.want, cmp.Comparer(proto.Equal)) {
228+
t.Errorf("FindBestMatchingxdsclient.VirtualHost() = %v, want %v", got, tt.want)
229+
}
230+
})
231+
}
232+
}

0 commit comments

Comments
 (0)