xds/googledirectpath: fix google-c2p resolver test case involving bootstrap env config (#6657)

This commit is contained in:
apolcyn 2023-09-22 15:43:47 -07:00 committed by GitHub
parent e61a14d768
commit a758b62537
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 13 deletions

View File

@ -70,11 +70,34 @@ func replaceResolvers() func() {
} }
} }
// Test that when bootstrap env is set, fallback to DNS. type testXDSClient struct {
xdsclient.XDSClient
closed chan struct{}
}
func (c *testXDSClient) Close() {
c.closed <- struct{}{}
}
// Test that when bootstrap env is set and we're running on GCE, don't fallback to DNS (because
// federation is enabled by default).
func TestBuildWithBootstrapEnvSet(t *testing.T) { func TestBuildWithBootstrapEnvSet(t *testing.T) {
defer replaceResolvers()() defer replaceResolvers()()
builder := resolver.Get(c2pScheme) builder := resolver.Get(c2pScheme)
// make the test behave the ~same whether it's running on or off GCE
oldOnGCE := onGCE
onGCE = func() bool { return true }
defer func() { onGCE = oldOnGCE }()
// don't actually read the bootstrap file contents
xdsClient := &testXDSClient{closed: make(chan struct{}, 1)}
oldNewClient := newClientWithConfig
newClientWithConfig = func(config *bootstrap.Config) (xdsclient.XDSClient, func(), error) {
return xdsClient, func() { xdsClient.Close() }, nil
}
defer func() { newClientWithConfig = oldNewClient }()
for i, envP := range []*string{&envconfig.XDSBootstrapFileName, &envconfig.XDSBootstrapFileContent} { for i, envP := range []*string{&envconfig.XDSBootstrapFileName, &envconfig.XDSBootstrapFileContent} {
t.Run(strconv.Itoa(i), func(t *testing.T) { t.Run(strconv.Itoa(i), func(t *testing.T) {
// Set bootstrap config env var. // Set bootstrap config env var.
@ -82,13 +105,14 @@ func TestBuildWithBootstrapEnvSet(t *testing.T) {
*envP = "does not matter" *envP = "does not matter"
defer func() { *envP = oldEnv }() defer func() { *envP = oldEnv }()
// Build should return DNS, not xDS. // Build should return xDS, not DNS.
r, err := builder.Build(resolver.Target{}, nil, resolver.BuildOptions{}) r, err := builder.Build(resolver.Target{}, nil, resolver.BuildOptions{})
if err != nil { if err != nil {
t.Fatalf("failed to build resolver: %v", err) t.Fatalf("failed to build resolver: %v", err)
} }
if r != testDNSResolver { rr := r.(*c2pResolver)
t.Fatalf("want dns resolver, got %#v", r) if rrr := rr.Resolver; rrr != testXDSResolver {
t.Fatalf("want xds resolver, got %#v", rrr)
} }
}) })
} }
@ -113,15 +137,6 @@ func TestBuildNotOnGCE(t *testing.T) {
} }
} }
type testXDSClient struct {
xdsclient.XDSClient
closed chan struct{}
}
func (c *testXDSClient) Close() {
c.closed <- struct{}{}
}
// Test that when xDS is built, the client is built with the correct config. // Test that when xDS is built, the client is built with the correct config.
func TestBuildXDS(t *testing.T) { func TestBuildXDS(t *testing.T) {
defer replaceResolvers()() defer replaceResolvers()()