/
authority_test.go
353 lines (312 loc) · 12.6 KB
/
authority_test.go
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
*
* Copyright 2021 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 (
"testing"
"time"
"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"
xdstestutils "google.golang.org/grpc/xds/internal/testutils"
"google.golang.org/grpc/xds/internal/xdsclient/bootstrap"
"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
"google.golang.org/grpc/xds/internal/xdsclient/xdsresource/version"
"google.golang.org/protobuf/testing/protocmp"
)
var (
serverConfigs = []*bootstrap.ServerConfig{
{
ServerURI: testXDSServer + "0",
Creds: grpc.WithTransportCredentials(insecure.NewCredentials()),
CredsType: "creds-0",
TransportAPI: version.TransportV2,
NodeProto: xdstestutils.EmptyNodeProtoV2,
},
{
ServerURI: testXDSServer + "1",
Creds: grpc.WithTransportCredentials(insecure.NewCredentials()),
CredsType: "creds-1",
TransportAPI: version.TransportV3,
NodeProto: xdstestutils.EmptyNodeProtoV3,
},
{
ServerURI: testXDSServer + "2",
Creds: grpc.WithTransportCredentials(insecure.NewCredentials()),
CredsType: "creds-2",
TransportAPI: version.TransportV2,
NodeProto: xdstestutils.EmptyNodeProtoV2,
},
}
serverConfigCmpOptions = cmp.Options{
cmpopts.IgnoreFields(bootstrap.ServerConfig{}, "Creds"),
protocmp.Transform(),
}
)
// watchAndFetchNewController starts a CDS watch on the client for the given
// resourceName, and tries to receive a new controller from the ctrlCh.
//
// It returns false if there's no controller in the ctrlCh.
func watchAndFetchNewController(t *testing.T, client *clientImpl, resourceName string, ctrlCh *testutils.Channel) (*testController, bool, func()) {
updateCh := testutils.NewChannel()
cancelWatch := client.WatchCluster(resourceName, func(update xdsresource.ClusterUpdate, err error) {
updateCh.Send(xdsresource.ClusterUpdateErrTuple{Update: update, Err: err})
})
// Clear the item in the watch channel, otherwise the next watch will block.
authority := xdsresource.ParseName(resourceName).Authority
var config *bootstrap.ServerConfig
if authority == "" {
config = client.config.XDSServer
} else {
authConfig, ok := client.config.Authorities[authority]
if !ok {
t.Fatalf("failed to find authority %q", authority)
}
config = authConfig.XDSServer
}
a := client.authorities[config.String()]
if a == nil {
t.Fatalf("authority for %q is not created", authority)
}
ctrlTemp := a.controller.(*testController)
// Clear the channel so the next watch on this controller can proceed.
ctrlTemp.addWatches[xdsresource.ClusterResource].ReceiveOrFail()
cancelWatchRet := func() {
cancelWatch()
ctrlTemp.removeWatches[xdsresource.ClusterResource].ReceiveOrFail()
}
// Try to receive a new controller.
c, ok := ctrlCh.ReceiveOrFail()
if !ok {
return nil, false, cancelWatchRet
}
ctrl := c.(*testController)
return ctrl, true, cancelWatchRet
}
// TestAuthorityDefaultAuthority covers that a watch for an old style resource
// name (one without authority) builds a controller using the top level server
// config.
func (s) TestAuthorityDefaultAuthority(t *testing.T) {
overrideFedEnvVar(t)
ctrlCh := overrideNewController(t)
client, err := newWithConfig(&bootstrap.Config{
XDSServer: serverConfigs[0],
Authorities: map[string]*bootstrap.Authority{testAuthority: {XDSServer: serverConfigs[1]}},
}, defaultWatchExpiryTimeout, defaultIdleAuthorityDeleteTimeout)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
t.Cleanup(client.Close)
ctrl, ok, _ := watchAndFetchNewController(t, client, testCDSName, ctrlCh)
if !ok {
t.Fatalf("want a new controller to be built, got none")
}
// Want the default server config.
wantConfig := serverConfigs[0]
if diff := cmp.Diff(ctrl.config, wantConfig, serverConfigCmpOptions); diff != "" {
t.Fatalf("controller is built with unexpected config, diff (-got +want): %v", diff)
}
}
// TestAuthorityNoneDefaultAuthority covers that a watch with a new style
// resource name creates a controller with the corresponding server config.
func (s) TestAuthorityNoneDefaultAuthority(t *testing.T) {
overrideFedEnvVar(t)
ctrlCh := overrideNewController(t)
client, err := newWithConfig(&bootstrap.Config{
XDSServer: serverConfigs[0],
Authorities: map[string]*bootstrap.Authority{testAuthority: {XDSServer: serverConfigs[1]}},
}, defaultWatchExpiryTimeout, defaultIdleAuthorityDeleteTimeout)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
t.Cleanup(client.Close)
resourceName := xdstestutils.BuildResourceName(xdsresource.ClusterResource, testAuthority, testCDSName, nil)
ctrl, ok, _ := watchAndFetchNewController(t, client, resourceName, ctrlCh)
if !ok {
t.Fatalf("want a new controller to be built, got none")
}
// Want the server config for this authority.
wantConfig := serverConfigs[1]
if diff := cmp.Diff(ctrl.config, wantConfig, serverConfigCmpOptions); diff != "" {
t.Fatalf("controller is built with unexpected config, diff (-got +want): %v", diff)
}
}
// TestAuthorityShare covers that
// - watch with the same authority name doesn't create new authority
// - watch with different authority name but same authority config doesn't
// create new authority
func (s) TestAuthorityShare(t *testing.T) {
overrideFedEnvVar(t)
ctrlCh := overrideNewController(t)
client, err := newWithConfig(&bootstrap.Config{
XDSServer: serverConfigs[0],
Authorities: map[string]*bootstrap.Authority{
testAuthority: {XDSServer: serverConfigs[1]},
testAuthority2: {XDSServer: serverConfigs[1]}, // Another authority name, but with the same config.
},
}, defaultWatchExpiryTimeout, defaultIdleAuthorityDeleteTimeout)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
t.Cleanup(client.Close)
resourceName := xdstestutils.BuildResourceName(xdsresource.ClusterResource, testAuthority, testCDSName, nil)
ctrl1, ok1, _ := watchAndFetchNewController(t, client, resourceName, ctrlCh)
if !ok1 {
t.Fatalf("want a new controller to be built, got none")
}
// Want the server config for this authority.
wantConfig := serverConfigs[1]
if diff := cmp.Diff(ctrl1.config, wantConfig, serverConfigCmpOptions); diff != "" {
t.Fatalf("controller is built with unexpected config, diff (-got +want): %v", diff)
}
// Call the watch with the same authority name. This shouldn't create a new
// controller.
resourceNameSameAuthority := xdstestutils.BuildResourceName(xdsresource.ClusterResource, testAuthority, testCDSName+"1", nil)
ctrl2, ok2, _ := watchAndFetchNewController(t, client, resourceNameSameAuthority, ctrlCh)
if ok2 {
t.Fatalf("an unexpected controller is built with config: %v", ctrl2.config)
}
// Call the watch with a different authority name, but the same server
// config. This shouldn't create a new controller.
resourceNameSameConfig := xdstestutils.BuildResourceName(xdsresource.ClusterResource, testAuthority2, testCDSName+"1", nil)
if ctrl, ok, _ := watchAndFetchNewController(t, client, resourceNameSameConfig, ctrlCh); ok {
t.Fatalf("an unexpected controller is built with config: %v", ctrl.config)
}
}
// TestAuthorityIdle covers that
// - authorities are put in a timeout cache when the last watch is canceled
// - idle authorities are not immediately closed. They will be closed after a
// timeout.
func (s) TestAuthorityIdleTimeout(t *testing.T) {
overrideFedEnvVar(t)
ctrlCh := overrideNewController(t)
const idleTimeout = 50 * time.Millisecond
client, err := newWithConfig(&bootstrap.Config{
XDSServer: serverConfigs[0],
Authorities: map[string]*bootstrap.Authority{
testAuthority: {XDSServer: serverConfigs[1]},
},
}, defaultWatchExpiryTimeout, idleTimeout)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
t.Cleanup(client.Close)
resourceName := xdstestutils.BuildResourceName(xdsresource.ClusterResource, testAuthority, testCDSName, nil)
ctrl1, ok1, cancelWatch1 := watchAndFetchNewController(t, client, resourceName, ctrlCh)
if !ok1 {
t.Fatalf("want a new controller to be built, got none")
}
var cancelWatch2 func()
// Call the watch with the same authority name. This shouldn't create a new
// controller.
resourceNameSameAuthority := xdstestutils.BuildResourceName(xdsresource.ClusterResource, testAuthority, testCDSName+"1", nil)
ctrl2, ok2, cancelWatch2 := watchAndFetchNewController(t, client, resourceNameSameAuthority, ctrlCh)
if ok2 {
t.Fatalf("an unexpected controller is built with config: %v", ctrl2.config)
}
cancelWatch1()
if ctrl1.done.HasFired() {
t.Fatalf("controller is closed immediately when the watch is canceled, wanted to be put in the idle cache")
}
// Cancel the second watch, should put controller in the idle cache.
cancelWatch2()
if ctrl1.done.HasFired() {
t.Fatalf("controller is closed when the second watch is closed")
}
time.Sleep(idleTimeout * 2)
if !ctrl1.done.HasFired() {
t.Fatalf("controller is not closed after idle timeout")
}
}
// TestAuthorityClientClose covers that the authorities in use and in idle cache
// are all closed when the client is closed.
func (s) TestAuthorityClientClose(t *testing.T) {
overrideFedEnvVar(t)
ctrlCh := overrideNewController(t)
client, err := newWithConfig(&bootstrap.Config{
XDSServer: serverConfigs[0],
Authorities: map[string]*bootstrap.Authority{
testAuthority: {XDSServer: serverConfigs[1]},
},
}, defaultWatchExpiryTimeout, defaultIdleAuthorityDeleteTimeout)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
t.Cleanup(client.Close)
resourceName := testCDSName
ctrl1, ok1, cancelWatch1 := watchAndFetchNewController(t, client, resourceName, ctrlCh)
if !ok1 {
t.Fatalf("want a new controller to be built, got none")
}
resourceNameWithAuthority := xdstestutils.BuildResourceName(xdsresource.ClusterResource, testAuthority, testCDSName, nil)
ctrl2, ok2, _ := watchAndFetchNewController(t, client, resourceNameWithAuthority, ctrlCh)
if !ok2 {
t.Fatalf("want a new controller to be built, got none")
}
cancelWatch1()
if ctrl1.done.HasFired() {
t.Fatalf("controller is closed immediately when the watch is canceled, wanted to be put in the idle cache")
}
// Close the client while watch2 is not canceled. ctrl1 is in the idle
// cache, ctrl2 is in use. Both should be closed.
client.Close()
if !ctrl1.done.HasFired() {
t.Fatalf("controller in idle cache is not closed after client is closed")
}
if !ctrl2.done.HasFired() {
t.Fatalf("controller in use is not closed after client is closed")
}
}
// TestAuthorityRevive covers that the authorities in the idle cache is revived
// when a new watch is started on this authority.
func (s) TestAuthorityRevive(t *testing.T) {
overrideFedEnvVar(t)
ctrlCh := overrideNewController(t)
const idleTimeout = 50 * time.Millisecond
client, err := newWithConfig(&bootstrap.Config{
XDSServer: serverConfigs[0],
Authorities: map[string]*bootstrap.Authority{
testAuthority: {XDSServer: serverConfigs[1]},
},
}, defaultWatchExpiryTimeout, idleTimeout)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
t.Cleanup(client.Close)
// Start a watch on the authority, and cancel it. This puts the authority in
// the idle cache.
resourceName := xdstestutils.BuildResourceName(xdsresource.ClusterResource, testAuthority, testCDSName, nil)
ctrl1, ok1, cancelWatch1 := watchAndFetchNewController(t, client, resourceName, ctrlCh)
if !ok1 {
t.Fatalf("want a new controller to be built, got none")
}
cancelWatch1()
// Start another watch on this authority, it should retrieve the authority
// from the cache, instead of creating a new one.
resourceNameWithAuthority := xdstestutils.BuildResourceName(xdsresource.ClusterResource, testAuthority, testCDSName+"1", nil)
ctrl2, ok2, _ := watchAndFetchNewController(t, client, resourceNameWithAuthority, ctrlCh)
if ok2 {
t.Fatalf("an unexpected controller is built with config: %v", ctrl2.config)
}
// Wait for double the idle timeout, the controller shouldn't be closed,
// since it was revived.
time.Sleep(idleTimeout * 2)
if ctrl1.done.HasFired() {
t.Fatalf("controller that was revived is closed after timeout, want not closed")
}
}