@@ -60,6 +60,10 @@ func (d *mdbTestClient) applyOps(entries []interface{}) error {
60
60
"op" : oe .Operation ,
61
61
"ns" : oe .Namespace ,
62
62
}
63
+ if oe .Operation == "c" && oe .Object != nil && len (oe .Object ) > 0 {
64
+ invParams ["cmd" ] = oe .Object [0 ].Key
65
+ invParams ["coll" ] = oe .Object [0 ].Value .(string )
66
+ }
63
67
d .applyOpsInv = append (d .applyOpsInv , invParams )
64
68
65
69
return nil
@@ -139,14 +143,114 @@ func TestIsOpForCloning(t *testing.T) {
139
143
}
140
144
141
145
func TestApply (t * testing.T ) {
142
- t .Run ("oplog restore" , func (t * testing.T ) {
143
- //todo:
146
+ t .Run ("collection restore" , func (t * testing.T ) {
147
+ testCases := []struct {
148
+ desc string
149
+ oplogFile string
150
+ resOps []string
151
+ resNS []string
152
+ resCmd []string
153
+ resColl []string
154
+ }{
155
+ {
156
+ desc : "collection: create-drop-create" ,
157
+ oplogFile : "ops_cmd_create_drop" ,
158
+ resOps : []string {"c" , "c" , "c" , "c" , "c" },
159
+ resNS : []string {"mydb.$cmd" , "mydb.$cmd" , "mydb.$cmd" , "mydb.$cmd" , "mydb.$cmd" },
160
+ resCmd : []string {"drop" , "create" , "drop" , "drop" , "create" },
161
+ resColl : []string {"c1" , "c1" , "c1" , "c2" , "c2" },
162
+ },
163
+ //todo: add more cases
164
+ }
165
+ for _ , tC := range testCases {
166
+ t .Run (tC .desc , func (t * testing.T ) {
167
+ db := newMDBTestClient ()
168
+ oRestore := newOplogRestoreTest (db )
169
+
170
+ fr := useTestFile (t , tC .oplogFile )
171
+
172
+ _ , err := oRestore .Apply (fr )
173
+ if err != nil {
174
+ t .Fatalf ("error while applying oplog: %v" , err )
175
+ }
176
+
177
+ if len (tC .resOps ) != len (db .applyOpsInv ) {
178
+ t .Errorf ("wrong number of applyOps invocation, want=%d, got=%d" , len (tC .resOps ), len (db .applyOpsInv ))
179
+ }
180
+ for i , wantOp := range tC .resOps {
181
+ gotOp := db .applyOpsInv [i ]["op" ]
182
+ if wantOp != gotOp {
183
+ t .Errorf ("wrong #%d. operation: want=%s, got=%s" , i , wantOp , gotOp )
184
+ }
185
+ }
186
+ for i , wantNS := range tC .resNS {
187
+ gotNS := db .applyOpsInv [i ]["ns" ]
188
+ if wantNS != gotNS {
189
+ t .Errorf ("wrong #%d. namespace: want=%s, got=%s" , i , wantNS , gotNS )
190
+ }
191
+ }
192
+ for i , wantCmd := range tC .resCmd {
193
+ gotCmd := db .applyOpsInv [i ]["cmd" ]
194
+ if wantCmd != gotCmd {
195
+ t .Errorf ("wrong #%d. command: want=%s, got=%s" , i , wantCmd , gotCmd )
196
+ }
197
+ }
198
+ for i , wantColl := range tC .resColl {
199
+ gotColl := db .applyOpsInv [i ]["coll" ]
200
+ if wantColl != gotColl {
201
+ t .Errorf ("wrong #%d. collection: want=%s, got=%s" , i , wantColl , gotColl )
202
+ }
203
+ }
204
+ })
205
+ }
144
206
})
145
207
146
208
t .Run ("selective restore" , func (t * testing.T ) {
147
209
//todo:
148
210
})
149
211
212
+ t .Run ("index restore" , func (t * testing.T ) {
213
+ testCases := []struct {
214
+ desc string
215
+ oplogFile string
216
+ db string
217
+ coll string
218
+ idxs bson.D
219
+ }{
220
+ {
221
+ desc : "index: dropIndexes-createIndexes" ,
222
+ oplogFile : "ops_cmd_createIndexes_dropIndexes" ,
223
+ db : "mydb" ,
224
+ coll : "c1" ,
225
+ idxs : bson.D {{"fieldX" , - 1 }, {"fieldZ" , - 1 }},
226
+ },
227
+ //todo: add more cases
228
+ }
229
+ for _ , tC := range testCases {
230
+ t .Run (tC .desc , func (t * testing.T ) {
231
+ db := newMDBTestClient ()
232
+ oRestore := newOplogRestoreTest (db )
233
+
234
+ fr := useTestFile (t , tC .oplogFile )
235
+
236
+ _ , err := oRestore .Apply (fr )
237
+ if err != nil {
238
+ t .Fatalf ("error while applying oplog: %v" , err )
239
+ }
240
+
241
+ idxs := oRestore .indexCatalog .GetIndexes (tC .db , tC .coll )
242
+ if len (idxs ) != len (tC .idxs ) {
243
+ t .Errorf ("wrong number of indexes: want=%d, got=%d" , len (tC .idxs ), len (idxs ))
244
+ }
245
+ for i , idx := range idxs {
246
+ if idx .Key [0 ].Key != tC .idxs [i ].Key {
247
+ t .Errorf ("wrong key: want=%v, got=%v" , tC .idxs [i ], idx .Key [0 ])
248
+ }
249
+ }
250
+ })
251
+ }
252
+ })
253
+
150
254
t .Run ("cloning namespace" , func (t * testing.T ) {
151
255
testCases := []struct {
152
256
desc string
@@ -180,6 +284,7 @@ func TestApply(t *testing.T) {
180
284
resOps : []string {},
181
285
resNS : []string {},
182
286
},
287
+ // add index creation
183
288
}
184
289
for _ , tC := range testCases {
185
290
t .Run (tC .desc , func (t * testing.T ) {
@@ -223,7 +328,7 @@ func useTestFile(t *testing.T, testFileName string) io.ReadCloser {
223
328
t .Fatalf ("failed to read test json file: filename=%s, err=%v" , f , err )
224
329
}
225
330
226
- var jsonDocs []map [ string ] interface {}
331
+ var jsonDocs []db. Oplog
227
332
err = bson .UnmarshalExtJSON (jsonData , false , & jsonDocs )
228
333
if err != nil {
229
334
t .Fatalf ("failed to parse test json array: filename=%s, err=%v" , f , err )
0 commit comments