-
Notifications
You must be signed in to change notification settings - Fork 117
/
sqlcontext_test.go
374 lines (337 loc) · 10.3 KB
/
sqlcontext_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
// Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 xray
import (
"encoding/json"
"errors"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
)
// utility functions for testing SQL
func mockPostgreSQL(mock sqlmock.Sqlmock, err error) {
row := sqlmock.NewRows([]string{"version()", "current_user", "current_database()"}).
AddRow("test version", "test user", "test database").
RowError(0, err)
mock.ExpectPrepare(`SELECT version\(\), current_user, current_database\(\)`).ExpectQuery().WillReturnRows(row)
}
func mockMySQL(mock sqlmock.Sqlmock, err error) {
row := sqlmock.NewRows([]string{"version()", "current_user()", "database()"}).
AddRow("test version", "test user", "test database").
RowError(0, err)
mock.ExpectPrepare(`SELECT version\(\), current_user\(\), database\(\)`).ExpectQuery().WillReturnRows(row)
}
func mockMSSQL(mock sqlmock.Sqlmock, err error) {
row := sqlmock.NewRows([]string{"@@version", "current_user", "db_name()"}).
AddRow("test version", "test user", "test database").
RowError(0, err)
mock.ExpectPrepare(`SELECT @@version, current_user, db_name\(\)`).ExpectQuery().WillReturnRows(row)
}
func mockOracle(mock sqlmock.Sqlmock, err error) {
row := sqlmock.NewRows([]string{"version", "user", "ora_database_name"}).
AddRow("test version", "test user", "test database").
RowError(0, err)
mock.ExpectPrepare(`SELECT version FROM v\$instance UNION SELECT user, ora_database_name FROM dual`).ExpectQuery().WillReturnRows(row)
}
func capturePing(dsn string) (*Segment, error) {
ctx, td := NewTestDaemon()
defer td.Close()
db, err := SQLContext("sqlmock", dsn)
if err != nil {
return nil, err
}
defer db.Close()
ctx, root := BeginSegment(ctx, "test")
if err := db.PingContext(ctx); err != nil {
return nil, err
}
root.Close(nil)
seg, err := td.Recv()
if err != nil {
return nil, err
}
var subseg *Segment
if err := json.Unmarshal(seg.Subsegments[0], &subseg); err != nil {
return nil, err
}
return subseg, nil
}
func TestDSN(t *testing.T) {
tc := []struct {
dsn string
url string
str string
name string
}{
{
dsn: "postgres://user@host:5432/database",
url: "postgres://user@host:5432/database",
name: "test database@host",
},
{
dsn: "postgres://user:password@host:5432/database",
url: "postgres://user@host:5432/database",
name: "test database@host",
},
{
dsn: "postgres://host:5432/database?password=password",
url: "postgres://host:5432/database",
name: "test database@host",
},
{
dsn: "user:password@host:5432/database",
url: "user@host:5432/database",
name: "test database@host",
},
{
dsn: "host:5432/database?password=password",
url: "host:5432/database",
name: "test database@host",
},
{
dsn: "user%2Fpassword@host:5432/database",
url: "user@host:5432/database",
name: "test database@host",
},
{
dsn: "user/password@host:5432/database",
url: "user@host:5432/database",
name: "test database@host",
},
{
dsn: "user=user database=database",
str: "user=user database=database",
name: "test database",
},
{
dsn: "user=user password=password database=database",
str: "user=user database=database",
name: "test database",
},
{
dsn: "odbc:server=localhost;user id=sa;password={foo}};bar};otherthing=thing",
str: "odbc:server=localhost;user id=sa;otherthing=thing",
name: "test database",
},
{
dsn: "postgres://host:5432/database?X-Amz-Security-Token=token",
url: "postgres://host:5432/database",
name: "test database@host",
},
{
dsn: "host:5432/database?X-Amz-Security-Token=token",
url: "host:5432/database",
name: "test database@host",
},
}
for _, tt := range tc {
tt := tt
t.Run(tt.dsn, func(t *testing.T) {
db, mock, err := sqlmock.NewWithDSN(tt.dsn)
if err != nil {
t.Fatal(err)
}
defer db.Close()
mockPostgreSQL(mock, nil)
subseg, err := capturePing(tt.dsn)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, mock.ExpectationsWereMet())
assert.Equal(t, "remote", subseg.Namespace)
assert.Equal(t, "Postgres", subseg.SQL.DatabaseType)
assert.Equal(t, tt.url, subseg.SQL.URL)
assert.Equal(t, tt.str, subseg.SQL.ConnectionString)
assert.Equal(t, "test version", subseg.SQL.DatabaseVersion)
assert.Equal(t, "test user", subseg.SQL.User)
assert.Equal(t, tt.name, subseg.Name)
assert.False(t, subseg.Throttle)
assert.False(t, subseg.Error)
assert.False(t, subseg.Fault)
})
}
}
func TestPostgreSQL(t *testing.T) {
dsn := "test-postgre"
db, mock, err := sqlmock.NewWithDSN(dsn)
if err != nil {
t.Fatal(err)
}
defer db.Close()
mockPostgreSQL(mock, nil)
subseg, err := capturePing(dsn)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, mock.ExpectationsWereMet())
assert.Equal(t, "remote", subseg.Namespace)
assert.Equal(t, "Postgres", subseg.SQL.DatabaseType)
assert.Equal(t, "", subseg.SQL.URL)
assert.Equal(t, dsn, subseg.SQL.ConnectionString)
assert.Equal(t, "test version", subseg.SQL.DatabaseVersion)
assert.Equal(t, "test user", subseg.SQL.User)
assert.False(t, subseg.Throttle)
assert.False(t, subseg.Error)
assert.False(t, subseg.Fault)
}
func TestMySQL(t *testing.T) {
dsn := "test-mysql"
db, mock, err := sqlmock.NewWithDSN(dsn)
if err != nil {
t.Fatal(err)
}
defer db.Close()
mockPostgreSQL(mock, errors.New("syntax error"))
mockMySQL(mock, nil)
subseg, err := capturePing(dsn)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, mock.ExpectationsWereMet())
assert.Equal(t, "remote", subseg.Namespace)
assert.Equal(t, "MySQL", subseg.SQL.DatabaseType)
assert.Equal(t, "", subseg.SQL.URL)
assert.Equal(t, dsn, subseg.SQL.ConnectionString)
assert.Equal(t, "test version", subseg.SQL.DatabaseVersion)
assert.Equal(t, "test user", subseg.SQL.User)
assert.False(t, subseg.Throttle)
assert.False(t, subseg.Error)
assert.False(t, subseg.Fault)
}
func TestMSSQL(t *testing.T) {
dsn := "test-mssql"
db, mock, err := sqlmock.NewWithDSN(dsn)
if err != nil {
t.Fatal(err)
}
defer db.Close()
mockPostgreSQL(mock, errors.New("syntax error"))
mockMySQL(mock, errors.New("syntax error"))
mockMSSQL(mock, nil)
subseg, err := capturePing(dsn)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, mock.ExpectationsWereMet())
assert.Equal(t, "remote", subseg.Namespace)
assert.Equal(t, "MS SQL", subseg.SQL.DatabaseType)
assert.Equal(t, "", subseg.SQL.URL)
assert.Equal(t, dsn, subseg.SQL.ConnectionString)
assert.Equal(t, "test version", subseg.SQL.DatabaseVersion)
assert.Equal(t, "test user", subseg.SQL.User)
assert.False(t, subseg.Throttle)
assert.False(t, subseg.Error)
assert.False(t, subseg.Fault)
}
func TestOracle(t *testing.T) {
dsn := "test-oracle"
db, mock, err := sqlmock.NewWithDSN(dsn)
if err != nil {
t.Fatal(err)
}
defer db.Close()
mockPostgreSQL(mock, errors.New("syntax error"))
mockMySQL(mock, errors.New("syntax error"))
mockMSSQL(mock, errors.New("syntax error"))
mockOracle(mock, nil)
subseg, err := capturePing(dsn)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, mock.ExpectationsWereMet())
assert.Equal(t, "remote", subseg.Namespace)
assert.Equal(t, "Oracle", subseg.SQL.DatabaseType)
assert.Equal(t, "", subseg.SQL.URL)
assert.Equal(t, dsn, subseg.SQL.ConnectionString)
assert.Equal(t, "test version", subseg.SQL.DatabaseVersion)
assert.Equal(t, "test user", subseg.SQL.User)
assert.False(t, subseg.Throttle)
assert.False(t, subseg.Error)
assert.False(t, subseg.Fault)
}
func TestUnknownDatabase(t *testing.T) {
dsn := "test-unknown"
db, mock, err := sqlmock.NewWithDSN(dsn)
if err != nil {
t.Fatal(err)
}
defer db.Close()
mockPostgreSQL(mock, errors.New("syntax error"))
mockMySQL(mock, errors.New("syntax error"))
mockMSSQL(mock, errors.New("syntax error"))
mockOracle(mock, errors.New("syntax error"))
subseg, err := capturePing(dsn)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, mock.ExpectationsWereMet())
assert.Equal(t, "remote", subseg.Namespace)
assert.Equal(t, "Unknown", subseg.SQL.DatabaseType)
assert.Equal(t, "", subseg.SQL.URL)
assert.Equal(t, dsn, subseg.SQL.ConnectionString)
assert.Equal(t, "Unknown", subseg.SQL.DatabaseVersion)
assert.Equal(t, "Unknown", subseg.SQL.User)
assert.False(t, subseg.Throttle)
assert.False(t, subseg.Error)
assert.False(t, subseg.Fault)
}
func TestStripPasswords(t *testing.T) {
tc := []struct {
in string
want string
}{
{
in: "user=user database=database",
want: "user=user database=database",
},
{
in: "user=user password=password database=database",
want: "user=user database=database",
},
{
in: "odbc:server=localhost;user id=sa;password={foo}};bar};otherthing=thing",
want: "odbc:server=localhost;user id=sa;otherthing=thing",
},
// see https://github.com/aws/aws-xray-sdk-go/issues/181
{
in: "password=",
want: "",
},
{
in: "pwd=",
want: "",
},
// test cases for https://github.com/go-sql-driver/mysql
{
in: "user:password@tcp(localhost:5555)/dbname?tls=skip-verify&autocommit=true",
want: "user@tcp(localhost:5555)/dbname?tls=skip-verify&autocommit=true",
},
{
in: "user@tcp(localhost:5555)/dbname?tls=skip-verify&autocommit=true",
want: "user@tcp(localhost:5555)/dbname?tls=skip-verify&autocommit=true",
},
{
in: "user:password@tcp([de:ad:be:ef::ca:fe]:80)/dbname?timeout=90s&collation=utf8mb4_unicode_ci",
want: "user@tcp([de:ad:be:ef::ca:fe]:80)/dbname?timeout=90s&collation=utf8mb4_unicode_ci",
},
{
in: "user@tcp([de:ad:be:ef::ca:fe]:80)/dbname?timeout=90s&collation=utf8mb4_unicode_ci",
want: "user@tcp([de:ad:be:ef::ca:fe]:80)/dbname?timeout=90s&collation=utf8mb4_unicode_ci",
},
{
in: "user:password@/",
want: "user@/",
},
}
for _, tt := range tc {
got := stripPasswords(tt.in)
if got != tt.want {
t.Errorf("%s: want %s, got %s", tt.in, tt.want, got)
}
}
}