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: add xDS transport custom Dialer support #7586

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
address review p5: embed credentials.Bundle interface in tests
  • Loading branch information
danielzhaotongliu committed Sep 18, 2024
commit 65feb3c0088ac9257d2d276d5a9ee2d601d63cd8
15 changes: 2 additions & 13 deletions test/xds/xds_client_custom_dialer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (t *testDialerCredsBuilder) Build(config json.RawMessage) (credentials.Bund
if err := json.Unmarshal(config, &cfg); err != nil {
return nil, func() {}, fmt.Errorf("failed to unmarshal config: %v", err)
}
return &testDialerCredsBundle{t.dialerCalled, cfg.MgmtServerAddress}, func() {}, nil
return &testDialerCredsBundle{insecure.NewBundle(), t.dialerCalled, cfg.MgmtServerAddress}, func() {}, nil
}

func (t *testDialerCredsBuilder) Name() string {
Expand All @@ -68,22 +68,11 @@ func (t *testDialerCredsBuilder) Name() string {
// `credentials` and encapsulates an insecure credential with a custom Dialer
// that specifies how to dial the xDS server.
type testDialerCredsBundle struct {
credentials.Bundle
dialerCalled chan struct{}
mgmtServerAddress string
}

func (t *testDialerCredsBundle) TransportCredentials() credentials.TransportCredentials {
return insecure.NewCredentials()
}

func (t *testDialerCredsBundle) PerRPCCredentials() credentials.PerRPCCredentials {
return nil
}

func (t *testDialerCredsBundle) NewWithMode(string) (credentials.Bundle, error) {
return &testDialerCredsBundle{}, nil
}

// Dialer specifies how to dial the xDS management server.
func (t *testDialerCredsBundle) Dialer(context.Context, string) (net.Conn, error) {
close(t.dialerCalled)
Expand Down
21 changes: 4 additions & 17 deletions xds/internal/xdsclient/transport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,13 @@ func (s) TestNewWithGRPCDial(t *testing.T) {

const testDialerCredsBuilderName = "test_dialer_creds"

func init() {
bootstrap.RegisterCredentials(&testDialerCredsBuilder{})
}

// testDialerCredsBuilder implements the `Credentials` interface defined in
// package `xds/bootstrap` and encapsulates an insecure credential with a
// custom Dialer that specifies how to dial the xDS server.
type testDialerCredsBuilder struct{}

func (t *testDialerCredsBuilder) Build(json.RawMessage) (credentials.Bundle, func(), error) {
return &testDialerCredsBundle{}, func() {}, nil
return &testDialerCredsBundle{insecure.NewBundle()}, func() {}, nil
}

func (t *testDialerCredsBuilder) Name() string {
Expand All @@ -111,25 +107,16 @@ func (t *testDialerCredsBuilder) Name() string {
// testDialerCredsBundle implements the `Bundle` interface defined in package
// `credentials` and encapsulates an insecure credential with a custom Dialer
// that specifies how to dial the xDS server.
type testDialerCredsBundle struct{}

func (t *testDialerCredsBundle) TransportCredentials() credentials.TransportCredentials {
return insecure.NewCredentials()
}

func (t *testDialerCredsBundle) PerRPCCredentials() credentials.PerRPCCredentials {
return nil
}

func (t *testDialerCredsBundle) NewWithMode(string) (credentials.Bundle, error) {
return &testDialerCredsBundle{}, nil
type testDialerCredsBundle struct {
credentials.Bundle
}

func (t *testDialerCredsBundle) Dialer(context.Context, string) (net.Conn, error) {
return nil, nil
}

func (s) TestNewWithDialerFromCredentialsBundle(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this really tests whether the transport is actually passing the new dial option to grpc.Dial. It is not possible to check the exact dial options being passed because dial options are implemented as functions, and it is not possible to compare them. But at least, we should check if the call to grpc.Dial or grpc.NewClient from the transport passes the expected number of dial options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
Updated this test such that initially it overrides internal.GRPCNewClient with a custom pass-through grpc.NewClient (then resets back to the original grpc.NewClient as to not have order-dependent tests) that gets the number of dial options. Later the test verifies the number of dial options passed to the custom grpc.NewClient is 3. Also, added a comment explaining why it is 3.

bootstrap.RegisterCredentials(&testDialerCredsBuilder{})
serverCfg, err := internalbootstrap.ServerConfigForTesting(internalbootstrap.ServerConfigTestingOptions{
URI: "trafficdirector.googleapis.com:443",
ChannelCreds: []internalbootstrap.ChannelCreds{{Type: testDialerCredsBuilderName}},
Expand Down