Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions xds/googledirectpath/googlec2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,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 @@ -131,15 +155,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