summaryrefslogtreecommitdiff
path: root/jstests
diff options
context:
space:
mode:
Diffstat (limited to 'jstests')
-rw-r--r--jstests/hooks/run_validate_collections.js16
-rw-r--r--jstests/hooks/validate_collections.js35
2 files changed, 51 insertions, 0 deletions
diff --git a/jstests/hooks/run_validate_collections.js b/jstests/hooks/run_validate_collections.js
new file mode 100644
index 00000000000..1e910b3e6cf
--- /dev/null
+++ b/jstests/hooks/run_validate_collections.js
@@ -0,0 +1,16 @@
+// Runner for validateCollections that runs full validation on all collections when loaded into
+// the mongo shell.
+'use strict';
+
+(function() {
+ assert.eq(typeof db, 'object', 'Invalid `db` object, is the shell connected to a mongod?');
+ load('jstests/hooks/validate_collections.js'); // For validateCollections
+
+ var dbNames = db.getMongo().getDBNames();
+
+ for (var dbName of dbNames) {
+ if (!validateCollections(db.getSiblingDB(dbName), {full: true})) {
+ throw new Error('Collection validation failed');
+ }
+ }
+})(); \ No newline at end of file
diff --git a/jstests/hooks/validate_collections.js b/jstests/hooks/validate_collections.js
new file mode 100644
index 00000000000..5024460f05a
--- /dev/null
+++ b/jstests/hooks/validate_collections.js
@@ -0,0 +1,35 @@
+// Wrapper around the validate command that can be used to validate index key counts.
+'use strict';
+
+function validateCollections(db, obj) {
+ function dumpCollection(coll, limit) {
+ print('Printing indexes in: ' + coll.getFullName());
+ printjson(coll.getIndexes());
+
+ print('Printing the first ' + limit + ' documents in: ' + coll.getFullName());
+ var res = coll.find().limit(limit);
+ while (res.hasNext()) {
+ printjson(res.next());
+ }
+ }
+
+ assert.eq(typeof db, 'object', 'Invalid `db` object, is the shell connected to a mongod?');
+ assert.eq(typeof obj, 'object', 'The `obj` argument must be an object');
+ assert(obj.hasOwnProperty('full'), 'Please specify whether to use full validation');
+
+ var full = obj.full;
+
+ var success = true;
+ var collNames = db.getCollectionNames();
+ for (var collName of collNames) {
+ var coll = db.getCollection(collName);
+ var res = coll.validate(full);
+
+ if (!res.ok || !res.valid) {
+ print('Collection validation failed with response: ' + tojson(res));
+ dumpCollection(coll, 100);
+ success = false;
+ }
+ }
+ return success;
+}