Skip to content

Commit 710dd10

Browse files
committed
Add skeleton for testing OplogRestore.Apply
... from json test file
1 parent 8efca19 commit 710dd10

File tree

2 files changed

+321
-2
lines changed

2 files changed

+321
-2
lines changed

pbm/oplog/restore_test.go

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
package oplog
22

33
import (
4+
"bytes"
45
"context"
6+
"io"
7+
"os"
8+
"path/filepath"
59
"testing"
610

711
"github.com/mongodb/mongo-tools/common/db"
8-
"github.com/percona/percona-backup-mongodb/pbm/snapshot"
12+
"github.com/mongodb/mongo-tools/common/idx"
13+
"github.com/mongodb/mongo-tools/mongorestore/ns"
914
"go.mongodb.org/mongo-driver/bson"
1015
"go.mongodb.org/mongo-driver/bson/primitive"
16+
17+
"github.com/percona/percona-backup-mongodb/pbm/snapshot"
1118
)
1219

1320
func newOplogRestoreTest(mdb mDBCl) *OplogRestore {
21+
noUUID, _ := ns.NewMatcher(dontPreserveUUID)
22+
matcher, _ := ns.NewMatcher(append(snapshot.ExcludeFromRestore, excludeFromOplog...))
1423
return &OplogRestore{
15-
mdb: mdb,
24+
mdb: mdb,
25+
ver: &db.Version{7, 0, 0},
26+
excludeNS: matcher,
27+
noUUIDns: noUUID,
28+
preserveUUIDopt: true,
29+
preserveUUID: true,
30+
indexCatalog: idx.NewIndexCatalog(),
31+
filter: DefaultOpFilter,
1632
}
1733
}
1834

@@ -104,6 +120,49 @@ func TestIsOpForCloning(t *testing.T) {
104120
}
105121
}
106122

