forked from brahma-adshonor/gorr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_hook_test.go
519 lines (421 loc) · 16.4 KB
/
mongo_hook_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
package gorr
import (
"context"
"fmt"
"testing"
"unsafe"
"github.com/brahma-adshonor/gohook"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
var (
globalDummyClient = &mongo.Client{}
)
func init() {
enableRegressionEngine(RegressionRecord)
GlobalMgr.SetStorage(NewMapStorage(100))
GlobalMgr.SetNotify(func(key, msg string, data []byte) {
fmt.Printf("hook msg:%s,%s,datasize:%d\n", key, msg, len(data))
})
}
func mgConnectDummy(ctx context.Context, opts ...*options.ClientOptions) (*mongo.Client, error) {
return globalDummyClient, nil
}
func buildCursor() *mongo.Cursor {
d := CursorData{
Id: 233,
Err: "dummy mongo err",
}
var doc1, doc2, elem1, elem2 []byte
elem1 = bsoncore.AppendStringElement(elem1, "$key1", "value1")
elem2 = bsoncore.AppendInt32Element(elem2, "$key2", int32(233))
doc1 = bsoncore.BuildDocumentFromElements(doc1, elem1)
doc2 = bsoncore.BuildDocumentFromElements(doc2, elem1, elem2)
d.Data = append(d.Data, doc1)
d.Data = append(d.Data, doc2)
h := buildCursorHolder(d, nil)
return &h.cursor
}
func checkCursor(t *testing.T, res *mongo.Cursor) {
var docs []bson.D
err := res.All(context.Background(), &docs)
assert.Nil(t, err)
assert.Equal(t, 2, len(docs))
ep1 := bson.D{{Key: "$key1", Value: "value1"}}
assert.Equal(t, ep1, docs[0])
ep2 := bson.D{{Key: "$key1", Value: "value1"}, {Key: "$key2", Value: int32(233)}}
assert.Equal(t, ep2, docs[1])
assert.Equal(t, int64(233), res.ID())
assert.Equal(t, "dummy mongo err", res.Err().Error())
assert.False(t, res.Next(context.Background()))
assert.False(t, res.TryNext(context.Background()))
assert.Nil(t, res.Close(context.Background()))
}
func TestMgConnect(t *testing.T) {
fmt.Printf("testing connect")
GlobalMgr.SetState(RegressionRecord)
err := EnableMongoHook()
assert.Nil(t, err)
defer DisableMongoHook()
err = gohook.Hook(mgConnectTramp, mgConnectDummy, nil)
assert.Nil(t, err)
c, err := mongo.Connect(context.Background())
assert.Nil(t, err)
p1 := unsafe.Pointer(c)
p2 := unsafe.Pointer(globalDummyClient)
assert.NotEqual(t, p1, p2)
cc := getClientHolder(c)
assert.NotNil(t, cc)
c3 := &cc.client
p3 := unsafe.Pointer(c3)
assert.Equal(t, p1, p3)
cc2 := getClientHolder(globalDummyClient)
assert.Nil(t, cc2)
}
func TestMongoCursorHook(t *testing.T) {
GlobalMgr.SetState(RegressionRecord)
err := EnableMongoHook()
assert.Nil(t, err)
defer DisableMongoHook()
cursor := buildCursor()
checkCursor(t, cursor)
var dd bson.D
assert.NotNil(t, cursor.Decode(&dd))
h := getCursorHolder(cursor)
h.cur--
assert.True(t, h.cursor.Next(context.Background()))
assert.Nil(t, h.cursor.Decode(&dd))
ep2 := bson.D{{Key: "$key1", Value: "value1"}, {Key: "$key2", Value: int32(233)}}
assert.Equal(t, ep2, dd)
}
func buildSingleResult() *mongo.SingleResult {
var doc, elem1, elem2 []byte
elem1 = bsoncore.AppendStringElement(elem1, "$key1", "value1")
elem2 = bsoncore.AppendInt32Element(elem2, "$key2", int32(233))
doc = bsoncore.BuildDocumentFromElements(doc, elem1, elem2)
d := SingleResultData{
Data: doc,
Err: "", //fmt.Errorf("dummy mongo err"),
}
h := buildSingleResultHolder(d, nil)
return &h.ret
}
func checkSingleResult(t *testing.T, sg *mongo.SingleResult) {
var doc, elem1, elem2 []byte
elem1 = bsoncore.AppendStringElement(elem1, "$key1", "value1")
elem2 = bsoncore.AppendInt32Element(elem2, "$key2", int32(233))
doc = bsoncore.BuildDocumentFromElements(doc, elem1, elem2)
var dd bson.D
assert.Nil(t, sg.Decode(&dd))
ep := bson.D{{Key: "$key1", Value: "value1"}, {Key: "$key2", Value: int32(233)}}
assert.Equal(t, dd, ep)
dd2, err := sg.DecodeBytes()
assert.Nil(t, err)
assert.Equal(t, doc, []byte(dd2))
h := getSingleResultHolder(sg)
h.sd.Err = "dummy mongo err"
assert.Equal(t, "dummy mongo err", sg.Err().Error())
}
func TestMongoSingleResultHook(t *testing.T) {
GlobalMgr.SetState(RegressionRecord)
err := EnableMongoHook()
assert.Nil(t, err)
defer DisableMongoHook()
sg := buildSingleResult()
checkSingleResult(t, sg)
}
func mgDummyConnectHook(ctx context.Context, opts ...*options.ClientOptions) (*mongo.Client, error) {
return mongo.NewClient(opts...)
}
func mgDummyClientListDatabasesHook(c *mongo.Client, ctx context.Context, filter interface{}, opts ...*options.ListDatabasesOptions) (mongo.ListDatabasesResult, error) {
return mongo.ListDatabasesResult{
TotalSize: 233,
Databases: []mongo.DatabaseSpecification{
mongo.DatabaseSpecification{Name: "n1", SizeOnDisk: 244, Empty: false},
mongo.DatabaseSpecification{Name: "n2", SizeOnDisk: 254, Empty: false},
},
}, nil
}
func mgDummyDatabaseRunCommand(db *mongo.Database, ctx context.Context, runCommand interface{}, opts ...*options.RunCmdOptions) *mongo.SingleResult {
return buildSingleResult()
}
func mgDummyDatabaseRunCommandCursor(db *mongo.Database, ctx context.Context, runCommand interface{}, opts ...*options.RunCmdOptions) (*mongo.Cursor, error) {
c := buildCursor()
return c, nil
}
func mgDummyCollectionCountDocuments(cl *mongo.Collection, ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error) {
return 233, nil
}
func mgDummyCollectionEstimatedDocumentCount(cl *mongo.Collection, ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error) {
return 233, nil
}
func mgDummyCollectionDelete(cl *mongo.Collection, ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
return &mongo.DeleteResult{DeletedCount: 233}, nil
}
var (
distinctAll = []interface{}{int32(11), int32(22), "dummy", int32(4), int32(5)}
)
func mgDummyCollectionDistinct(cl *mongo.Collection, ctx context.Context, fieldName string, filter interface{},
opts ...*options.DistinctOptions) ([]interface{}, error) {
return distinctAll, nil
}
func mgDummyCollectionFind(cl *mongo.Collection, ctx context.Context, filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) {
c := buildCursor()
return c, nil
}
func mgDummyCollectionFindOneAndReplace(cl *mongo.Collection, ctx context.Context, filter interface{},
replacement interface{}, opts ...*options.FindOneAndReplaceOptions) *mongo.SingleResult {
return buildSingleResult()
}
func mgDummyCollectionFindOneAndDelete(cl *mongo.Collection, ctx context.Context, filter interface{},
opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult {
return buildSingleResult()
}
func mgDummyCollectionFindOneAndUpdate(cl *mongo.Collection, ctx context.Context, filter interface{},
update interface{}, opts ...*options.FindOneAndUpdateOptions) *mongo.SingleResult {
fmt.Printf("@@@@@@@@@@@@@@@@@@@@@\n")
return buildSingleResult()
}
func buildUpdateResult() *mongo.UpdateResult {
d1 := bson.D{{Key: "v22_id", Value: 233}}
id1, _ := getDocId(nil, d1)
return &mongo.UpdateResult{
MatchedCount: 233,
ModifiedCount: 244,
UpsertedCount: 255,
UpsertedID: id1,
}
}
func mgDummyCollectionUpdateMany(cl *mongo.Collection, ctx context.Context, filter interface{},
update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
return buildUpdateResult(), nil
}
func mgDummyCollectionReplaceOne(cl *mongo.Collection, ctx context.Context, filter interface{},
replacement interface{}, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error) {
return buildUpdateResult(), nil
}
func mgDummuyCollectionAggregate(db *mongo.Collection, ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error) {
return buildCursor(), nil
}
func mgDummyDatabaseAggregate(db *mongo.Database, ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error) {
return buildCursor(), nil
}
func mgDummyDatabaseListCollections(db *mongo.Database, ctx context.Context, filter interface{}, opts ...*options.ListCollectionsOptions) (*mongo.Cursor, error) {
return buildCursor(), nil
}
func TestMongoClientHook(t *testing.T) {
var r1, r2 mongo.ListDatabasesResult
{
GlobalMgr.SetState(RegressionRecord)
err := EnableMongoHook()
assert.Nil(t, err)
defer DisableMongoHook()
gohook.Hook(mgConnectTramp, mgDummyConnectHook, nil)
defer gohook.UnHook(mgConnectTramp)
gohook.Hook(mgClientListDatabasesTramp, mgDummyClientListDatabasesHook, nil)
defer gohook.UnHook(mgDummyClientListDatabasesHook)
gohook.Hook(mgDatabaseRunCommandTramp, mgDummyDatabaseRunCommand, nil)
defer gohook.UnHook(mgDatabaseRunCommandHook)
gohook.Hook(mgDatabaseRunCommandCursorTramp, mgDummyDatabaseRunCommandCursor, nil)
defer gohook.UnHook(mgDatabaseRunCommandCursorHook)
gohook.Hook(mgCollectionCountDocumentsTramp, mgDummyCollectionCountDocuments, nil)
defer gohook.UnHook(mgCollectionCountDocumentsTramp)
gohook.Hook(mgCollectionDeleteManyTramp, mgDummyCollectionDelete, nil)
defer gohook.UnHook(mgCollectionDeleteManyTramp)
gohook.Hook(mgCollectionDeleteOneTramp, mgDummyCollectionDelete, nil)
defer gohook.UnHook(mgCollectionDeleteOneTramp)
gohook.Hook(mgCollectionDistinctTramp, mgDummyCollectionDistinct, nil)
defer gohook.UnHook(mgCollectionDistinctTramp)
gohook.Hook(mgCollectionFindTramp, mgDummyCollectionFind, nil)
defer gohook.UnHook(mgCollectionFindTramp)
gohook.Hook(mgCollectionFindOneAndReplaceTramp, mgDummyCollectionFindOneAndReplace, nil)
defer gohook.UnHook(mgCollectionFindOneAndReplaceTramp)
gohook.Hook(mgCollectionFindOneAndDeleteTramp, mgDummyCollectionFindOneAndDelete, nil)
defer gohook.UnHook(mgCollectionFindOneAndDeleteTramp)
gohook.Hook(mgCollectionEstimatedDocumentCountTramp, mgDummyCollectionEstimatedDocumentCount, nil)
defer gohook.UnHook(mgCollectionEstimatedDocumentCountTramp)
assert.Nil(t, gohook.Hook(mgCollectionFindOneAndUpdateTramp, mgDummyCollectionFindOneAndUpdate, nil))
defer gohook.UnHook(mgCollectionFindOneAndUpdateTramp)
assert.Nil(t, gohook.Hook(mgCollectionUpdateManyTramp, mgDummyCollectionUpdateMany, nil))
defer gohook.UnHook(mgCollectionUpdateManyTramp)
assert.Nil(t, gohook.Hook(mgCollectionReplaceOneTramp, mgDummyCollectionReplaceOne, nil))
defer gohook.UnHook(mgCollectionReplaceOneTramp)
assert.Nil(t, gohook.Hook(mgCollectionAggregateTramp, mgDummuyCollectionAggregate, nil))
defer gohook.UnHook(mgCollectionAggregateTramp)
assert.Nil(t, gohook.Hook(mgDatabaseAggregateTramp, mgDummyDatabaseAggregate, nil))
defer gohook.UnHook(mgDatabaseAggregateTramp)
assert.Nil(t, gohook.Hook(mgDatabaseListCollectionsTramp, mgDummyDatabaseListCollections, nil))
defer gohook.UnHook(mgDatabaseListCollectionsTramp)
opt := options.Client().ApplyURI("mongodb://localhost:27017")
c1, err1 := mongo.Connect(context.Background(), opt)
assert.Nil(t, err1)
assert.NotNil(t, c1)
assert.NotNil(t, getClientHolder(c1))
// test client
filter := bson.D{{Key: "key1", Value: "value1"}}
r1, err = c1.ListDatabases(context.Background(), filter)
assert.Nil(t, err)
assert.Equal(t, int64(233), r1.TotalSize)
assert.Equal(t, 2, len(r1.Databases))
assert.Equal(t, "n1", r1.Databases[0].Name)
assert.Equal(t, "n2", r1.Databases[1].Name)
// test database
db := c1.Database("db1")
assert.NotNil(t, db)
cmd1 := bson.D{{Key: "x", Value: 1}}
res1 := db.RunCommand(context.Background(), cmd1)
assert.NotNil(t, res1)
assert.Nil(t, res1.Err())
checkSingleResult(t, res1)
cr2, err := db.Aggregate(context.Background(), cmd1)
checkCursor(t, cr2)
cmd2 := bson.D{{Key: "$xy", Value: 1}}
cr3, err := db.ListCollections(context.Background(), cmd2)
checkCursor(t, cr3)
// database RunCommandCursor
res2, err := db.RunCommandCursor(context.Background(), cmd2)
assert.Nil(t, err)
assert.NotNil(t, res2)
checkCursor(t, res2)
// test collection
cl := db.Collection("cl1")
assert.NotNil(t, cl)
cnt, err := cl.CountDocuments(context.Background(), cmd2)
assert.Nil(t, err)
assert.Equal(t, int64(233), cnt)
cnt2, err := cl.EstimatedDocumentCount(context.Background())
assert.Nil(t, err)
assert.Equal(t, int64(233), cnt2)
resd1, err := cl.DeleteMany(context.Background(), cmd2)
assert.Nil(t, err)
assert.Equal(t, int64(233), resd1.DeletedCount)
resd2, err := cl.DeleteOne(context.Background(), cmd2)
assert.Nil(t, err)
assert.Equal(t, int64(233), resd2.DeletedCount)
rdis1, err := cl.Distinct(context.Background(), "xxx", cmd2)
assert.Nil(t, err)
assert.NotNil(t, rdis1)
assert.Equal(t, distinctAll, rdis1)
rf1, err := cl.Find(context.Background(), cmd2)
assert.Nil(t, err)
assert.NotNil(t, rf1)
checkCursor(t, rf1)
rfr1 := cl.FindOneAndReplace(context.Background(), cmd1, cmd2)
checkSingleResult(t, rfr1)
rfr2 := cl.FindOneAndDelete(context.Background(), cmd1)
checkSingleResult(t, rfr2)
rfr3 := cl.FindOneAndUpdate(context.Background(), cmd1, cmd2)
checkSingleResult(t, rfr3)
ru1, err := cl.UpdateMany(context.Background(), cmd1, cmd2)
assert.Nil(t, err)
assert.NotNil(t, ru1.UpsertedID)
ru2 := buildUpdateResult()
ru1.UpsertedID = nil
ru2.UpsertedID = nil
assert.Equal(t, ru2, ru1)
cmd3 := bson.D{{Key: "xy", Value: 1}}
ru3, err := cl.ReplaceOne(context.Background(), cmd1, cmd3)
assert.Nil(t, err)
assert.NotNil(t, ru3.UpsertedID)
ru3.UpsertedID = nil
assert.Equal(t, ru2, ru3)
cr1, err := cl.Aggregate(context.Background(), cmd2)
assert.Nil(t, err)
assert.NotNil(t, cr1)
checkCursor(t, cr1)
}
{
GlobalMgr.SetState(RegressionReplay)
err := EnableMongoHook()
assert.Nil(t, err)
defer DisableMongoHook()
// test client
opt := options.Client().ApplyURI("mongodb://localhost:27017")
c1, err1 := mongo.Connect(context.Background(), opt)
assert.Nil(t, err1)
assert.NotNil(t, c1)
assert.NotNil(t, getClientHolder(c1))
assert.Nil(t, c1.Disconnect(context.Background()))
filter := bson.D{{Key: "key1", Value: "value1"}}
r2, err = c1.ListDatabases(context.Background(), filter)
assert.Nil(t, err)
assert.Equal(t, r1, r2)
// test database
db := c1.Database("db1")
assert.NotNil(t, db)
assert.Nil(t, db.Drop(context.TODO()))
cmd1 := bson.D{{Key: "x", Value: 1}}
res1 := db.RunCommand(context.Background(), cmd1)
assert.NotNil(t, res1)
assert.Nil(t, res1.Err())
checkSingleResult(t, res1)
cr2, err := db.Aggregate(context.Background(), cmd1)
checkCursor(t, cr2)
cmd2 := bson.D{{Key: "$xy", Value: 1}}
cr3, err := db.ListCollections(context.Background(), cmd2)
checkCursor(t, cr3)
// database RunCommandCursor
res2, err := db.RunCommandCursor(context.Background(), cmd2)
assert.Nil(t, err)
assert.NotNil(t, res2)
checkCursor(t, res2)
// test collection
cl := db.Collection("cl1")
assert.NotNil(t, cl)
assert.Nil(t, cl.Drop(context.Background()))
cnt, err := cl.CountDocuments(context.Background(), cmd2)
assert.Nil(t, err)
assert.Equal(t, int64(233), cnt)
cnt2, err := cl.EstimatedDocumentCount(context.Background())
assert.Nil(t, err)
assert.Equal(t, int64(233), cnt2)
resd1, err := cl.DeleteMany(context.Background(), cmd2)
assert.Nil(t, err)
assert.Equal(t, int64(233), resd1.DeletedCount)
resd2, err := cl.DeleteOne(context.Background(), cmd2)
assert.Nil(t, err)
assert.Equal(t, int64(233), resd2.DeletedCount)
rdis1, err := cl.Distinct(context.Background(), "xxx", cmd2)
assert.Nil(t, err)
assert.NotNil(t, rdis1)
assert.Equal(t, distinctAll, rdis1)
// find
rf1, err := cl.Find(context.Background(), cmd2)
assert.Nil(t, err)
assert.NotNil(t, rf1)
checkCursor(t, rf1)
rfr1 := cl.FindOneAndReplace(context.Background(), cmd1, cmd2)
checkSingleResult(t, rfr1)
rfr2 := cl.FindOneAndDelete(context.Background(), cmd1)
checkSingleResult(t, rfr2)
rfr3 := cl.FindOneAndUpdate(context.Background(), cmd1, cmd2)
checkSingleResult(t, rfr3)
docs2 := []interface{}{bson.D{{Key: "$vv", Value: 23}}, bson.D{{Key: "$v2", Value: 2}}}
r2, err := cl.InsertMany(context.Background(), docs2)
assert.Nil(t, err)
assert.Equal(t, 2, len(r2.InsertedIDs))
r3, err := cl.InsertOne(context.Background(), bson.D{{Key: "$vv", Value: 1}})
assert.Nil(t, err)
assert.NotNil(t, r3.InsertedID)
ru1, err := cl.UpdateMany(context.Background(), cmd1, cmd2)
assert.Nil(t, err)
assert.NotNil(t, ru1.UpsertedID)
ru2 := buildUpdateResult()
ru1.UpsertedID = nil
ru2.UpsertedID = nil
assert.Equal(t, ru2, ru1)
cmd3 := bson.D{{Key: "xy", Value: 1}}
ru3, err := cl.ReplaceOne(context.Background(), cmd1, cmd3)
assert.Nil(t, err)
assert.NotNil(t, ru3.UpsertedID)
ru3.UpsertedID = nil
assert.Equal(t, ru2, ru3)
cr1, err := cl.Aggregate(context.Background(), cmd2)
assert.Nil(t, err)
assert.NotNil(t, cr1)
checkCursor(t, cr1)
}
}