Repository: grpc/grpc-go
File: xds/internal/xdsclient/tests/client_custom_dialopts_test.go (Go)

Code before:

const testCredsBuilderName = "test_dialer_creds"

var testDialOptNames = []string{"opt1", "opt2", "opt3"}

// testCredsBundle implements `credentials.Bundle`  and `extraDialOptions`.
type testCredsBundle struct {
	credentials.Bundle
	dialOpts []grpc.DialOption
}

func (t *testCredsBundle) DialOptions() []grpc.DialOption {
	return t.dialOpts
}

type testCredsBuilder struct{}

func (t *testCredsBuilder) Build(config json.RawMessage) (credentials.Bundle, func(), error) {
	return &testCredsBundle{insecure.NewBundle(),
		func() []grpc.DialOption {
			var opts []grpc.DialOption
			for _, name := range testDialOptNames {
				opts = append(opts, &mockDialOption{name: name})
			}
			return opts
		}(),
	}, func() {}, nil
}

func (t *testCredsBuilder) Name() string {
	return testCredsBuilderName
}

func (s) TestClientCustomDialOptsFromCredentialsBundle(t *testing.T) {
	// Create and register the credentials bundle builder.
	credsBuilder := &testCredsBuilder{}
	bootstrap.RegisterCredentials(credsBuilder)

	// Start an xDS management server.
	mgmtServer := e2e.StartManagementServer(t, e2e.ManagementServerOptions{})

	// Create bootstrap configuration pointing to the above management server.
	nodeID := uuid.New().String()
	bc, err := internalbootstrap.NewContentsForTesting(internalbootstrap.ConfigOptionsForTesting{
		Servers: []byte(fmt.Sprintf(`[{
			"server_uri": %q,
			"channel_creds": [{
				"type": %q,
				"config": {"mgmt_server_address": %q}
			}]
		}]`, mgmtServer.Address, testCredsBuilderName, mgmtServer.Address)),

Review comment (suggestion):
Optional. To make this more readable, maybe just pass the dial option names to the `testCredsBundle` and let it build the dial options, instead of passing a closure.

Code after:

// testCredsBundle implements `credentials.Bundle` and `extraDialOptions`.
type testCredsBundle struct {
	credentials.Bundle
	testDialOptNames []string
}

func (t *testCredsBundle) DialOptions() []grpc.DialOption {
	var opts []grpc.DialOption
	for _, name := range t.testDialOptNames {
		opts = append(opts, &nopDialOption{name: name})
	}
	return opts
}

type testCredsBuilder struct {
	testDialOptNames []string
}

func (t *testCredsBuilder) Build(config json.RawMessage) (credentials.Bundle, func(), error) {
	return &testCredsBundle{
		Bundle:           insecure.NewBundle(),
		testDialOptNames: t.testDialOptNames,
	}, func() {}, nil
}

func (t *testCredsBuilder) Name() string {
	return "test_dialer_creds"
}

func (s) TestClientCustomDialOptsFromCredentialsBundle(t *testing.T) {
	// Create and register the credentials bundle builder.
	credsBuilder := &testCredsBuilder{
		testDialOptNames: []string{"opt1", "opt2", "opt3"},
	}
	bootstrap.RegisterCredentials(credsBuilder)

	// Start an xDS management server.
	mgmtServer := e2e.StartManagementServer(t, e2e.ManagementServerOptions{})

	// Create bootstrap configuration pointing to the above management server.
	nodeID := uuid.New().String()
	bc, err := internalbootstrap.NewContentsForTesting(internalbootstrap.ConfigOptionsForTesting{
		Servers: []byte(fmt.Sprintf(`[{
			"server_uri": %q,
			"channel_creds": [{
				"type": %q,
				"config": {"mgmt_server_address": %q}
			}]
		}]`, mgmtServer.Address, credsBuilder.Name(), mgmtServer.Address)),
		Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),