1/*
2 *
3 * Copyright 2020 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 lrs
20
21import (
22	"testing"
23
24	"github.com/google/go-cmp/cmp"
25	"google.golang.org/grpc/balancer/roundrobin"
26	internalserviceconfig "google.golang.org/grpc/internal/serviceconfig"
27	xdsinternal "google.golang.org/grpc/xds/internal"
28)
29
30const (
31	testClusterName   = "test-cluster"
32	testServiceName   = "test-eds-service"
33	testLRSServerName = "test-lrs-name"
34)
35
36func TestParseConfig(t *testing.T) {
37	tests := []struct {
38		name    string
39		js      string
40		want    *lbConfig
41		wantErr bool
42	}{
43		{
44			name: "no cluster name",
45			js: `{
46  "edsServiceName": "test-eds-service",
47  "lrsLoadReportingServerName": "test-lrs-name",
48  "locality": {
49    "region": "test-region",
50    "zone": "test-zone",
51    "subZone": "test-sub-zone"
52  },
53  "childPolicy":[{"round_robin":{}}]
54}
55			`,
56			wantErr: true,
57		},
58		{
59			name: "no LRS server name",
60			js: `{
61  "clusterName": "test-cluster",
62  "edsServiceName": "test-eds-service",
63  "locality": {
64    "region": "test-region",
65    "zone": "test-zone",
66    "subZone": "test-sub-zone"
67  },
68  "childPolicy":[{"round_robin":{}}]
69}
70			`,
71			wantErr: true,
72		},
73		{
74			name: "no locality",
75			js: `{
76  "clusterName": "test-cluster",
77  "edsServiceName": "test-eds-service",
78  "lrsLoadReportingServerName": "test-lrs-name",
79  "childPolicy":[{"round_robin":{}}]
80}
81			`,
82			wantErr: true,
83		},
84		{
85			name: "good",
86			js: `{
87  "clusterName": "test-cluster",
88  "edsServiceName": "test-eds-service",
89  "lrsLoadReportingServerName": "test-lrs-name",
90  "locality": {
91    "region": "test-region",
92    "zone": "test-zone",
93    "subZone": "test-sub-zone"
94  },
95  "childPolicy":[{"round_robin":{}}]
96}
97			`,
98			want: &lbConfig{
99				ClusterName:                testClusterName,
100				EdsServiceName:             testServiceName,
101				LrsLoadReportingServerName: testLRSServerName,
102				Locality: &xdsinternal.LocalityID{
103					Region:  "test-region",
104					Zone:    "test-zone",
105					SubZone: "test-sub-zone",
106				},
107				ChildPolicy: &internalserviceconfig.BalancerConfig{
108					Name:   roundrobin.Name,
109					Config: nil,
110				},
111			},
112			wantErr: false,
113		},
114	}
115	for _, tt := range tests {
116		t.Run(tt.name, func(t *testing.T) {
117			got, err := parseConfig([]byte(tt.js))
118			if (err != nil) != tt.wantErr {
119				t.Errorf("parseConfig() error = %v, wantErr %v", err, tt.wantErr)
120				return
121			}
122			if diff := cmp.Diff(got, tt.want); diff != "" {
123				t.Errorf("parseConfig() got = %v, want %v, diff: %s", got, tt.want, diff)
124			}
125		})
126	}
127}
128