-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdriver_test.go
591 lines (556 loc) · 19.6 KB
/
driver_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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// 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 spannerdriver
import (
"context"
"database/sql/driver"
"net"
"reflect"
"testing"
"time"
"cloud.google.com/go/spanner"
"cloud.google.com/go/spanner/apiv1/spannerpb"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"google.golang.org/grpc/codes"
)
func TestExtractDnsParts(t *testing.T) {
tests := []struct {
input string
wantConnectorConfig connectorConfig
wantSpannerConfig spanner.ClientConfig
wantErr bool
}{
{
input: "projects/p/instances/i/databases/d",
wantConnectorConfig: connectorConfig{
project: "p",
instance: "i",
database: "d",
params: map[string]string{},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
},
},
{
input: "projects/DEFAULT_PROJECT_ID/instances/test-instance/databases/test-database",
wantConnectorConfig: connectorConfig{
project: "DEFAULT_PROJECT_ID",
instance: "test-instance",
database: "test-database",
params: map[string]string{},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
},
},
{
input: "localhost:9010/projects/p/instances/i/databases/d",
wantConnectorConfig: connectorConfig{
host: "localhost:9010",
project: "p",
instance: "i",
database: "d",
params: map[string]string{},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
},
},
{
input: "spanner.googleapis.com/projects/p/instances/i/databases/d",
wantConnectorConfig: connectorConfig{
host: "spanner.googleapis.com",
project: "p",
instance: "i",
database: "d",
params: map[string]string{},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
},
},
{
input: "spanner.googleapis.com/projects/p/instances/i/databases/d?usePlainText=true",
wantConnectorConfig: connectorConfig{
host: "spanner.googleapis.com",
project: "p",
instance: "i",
database: "d",
params: map[string]string{
"useplaintext": "true",
},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
DisableNativeMetrics: true,
},
},
{
input: "spanner.googleapis.com/projects/p/instances/i/databases/d;credentials=/path/to/credentials.json",
wantConnectorConfig: connectorConfig{
host: "spanner.googleapis.com",
project: "p",
instance: "i",
database: "d",
params: map[string]string{
"credentials": "/path/to/credentials.json",
},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
},
},
{
input: "spanner.googleapis.com/projects/p/instances/i/databases/d?credentials=/path/to/credentials.json;readonly=true",
wantConnectorConfig: connectorConfig{
host: "spanner.googleapis.com",
project: "p",
instance: "i",
database: "d",
params: map[string]string{
"credentials": "/path/to/credentials.json",
"readonly": "true",
},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
},
},
{
input: "spanner.googleapis.com/projects/p/instances/i/databases/d?usePlainText=true;",
wantConnectorConfig: connectorConfig{
host: "spanner.googleapis.com",
project: "p",
instance: "i",
database: "d",
params: map[string]string{
"useplaintext": "true",
},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
UserAgent: userAgent,
DisableNativeMetrics: true,
},
},
{
input: "spanner.googleapis.com/projects/p/instances/i/databases/d?minSessions=200;maxSessions=1000;numChannels=10;disableRouteToLeader=true;enableEndToEndTracing=true;disableNativeMetrics=true;rpcPriority=Medium;optimizerVersion=1;optimizerStatisticsPackage=latest;databaseRole=child",
wantConnectorConfig: connectorConfig{
host: "spanner.googleapis.com",
project: "p",
instance: "i",
database: "d",
params: map[string]string{
"minsessions": "200",
"maxsessions": "1000",
"numchannels": "10",
"disableroutetoleader": "true",
"enableendtoendtracing": "true",
"disablenativemetrics": "true",
"rpcpriority": "Medium",
"optimizerversion": "1",
"optimizerstatisticspackage": "latest",
"databaserole": "child",
},
},
wantSpannerConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.SessionPoolConfig{
MinOpened: 200,
MaxOpened: 1000,
WriteSessions: 0.2,
HealthCheckInterval: spanner.DefaultSessionPoolConfig.HealthCheckInterval,
HealthCheckWorkers: spanner.DefaultSessionPoolConfig.HealthCheckWorkers,
MaxBurst: spanner.DefaultSessionPoolConfig.MaxBurst, //lint:ignore SA1019 because it's a spanner default.
MaxIdle: spanner.DefaultSessionPoolConfig.MaxIdle,
TrackSessionHandles: spanner.DefaultSessionPoolConfig.TrackSessionHandles,
InactiveTransactionRemovalOptions: spanner.DefaultSessionPoolConfig.InactiveTransactionRemovalOptions,
},
UserAgent: userAgent,
DisableRouteToLeader: true,
EnableEndToEndTracing: true,
DisableNativeMetrics: true,
QueryOptions: spanner.QueryOptions{Priority: spannerpb.RequestOptions_PRIORITY_MEDIUM, Options: &spannerpb.ExecuteSqlRequest_QueryOptions{OptimizerVersion: "1", OptimizerStatisticsPackage: "latest"}},
ReadOptions: spanner.ReadOptions{Priority: spannerpb.RequestOptions_PRIORITY_MEDIUM},
TransactionOptions: spanner.TransactionOptions{CommitPriority: spannerpb.RequestOptions_PRIORITY_MEDIUM},
DatabaseRole: "child",
},
},
{
// intentional error case
input: "project/p/instances/i/databases/d",
wantErr: true,
},
}
for _, tc := range tests {
t.Run(tc.input, func(t *testing.T) {
config, err := extractConnectorConfig(tc.input)
if err != nil {
if tc.wantErr {
return
}
t.Errorf("extract failed for %q: %v", tc.input, err)
} else {
if tc.wantErr {
t.Error("did not encounter expected error")
}
if diff := cmp.Diff(config, tc.wantConnectorConfig, cmp.AllowUnexported(connectorConfig{})); diff != "" {
t.Errorf("connector config mismatch for %q\n%v", tc.input, diff)
}
conn, err := newConnector(&Driver{connectors: make(map[string]*connector)}, tc.input)
if err != nil {
t.Errorf("failed to get connector for %q: %v", tc.input, err)
}
if diff := cmp.Diff(conn.spannerClientConfig, tc.wantSpannerConfig, cmpopts.IgnoreUnexported(spanner.ClientConfig{}, spanner.SessionPoolConfig{}, spanner.InactiveTransactionRemovalOptions{}, spannerpb.ExecuteSqlRequest_QueryOptions{})); diff != "" {
t.Errorf("connector Spanner client config mismatch for %q\n%v", tc.input, diff)
}
}
})
}
}
func TestConnection_Reset(t *testing.T) {
txClosed := false
c := conn{
readOnlyStaleness: spanner.ExactStaleness(time.Second),
batch: &batch{tp: dml},
commitTs: &time.Time{},
tx: &readOnlyTransaction{
close: func() {
txClosed = true
},
},
}
if err := c.ResetSession(context.Background()); err != nil {
t.Fatalf("failed to reset session: %v", err)
}
if !cmp.Equal(c.readOnlyStaleness, spanner.TimestampBound{}, cmp.AllowUnexported(spanner.TimestampBound{})) {
t.Error("failed to reset read-only staleness")
}
if c.inBatch() {
t.Error("failed to clear batch")
}
if c.commitTs != nil {
t.Errorf("failed to clear commit timestamp")
}
if !txClosed {
t.Error("failed to close transaction")
}
}
func TestConnection_NoNestedTransactions(t *testing.T) {
c := conn{
tx: &readOnlyTransaction{},
}
_, err := c.BeginTx(context.Background(), driver.TxOptions{})
if err == nil {
t.Errorf("unexpected nil error for BeginTx call")
}
}
func TestConn_AbortBatch(t *testing.T) {
c := &conn{}
if err := c.StartBatchDDL(); err != nil {
t.Fatalf("failed to start DDL batch: %v", err)
}
if !c.InDDLBatch() {
t.Fatal("connection did not start DDL batch")
}
if err := c.AbortBatch(); err != nil {
t.Fatalf("failed to abort DDL batch: %v", err)
}
if c.InDDLBatch() {
t.Fatal("connection did not abort DDL batch")
}
if err := c.StartBatchDML(); err != nil {
t.Fatalf("failed to start DML batch: %v", err)
}
if !c.InDMLBatch() {
t.Fatal("connection did not start DML batch")
}
if err := c.AbortBatch(); err != nil {
t.Fatalf("failed to abort DML batch: %v", err)
}
if c.InDMLBatch() {
t.Fatal("connection did not abort DML batch")
}
}
func TestConn_StartBatchDdl(t *testing.T) {
for _, test := range []struct {
name string
c *conn
wantErr bool
}{
{"Default", &conn{}, false},
{"In DDL batch", &conn{batch: &batch{tp: ddl}}, true},
{"In DML batch", &conn{batch: &batch{tp: dml}}, true},
{"In read/write transaction", &conn{tx: &readWriteTransaction{}}, true},
{"In read-only transaction", &conn{tx: &readOnlyTransaction{}}, true},
{"In read/write transaction with a DML batch", &conn{tx: &readWriteTransaction{batch: &batch{tp: dml}}}, true},
} {
err := test.c.StartBatchDDL()
if test.wantErr {
if err == nil {
t.Fatalf("%s: no error returned for StartDdlBatch: %v", test.name, err)
}
} else {
if err != nil {
t.Fatalf("%s: failed to start DDL batch: %v", test.name, err)
}
if !test.c.InDDLBatch() {
t.Fatalf("%s: connection did not start DDL batch", test.name)
}
}
}
}
func TestConn_StartBatchDml(t *testing.T) {
for _, test := range []struct {
name string
c *conn
wantErr bool
}{
{"Default", &conn{}, false},
{"In DDL batch", &conn{batch: &batch{tp: ddl}}, true},
{"In DML batch", &conn{batch: &batch{tp: dml}}, true},
{"In read/write transaction", &conn{tx: &readWriteTransaction{}}, false},
{"In read-only transaction", &conn{tx: &readOnlyTransaction{}}, true},
{"In read/write transaction with a DML batch", &conn{tx: &readWriteTransaction{batch: &batch{tp: dml}}}, true},
} {
err := test.c.StartBatchDML()
if test.wantErr {
if err == nil {
t.Fatalf("%s: no error returned for StartDmlBatch: %v", test.name, err)
}
} else {
if err != nil {
t.Fatalf("%s: failed to start DML batch: %v", test.name, err)
}
if !test.c.InDMLBatch() {
t.Fatalf("%s: connection did not start DML batch", test.name)
}
}
}
}
func TestConn_NonDdlStatementsInDdlBatch(t *testing.T) {
c := &conn{
batch: &batch{tp: ddl},
execSingleQuery: func(ctx context.Context, c *spanner.Client, statement spanner.Statement, tb spanner.TimestampBound) *spanner.RowIterator {
return &spanner.RowIterator{}
},
execSingleDMLTransactional: func(ctx context.Context, c *spanner.Client, statement spanner.Statement, options spanner.TransactionOptions) (int64, time.Time, error) {
return 0, time.Time{}, nil
},
execSingleDMLPartitioned: func(ctx context.Context, c *spanner.Client, statement spanner.Statement, options spanner.QueryOptions) (int64, error) {
return 0, nil
},
}
ctx := context.Background()
// Starting a transaction while in a DDL batch is not allowed.
if _, err := c.BeginTx(ctx, driver.TxOptions{}); err == nil {
t.Fatal("missing error for BeginTx")
}
// Starting a DML batch while in a DDL batch is not allowed.
if err := c.StartBatchDML(); err == nil {
t.Fatalf("missing error for StartBatchDML")
}
// Executing a single DML or query during a DDL batch is allowed.
if _, err := c.ExecContext(ctx, "INSERT INTO Foo (Id, Value) VALUES (1, 'One')", []driver.NamedValue{}); err != nil {
t.Fatalf("executing DML statement failed: %v", err)
}
if _, err := c.QueryContext(ctx, "SELECT * FROM Foo", []driver.NamedValue{}); err != nil {
t.Fatalf("executing query failed: %v", err)
}
}
func TestConn_NonDmlStatementsInDmlBatch(t *testing.T) {
c := &conn{
batch: &batch{tp: dml},
execSingleQuery: func(ctx context.Context, c *spanner.Client, statement spanner.Statement, tb spanner.TimestampBound) *spanner.RowIterator {
return &spanner.RowIterator{}
},
execSingleDMLTransactional: func(ctx context.Context, c *spanner.Client, statement spanner.Statement, options spanner.TransactionOptions) (int64, time.Time, error) {
return 0, time.Time{}, nil
},
execSingleDMLPartitioned: func(ctx context.Context, c *spanner.Client, statement spanner.Statement, options spanner.QueryOptions) (int64, error) {
return 0, nil
},
}
ctx := context.Background()
// Starting a transaction while in a DML batch is not allowed.
if _, err := c.BeginTx(ctx, driver.TxOptions{}); err == nil {
t.Fatal("missing error for BeginTx")
}
// Starting a DDL batch while in a DML batch is not allowed.
if err := c.StartBatchDDL(); err == nil {
t.Fatal("missing error for StartBatchDDL")
}
// Executing a single DDL statement during a DML batch is not allowed.
if _, err := c.ExecContext(ctx, "CREATE TABLE Foo", []driver.NamedValue{}); err == nil {
t.Fatal("missing error for DDL statement")
}
// Executing a single query during a DML batch is allowed.
if _, err := c.QueryContext(ctx, "SELECT * FROM Foo", []driver.NamedValue{}); err != nil {
t.Fatalf("executing query failed: %v", err)
}
}
func TestConn_GetBatchedStatements(t *testing.T) {
t.Parallel()
ctx := context.Background()
c := &conn{}
if !reflect.DeepEqual(c.GetBatchedStatements(), []spanner.Statement{}) {
t.Fatal("conn should return an empty slice when no batch is active")
}
if err := c.StartBatchDDL(); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(c.GetBatchedStatements(), []spanner.Statement{}) {
t.Fatal("conn should return an empty slice when a batch contains no statements")
}
if _, err := c.ExecContext(ctx, "create table table1", []driver.NamedValue{}); err != nil {
t.Fatal(err)
}
if _, err := c.ExecContext(ctx, "create table table2", []driver.NamedValue{}); err != nil {
t.Fatal(err)
}
batchedStatements := c.GetBatchedStatements()
if !reflect.DeepEqual([]spanner.Statement{
{SQL: "create table table1", Params: map[string]interface{}{}},
{SQL: "create table table2", Params: map[string]interface{}{}},
}, batchedStatements) {
t.Errorf("unexpected batched statements: %v", batchedStatements)
}
// Changing the returned slice does not change the batched statements.
batchedStatements[0] = spanner.Statement{SQL: "drop table table1"}
batchedStatements2 := c.GetBatchedStatements()
if !reflect.DeepEqual([]spanner.Statement{
{SQL: "create table table1", Params: map[string]interface{}{}},
{SQL: "create table table2", Params: map[string]interface{}{}},
}, batchedStatements2) {
t.Errorf("unexpected batched statements: %v", batchedStatements2)
}
if err := c.AbortBatch(); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(c.GetBatchedStatements(), []spanner.Statement{}) {
t.Fatal("conn should return an empty slice when no batch is active")
}
}
func TestConn_GetCommitTimestampAfterAutocommitDml(t *testing.T) {
want := time.Now()
c := &conn{
execSingleQuery: func(ctx context.Context, c *spanner.Client, statement spanner.Statement, tb spanner.TimestampBound) *spanner.RowIterator {
return &spanner.RowIterator{}
},
execSingleDMLTransactional: func(ctx context.Context, c *spanner.Client, statement spanner.Statement, options spanner.TransactionOptions) (int64, time.Time, error) {
return 0, want, nil
},
execSingleDMLPartitioned: func(ctx context.Context, c *spanner.Client, statement spanner.Statement, options spanner.QueryOptions) (int64, error) {
return 0, nil
},
}
ctx := context.Background()
if _, err := c.ExecContext(ctx, "UPDATE FOO SET BAR=1 WHERE TRUE", []driver.NamedValue{}); err != nil {
t.Fatalf("failed to execute DML statement: %v", err)
}
got, err := c.CommitTimestamp()
if err != nil {
t.Fatalf("failed to get commit timestamp: %v", err)
}
if !cmp.Equal(want, got) {
t.Fatalf("commit timestamp mismatch\n Got: %v\nWant: %v", got, want)
}
}
func TestConn_GetCommitTimestampAfterAutocommitQuery(t *testing.T) {
c := &conn{
execSingleQuery: func(ctx context.Context, c *spanner.Client, statement spanner.Statement, tb spanner.TimestampBound) *spanner.RowIterator {
return &spanner.RowIterator{}
},
execSingleDMLTransactional: func(ctx context.Context, c *spanner.Client, statement spanner.Statement, options spanner.TransactionOptions) (int64, time.Time, error) {
return 0, time.Time{}, nil
},
execSingleDMLPartitioned: func(ctx context.Context, c *spanner.Client, statement spanner.Statement, options spanner.QueryOptions) (int64, error) {
return 0, nil
},
}
ctx := context.Background()
if _, err := c.QueryContext(ctx, "SELECT * FROM Foo", []driver.NamedValue{}); err != nil {
t.Fatalf("failed to execute query: %v", err)
}
_, err := c.CommitTimestamp()
if g, w := spanner.ErrCode(err), codes.FailedPrecondition; g != w {
t.Fatalf("error code mismatch\n Got: %v\nWant: %v", g, w)
}
}
func TestConn_CheckNamedValue(t *testing.T) {
c := &conn{}
type Person struct {
Name string
Age int64
}
var testNil *ValuerNil
tests := []struct {
in any
want any
}{
// basic types should stay unchanged
{in: int64(256), want: int64(256)},
// driver.Valuer types should call .Value func
{in: &ValuerPerson{Name: "hello", Age: 123}, want: "hello"},
// structs should be sent to spanner
{in: &Person{Name: "hello", Age: 123}, want: &Person{Name: "hello", Age: 123}},
// nil pointer of type that implements driver.Valuer via value receiver should use nil
{in: testNil, want: nil},
// net.IP reflects to []byte. Allow model structs to have fields with
// types that reflect to types supported by spanner.
{in: net.IPv6loopback, want: []byte(net.IPv6loopback)},
// Similarly, time.Duration is just an int64.
{in: time.Duration(1), want: int64(time.Duration(1))},
}
for _, test := range tests {
value := &driver.NamedValue{
Ordinal: 1,
Value: test.in,
}
err := c.CheckNamedValue(value)
if err != nil {
t.Errorf("expected no error, got: %v", err)
continue
}
if diff := cmp.Diff(test.want, value.Value); diff != "" {
t.Errorf("wrong result, got %#v expected %#v:\n%v", value.Value, test.want, diff)
}
}
}
// ValuerPerson implements driver.Valuer
type ValuerPerson struct {
Name string
Age int64
}
// Value implements conversion func for database.
func (p *ValuerPerson) Value() (driver.Value, error) {
return p.Name, nil
}
// ValuerNil implements driver.Valuer
type ValuerNil struct {
Name string
}
// Value implements conversion func for database. It explicitly implements driver.Valuer via a value receiver to
// test the driver for its ability to handle nil pointers to structs that implement driver.Valuer via value receivers
func (v ValuerNil) Value() (driver.Value, error) {
return v.Name, nil
}