forked from compose/transporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
461 lines (448 loc) · 12.1 KB
/
client_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
package mongodb
import (
"crypto/tls"
"crypto/x509"
"errors"
"io/ioutil"
"os"
"reflect"
"testing"
"time"
"github.com/compose/transporter/client"
mgo "gopkg.in/mgo.v2"
)
var errorTests = []struct {
name string
expected string
e error
}{
{
"OplogAccessError",
"oplog access failed, database missing oplog.rs collection",
OplogAccessError{"database missing oplog.rs collection"},
},
{
"InvalidReadPreferenceError",
"Invalid Read Preference, fakeReadPreference",
InvalidReadPreferenceError{"fakeReadPreference"},
},
}
func TestErrors(t *testing.T) {
for _, et := range errorTests {
if et.e.Error() != et.expected {
t.Errorf("[%s] wrong Error(), expected %s, got %s", et.name, et.expected, et.e.Error())
}
}
}
const rootPEM = `
-----BEGIN CERTIFICATE-----
MIIEBDCCAuygAwIBAgIDAjppMA0GCSqGSIb3DQEBBQUAMEIxCzAJBgNVBAYTAlVT
MRYwFAYDVQQKEw1HZW9UcnVzdCBJbmMuMRswGQYDVQQDExJHZW9UcnVzdCBHbG9i
YWwgQ0EwHhcNMTMwNDA1MTUxNTU1WhcNMTUwNDA0MTUxNTU1WjBJMQswCQYDVQQG
EwJVUzETMBEGA1UEChMKR29vZ2xlIEluYzElMCMGA1UEAxMcR29vZ2xlIEludGVy
bmV0IEF1dGhvcml0eSBHMjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
AJwqBHdc2FCROgajguDYUEi8iT/xGXAaiEZ+4I/F8YnOIe5a/mENtzJEiaB0C1NP
VaTOgmKV7utZX8bhBYASxF6UP7xbSDj0U/ck5vuR6RXEz/RTDfRK/J9U3n2+oGtv
h8DQUB8oMANA2ghzUWx//zo8pzcGjr1LEQTrfSTe5vn8MXH7lNVg8y5Kr0LSy+rE
ahqyzFPdFUuLH8gZYR/Nnag+YyuENWllhMgZxUYi+FOVvuOAShDGKuy6lyARxzmZ
EASg8GF6lSWMTlJ14rbtCMoU/M4iarNOz0YDl5cDfsCx3nuvRTPPuj5xt970JSXC
DTWJnZ37DhF5iR43xa+OcmkCAwEAAaOB+zCB+DAfBgNVHSMEGDAWgBTAephojYn7
qwVkDBF9qn1luMrMTjAdBgNVHQ4EFgQUSt0GFhu89mi1dvWBtrtiGrpagS8wEgYD
VR0TAQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAQYwOgYDVR0fBDMwMTAvoC2g
K4YpaHR0cDovL2NybC5nZW90cnVzdC5jb20vY3Jscy9ndGdsb2JhbC5jcmwwPQYI
KwYBBQUHAQEEMTAvMC0GCCsGAQUFBzABhiFodHRwOi8vZ3RnbG9iYWwtb2NzcC5n
ZW90cnVzdC5jb20wFwYDVR0gBBAwDjAMBgorBgEEAdZ5AgUBMA0GCSqGSIb3DQEB
BQUAA4IBAQA21waAESetKhSbOHezI6B1WLuxfoNCunLaHtiONgaX4PCVOzf9G0JY
/iLIa704XtE7JW4S615ndkZAkNoUyHgN7ZVm2o6Gb4ChulYylYbc3GrKBIxbf/a/
zG+FA1jDaFETzf3I93k9mTXwVqO94FntT0QJo544evZG0R0SnU++0ED8Vf4GXjza
HFa9llF7b1cq26KqltyMdMKVvvBulRP/F/A8rLIQjcxz++iPAsbw+zOzlTvjwsto
WHPbqCRiOwY1nQ2pM714A5AuTHhdUDqB1O6gyHA43LL5Z/qHQF1hwFGPa4NrzQU6
yuGnBXj8ytqU0CwIPX4WecigUCAkVDNx
-----END CERTIFICATE-----`
var (
defaultClient = &Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
readPreference: DefaultReadPreference,
}
certPool = func() *x509.CertPool {
pool := x509.NewCertPool()
pool.AppendCertsFromPEM([]byte(rootPEM))
return pool
}
)
var clientTests = []struct {
name string
options []ClientOptionFunc // input
expected *Client // expected result
expectedErr error // expected error
}{
{
"default_client",
make([]ClientOptionFunc, 0),
defaultClient,
nil,
},
{
"with_url_fake",
[]ClientOptionFunc{WithURI("mongodb://fakeurl:27017")},
&Client{
uri: "mongodb://fakeurl:27017",
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
readPreference: DefaultReadPreference,
},
nil,
},
{
"with_uri_invalid",
[]ClientOptionFunc{WithURI("mongodb://@nopassword:27017")},
&Client{},
client.InvalidURIError{URI: "mongodb://@nopassword:27017", Err: "credentials must be provided as user:pass@host"},
},
{
"with_timeout",
[]ClientOptionFunc{WithTimeout("30s")},
&Client{
uri: DefaultURI,
sessionTimeout: 30 * time.Second,
safety: DefaultSafety,
readPreference: DefaultReadPreference,
},
nil,
},
{
"with_timeout_empty",
[]ClientOptionFunc{WithTimeout("")},
defaultClient,
nil,
},
{
"with_timeout_invalid",
[]ClientOptionFunc{WithTimeout("blah")},
&Client{},
client.InvalidTimeoutError{Timeout: "blah"},
},
{
"with_write_concern",
[]ClientOptionFunc{WithWriteConcern(2)},
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
safety: mgo.Safe{W: 2},
readPreference: DefaultReadPreference,
},
nil,
},
{
"with_fsync",
[]ClientOptionFunc{WithFsync(true)},
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
safety: mgo.Safe{FSync: true},
readPreference: DefaultReadPreference,
},
nil,
},
{
"with_tail",
[]ClientOptionFunc{WithTail(true)},
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
tail: true,
readPreference: DefaultReadPreference,
},
nil,
},
{
"with_ssl",
[]ClientOptionFunc{WithSSL(true)},
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
tlsConfig: &tls.Config{InsecureSkipVerify: true, RootCAs: x509.NewCertPool()},
readPreference: DefaultReadPreference,
},
nil,
},
{
"with_ssl_with_certs",
[]ClientOptionFunc{WithSSL(true), WithCACerts([]string{rootPEM})},
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
tlsConfig: &tls.Config{InsecureSkipVerify: false, RootCAs: certPool()},
readPreference: DefaultReadPreference,
},
nil,
},
{
"with_ssl_with_cert_file",
[]ClientOptionFunc{WithSSL(true), WithCACerts([]string{"testdata/ca.pem"})},
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
tlsConfig: &tls.Config{InsecureSkipVerify: false, RootCAs: certPool()},
readPreference: DefaultReadPreference,
},
nil,
},
{
"with_ssl_with_cert_file_permission_denied",
[]ClientOptionFunc{WithSSL(true), WithCACerts([]string{"testdata/ca_no_perms.pem"})},
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
tlsConfig: &tls.Config{InsecureSkipVerify: false, RootCAs: certPool()},
readPreference: DefaultReadPreference,
},
&os.PathError{Op: "open", Path: "testdata/ca_no_perms.pem", Err: os.ErrPermission},
},
{
"with_certs",
[]ClientOptionFunc{WithCACerts([]string{rootPEM})},
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
tlsConfig: &tls.Config{InsecureSkipVerify: false, RootCAs: certPool()},
readPreference: DefaultReadPreference,
},
nil,
},
{
"with_certs_not_found",
[]ClientOptionFunc{WithCACerts([]string{"thisfiledoesnotexist"})},
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
tlsConfig: &tls.Config{InsecureSkipVerify: false, RootCAs: certPool()},
readPreference: DefaultReadPreference,
},
errors.New("Cert file not found"),
},
{
"with_certs_invalid",
[]ClientOptionFunc{WithCACerts([]string{"testdata/ca_invalid.pem"})},
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
tlsConfig: &tls.Config{InsecureSkipVerify: false, RootCAs: certPool()},
readPreference: DefaultReadPreference,
},
client.ErrInvalidCert,
},
{
"with_read_preference_invalid",
[]ClientOptionFunc{WithReadPreference("blah")},
&Client{},
InvalidReadPreferenceError{ReadPreference: "blah"},
},
{
"with_primary_read_preference",
[]ClientOptionFunc{WithReadPreference("Primary")},
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
readPreference: 2,
},
nil,
},
{
"with_primary_preferred_read_preference_valid",
[]ClientOptionFunc{WithReadPreference("PrimaryPreferred")},
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
readPreference: 3,
},
nil,
},
{
"with_secondary_read_preference_valid",
[]ClientOptionFunc{WithReadPreference("Secondary")},
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
readPreference: 4,
},
nil,
},
{
"with_secondary_preferred_read_preference_valid",
[]ClientOptionFunc{WithReadPreference("SecondaryPreferred")},
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
readPreference: 5,
},
nil,
},
{
"with_nearest_read_preference_valid",
[]ClientOptionFunc{WithReadPreference("Nearest")},
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
readPreference: 6,
},
nil,
},
{
"with_default_read_preference",
[]ClientOptionFunc{WithReadPreference("")},
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
readPreference: 2,
},
nil,
},
}
func TestNewClient(t *testing.T) {
os.Chmod("testdata/ca_no_perms.pem", 0222)
defer os.Chmod("testdata/ca_no_perms.pem", 0644)
for _, ct := range clientTests {
actual, err := NewClient(ct.options...)
if ct.expectedErr != nil && !reflect.DeepEqual(err.Error(), ct.expectedErr.Error()) {
t.Fatalf("[%s] unexpected NewClient error, expected %+v, got %+v\n", ct.name, ct.expectedErr, err)
}
if err == nil && !reflect.DeepEqual(ct.expected, actual) {
t.Errorf("[%s] Client mismatch\nexpected %+v\ngot %+v", ct.name, ct.expected, actual)
}
}
}
var (
caCertPool = func() *x509.CertPool {
pool := x509.NewCertPool()
c, _ := ioutil.ReadFile("/tmp/mongodb/ca.crt")
pool.AppendCertsFromPEM(c)
return pool
}
connectTests = []struct {
name string
client *Client
expectedErr error
}{
{
"default connect",
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
},
nil,
},
{
"timeout connect",
&Client{
uri: "mongodb://localhost:37017",
sessionTimeout: 2 * time.Second,
safety: DefaultSafety,
},
client.ConnectError{Reason: "no reachable servers"},
},
{
"authenticated connect",
&Client{
uri: "mongodb://transporter:transporter@127.0.0.1:10000,127.0.0.1:10001/admin",
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
},
nil,
},
{
"failed authenticated connect",
&Client{
uri: "mongodb://transporter:wrongpassword@127.0.0.1:10000,127.0.0.1:10001/admin",
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
},
client.ConnectError{Reason: "server returned error on SASL authentication step: Authentication failed."},
},
{
"connect with ssl and verify",
&Client{
uri: "mongodb://localhost:11112/test",
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
tlsConfig: &tls.Config{InsecureSkipVerify: false, RootCAs: caCertPool()},
},
nil,
},
{
"connect with ssl skip verify",
&Client{
uri: "mongodb://localhost:11112/test",
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
tlsConfig: &tls.Config{InsecureSkipVerify: true, RootCAs: x509.NewCertPool()},
},
nil,
},
{
"with_tail",
&Client{
uri: DefaultURI,
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
tail: true,
},
nil,
},
{
"with tail not replset",
&Client{
uri: "mongodb://127.0.0.1:29017",
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
tail: true,
},
OplogAccessError{"database missing oplog.rs collection"},
},
{
"with tail no access",
&Client{
uri: "mongodb://list_but_cant_read:xyz123@127.0.0.1:10000,127.0.0.1:10001/test",
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
tail: true,
},
OplogAccessError{"not authorized for oplog.rs collection"},
},
{
"with tail no privileges",
&Client{
uri: "mongodb://cant_read:limited1234@127.0.0.1:10000,127.0.0.1:10001/test",
sessionTimeout: DefaultSessionTimeout,
safety: DefaultSafety,
tail: true,
},
OplogAccessError{"unable to list collections on local database"},
},
}
)
func TestConnect(t *testing.T) {
if testing.Short() {
t.Skip("skipping Connect in short mode")
}
for _, ct := range connectTests {
_, err := ct.client.Connect()
if err != ct.expectedErr {
t.Fatalf("[%s] unexpected Connect error, expected %+v, got %+v\n", ct.name, ct.expectedErr, err)
}
if err == nil {
ct.client.Close()
}
}
}