forked from microsoft/go-mssqldb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmssql_test.go
More file actions
348 lines (313 loc) · 10.3 KB
/
Copy pathmssql_test.go
File metadata and controls
348 lines (313 loc) · 10.3 KB
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
package mssql
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"io"
"net"
"reflect"
"testing"
"github.com/microsoft/go-mssqldb/msdsn"
)
func TestBadOpen(t *testing.T) {
tl := testLogger{t: t}
defer tl.StopLogging()
drv := driverWithProcess(t, &tl)
_, err := drv.open(context.Background(), "port=bad")
if err == nil {
t.Fail()
}
}
func TestIsProc(t *testing.T) {
list := []struct {
s string
is bool
}{
{"proc", true},
{"select 1;", false},
{"select 1", false},
{"[proc 1]", true},
{"[proc\n1]", true},
{"schema.name", true},
{"[schema].[name]", true},
{"schema.[name]", true},
{"[schema].name", true},
{"schema.[proc name]", true},
{"db.schema.[proc name]", true},
{"db..[proc name]", true},
{"#temp_@_proc", true},
{"_temp.[_proc]", true},
{"raiserror(13000,1,1)", false},
{"select*from(@table)", false},
{"select[A]]]+1from[B]", false},
{"--proc", false},
{"[proc;]", true},
{" proc", false},
{"RECONFIGURE", false},
{"SHUTDOWN", false},
{"CHECKPOINT", false},
{"COMMIT", false},
{"ROLLBACK", false},
{"REVERT", false},
}
for _, item := range list {
got := isProc(item.s)
if got != item.is {
t.Errorf("for %q, got %t want %t", item.s, got, item.is)
}
}
}
func TestConvertIsolationLevel(t *testing.T) {
level, err := convertIsolationLevel(sql.LevelReadUncommitted)
if level != isolationReadUncommited || err != nil {
t.Fatal("invalid value returned")
}
level, err = convertIsolationLevel(sql.LevelReadCommitted)
if level != isolationReadCommited || err != nil {
t.Fatal("invalid value returned")
}
level, err = convertIsolationLevel(sql.LevelRepeatableRead)
if level != isolationRepeatableRead || err != nil {
t.Fatal("invalid value returned")
}
level, err = convertIsolationLevel(sql.LevelSnapshot)
if level != isolationSnapshot || err != nil {
t.Fatal("invalid value returned")
}
_, err = convertIsolationLevel(sql.LevelWriteCommitted)
if err == nil {
t.Fatal("must fail but it didn't")
}
_, err = convertIsolationLevel(sql.LevelLinearizable)
if err == nil {
t.Fatal("must fail but it didn't")
}
_, err = convertIsolationLevel(sql.IsolationLevel(1000))
if err == nil {
t.Fatal("must fail but it didn't")
}
}
func TestFailoverPartnerParamsReturnsNilWhenNoPartner(t *testing.T) {
params := msdsn.Config{
Host: "primary",
Port: 1433,
ServerSPN: "MSSQLSvc/primary:1433",
}
result := failoverPartnerParams(params)
if result != nil {
t.Fatal("expected nil when no failover partner is set, got non-nil result")
}
}
func TestFailoverPartnerParams(t *testing.T) {
tests := []struct {
name string
params msdsn.Config
expectedHost string
expectedPort uint64
expectedSPN string
}{
{
name: "FailoverPartnerSPN overrides ServerSPN",
params: msdsn.Config{
Host: "primary",
Port: 1033,
FailOverPartner: "mirror",
ServerSPN: "MSSQLSvc/primary",
FailOverPartnerSPN: "MSSQLSvc/mirror",
},
expectedHost: "mirror",
expectedPort: 1033,
expectedSPN: "MSSQLSvc/mirror",
},
{
name: "ServerSPN preserved when FailoverPartnerSPN not set",
params: msdsn.Config{
Host: "primary",
Port: 1033,
FailOverPartner: "mirror",
ServerSPN: "MSSQLSvc/unique-spn",
},
expectedHost: "mirror",
expectedPort: 1033,
expectedSPN: "MSSQLSvc/unique-spn",
},
{
name: "FailOverPort overrides Port",
params: msdsn.Config{
Host: "primary",
Port: 1033,
FailOverPartner: "mirror",
FailOverPort: 2033,
ServerSPN: "MSSQLSvc/unique-spn",
},
expectedHost: "mirror",
expectedPort: 2033,
expectedSPN: "MSSQLSvc/unique-spn",
},
{
name: "Port preserved when FailOverPort not set",
params: msdsn.Config{
Host: "primary",
Port: 1033,
FailOverPartner: "mirror",
ServerSPN: "MSSQLSvc/unique-spn",
},
expectedHost: "mirror",
expectedPort: 1033,
expectedSPN: "MSSQLSvc/unique-spn",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := failoverPartnerParams(tt.params)
if result == nil {
t.Fatal("expected non-nil result, got nil")
}
if result.Host != tt.expectedHost {
t.Errorf("Host = %q, want %q", result.Host, tt.expectedHost)
}
if result.Port != tt.expectedPort {
t.Errorf("Port = %d, want %d", result.Port, tt.expectedPort)
}
if result.ServerSPN != tt.expectedSPN {
t.Errorf("ServerSPN = %q, want %q", result.ServerSPN, tt.expectedSPN)
}
})
}
}
// equalErrors is a helper function that compares two errors
// by comparing their nilness, underlying type, and Error messages
func equalErrors(e1 error, e2 error) bool {
if e1 == nil && e2 == nil {
return true
}
if e1 == nil && e2 != nil || e1 != nil && e2 == nil {
return false
}
return reflect.TypeOf(e1) == reflect.TypeOf(e2) &&
e1.Error() == e2.Error()
}
// TestCheckBadConn verifies that different combinations of
// configuration, inputs and errors result in the proper output
// error and connection state.
func TestCheckBadConn(t *testing.T) {
netErr := &net.OpError{Err: fmt.Errorf("fake net.Error")}
streamErr := StreamError{InnerError: fmt.Errorf("fake StreamError")}
serverErr := ServerError{sqlError: Error{Message: "fake ServerError"}}
goodConnErr := fmt.Errorf("fake error that leaves connection good")
testInputs := []struct {
err error
mayRetry bool
disableRetry bool
expectedErr error
expectedConnGood bool
}{
{nil, false, false, nil, true},
{nil, true, false, nil, true},
{nil, false, true, nil, true},
{nil, true, true, nil, true},
{io.EOF, false, false, io.EOF, false},
{io.EOF, true, false, newRetryableError(io.EOF), false},
{io.EOF, false, true, io.EOF, false},
{io.EOF, true, true, io.EOF, false},
{netErr, false, false, netErr, false},
{netErr, true, false, newRetryableError(netErr), false},
{netErr, false, true, netErr, false},
{netErr, true, true, netErr, false},
{streamErr, false, false, streamErr, false},
{streamErr, true, false, newRetryableError(streamErr), false},
{streamErr, false, true, streamErr, false},
{streamErr, true, true, streamErr, false},
{serverErr, false, false, serverErr, false},
{serverErr, true, false, newRetryableError(serverErr), false},
{serverErr, false, true, serverErr, false},
{serverErr, true, true, serverErr, false},
{goodConnErr, false, false, goodConnErr, true},
{goodConnErr, true, false, goodConnErr, true},
{goodConnErr, false, true, goodConnErr, true},
{goodConnErr, true, true, goodConnErr, true},
}
c := Conn{
connector: &Connector{
params: msdsn.Config{},
},
sess: &tdsSession{
logger: optionalLogger{},
},
}
for _, ti := range testInputs {
c.connectionGood = true
c.connector.params.DisableRetry = ti.disableRetry
actualErr := c.checkBadConn(context.Background(), ti.err, ti.mayRetry)
if !equalErrors(actualErr, ti.expectedErr) ||
c.connectionGood != ti.expectedConnGood {
t.Fatalf("checkBadConn returned unexpected result for input err = '%+v', mayRetry = '%t', disableRetry = '%t': "+
"got output err = '%+v', connectionGood = '%t', "+
"wanted output err = '%+v', connectionGood = '%t'",
ti.err, ti.mayRetry, ti.disableRetry, actualErr, c.connectionGood, ti.expectedErr, ti.expectedConnGood)
}
}
// This must be the final test in this function, because we expect it to panic
defer func() { recover() }()
c.checkBadConn(context.Background(), driver.ErrBadConn, true)
t.Fatalf("checkBadConn did not panic as expected when passed driverErrBadConn")
}
// TestBadConnRejection verifies that database operations that start
// with a bad connection fail with the sentinel error driver.ErrBadConn.
// That instructs the database/sql connection pool logic to discard the
// bad connection and, if appropriate, attempt to retry the operation
// with another connection.
func TestBadConnRejection(t *testing.T) {
c := Conn{connectionGood: false}
if _, err := c.Begin(); err != driver.ErrBadConn {
t.Fatalf("Begin did not fail on bad connection: "+
"got err = '%+v', wanted err = '%+v'", err, driver.ErrBadConn)
}
if err := c.Commit(); err != driver.ErrBadConn {
t.Fatalf("Commit did not fail on bad connection: "+
"got err = '%+v', wanted err = '%+v'", err, driver.ErrBadConn)
}
if err := c.Rollback(); err != driver.ErrBadConn {
t.Fatalf("Rollback did not fail on bad connection: "+
"got err = '%+v', wanted err = '%+v'", err, driver.ErrBadConn)
}
if _, err := c.Prepare("query"); err != driver.ErrBadConn {
t.Fatalf("Prepare did not fail on bad connection: "+
"got err = '%+v', wanted err = '%+v'", err, driver.ErrBadConn)
}
if _, err := c.PrepareContext(context.Background(), "query"); err != driver.ErrBadConn {
t.Fatalf("PrepareContext did not fail on bad connection: "+
"got err = '%+v', wanted err = '%+v'", err, driver.ErrBadConn)
}
if err := c.Ping(context.Background()); err != driver.ErrBadConn {
t.Fatalf("Ping did not fail on bad connection: "+
"got err = '%+v', wanted err = '%+v'", err, driver.ErrBadConn)
}
if _, err := c.BeginTx(context.Background(), driver.TxOptions{}); err != driver.ErrBadConn {
t.Fatalf("BeginTx did not fail on bad connection: "+
"got err = '%+v', wanted err = '%+v'", err, driver.ErrBadConn)
}
s := &Stmt{c: &c}
if _, err := s.Query(nil); err != driver.ErrBadConn {
t.Fatalf("Query did not fail on bad connection: "+
"got err = '%+v', wanted err = '%+v'", err, driver.ErrBadConn)
}
if _, err := s.QueryContext(context.Background(), nil); err != driver.ErrBadConn {
t.Fatalf("QueryContext did not fail on bad connection: "+
"got err = '%+v', wanted err = '%+v'", err, driver.ErrBadConn)
}
if _, err := s.Exec(nil); err != driver.ErrBadConn {
t.Fatalf("Exec did not fail on bad connection: "+
"got err = '%+v', wanted err = '%+v'", err, driver.ErrBadConn)
}
if _, err := s.ExecContext(context.Background(), nil); err != driver.ErrBadConn {
t.Fatalf("ExecContext did not fail on bad connection: "+
"got err = '%+v', wanted err = '%+v'", err, driver.ErrBadConn)
}
r := &Rows{stmt: s}
if err := r.Next(nil); err != driver.ErrBadConn {
t.Fatalf("Next did not fail on bad connection: "+
"got err = '%+v', wanted err = '%+v'", err, driver.ErrBadConn)
}
}