1/*
2 *
3 * Copyright 2019 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19package edsbalancer
20
21import (
22	"testing"
23
24	"google.golang.org/grpc/attributes"
25	"google.golang.org/grpc/balancer"
26	"google.golang.org/grpc/resolver"
27	xdsinternal "google.golang.org/grpc/xds/internal"
28	"google.golang.org/grpc/xds/internal/testutils/fakeclient"
29)
30
31// TestXDSLoadReporting verifies that the edsBalancer starts the loadReport
32// stream when the lbConfig passed to it contains a valid value for the LRS
33// server (empty string).
34func (s) TestXDSLoadReporting(t *testing.T) {
35	builder := balancer.Get(edsName)
36	cc := newNoopTestClientConn()
37	edsB, ok := builder.Build(cc, balancer.BuildOptions{Target: resolver.Target{Endpoint: testEDSClusterName}}).(*edsBalancer)
38	if !ok {
39		t.Fatalf("builder.Build(%s) returned type {%T}, want {*edsBalancer}", edsName, edsB)
40	}
41	defer edsB.Close()
42
43	xdsC := fakeclient.NewClient()
44	edsB.UpdateClientConnState(balancer.ClientConnState{
45		ResolverState:  resolver.State{Attributes: attributes.New(xdsinternal.XDSClientID, xdsC)},
46		BalancerConfig: &EDSConfig{LrsLoadReportingServerName: new(string)},
47	})
48
49	gotCluster, err := xdsC.WaitForWatchEDS()
50	if err != nil {
51		t.Fatalf("xdsClient.WatchEndpoints failed with error: %v", err)
52	}
53	if gotCluster != testEDSClusterName {
54		t.Fatalf("xdsClient.WatchEndpoints() called with cluster: %v, want %v", gotCluster, testEDSClusterName)
55	}
56
57	got, err := xdsC.WaitForReportLoad()
58	if err != nil {
59		t.Fatalf("xdsClient.ReportLoad failed with error: %v", err)
60	}
61	if got.Server != "" || got.Cluster != testEDSClusterName {
62		t.Fatalf("xdsClient.ReportLoad called with {%v, %v}: want {\"\", %v}", got.Server, got.Cluster, testEDSClusterName)
63	}
64}
65