forked from GoogleCloudPlatform/alloydb-go-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialer_test.go
553 lines (495 loc) · 14.6 KB
/
dialer_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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Copyright 2021 Google LLC
//
// 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
//
// https://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 alloydbconn
import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io"
"net"
"os"
"strings"
"sync"
"testing"
"time"
alloydbadmin "cloud.google.com/go/alloydb/apiv1alpha"
"cloud.google.com/go/alloydbconn/errtype"
"cloud.google.com/go/alloydbconn/internal/alloydb"
"cloud.google.com/go/alloydbconn/internal/mock"
"golang.org/x/oauth2"
"google.golang.org/api/option"
)
const testInstanceURI = "projects/my-project/locations/my-region/" +
"clusters/my-cluster/instances/my-instance"
type stubTokenSource struct{}
func (stubTokenSource) Token() (*oauth2.Token, error) {
return &oauth2.Token{}, nil
}
func TestDialerCanConnectToInstance(t *testing.T) {
ctx := context.Background()
inst := mock.NewFakeInstance(
"my-project", "my-region", "my-cluster", "my-instance",
)
mc, url, cleanup := mock.HTTPClient(
mock.InstanceGetSuccess(inst, 1),
mock.CreateEphemeralSuccess(inst, 1),
)
stop := mock.StartServerProxy(t, inst)
defer func() {
stop()
if err := cleanup(); err != nil {
t.Fatalf("%v", err)
}
}()
c, err := alloydbadmin.NewAlloyDBAdminRESTClient(
ctx, option.WithHTTPClient(mc), option.WithEndpoint(url))
if err != nil {
t.Fatalf("expected NewClient to succeed, but got error: %v", err)
}
d, err := NewDialer(ctx, WithTokenSource(stubTokenSource{}))
if err != nil {
t.Fatalf("expected NewDialer to succeed, but got error: %v", err)
}
d.client = c
// Run several tests to ensure the underlying shared buffer is properly
// reset between connections.
for i := 0; i < 10; i++ {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
conn, err := d.Dial(ctx, testInstanceURI)
if err != nil {
t.Fatalf("expected Dial to succeed, but got error: %v", err)
}
defer conn.Close()
data, err := io.ReadAll(conn)
if err != nil {
t.Fatalf("expected ReadAll to succeed, got error %v", err)
}
if string(data) != "my-instance" {
t.Fatalf("expected known response from the server, but got %v", string(data))
}
})
}
}
func writeStaticInfo(t *testing.T, i mock.FakeAlloyDBInstance) io.Reader {
t.Helper()
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
pub := x509.MarshalPKCS1PublicKey(&key.PublicKey)
pubPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PUBLIC KEY", Bytes: pub})
if pubPEM == nil {
t.Fatal("public key encoding failed")
}
priv := x509.MarshalPKCS1PrivateKey(key)
privPEM := pem.EncodeToMemory(
&pem.Block{Type: "OPENSSH PRIVATE KEY", Bytes: priv},
)
if privPEM == nil {
t.Fatal("private key encoding failed")
}
static := map[string]interface{}{}
static["publicKey"] = string(pubPEM)
static["privateKey"] = string(privPEM)
info := make(map[string]interface{})
info["ipAddress"] = "127.0.0.1" // "private" IP is localhost in testing
chain, err := i.GeneratePEMCertificateChain(&key.PublicKey)
if err != nil {
t.Fatal(err)
}
info["pemCertificateChain"] = chain
info["caCert"] = chain[len(chain)-1] // CA cert is last in chain
static[i.String()] = info
data, err := json.Marshal(static)
if err != nil {
t.Fatal(err)
}
return bytes.NewReader(data)
}
func TestDialerWorksWithStaticConnectionInfo(t *testing.T) {
ctx := context.Background()
inst := mock.NewFakeInstance(
"my-project", "my-region", "my-cluster", "my-instance",
)
stop := mock.StartServerProxy(t, inst)
t.Cleanup(stop)
staticPath := writeStaticInfo(t, inst)
d, err := NewDialer(
ctx,
WithTokenSource(stubTokenSource{}),
WithStaticConnectionInfo(staticPath),
)
if err != nil {
t.Fatalf("expected NewDialer to succeed, but got error: %v", err)
}
conn, err := d.Dial(ctx, testInstanceURI)
if err != nil {
t.Fatalf("expected Dial to succeed, but got error: %v", err)
}
defer conn.Close()
data, err := io.ReadAll(conn)
if err != nil {
t.Fatalf("expected ReadAll to succeed, got error %v", err)
}
if string(data) != "my-instance" {
t.Fatalf("expected known response from the server, but got %v", string(data))
}
}
func TestDialWithAdminAPIErrors(t *testing.T) {
ctx := context.Background()
mc, url, cleanup := mock.HTTPClient()
defer func() {
if err := cleanup(); err != nil {
t.Fatalf("%v", err)
}
}()
c, err := alloydbadmin.NewAlloyDBAdminRESTClient(ctx, option.WithHTTPClient(mc), option.WithEndpoint(url))
if err != nil {
t.Fatalf("expected NewClient to succeed, but got error: %v", err)
}
d, err := NewDialer(ctx, WithTokenSource(stubTokenSource{}))
if err != nil {
t.Fatalf("expected NewDialer to succeed, but got error: %v", err)
}
d.client = c
_, err = d.Dial(ctx, "bad-instance-name")
var wantErr1 *errtype.ConfigError
if !errors.As(err, &wantErr1) {
t.Fatalf("when instance name is invalid, want = %T, got = %v", wantErr1, err)
}
ctx, cancel := context.WithCancel(ctx)
cancel()
_, err = d.Dial(ctx, testInstanceURI)
if !errors.Is(err, context.Canceled) {
t.Fatalf("when context is canceled, want = %T, got = %v", context.Canceled, err)
}
_, err = d.Dial(context.Background(), testInstanceURI)
var wantErr2 *errtype.RefreshError
if !errors.As(err, &wantErr2) {
t.Fatalf("when API call fails, want = %T, got = %v", wantErr2, err)
}
}
func TestDialWithUnavailableServerErrors(t *testing.T) {
ctx := context.Background()
inst := mock.NewFakeInstance(
"my-project", "my-region", "my-cluster", "my-instance",
)
// Don't use the cleanup function. Because this test is about error
// cases, API requests (started in two separate goroutines) will
// sometimes succeed and clear the mock, and sometimes not.
// This test is about error return values from Dial, not API interaction.
mc, url, _ := mock.HTTPClient(
mock.InstanceGetSuccess(inst, 2),
mock.CreateEphemeralSuccess(inst, 2),
)
c, err := alloydbadmin.NewAlloyDBAdminRESTClient(ctx, option.WithHTTPClient(mc), option.WithEndpoint(url))
if err != nil {
t.Fatalf("expected NewClient to succeed, but got error: %v", err)
}
d, err := NewDialer(ctx, WithTokenSource(stubTokenSource{}))
if err != nil {
t.Fatalf("expected NewDialer to succeed, but got error: %v", err)
}
d.client = c
_, err = d.Dial(ctx, testInstanceURI)
var wantErr2 *errtype.DialError
if !errors.As(err, &wantErr2) {
t.Fatalf("when server proxy socket is unavailable, want = %T, got = %v", wantErr2, err)
}
}
func TestDialerWithCustomDialFunc(t *testing.T) {
ctx := context.Background()
inst := mock.NewFakeInstance(
"my-project", "my-region", "my-cluster", "my-instance",
)
mc, url, cleanup := mock.HTTPClient(
mock.InstanceGetSuccess(inst, 1),
mock.CreateEphemeralSuccess(inst, 1),
)
stop := mock.StartServerProxy(t, inst)
defer func() {
stop()
if err := cleanup(); err != nil {
t.Fatalf("%v", err)
}
}()
c, err := alloydbadmin.NewAlloyDBAdminRESTClient(ctx, option.WithHTTPClient(mc), option.WithEndpoint(url))
if err != nil {
t.Fatalf("expected NewClient to succeed, but got error: %v", err)
}
d, err := NewDialer(ctx,
WithDialFunc(func(_ context.Context, _, _ string) (net.Conn, error) {
return nil, errors.New("sentinel error")
}),
WithTokenSource(stubTokenSource{}),
)
if err != nil {
t.Fatalf("expected NewDialer to succeed, but got error: %v", err)
}
d.client = c
_, err = d.Dial(ctx, testInstanceURI)
if !strings.Contains(err.Error(), "sentinel error") {
t.Fatalf("want = sentinel error, got = %v", err)
}
}
func TestDialerUserAgent(t *testing.T) {
data, err := os.ReadFile("version.txt")
if err != nil {
t.Fatalf("failed to read version.txt: %v", err)
}
ver := strings.TrimSpace(string(data))
want := "alloydb-go-connector/" + ver
if want != userAgent {
t.Errorf("embed version mismatched: want %q, got %q", want, userAgent)
}
}
func TestDialerRemovesInvalidInstancesFromCache(t *testing.T) {
// When a dialer attempts to retrieve connection info for a
// non-existent instance, it should delete the instance from
// the cache and ensure no background refresh happens (which would be
// wasted cycles).
d, err := NewDialer(context.Background(),
WithTokenSource(stubTokenSource{}),
WithRefreshTimeout(time.Second),
)
if err != nil {
t.Fatalf("expected NewDialer to succeed, but got error: %v", err)
}
defer func(d *Dialer) {
err := d.Close()
if err != nil {
t.Log(err)
}
}(d)
// Populate instance map with connection info cache that will always fail.
// This allows the test to verify the error case path invoking close.
badInstanceName := "projects/bad/locations/bad/clusters/bad/instances/bad"
tcs := []struct {
desc string
uri string
resp connectionInfoResp
opts []DialOption
}{
{
desc: "dialing a bad instance URI",
uri: badInstanceName,
resp: connectionInfoResp{
err: errors.New("connect info failed"),
},
},
{
desc: "specifying an invalid IP type",
uri: testInstanceURI,
resp: connectionInfoResp{
info: alloydb.ConnectionInfo{
IPAddrs: map[string]string{
// no public IP
alloydb.PrivateIP: "10.0.0.1",
},
Expiration: time.Now().Add(time.Hour),
},
},
opts: []DialOption{WithPublicIP()},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
// Manually populate the internal cache with a spy
inst, _ := alloydb.ParseInstURI(tc.uri)
spy := &spyConnectionInfoCache{
connectInfoCalls: []connectionInfoResp{tc.resp},
}
d.cache[inst] = monitoredCache{
connectionInfoCache: spy,
}
_, err = d.Dial(context.Background(), tc.uri, tc.opts...)
if err == nil {
t.Fatal("expected Dial to return error")
}
// Verify that the connection info cache was closed (to prevent
// further failed refresh operations)
if got, want := spy.CloseWasCalled(), true; got != want {
t.Fatal("Close was not called")
}
// Now verify that bad connection name has been deleted from map.
d.lock.RLock()
_, ok := d.cache[inst]
d.lock.RUnlock()
if ok {
t.Fatal("connection info was not removed from cache")
}
})
}
}
func TestDialRefreshesExpiredCertificates(t *testing.T) {
d, err := NewDialer(
context.Background(),
WithTokenSource(stubTokenSource{}),
)
if err != nil {
t.Fatalf("expected NewDialer to succeed, but got error: %v", err)
}
sentinel := errors.New("connect info failed")
inst := testInstanceURI
cn, _ := alloydb.ParseInstURI(inst)
spy := &spyConnectionInfoCache{
connectInfoCalls: []connectionInfoResp{
// First call returns expired certificate
{
info: alloydb.ConnectionInfo{
Expiration: time.Now().Add(-10 * time.Hour),
},
},
// Second call errors to validate error path
{
err: sentinel,
},
},
}
d.cache[cn] = monitoredCache{
connectionInfoCache: spy,
}
_, err = d.Dial(context.Background(), inst)
if !errors.Is(err, sentinel) {
t.Fatalf("expected Dial to return sentinel error, instead got = %v", err)
}
// Verify that the cache was refreshed
if got, want := spy.ForceRefreshWasCalled(), true; got != want {
t.Fatal("ForceRefresh was not called")
}
// Verify that the connection info cache was closed (to prevent
// further failed refresh operations)
if got, want := spy.CloseWasCalled(), true; got != want {
t.Fatal("Close was not called")
}
// Now verify that bad connection name has been deleted from map.
d.lock.RLock()
_, ok := d.cache[cn]
d.lock.RUnlock()
if ok {
t.Fatal("bad instance was not removed from the cache")
}
}
type connectionInfoResp struct {
info alloydb.ConnectionInfo
err error
}
type spyConnectionInfoCache struct {
mu sync.Mutex
connectInfoIndex int
connectInfoCalls []connectionInfoResp
closed bool
forceRefreshWasCalled bool
// embed interface to avoid having to implement irrelevant methods
connectionInfoCache
}
func (s *spyConnectionInfoCache) ConnectionInfo(context.Context) (alloydb.ConnectionInfo, error) {
s.mu.Lock()
defer s.mu.Unlock()
res := s.connectInfoCalls[s.connectInfoIndex]
s.connectInfoIndex++
return res.info, res.err
}
func (s *spyConnectionInfoCache) ForceRefresh() {
s.mu.Lock()
defer s.mu.Unlock()
s.forceRefreshWasCalled = true
}
func (s *spyConnectionInfoCache) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
s.closed = true
return nil
}
func (s *spyConnectionInfoCache) CloseWasCalled() bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.closed
}
func (s *spyConnectionInfoCache) ForceRefreshWasCalled() bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.forceRefreshWasCalled
}
func TestDialerSupportsOneOffDialFunction(t *testing.T) {
ctx := context.Background()
inst := mock.NewFakeInstance(
"my-project", "my-region", "my-cluster", "my-instance",
)
mc, url, cleanup := mock.HTTPClient(
mock.InstanceGetSuccess(inst, 1),
mock.CreateEphemeralSuccess(inst, 1),
)
stop := mock.StartServerProxy(t, inst)
defer func() {
stop()
if err := cleanup(); err != nil {
t.Fatalf("%v", err)
}
}()
c, err := alloydbadmin.NewAlloyDBAdminRESTClient(ctx, option.WithHTTPClient(mc), option.WithEndpoint(url))
if err != nil {
t.Fatalf("expected NewClient to succeed, but got error: %v", err)
}
d, err := NewDialer(ctx,
WithDialFunc(func(_ context.Context, _, _ string) (net.Conn, error) {
return nil, errors.New("sentinel error")
}),
WithTokenSource(stubTokenSource{}),
)
if err != nil {
t.Fatalf("expected NewDialer to succeed, but got error: %v", err)
}
d.client = c
defer func() {
if err := d.Close(); err != nil {
t.Log(err)
}
_ = cleanup()
}()
sentinelErr := errors.New("dial func was called")
f := func(context.Context, string, string) (net.Conn, error) {
return nil, sentinelErr
}
_, err = d.Dial(ctx, testInstanceURI, WithOneOffDialFunc(f))
if !errors.Is(err, sentinelErr) {
t.Fatal("one-off dial func was not called")
}
}
func TestDialerCloseReportsFriendlyError(t *testing.T) {
d, err := NewDialer(
context.Background(),
WithTokenSource(stubTokenSource{}),
)
if err != nil {
t.Fatal(err)
}
_ = d.Close()
_, err = d.Dial(context.Background(), testInstanceURI)
if !errors.Is(err, ErrDialerClosed) {
t.Fatalf("want = %v, got = %v", ErrDialerClosed, err)
}
// Ensure multiple calls to close don't panic
_ = d.Close()
_, err = d.Dial(context.Background(), testInstanceURI)
if !errors.Is(err, ErrDialerClosed) {
t.Fatalf("want = %v, got = %v", ErrDialerClosed, err)
}
}