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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
|
/**
* Copyright (C) 2015 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects
* for all of the code used other than as permitted herein. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you do not
* wish to do so, delete this exception statement from your version. If you
* delete this exception statement from all source files in the program,
* then also delete it in the license file.
*/
#include "mongo/platform/basic.h"
#include <cstdint>
#include "mongo/db/catalog/collection.h"
#include "mongo/db/catalog/index_catalog.h"
#include "mongo/db/catalog/index_create.h"
#include "mongo/db/client.h"
#include "mongo/db/db_raii.h"
#include "mongo/db/dbdirectclient.h"
#include "mongo/db/dbhelpers.h"
#include "mongo/db/index/index_descriptor.h"
#include "mongo/db/service_context.h"
#include "mongo/db/service_context_d.h"
#include "mongo/dbtests/dbtests.h"
namespace ValidateTests {
using std::unique_ptr;
static const char* const _ns = "unittests.validate_tests";
/**
* Test fixture for a write locked test using collection _ns. Includes functionality to
* partially construct a new IndexDetails in a manner that supports proper cleanup in
* dropCollection().
*/
class ValidateBase {
public:
explicit ValidateBase(bool full) : _ctx(&_txn, _ns), _client(&_txn), _full(full) {
_client.createCollection(_ns);
}
~ValidateBase() {
_client.dropCollection(_ns);
getGlobalServiceContext()->unsetKillAllOperations();
}
Collection* collection() {
return _ctx.getCollection();
}
protected:
bool checkValid() {
ValidateResults results;
BSONObjBuilder output;
ASSERT_OK(collection()->validate(
&_txn, _full ? kValidateFull : kValidateIndex, &results, &output));
// Check if errors are reported if and only if valid is set to false.
ASSERT_EQ(results.valid, results.errors.empty());
if (_full) {
BSONObj outputObj = output.done();
bool allIndexesValid = true;
for (auto elem : outputObj["indexDetails"].Obj()) {
BSONObj indexDetail(elem.value());
allIndexesValid = indexDetail["valid"].boolean() ? allIndexesValid : false;
}
ASSERT_EQ(results.valid, allIndexesValid);
}
return results.valid;
}
const ServiceContext::UniqueOperationContext _txnPtr = cc().makeOperationContext();
OperationContext& _txn = *_txnPtr;
OldClientWriteContext _ctx;
DBDirectClient _client;
bool _full;
};
template <bool full>
class ValidateIdIndexCount : public ValidateBase {
public:
ValidateIdIndexCount() : ValidateBase(full) {}
void run() {
// Create a new collection, insert records {_id: 1} and {_id: 2} and check it's valid.
Database* db = _ctx.db();
Collection* coll;
RecordId id1;
{
OpDebug* const nullOpDebug = nullptr;
WriteUnitOfWork wunit(&_txn);
ASSERT_OK(db->dropCollection(&_txn, _ns));
coll = db->createCollection(&_txn, _ns);
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 1), nullOpDebug, true));
id1 = coll->getCursor(&_txn)->next()->id;
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 2), nullOpDebug, true));
wunit.commit();
}
ASSERT_TRUE(checkValid());
RecordStore* rs = coll->getRecordStore();
// Remove {_id: 1} from the record store, so we get more _id entries than records.
{
WriteUnitOfWork wunit(&_txn);
rs->deleteRecord(&_txn, id1);
wunit.commit();
}
ASSERT_FALSE(checkValid());
// Insert records {_id: 0} and {_id: 1} , so we get too few _id entries, and verify
// validate fails.
{
WriteUnitOfWork wunit(&_txn);
for (int j = 0; j < 2; j++) {
auto doc = BSON("_id" << j);
ASSERT_OK(
rs->insertRecord(&_txn, doc.objdata(), doc.objsize(), /*enforceQuota*/ false));
}
wunit.commit();
}
ASSERT_FALSE(checkValid());
}
};
template <bool full>
class ValidateSecondaryIndexCount : public ValidateBase {
public:
ValidateSecondaryIndexCount() : ValidateBase(full) {}
void run() {
// Create a new collection, insert two documents.
Database* db = _ctx.db();
Collection* coll;
RecordId id1;
{
OpDebug* const nullOpDebug = nullptr;
WriteUnitOfWork wunit(&_txn);
ASSERT_OK(db->dropCollection(&_txn, _ns));
coll = db->createCollection(&_txn, _ns);
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 1 << "a" << 1), nullOpDebug, true));
id1 = coll->getCursor(&_txn)->next()->id;
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 2 << "a" << 2), nullOpDebug, true));
wunit.commit();
}
auto status =
dbtests::createIndexFromSpec(&_txn,
coll->ns().ns(),
BSON("name"
<< "a"
<< "ns" << coll->ns().ns() << "key" << BSON("a" << 1)
<< "background" << false));
ASSERT_OK(status);
ASSERT_TRUE(checkValid());
RecordStore* rs = coll->getRecordStore();
// Remove a record, so we get more _id entries than records, and verify validate fails.
{
WriteUnitOfWork wunit(&_txn);
rs->deleteRecord(&_txn, id1);
wunit.commit();
}
ASSERT_FALSE(checkValid());
// Insert two more records, so we get too few entries for a non-sparse index, and
// verify validate fails.
{
WriteUnitOfWork wunit(&_txn);
for (int j = 0; j < 2; j++) {
auto doc = BSON("_id" << j);
ASSERT_OK(
rs->insertRecord(&_txn, doc.objdata(), doc.objsize(), /*enforceQuota*/ false));
}
wunit.commit();
}
ASSERT_FALSE(checkValid());
}
};
class ValidateSecondaryIndex : public ValidateBase {
public:
ValidateSecondaryIndex() : ValidateBase(true) {}
void run() {
// Create a new collection, insert three records.
Database* db = _ctx.db();
OpDebug* const nullOpDebug = nullptr;
Collection* coll;
RecordId id1;
{
WriteUnitOfWork wunit(&_txn);
ASSERT_OK(db->dropCollection(&_txn, _ns));
coll = db->createCollection(&_txn, _ns);
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 1 << "a" << 1), nullOpDebug, true));
id1 = coll->getCursor(&_txn)->next()->id;
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 2 << "a" << 2), nullOpDebug, true));
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 3 << "b" << 3), nullOpDebug, true));
wunit.commit();
}
auto status =
dbtests::createIndexFromSpec(&_txn,
coll->ns().ns(),
BSON("name"
<< "a"
<< "ns" << coll->ns().ns() << "key" << BSON("a" << 1)
<< "background" << false));
ASSERT_OK(status);
ASSERT_TRUE(checkValid());
RecordStore* rs = coll->getRecordStore();
// Update {a: 1} to {a: 9} without updating the index, so we get inconsistent values
// between the index and the document. Verify validate fails.
{
WriteUnitOfWork wunit(&_txn);
auto doc = BSON("_id" << 1 << "a" << 9);
auto updateStatus = rs->updateRecord(
&_txn, id1, doc.objdata(), doc.objsize(), /*enforceQuota*/ false, NULL);
ASSERT_OK(updateStatus);
wunit.commit();
}
ASSERT_FALSE(checkValid());
}
};
class ValidateIdIndex : public ValidateBase {
public:
ValidateIdIndex() : ValidateBase(true) {}
void run() {
// Create a new collection, insert records {_id: 1} and {_id: 2} and check it's valid.
Database* db = _ctx.db();
OpDebug* const nullOpDebug = nullptr;
Collection* coll;
RecordId id1;
{
WriteUnitOfWork wunit(&_txn);
ASSERT_OK(db->dropCollection(&_txn, _ns));
coll = db->createCollection(&_txn, _ns);
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 1), nullOpDebug, true));
id1 = coll->getCursor(&_txn)->next()->id;
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 2), nullOpDebug, true));
wunit.commit();
}
ASSERT_TRUE(checkValid());
RecordStore* rs = coll->getRecordStore();
// Update {_id: 1} to {_id: 9} without updating the index, so we get inconsistent values
// between the index and the document. Verify validate fails.
{
WriteUnitOfWork wunit(&_txn);
auto doc = BSON("_id" << 9);
auto updateStatus = rs->updateRecord(
&_txn, id1, doc.objdata(), doc.objsize(), /*enforceQuota*/ false, NULL);
ASSERT_OK(updateStatus);
wunit.commit();
}
ASSERT_FALSE(checkValid());
// Revert {_id: 9} to {_id: 1} and verify that validate succeeds.
{
WriteUnitOfWork wunit(&_txn);
auto doc = BSON("_id" << 1);
auto updateStatus = rs->updateRecord(
&_txn, id1, doc.objdata(), doc.objsize(), /*enforceQuota*/ false, NULL);
ASSERT_OK(updateStatus);
wunit.commit();
}
ASSERT_TRUE(checkValid());
// Remove the {_id: 1} document and insert a new document without an index entry, so there
// will still be the same number of index entries and documents, but one document will not
// have an index entry.
{
WriteUnitOfWork wunit(&_txn);
rs->deleteRecord(&_txn, id1);
auto doc = BSON("_id" << 3);
ASSERT_OK(rs->insertRecord(&_txn, doc.objdata(), doc.objsize(), /*enforceQuota*/ false)
.getStatus());
wunit.commit();
}
ASSERT_FALSE(checkValid());
}
};
class ValidateMultiKeyIndex : public ValidateBase {
public:
ValidateMultiKeyIndex() : ValidateBase(true) {}
void run() {
// Create a new collection, insert three records and check it's valid.
Database* db = _ctx.db();
OpDebug* const nullOpDebug = nullptr;
Collection* coll;
RecordId id1;
// {a: [b: 1, c: 2]}, {a: [b: 2, c: 2]}, {a: [b: 1, c: 1]}
auto doc1 = BSON("_id" << 1 << "a" << BSON_ARRAY(BSON("b" << 1) << BSON("c" << 2)));
auto doc1_b = BSON("_id" << 1 << "a" << BSON_ARRAY(BSON("b" << 2) << BSON("c" << 2)));
auto doc1_c = BSON("_id" << 1 << "a" << BSON_ARRAY(BSON("b" << 1) << BSON("c" << 1)));
// {a: [b: 2]}
auto doc2 = BSON("_id" << 2 << "a" << BSON_ARRAY(BSON("b" << 2)));
// {a: [c: 1]}
auto doc3 = BSON("_id" << 3 << "a" << BSON_ARRAY(BSON("c" << 1)));
{
WriteUnitOfWork wunit(&_txn);
ASSERT_OK(db->dropCollection(&_txn, _ns));
coll = db->createCollection(&_txn, _ns);
ASSERT_OK(coll->insertDocument(&_txn, doc1, nullOpDebug, true));
id1 = coll->getCursor(&_txn)->next()->id;
ASSERT_OK(coll->insertDocument(&_txn, doc2, nullOpDebug, true));
ASSERT_OK(coll->insertDocument(&_txn, doc3, nullOpDebug, true));
wunit.commit();
}
ASSERT_TRUE(checkValid());
// Create multi-key index.
auto status =
dbtests::createIndexFromSpec(&_txn,
coll->ns().ns(),
BSON("name"
<< "multikey_index"
<< "ns" << coll->ns().ns() << "key"
<< BSON("a.b" << 1) << "background" << false));
ASSERT_OK(status);
ASSERT_TRUE(checkValid());
RecordStore* rs = coll->getRecordStore();
// Update a document's indexed field without updating the index.
{
WriteUnitOfWork wunit(&_txn);
auto updateStatus = rs->updateRecord(
&_txn, id1, doc1_b.objdata(), doc1_b.objsize(), /*enforceQuota*/ false, NULL);
ASSERT_OK(updateStatus);
wunit.commit();
}
ASSERT_FALSE(checkValid());
// Update a document's non-indexed field without updating the index.
// Index validation should still be valid.
{
WriteUnitOfWork wunit(&_txn);
auto updateStatus = rs->updateRecord(
&_txn, id1, doc1_c.objdata(), doc1_c.objsize(), /*enforceQuota*/ false, NULL);
ASSERT_OK(updateStatus);
wunit.commit();
}
ASSERT_TRUE(checkValid());
}
};
class ValidateSparseIndex : public ValidateBase {
public:
ValidateSparseIndex() : ValidateBase(true) {}
void run() {
// Create a new collection, insert three records and check it's valid.
Database* db = _ctx.db();
OpDebug* const nullOpDebug = nullptr;
Collection* coll;
RecordId id1;
{
WriteUnitOfWork wunit(&_txn);
ASSERT_OK(db->dropCollection(&_txn, _ns));
coll = db->createCollection(&_txn, _ns);
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 1 << "a" << 1), nullOpDebug, true));
id1 = coll->getCursor(&_txn)->next()->id;
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 2 << "a" << 2), nullOpDebug, true));
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 3 << "b" << 1), nullOpDebug, true));
wunit.commit();
}
// Create a sparse index.
auto status =
dbtests::createIndexFromSpec(&_txn,
coll->ns().ns(),
BSON("name"
<< "sparse_index"
<< "ns" << coll->ns().ns() << "key" << BSON("a" << 1)
<< "background" << false << "sparse" << true));
ASSERT_OK(status);
ASSERT_TRUE(checkValid());
RecordStore* rs = coll->getRecordStore();
// Update a document's indexed field without updating the index.
{
WriteUnitOfWork wunit(&_txn);
auto doc = BSON("_id" << 2 << "a" << 3);
auto updateStatus = rs->updateRecord(
&_txn, id1, doc.objdata(), doc.objsize(), /*enforceQuota*/ false, NULL);
ASSERT_OK(updateStatus);
wunit.commit();
}
ASSERT_FALSE(checkValid());
}
};
class ValidatePartialIndex : public ValidateBase {
public:
ValidatePartialIndex() : ValidateBase(true) {}
void run() {
// Create a new collection, insert three records and check it's valid.
Database* db = _ctx.db();
OpDebug* const nullOpDebug = nullptr;
Collection* coll;
RecordId id1;
{
WriteUnitOfWork wunit(&_txn);
ASSERT_OK(db->dropCollection(&_txn, _ns));
coll = db->createCollection(&_txn, _ns);
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 1 << "a" << 1), nullOpDebug, true));
id1 = coll->getCursor(&_txn)->next()->id;
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 2 << "a" << 2), nullOpDebug, true));
// Explicitly test that multi-key partial indexes containing documents that
// don't match the filter expression are handled correctly.
ASSERT_OK(coll->insertDocument(
&_txn, BSON("_id" << 3 << "a" << BSON_ARRAY(-1 << -2 << -3)), nullOpDebug, true));
wunit.commit();
}
// Create a partial index.
auto status =
dbtests::createIndexFromSpec(&_txn,
coll->ns().ns(),
BSON("name"
<< "partial_index"
<< "ns" << coll->ns().ns() << "key" << BSON("a" << 1)
<< "background" << false << "partialFilterExpression"
<< BSON("a" << BSON("$gt" << 1))));
ASSERT_OK(status);
ASSERT_TRUE(checkValid());
RecordStore* rs = coll->getRecordStore();
// Update an unindexed document without updating the index.
{
WriteUnitOfWork wunit(&_txn);
auto doc = BSON("_id" << 1);
auto updateStatus = rs->updateRecord(
&_txn, id1, doc.objdata(), doc.objsize(), /*enforceQuota*/ false, NULL);
ASSERT_OK(updateStatus);
wunit.commit();
}
ASSERT_TRUE(checkValid());
}
};
class ValidateCompoundIndex : public ValidateBase {
public:
ValidateCompoundIndex() : ValidateBase(true) {}
void run() {
// Create a new collection, insert five records and check it's valid.
Database* db = _ctx.db();
OpDebug* const nullOpDebug = nullptr;
Collection* coll;
RecordId id1;
{
WriteUnitOfWork wunit(&_txn);
ASSERT_OK(db->dropCollection(&_txn, _ns));
coll = db->createCollection(&_txn, _ns);
ASSERT_OK(coll->insertDocument(
&_txn, BSON("_id" << 1 << "a" << 1 << "b" << 4), nullOpDebug, true));
id1 = coll->getCursor(&_txn)->next()->id;
ASSERT_OK(coll->insertDocument(
&_txn, BSON("_id" << 2 << "a" << 2 << "b" << 5), nullOpDebug, true));
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 3 << "a" << 3), nullOpDebug, true));
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 4 << "b" << 6), nullOpDebug, true));
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 5 << "c" << 7), nullOpDebug, true));
wunit.commit();
}
// Create two compound indexes, one forward and one reverse, to test
// validate()'s index direction parsing.
auto status = dbtests::createIndexFromSpec(&_txn,
coll->ns().ns(),
BSON("name"
<< "compound_index_1"
<< "ns" << coll->ns().ns() << "key"
<< BSON("a" << 1 << "b" << -1)
<< "background" << false));
ASSERT_OK(status);
status = dbtests::createIndexFromSpec(&_txn,
coll->ns().ns(),
BSON("name"
<< "compound_index_2"
<< "ns" << coll->ns().ns() << "key"
<< BSON("a" << -1 << "b" << 1) << "background"
<< false));
ASSERT_OK(status);
ASSERT_TRUE(checkValid());
RecordStore* rs = coll->getRecordStore();
// Update a document's indexed field without updating the index.
{
WriteUnitOfWork wunit(&_txn);
auto doc = BSON("_id" << 1 << "a" << 1 << "b" << 3);
auto updateStatus = rs->updateRecord(
&_txn, id1, doc.objdata(), doc.objsize(), /*enforceQuota*/ false, NULL);
ASSERT_OK(updateStatus);
wunit.commit();
}
ASSERT_FALSE(checkValid());
}
};
class ValidateIndexEntry : public ValidateBase {
public:
ValidateIndexEntry() : ValidateBase(true) {}
void run() {
// Create a new collection, insert three records and check it's valid.
Database* db = _ctx.db();
OpDebug* const nullOpDebug = nullptr;
Collection* coll;
RecordId id1;
{
WriteUnitOfWork wunit(&_txn);
ASSERT_OK(db->dropCollection(&_txn, _ns));
coll = db->createCollection(&_txn, _ns);
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 1 << "a" << 1), nullOpDebug, true));
id1 = coll->getCursor(&_txn)->next()->id;
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 2 << "a" << 2), nullOpDebug, true));
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 3 << "b" << 1), nullOpDebug, true));
wunit.commit();
}
const std::string indexName = "bad_index";
auto status = dbtests::createIndexFromSpec(
&_txn,
coll->ns().ns(),
BSON("name" << indexName << "ns" << coll->ns().ns() << "key" << BSON("a" << 1)
<< "background" << false));
ASSERT_OK(status);
ASSERT_TRUE(checkValid());
// Replace a correct index entry with a bad one and check it's invalid.
IndexCatalog* indexCatalog = coll->getIndexCatalog();
IndexDescriptor* descriptor = indexCatalog->findIndexByName(&_txn, indexName);
IndexAccessMethod* iam = indexCatalog->getIndex(descriptor);
{
WriteUnitOfWork wunit(&_txn);
int64_t numDeleted;
int64_t numInserted;
const BSONObj actualKey = BSON("a" << 1);
const BSONObj badKey = BSON("a" << -1);
InsertDeleteOptions options;
options.dupsAllowed = true;
options.logIfError = true;
auto removeStatus = iam->remove(&_txn, actualKey, id1, options, &numDeleted);
auto insertStatus = iam->insert(&_txn, badKey, id1, options, &numInserted);
ASSERT_EQUALS(numDeleted, 1);
ASSERT_EQUALS(numInserted, 1);
ASSERT_OK(removeStatus);
ASSERT_OK(insertStatus);
wunit.commit();
}
ASSERT_FALSE(checkValid());
}
};
class ValidateIndexOrdering : public ValidateBase {
public:
ValidateIndexOrdering() : ValidateBase(true) {}
void run() {
// Create a new collection, insert three records and check it's valid.
Database* db = _ctx.db();
OpDebug* const nullOpDebug = nullptr;
Collection* coll;
RecordId id1;
{
WriteUnitOfWork wunit(&_txn);
ASSERT_OK(db->dropCollection(&_txn, _ns));
coll = db->createCollection(&_txn, _ns);
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 1 << "a" << 1), nullOpDebug, true));
id1 = coll->getCursor(&_txn)->next()->id;
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 2 << "a" << 2), nullOpDebug, true));
ASSERT_OK(coll->insertDocument(&_txn, BSON("_id" << 3 << "b" << 1), nullOpDebug, true));
wunit.commit();
}
const std::string indexName = "bad_index";
auto status = dbtests::createIndexFromSpec(
&_txn,
coll->ns().ns(),
BSON("name" << indexName << "ns" << coll->ns().ns() << "key" << BSON("a" << 1)
<< "background" << false));
ASSERT_OK(status);
ASSERT_TRUE(checkValid());
// Change the IndexDescriptor's keyPattern to descending so the index ordering
// appears wrong.
IndexCatalog* indexCatalog = coll->getIndexCatalog();
IndexDescriptor* descriptor = indexCatalog->findIndexByName(&_txn, indexName);
descriptor->setKeyPatternForTest(BSON("a" << -1));
ASSERT_FALSE(checkValid());
}
};
class ValidateTests : public Suite {
public:
ValidateTests() : Suite("validate_tests") {}
void setupTests() {
// Add tests for both full validate and non-full validate.
add<ValidateIdIndexCount<true>>();
add<ValidateIdIndexCount<false>>();
add<ValidateSecondaryIndexCount<true>>();
add<ValidateSecondaryIndexCount<false>>();
// These tests are only needed for full validate.
add<ValidateIdIndex>();
add<ValidateSecondaryIndex>();
add<ValidateMultiKeyIndex>();
add<ValidateSparseIndex>();
add<ValidateCompoundIndex>();
add<ValidatePartialIndex>();
// Tests for index validation.
add<ValidateIndexEntry>();
add<ValidateIndexOrdering>();
}
} validateTests;
} // namespace ValidateTests
|