forked from grpc/grpc-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclientimpl_test.go
More file actions
261 lines (251 loc) · 14.7 KB
/
Copy pathclientimpl_test.go
File metadata and controls
261 lines (251 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
*
* Copyright 2025 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package xdsclient
import (
"encoding/json"
"fmt"
"reflect"
"sync"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/internal/testutils/stats"
"google.golang.org/grpc/internal/xds/bootstrap"
"google.golang.org/grpc/internal/xds/clients"
"google.golang.org/grpc/internal/xds/clients/grpctransport"
"google.golang.org/grpc/internal/xds/clients/xdsclient"
"google.golang.org/grpc/internal/xds/xdsclient/xdsresource"
"google.golang.org/grpc/internal/xds/xdsclient/xdsresource/version"
"google.golang.org/protobuf/testing/protocmp"
)
const (
testXDSServerURL = "xds.example.com:8080"
testXDSServerURL2 = "xds.example.com:8081"
testNodeID = "test-node-id"
testClusterName = "test-cluster"
testUserAgentName = "test-ua-name"
testUserAgentVer = "test-ua-ver"
testLocalityRegion = "test-region"
testLocalityZone = "test-zone"
testLocalitySubZone = "test-sub-zone"
testTargetName = "test-target"
)
var (
testMetadataJSON, _ = json.Marshal(map[string]any{"foo": "bar", "baz": float64(1)})
)
func (s) TestBuildXDSClientConfig_Success(t *testing.T) {
tests := []struct {
name string
bootstrapContents []byte
wantXDSClientConfig func(bootstrapCfg *bootstrap.Config) xdsclient.Config
}{
{
name: "without authorities",
bootstrapContents: []byte(fmt.Sprintf(`{
"xds_servers": [{"server_uri": "%s", "channel_creds": [{"type": "insecure"}]}],
"node": {
"id": "%s", "cluster": "%s", "metadata": %s,
"locality": {"region": "%s", "zone": "%s", "sub_zone": "%s"},
"user_agent_name": "%s", "user_agent_version": "%s"
}
}`, testXDSServerURL, testNodeID, testClusterName, testMetadataJSON, testLocalityRegion, testLocalityZone, testLocalitySubZone, testUserAgentName, testUserAgentVer)),
wantXDSClientConfig: func(c *bootstrap.Config) xdsclient.Config {
node, serverCfg := c.Node(), c.XDSServers()[0]
expectedServer := xdsclient.ServerConfig{ServerIdentifier: clients.ServerIdentifier{ServerURI: serverCfg.ServerURI(), Extensions: grpctransport.ServerIdentifierExtension{ConfigName: "insecure"}}}
gServerCfgMap := map[xdsclient.ServerConfig]*bootstrap.ServerConfig{expectedServer: serverCfg}
return xdsclient.Config{
Servers: []xdsclient.ServerConfig{expectedServer},
Node: clients.Node{ID: node.GetId(), Cluster: node.GetCluster(), Metadata: node.Metadata, Locality: clients.Locality{Region: node.Locality.Region, Zone: node.Locality.Zone, SubZone: node.Locality.SubZone}, UserAgentName: node.UserAgentName, UserAgentVersion: node.GetUserAgentVersion()},
Authorities: map[string]xdsclient.Authority{},
ResourceTypes: map[string]xdsclient.ResourceType{
version.V3ListenerURL: {TypeURL: version.V3ListenerURL, TypeName: xdsresource.ListenerResourceTypeName, AllResourcesRequiredInSotW: true, Decoder: xdsresource.NewGenericListenerResourceTypeDecoder(c)},
version.V3RouteConfigURL: {TypeURL: version.V3RouteConfigURL, TypeName: xdsresource.RouteConfigTypeName, AllResourcesRequiredInSotW: false, Decoder: xdsresource.NewGenericRouteConfigResourceTypeDecoder()},
version.V3ClusterURL: {TypeURL: version.V3ClusterURL, TypeName: xdsresource.ClusterResourceTypeName, AllResourcesRequiredInSotW: true, Decoder: xdsresource.NewGenericClusterResourceTypeDecoder(c, gServerCfgMap)},
version.V3EndpointsURL: {TypeURL: version.V3EndpointsURL, TypeName: xdsresource.EndpointsResourceTypeName, AllResourcesRequiredInSotW: false, Decoder: xdsresource.NewGenericEndpointsResourceTypeDecoder()},
},
MetricsReporter: &metricsReporter{recorder: stats.NewTestMetricsRecorder(), target: testTargetName},
TransportBuilder: grpctransport.NewBuilder(map[string]grpctransport.Config{
"insecure": {
Credentials: insecure.NewBundle(),
GRPCNewClient: func(target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
opts = append(opts, serverCfg.DialOptions()...)
return grpc.NewClient(target, opts...)
}},
}),
}
},
},
{
name: "with authorities",
bootstrapContents: []byte(fmt.Sprintf(`{
"xds_servers": [{"server_uri": "%s", "channel_creds": [{"type": "insecure"}]}],
"node": {"id": "%s"},
"authorities": {
"auth1": {},
"auth2": {"xds_servers": [{"server_uri": "%s", "channel_creds": [{"type": "insecure"}]}]}
}
}`, testXDSServerURL, testNodeID, testXDSServerURL2)),
wantXDSClientConfig: func(c *bootstrap.Config) xdsclient.Config {
node := c.Node()
topLevelSCfg, auth2SCfg := c.XDSServers()[0], c.Authorities()["auth2"].XDSServers[0]
expTopLevelS := xdsclient.ServerConfig{ServerIdentifier: clients.ServerIdentifier{ServerURI: topLevelSCfg.ServerURI(), Extensions: grpctransport.ServerIdentifierExtension{ConfigName: "insecure"}}}
expAuth2S := xdsclient.ServerConfig{ServerIdentifier: clients.ServerIdentifier{ServerURI: auth2SCfg.ServerURI(), Extensions: grpctransport.ServerIdentifierExtension{ConfigName: "insecure"}}}
gSCfgMap := map[xdsclient.ServerConfig]*bootstrap.ServerConfig{expTopLevelS: topLevelSCfg, expAuth2S: auth2SCfg}
return xdsclient.Config{
Servers: []xdsclient.ServerConfig{expTopLevelS},
Node: clients.Node{ID: node.GetId(), Cluster: node.GetCluster(), Metadata: node.Metadata, UserAgentName: node.UserAgentName, UserAgentVersion: node.GetUserAgentVersion()},
Authorities: map[string]xdsclient.Authority{"auth1": {XDSServers: []xdsclient.ServerConfig{expTopLevelS}}, "auth2": {XDSServers: []xdsclient.ServerConfig{expAuth2S}}},
ResourceTypes: map[string]xdsclient.ResourceType{
version.V3ListenerURL: {TypeURL: version.V3ListenerURL, TypeName: xdsresource.ListenerResourceTypeName, AllResourcesRequiredInSotW: true, Decoder: xdsresource.NewGenericListenerResourceTypeDecoder(c)},
version.V3RouteConfigURL: {TypeURL: version.V3RouteConfigURL, TypeName: xdsresource.RouteConfigTypeName, AllResourcesRequiredInSotW: false, Decoder: xdsresource.NewGenericRouteConfigResourceTypeDecoder()},
version.V3ClusterURL: {TypeURL: version.V3ClusterURL, TypeName: xdsresource.ClusterResourceTypeName, AllResourcesRequiredInSotW: true, Decoder: xdsresource.NewGenericClusterResourceTypeDecoder(c, gSCfgMap)},
version.V3EndpointsURL: {TypeURL: version.V3EndpointsURL, TypeName: xdsresource.EndpointsResourceTypeName, AllResourcesRequiredInSotW: false, Decoder: xdsresource.NewGenericEndpointsResourceTypeDecoder()},
},
MetricsReporter: &metricsReporter{recorder: stats.NewTestMetricsRecorder(), target: testTargetName},
TransportBuilder: grpctransport.NewBuilder(map[string]grpctransport.Config{
"insecure": {
Credentials: insecure.NewBundle(),
GRPCNewClient: func(target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
opts = append(opts, topLevelSCfg.DialOptions()...)
return grpc.NewClient(target, opts...)
}},
}),
}
},
},
{
name: "server features with ignore_resource_deletion",
bootstrapContents: []byte(fmt.Sprintf(`{
"xds_servers": [{"server_uri": "%s", "channel_creds": [{"type": "insecure"}], "server_features": ["ignore_resource_deletion"]}],
"node": {"id": "%s"}
}`, testXDSServerURL, testNodeID)),
wantXDSClientConfig: func(c *bootstrap.Config) xdsclient.Config {
node, serverCfg := c.Node(), c.XDSServers()[0]
expectedServer := xdsclient.ServerConfig{ServerIdentifier: clients.ServerIdentifier{ServerURI: serverCfg.ServerURI(), Extensions: grpctransport.ServerIdentifierExtension{ConfigName: "insecure"}}, IgnoreResourceDeletion: true}
gServerCfgMap := map[xdsclient.ServerConfig]*bootstrap.ServerConfig{expectedServer: serverCfg}
return xdsclient.Config{
Servers: []xdsclient.ServerConfig{expectedServer},
Node: clients.Node{ID: node.GetId(), Cluster: node.GetCluster(), Metadata: node.Metadata, UserAgentName: node.UserAgentName, UserAgentVersion: node.GetUserAgentVersion()},
Authorities: map[string]xdsclient.Authority{},
ResourceTypes: map[string]xdsclient.ResourceType{
version.V3ListenerURL: {TypeURL: version.V3ListenerURL, TypeName: xdsresource.ListenerResourceTypeName, AllResourcesRequiredInSotW: true, Decoder: xdsresource.NewGenericListenerResourceTypeDecoder(c)},
version.V3RouteConfigURL: {TypeURL: version.V3RouteConfigURL, TypeName: xdsresource.RouteConfigTypeName, AllResourcesRequiredInSotW: false, Decoder: xdsresource.NewGenericRouteConfigResourceTypeDecoder()},
version.V3ClusterURL: {TypeURL: version.V3ClusterURL, TypeName: xdsresource.ClusterResourceTypeName, AllResourcesRequiredInSotW: true, Decoder: xdsresource.NewGenericClusterResourceTypeDecoder(c, gServerCfgMap)},
version.V3EndpointsURL: {TypeURL: version.V3EndpointsURL, TypeName: xdsresource.EndpointsResourceTypeName, AllResourcesRequiredInSotW: false, Decoder: xdsresource.NewGenericEndpointsResourceTypeDecoder()},
},
MetricsReporter: &metricsReporter{recorder: stats.NewTestMetricsRecorder(), target: testTargetName},
TransportBuilder: grpctransport.NewBuilder(map[string]grpctransport.Config{
"insecure": {
Credentials: insecure.NewBundle(),
GRPCNewClient: func(target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
opts = append(opts, serverCfg.DialOptions()...)
return grpc.NewClient(target, opts...)
}},
}),
}
},
},
{
name: "channel creds - unknown type skipped",
bootstrapContents: []byte(fmt.Sprintf(`{
"xds_servers": [{"server_uri": "%s", "channel_creds": [{"type": "unknown-type"}, {"type": "insecure"}]}],
"node": {"id": "%s"}
}`, testXDSServerURL, testNodeID)), // "insecure" is selected
wantXDSClientConfig: func(c *bootstrap.Config) xdsclient.Config {
node, serverCfg := c.Node(), c.XDSServers()[0] // SelectedCreds will be "insecure"
expectedServer := xdsclient.ServerConfig{ServerIdentifier: clients.ServerIdentifier{ServerURI: serverCfg.ServerURI(), Extensions: grpctransport.ServerIdentifierExtension{ConfigName: "insecure"}}}
gServerCfgMap := map[xdsclient.ServerConfig]*bootstrap.ServerConfig{expectedServer: serverCfg}
return xdsclient.Config{
Servers: []xdsclient.ServerConfig{expectedServer},
Node: clients.Node{ID: node.GetId(), Cluster: node.GetCluster(), Metadata: node.Metadata, UserAgentName: node.UserAgentName, UserAgentVersion: node.GetUserAgentVersion()},
Authorities: map[string]xdsclient.Authority{},
ResourceTypes: map[string]xdsclient.ResourceType{
version.V3ListenerURL: {TypeURL: version.V3ListenerURL, TypeName: xdsresource.ListenerResourceTypeName, AllResourcesRequiredInSotW: true, Decoder: xdsresource.NewGenericListenerResourceTypeDecoder(c)},
version.V3RouteConfigURL: {TypeURL: version.V3RouteConfigURL, TypeName: xdsresource.RouteConfigTypeName, AllResourcesRequiredInSotW: false, Decoder: xdsresource.NewGenericRouteConfigResourceTypeDecoder()},
version.V3ClusterURL: {TypeURL: version.V3ClusterURL, TypeName: xdsresource.ClusterResourceTypeName, AllResourcesRequiredInSotW: true, Decoder: xdsresource.NewGenericClusterResourceTypeDecoder(c, gServerCfgMap)},
version.V3EndpointsURL: {TypeURL: version.V3EndpointsURL, TypeName: xdsresource.EndpointsResourceTypeName, AllResourcesRequiredInSotW: false, Decoder: xdsresource.NewGenericEndpointsResourceTypeDecoder()},
},
MetricsReporter: &metricsReporter{recorder: stats.NewTestMetricsRecorder(), target: testTargetName},
TransportBuilder: grpctransport.NewBuilder(map[string]grpctransport.Config{
"insecure": {
Credentials: insecure.NewBundle(),
GRPCNewClient: func(target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
opts = append(opts, serverCfg.DialOptions()...)
return grpc.NewClient(target, opts...)
}},
}),
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bootstrapConfig, err := bootstrap.NewConfigFromContents(tt.bootstrapContents)
if err != nil {
t.Fatalf("Failed to create bootstrap config: %v", err)
}
gotCfg, err := buildXDSClientConfig(bootstrapConfig, stats.NewTestMetricsRecorder(), testTargetName, 0)
if err != nil {
t.Fatalf("Failed to build XDSClientConfig: %v", err)
}
wantCfg := tt.wantXDSClientConfig(bootstrapConfig)
unexportedTypeOpts := cmpopts.IgnoreUnexported(clients.Node{}, grpctransport.Builder{})
ignoreTypeOpts := cmpopts.IgnoreTypes(sync.Mutex{})
resourceTypeCmpOpts := cmp.Comparer(func(a, b xdsclient.ResourceType) bool {
return a.TypeURL == b.TypeURL && a.TypeName == b.TypeName && a.AllResourcesRequiredInSotW == b.AllResourcesRequiredInSotW && reflect.TypeOf(a.Decoder) == reflect.TypeOf(b.Decoder)
})
metricsReporterCmpOpts := cmp.Comparer(func(a, b clients.MetricsReporter) bool {
if (a == nil) != (b == nil) {
return false
}
if a == nil { // Both are nil
return true
}
// Both are non-nil, compare type and target.
aConcrete, aOK := a.(*metricsReporter)
bConcrete, bOK := b.(*metricsReporter)
if !(aOK && bOK && aConcrete.target == bConcrete.target) {
return false
}
// Compare recorder by type.
if (aConcrete.recorder == nil) != (bConcrete.recorder == nil) {
return false
}
// If both are nil, recorder check passes. If both non-nil, check types.
return aConcrete.recorder == nil || reflect.TypeOf(aConcrete.recorder) == reflect.TypeOf(bConcrete.recorder)
})
transportBuilderCmpOpts := cmp.Comparer(func(a, b grpctransport.Config) bool {
// Compare Credentials by type
credsEqual := true
if (a.Credentials == nil) != (b.Credentials == nil) {
credsEqual = false
} else if a.Credentials != nil && reflect.TypeOf(a.Credentials) != reflect.TypeOf(b.Credentials) {
credsEqual = false
}
// Compare GRPCNewClient by nil-ness
newClientEqual := (a.GRPCNewClient == nil) == (b.GRPCNewClient == nil)
return credsEqual && newClientEqual
})
if diff := cmp.Diff(wantCfg, gotCfg, protocmp.Transform(), unexportedTypeOpts, ignoreTypeOpts, resourceTypeCmpOpts, metricsReporterCmpOpts, transportBuilderCmpOpts); diff != "" {
t.Errorf("buildXDSClientConfig() mismatch (-want +got):\n%s", diff)
}
})
}
}