Repository: grpc/grpc-go
File: xds/internal/xdsclient/transport/transport_test.go (Go)

Code before:

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
}

func (t *testDialerCredsBuilder) Name() string {
	return testDialerCredsBuilderName
}

// 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
}

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

func (s) TestNewWithDialerFromCredentialsBundle(t *testing.T) {
	serverCfg, err := internalbootstrap.ServerConfigForTesting(internalbootstrap.ServerConfigTestingOptions{
		URI:          "trafficdirector.googleapis.com:443",
		ChannelCreds: []internalbootstrap.ChannelCreds{{Type: testDialerCredsBuilderName}},
	})
	if err != nil {
		t.Fatalf("Failed to create server config for testing: %v", err)
	}
	if serverCfg.DialerOption() == nil {
		t.Fatalf("Dialer for xDS transport in server config for testing is nil, want non-nil")
	}
	// Create a new transport.

Review comment (suggestion):
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. 

Code after:

// 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{insecure.NewBundle()}, func() {}, nil
}

func (t *testDialerCredsBuilder) Name() string {
	return testDialerCredsBuilderName
}

// 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 {
	credentials.Bundle
}

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

func (s) TestNewWithDialerFromCredentialsBundle(t *testing.T) {
	// Override grpc.NewClient with a custom one.
	doptsLen := 0
	customGRPCNewClient := func(target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
		doptsLen = len(opts)
		return grpc.NewClient(target, opts...)
	}
	oldGRPCNewClient := internal.GRPCNewClient
	internal.GRPCNewClient = customGRPCNewClient
	defer func() { internal.GRPCNewClient = oldGRPCNewClient }()

	bootstrap.RegisterCredentials(&testDialerCredsBuilder{})
	serverCfg, err := internalbootstrap.ServerConfigForTesting(internalbootstrap.ServerConfigTestingOptions{
		URI:          "trafficdirector.googleapis.com:443",
		ChannelCreds: []internalbootstrap.ChannelCreds{{Type: testDialerCredsBuilderName}},
	})
	if err != nil {
		t.Fatalf("Failed to create server config for testing: %v", err)
	}
	if serverCfg.DialerOption() == nil {
		t.Fatalf("Dialer for xDS transport in server config for testing is nil, want non-nil")
	}
	// Create a new transport.
	opts := transport.Options{
		ServerCfg: serverCfg,
		NodeProto: &v3corepb.Node{},