-
Notifications
You must be signed in to change notification settings - Fork 429
/
sqltest.go
313 lines (276 loc) · 8.71 KB
/
sqltest.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
package sqltest // import "gopkg.in/DataDog/dd-trace-go.v1/contrib/internal/sqltest"
import (
"context"
"database/sql"
"fmt"
"log"
"testing"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"github.com/stretchr/testify/assert"
)
// Prepare sets up a table with the given name in both the MySQL and Postgres databases and returns
// a teardown function which will drop it.
func Prepare(tableName string) func() {
queryDrop := fmt.Sprintf("DROP TABLE IF EXISTS %s", tableName)
queryCreate := fmt.Sprintf("CREATE TABLE %s (id integer NOT NULL DEFAULT '0', name text)", tableName)
mysql, err := sql.Open("mysql", "test:test@tcp(127.0.0.1:3306)/test")
defer mysql.Close()
if err != nil {
log.Fatal(err)
}
mysql.Exec(queryDrop)
mysql.Exec(queryCreate)
postgres, err := sql.Open("postgres", "postgres://postgres:postgres@127.0.0.1:5432/postgres?sslmode=disable")
defer postgres.Close()
if err != nil {
log.Fatal(err)
}
postgres.Exec(queryDrop)
postgres.Exec(queryCreate)
mssql, err := sql.Open("sqlserver", "sqlserver://sa:myPassw0rd@localhost:1433?database=master")
defer mssql.Close()
if err != nil {
log.Fatal(err)
}
mssql.Exec(queryDrop)
mssql.Exec(queryCreate)
return func() {
mysql.Exec(queryDrop)
postgres.Exec(queryDrop)
mssql.Exec(queryDrop)
}
}
// RunAll applies a sequence of unit tests to check the correct tracing of sql features.
func RunAll(t *testing.T, cfg *Config) {
cfg.mockTracer = mocktracer.Start()
defer cfg.mockTracer.Stop()
cfg.DB.SetMaxIdleConns(0)
for name, test := range map[string]func(*Config) func(*testing.T){
"Connect": testConnect,
"Ping": testPing,
"Query": testQuery,
"Statement": testStatement,
"BeginRollback": testBeginRollback,
"Exec": testExec,
} {
t.Run(name, test(cfg))
}
}
func testConnect(cfg *Config) func(*testing.T) {
return func(t *testing.T) {
cfg.mockTracer.Reset()
assert := assert.New(t)
err := cfg.DB.Ping()
assert.Nil(err)
spans := cfg.mockTracer.FinishedSpans()
assert.Len(spans, 2)
span := spans[0]
assert.Equal(cfg.ExpectName, span.OperationName())
cfg.ExpectTags["sql.query_type"] = "Connect"
for k, v := range cfg.ExpectTags {
assert.Equal(v, span.Tag(k), "Value mismatch on tag %s", k)
}
}
}
func testPing(cfg *Config) func(*testing.T) {
return func(t *testing.T) {
cfg.mockTracer.Reset()
assert := assert.New(t)
err := cfg.DB.Ping()
assert.Nil(err)
spans := cfg.mockTracer.FinishedSpans()
assert.Len(spans, 2)
verifyConnectSpan(spans[0], assert, cfg)
span := spans[1]
assert.Equal(cfg.ExpectName, span.OperationName())
cfg.ExpectTags["sql.query_type"] = "Ping"
for k, v := range cfg.ExpectTags {
assert.Equal(v, span.Tag(k), "Value mismatch on tag %s", k)
}
}
}
func testQuery(cfg *Config) func(*testing.T) {
var query string
switch cfg.DriverName {
case "postgres", "pgx", "mysql":
query = fmt.Sprintf("SELECT id, name FROM %s LIMIT 5", cfg.TableName)
case "sqlserver":
query = fmt.Sprintf("SELECT TOP 5 id, name FROM %s", cfg.TableName)
}
return func(t *testing.T) {
cfg.mockTracer.Reset()
assert := assert.New(t)
rows, err := cfg.DB.Query(query)
defer rows.Close()
assert.Nil(err)
spans := cfg.mockTracer.FinishedSpans()
var querySpan mocktracer.Span
if cfg.DriverName == "sqlserver" {
//The mssql driver doesn't support non-prepared queries so there are 3 spans
//connect, prepare, and query
assert.Len(spans, 3)
span := spans[1]
cfg.ExpectTags["sql.query_type"] = "Prepare"
assert.Equal(cfg.ExpectName, span.OperationName())
for k, v := range cfg.ExpectTags {
assert.Equal(v, span.Tag(k), "Value mismatch on tag %s", k)
}
querySpan = spans[2]
} else {
assert.Len(spans, 2)
querySpan = spans[1]
}
verifyConnectSpan(spans[0], assert, cfg)
cfg.ExpectTags["sql.query_type"] = "Query"
assert.Equal(cfg.ExpectName, querySpan.OperationName())
for k, v := range cfg.ExpectTags {
assert.Equal(v, querySpan.Tag(k), "Value mismatch on tag %s", k)
}
}
}
func testStatement(cfg *Config) func(*testing.T) {
query := "INSERT INTO %s(name) VALUES(%s)"
switch cfg.DriverName {
case "postgres", "pgx":
query = fmt.Sprintf(query, cfg.TableName, "$1")
case "mysql":
query = fmt.Sprintf(query, cfg.TableName, "?")
case "sqlserver":
query = fmt.Sprintf(query, cfg.TableName, "@p1")
}
return func(t *testing.T) {
cfg.mockTracer.Reset()
assert := assert.New(t)
stmt, err := cfg.DB.Prepare(query)
assert.Equal(nil, err)
spans := cfg.mockTracer.FinishedSpans()
assert.Len(spans, 3)
verifyConnectSpan(spans[0], assert, cfg)
span := spans[1]
assert.Equal(cfg.ExpectName, span.OperationName())
cfg.ExpectTags["sql.query_type"] = "Prepare"
for k, v := range cfg.ExpectTags {
assert.Equal(v, span.Tag(k), "Value mismatch on tag %s", k)
}
cfg.mockTracer.Reset()
_, err2 := stmt.Exec("New York")
assert.Equal(nil, err2)
spans = cfg.mockTracer.FinishedSpans()
assert.Len(spans, 4)
span = spans[2]
assert.Equal(cfg.ExpectName, span.OperationName())
cfg.ExpectTags["sql.query_type"] = "Exec"
for k, v := range cfg.ExpectTags {
assert.Equal(v, span.Tag(k), "Value mismatch on tag %s", k)
}
}
}
func testBeginRollback(cfg *Config) func(*testing.T) {
return func(t *testing.T) {
cfg.mockTracer.Reset()
assert := assert.New(t)
tx, err := cfg.DB.Begin()
assert.Equal(nil, err)
spans := cfg.mockTracer.FinishedSpans()
assert.Len(spans, 2)
verifyConnectSpan(spans[0], assert, cfg)
span := spans[1]
assert.Equal(cfg.ExpectName, span.OperationName())
cfg.ExpectTags["sql.query_type"] = "Begin"
for k, v := range cfg.ExpectTags {
assert.Equal(v, span.Tag(k), "Value mismatch on tag %s", k)
}
cfg.mockTracer.Reset()
err = tx.Rollback()
assert.Equal(nil, err)
spans = cfg.mockTracer.FinishedSpans()
assert.Len(spans, 1)
span = spans[0]
assert.Equal(cfg.ExpectName, span.OperationName())
cfg.ExpectTags["sql.query_type"] = "Rollback"
for k, v := range cfg.ExpectTags {
assert.Equal(v, span.Tag(k), "Value mismatch on tag %s", k)
}
}
}
func testExec(cfg *Config) func(*testing.T) {
return func(t *testing.T) {
assert := assert.New(t)
query := fmt.Sprintf("INSERT INTO %s(name) VALUES('New York')", cfg.TableName)
parent, ctx := tracer.StartSpanFromContext(context.Background(), "test.parent",
tracer.ServiceName("test"),
tracer.ResourceName("parent"),
)
cfg.mockTracer.Reset()
tx, err := cfg.DB.BeginTx(ctx, nil)
assert.Equal(nil, err)
_, err = tx.ExecContext(ctx, query)
assert.Equal(nil, err)
err = tx.Commit()
assert.Equal(nil, err)
parent.Finish() // flush children
spans := cfg.mockTracer.FinishedSpans()
if cfg.DriverName == "sqlserver" {
//The mssql driver doesn't support non-prepared exec so there are 2 extra spans for the exec:
//prepare, exec, and then a close
assert.Len(spans, 7)
span := spans[2]
cfg.ExpectTags["sql.query_type"] = "Prepare"
assert.Equal(cfg.ExpectName, span.OperationName())
for k, v := range cfg.ExpectTags {
assert.Equal(v, span.Tag(k), "Value mismatch on tag %s", k)
}
span = spans[4]
cfg.ExpectTags["sql.query_type"] = "Close"
assert.Equal(cfg.ExpectName, span.OperationName())
for k, v := range cfg.ExpectTags {
assert.Equal(v, span.Tag(k), "Value mismatch on tag %s", k)
}
} else {
assert.Len(spans, 5)
}
var span mocktracer.Span
for _, s := range spans {
if s.OperationName() == cfg.ExpectName && s.Tag(ext.ResourceName) == query {
span = s
}
}
assert.NotNil(span, "span not found")
cfg.ExpectTags["sql.query_type"] = "Exec"
for k, v := range cfg.ExpectTags {
assert.Equal(v, span.Tag(k), "Value mismatch on tag %s", k)
}
for _, s := range spans {
if s.OperationName() == cfg.ExpectName && s.Tag(ext.ResourceName) == "Commit" {
span = s
}
}
assert.NotNil(span, "span not found")
cfg.ExpectTags["sql.query_type"] = "Commit"
for k, v := range cfg.ExpectTags {
assert.Equal(v, span.Tag(k), "Value mismatch on tag %s", k)
}
}
}
func verifyConnectSpan(span mocktracer.Span, assert *assert.Assertions, cfg *Config) {
assert.Equal(cfg.ExpectName, span.OperationName())
cfg.ExpectTags["sql.query_type"] = "Connect"
for k, v := range cfg.ExpectTags {
assert.Equal(v, span.Tag(k), "Value mismatch on tag %s", k)
}
}
// Config holds the test configuration.
type Config struct {
*sql.DB
mockTracer mocktracer.Tracer
DriverName string
TableName string
ExpectName string
ExpectTags map[string]interface{}
}