Skip to content

Commit

Permalink
xds/googledirectpath: fix google-c2p resolver test case involving boo…
Browse files Browse the repository at this point in the history
…tstrap env config (grpc#6657)
  • Loading branch information
apolcyn authored Sep 22, 2023
1 parent e61a14d commit a758b62
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions xds/googledirectpath/googlec2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,49 @@ 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) {
defer replaceResolvers()()
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} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
// Set bootstrap config env var.
oldEnv := *envP
*envP = "does not matter"
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{})
if err != nil {
t.Fatalf("failed to build resolver: %v", err)
}
if r != testDNSResolver {
t.Fatalf("want dns resolver, got %#v", r)
rr := r.(*c2pResolver)
if rrr := rr.Resolver; rrr != testXDSResolver {
t.Fatalf("want xds resolver, got %#v", rrr)
}
})
}
Expand All @@ -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.
func TestBuildXDS(t *testing.T) {
defer replaceResolvers()()
Expand Down

0 comments on commit a758b62

Please sign in to comment.