Repository: grpc/grpc-go
File: internal/xds/balancer/clusterresolver/configbuilder_test.go (Go)

Code before:
	if diff := cmp.Diff(gotConfig, wantConfig); diff != "" {
		t.Errorf("buildPriorityConfig() diff (-got +want) %v", diff)
	}
}

func testEndpointForDNS(endpoint []resolver.Endpoint, localityWeight uint32, path []string) resolver.Endpoint {
	retEndpoint := resolver.Endpoint{}
	for _, e := range endpoint {
		retEndpoint.Addresses = append(retEndpoint.Addresses, e.Addresses...)
	}
	retEndpoint = hierarchy.SetInEndpoint(retEndpoint, path)
	retEndpoint = wrrlocality.SetAddrInfoInEndpoint(retEndpoint, wrrlocality.AddrInfo{LocalityWeight: localityWeight})
	return retEndpoint
}

func (s) TestBuildClusterImplConfigForDNS(t *testing.T) {
	wantName := "priority-3"
	localityStr := xdsinternal.LocalityString(clients.Locality{})
	wantConfig := &clusterimpl.LBConfig{
		Cluster:     testClusterName2,
		ChildPolicy: nil,
	}
	for _, tt := range []struct {
		name         string
		endpoint     []resolver.Endpoint
		wantEndpoint []resolver.Endpoint
	}{
		{
			name:         "one_endpoint_one_address",
			endpoint:     []resolver.Endpoint{{Addresses: []resolver.Address{{Addr: "addr-0-0"}}}},
			wantEndpoint: []resolver.Endpoint{testEndpointForDNS([]resolver.Endpoint{{Addresses: []resolver.Address{{Addr: "addr-0-0"}}}}, 1, []string{wantName, localityStr})},
		},
		{
			name: "one_endpoint_multiple_addresses",
			endpoint: []resolver.Endpoint{{Addresses: []resolver.Address{
				{Addr: "addr-0-0"},
				{Addr: "addr-0-1"},
			}}},
			wantEndpoint: []resolver.Endpoint{
				testEndpointForDNS([]resolver.Endpoint{{Addresses: []resolver.Address{
					{Addr: "addr-0-0"},
					{Addr: "addr-0-1"},
				}}}, 1, []string{wantName, localityStr}),
			},
		},
		{
			name: "multiple_endpoints_one_address_each",
			endpoint: []resolver.Endpoint{
				{Addresses: []resolver.Address{{Addr: "addr-0-0"}}},
				{Addresses: []resolver.Address{{Addr: "addr-0-1"}}},
			},

Review comment (suggestion):
The entries for `wantEndpoint` in the table entries are very very hard to read. See this section for more details: go/go-style/decisions#indentation-confusion

I would recommend that you instead call `testEndpointForDNS` from inside the test body with the endpoints parameter being set to `tt.endpoints`.

Code after:
		},
		Priorities: []string{"priority-0-0", "priority-0-1", "priority-1"},
	}
	if diff := cmp.Diff(gotConfig, wantConfig); diff != "" {
		t.Errorf("buildPriorityConfig() diff (-got +want) %v", diff)
	}
}

func testEndpointForDNS(endpoints []resolver.Endpoint, localityWeight uint32, path []string) resolver.Endpoint {
	retEndpoint := resolver.Endpoint{}
	for _, e := range endpoints {
		retEndpoint.Addresses = append(retEndpoint.Addresses, e.Addresses...)
	}
	retEndpoint = hierarchy.SetInEndpoint(retEndpoint, path)
	retEndpoint = wrrlocality.SetAddrInfo(retEndpoint, wrrlocality.AddrInfo{LocalityWeight: localityWeight})
	return retEndpoint
}

func (s) TestBuildClusterImplConfigForDNS(t *testing.T) {
	for _, tt := range []struct {
		name        string
		endpoints   []resolver.Endpoint
		xdsLBPolicy *iserviceconfig.BalancerConfig
	}{
		{
			name:        "one_endpoint_one_address",
			endpoints:   []resolver.Endpoint{{Addresses: []resolver.Address{{Addr: "addr-0-0"}}}},
			xdsLBPolicy: &iserviceconfig.BalancerConfig{Name: pickfirst.Name},
		},
		{
			name: "one_endpoint_multiple_addresses",
			endpoints: []resolver.Endpoint{{Addresses: []resolver.Address{
				{Addr: "addr-0-0"},
				{Addr: "addr-0-1"},
			}}},
			xdsLBPolicy: &iserviceconfig.BalancerConfig{Name: wrrlocality.Name},
		},
		{
			name: "multiple_endpoints_one_address_each",
			endpoints: []resolver.Endpoint{
				{Addresses: []resolver.Address{{Addr: "addr-0-0"}}},
				{Addresses: []resolver.Address{{Addr: "addr-0-1"}}},
			},
			xdsLBPolicy: &iserviceconfig.BalancerConfig{Name: roundrobin.Name},
		},
		{
			name: "multiple_endpoints_multiple_addresses",
			endpoints: []resolver.Endpoint{
				{Addresses: []resolver.Address{
					{Addr: "addr-0-0"},
					{Addr: "addr-0-1"},