123+
func TestApply(t *testing.T) {
124+
oRestore := newOplogRestoreTest(&mdbTestClient{})
125+
oRestore.SetCloneNS(context.Background(), snapshot.CloneNS{FromNS: "mydb.c5", ToNS: "mydb.c5"})
126+
127+
fr := useTestFile(t, "oplog_test.json")
128+
129+
lts, err := oRestore.Apply(fr)
130+
if err != nil {
131+
t.Fatal(err)
132+
}
133+
134+
t.Log(lts)
135+
}
136+
137+
func useTestFile(t *testing.T, testFileName string) io.ReadCloser {
138+
t.Helper()
139+
140+
jsonData, err := os.ReadFile(filepath.Join("./testdata", testFileName))
141+
if err != nil {
142+
t.Fatalf("failed to read test json file: filename=%s, err=%v", testFileName, err)
143+
}
144+
145+
var jsonDocs []map[string]interface{}
146+
err = bson.UnmarshalExtJSON(jsonData, false, &jsonDocs)
147+
if err != nil {
148+
t.Fatalf("failed to parse test json array: filename=%s, err=%v", testFileName, err)
149+
}
150+
151+
b := &bytes.Buffer{}
152+
for _, jsonDoc := range jsonDocs {
153+
bsonDoc, err := bson.Marshal(jsonDoc)
154+
if err != nil {
155+
t.Fatalf("failed to marshal json to bson: %v", err)
156+
}
157+
_, err = b.Write(bsonDoc)
158+
if err != nil {
159+
t.Fatalf("Failed to write BSON: %v", err)
160+
}
161+
}
162+
163+
return io.NopCloser(b)
164+
}
165+
107166
func createInsertOp(t *testing.T, ns string) *db.Oplog {
108167
t.Helper()
109168
iOpJSON := `

pbm/oplog/testdata/oplog_test.json

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
[
2+
{
3+
"op": "c",
4+
"ns": "mydb.$cmd",
5+
"ui": {
6+
"$binary": {
7+
"base64": "f774YvKERIKXSJVH+xCtPw==",
8+
"subType": "04"
9+
}
10+
},
11+
"o": {
12+
"create": "c5",
13+
"idIndex": {
14+
"v": 2,
15+
"key": {
16+
"_id": 1
17+
},
18+
"name": "_id_"
19+
}
20+
},
21+
"ts": {
22+
"$timestamp": {
23+
"t": 1730733015,
24+
"i": 1
25+
}
26+
},
27+
"t": {
28+
"$numberLong": "7"
29+
},
30+
"v": {
31+
"$numberLong": "2"
32+
},
33+
"wall": {
34+
"$date": "2024-11-04T15:10:15.564Z"
35+
}
36+
},
37+
{
38+
"lsid": {
39+
"id": {
40+
"$binary": {
41+
"base64": "HxXre7SSRxe8eq+OjOQOhw==",
42+
"subType": "04"
43+
}
44+
},
45+
"uid": {
46+
"$binary": {
47+
"base64": "Bh/Anp+//gSHltMgOtOX+7sunrF/VwW+VDdA3fRANl0=",
48+
"subType": "00"
49+
}
50+
}
51+
},
52+
"txnNumber": {
53+
"$numberLong": "4"
54+
},
55+
"op": "i",
56+
"ns": "mydb.c5",
57+
"ui": {
58+
"$binary": {
59+
"base64": "f774YvKERIKXSJVH+xCtPw==",
60+
"subType": "04"
61+
}
62+
},
63+
"o": {
64+
"_id": {
65+
"$oid": "6728e3fcedfb509c06f01307"
66+
},
67+
"person": "jure",
68+
"age": 37
69+
},
70+
"o2": {
71+
"_id": {
72+
"$oid": "6728e3fcedfb509c06f01307"
73+
}
74+
},
75+
"stmtId": 0,
76+
"ts": {
77+
"$timestamp": {
78+
"t": 1730733052,
79+
"i": 1
80+
}
81+
},
82+
"t": {
83+
"$numberLong": "7"
84+
},
85+
"v": {
86+
"$numberLong": "2"
87+
},
88+
"wall": {
89+
"$date": "2024-11-04T15:10:52.775Z"
90+
},
91+
"prevOpTime": {
92+
"ts": {
93+
"$timestamp": {
94+
"t": 0,
95+
"i": 0
96+
}
97+
},
98+
"t": {
99+
"$numberLong": "-1"
100+
}
101+
}
102+
},
103+
{
104+
"lsid": {
105+
"id": {
106+
"$binary": {
107+
"base64": "HxXre7SSRxe8eq+OjOQOhw==",
108+
"subType": "04"
109+
}
110+
},
111+
"uid": {
112+
"$binary": {
113+
"base64": "Bh/Anp+//gSHltMgOtOX+7sunrF/VwW+VDdA3fRANl0=",
114+
"subType": "00"
115+
}
116+
}
117+
},
118+
"txnNumber": {
119+
"$numberLong": "5"
120+
},
121+
"op": "u",
122+
"ns": "mydb.c5",
123+
"ui": {
124+
"$binary": {
125+
"base64": "f774YvKERIKXSJVH+xCtPw==",
126+
"subType": "04"
127+
}
128+
},
129+
"o": {
130+
"$v": 2,
131+
"diff": {
132+
"i": {
133+
"city": "split"
134+
}
135+
}
136+
},
137+
"o2": {
138+
"_id": {
139+
"$oid": "6728e3fcedfb509c06f01307"
140+
}
141+
},
142+
"stmtId": 0,
143+
"ts": {
144+
"$timestamp": {
145+
"t": 1730733212,
146+
"i": 1
147+
}
148+
},
149+
"t": {
150+
"$numberLong": "7"
151+
},
152+
"v": {
153+
"$numberLong": "2"
154+
},
155+
"wall": {
156+
"$date": "2024-11-04T15:13:32.123Z"
157+
},
158+
"prevOpTime": {
159+
"ts": {
160+
"$timestamp": {
161+
"t": 0,
162+
"i": 0
163+
}
164+
},
165+
"t": {
166+
"$numberLong": "-1"
167+
}
168+
}
169+
},
170+
{
171+
"lsid": {
172+
"id": {
173+
"$binary": {
174+
"base64": "HxXre7SSRxe8eq+OjOQOhw==",
175+
"subType": "04"
176+
}
177+
},
178+
"uid": {
179+
"$binary": {
180+
"base64": "Bh/Anp+//gSHltMgOtOX+7sunrF/VwW+VDdA3fRANl0=",
181+
"subType": "00"
182+
}
183+
}
184+
},
185+
"txnNumber": {
186+
"$numberLong": "6"
187+
},
188+
"op": "d",
189+
"ns": "mydb.c5",
190+
"ui": {
191+
"$binary": {
192+
"base64": "f774YvKERIKXSJVH+xCtPw==",
193+
"subType": "04"
194+
}
195+
},
196+
"o": {
197+
"_id": {
198+
"$oid": "6728e3fcedfb509c06f01307"
199+
}
200+
},
201+
"stmtId": 0,
202+
"ts": {
203+
"$timestamp": {
204+
"t": 1730733256,
205+
"i": 1
206+
}
207+
},
208+
"t": {
209+
"$numberLong": "7"
210+
},
211+
"v": {
212+
"$numberLong": "2"
213+
},
214+
"wall": {
215+
"$date": "2024-11-04T15:14:16.626Z"
216+
},
217+
"prevOpTime": {
218+
"ts": {
219+
"$timestamp": {
220+
"t": 0,
221+
"i": 0
222+
}
223+
},
224+
"t": {
225+
"$numberLong": "-1"
226+
}
227+
}
228+
},
229+
{
230+
"op": "c",
231+
"ns": "mydb.$cmd",
232+
"ui": {
233+
"$binary": {
234+
"base64": "f774YvKERIKXSJVH+xCtPw==",
235+
"subType": "04"
236+
}
237+
},
238+
"o": {
239+
"drop": "c5"
240+
},
241+
"o2": {
242+
"numRecords": 0
243+
},
244+
"ts": {
245+
"$timestamp": {
246+
"t": 1730733285,
247+
"i": 1
248+
}
249+
},
250+
"t": {
251+
"$numberLong": "7"
252+
},
253+
"v": {
254+
"$numberLong": "2"
255+
},
256+
"wall": {
257+
"$date": "2024-11-04T15:14:45.192Z"
258+
}
259+
}
260+
]

0 commit comments

Comments
 (0)