summaryrefslogtreecommitdiff
path: root/jstests/core/views/invalid_system_views.js
diff options
context:
space:
mode:
authorKyle Suarez <kyle.suarez@mongodb.com>2016-11-21 14:20:58 -0500
committerKyle Suarez <kyle.suarez@mongodb.com>2016-12-09 15:35:55 -0500
commitaf3b0c646cc6953ac28a2b0dccd5ed16c263d424 (patch)
tree6c4d919cde49af0f91767a5a0d5e4169ecd297e9 /jstests/core/views/invalid_system_views.js
parentf30f645e4e6acde092d8e0c826201b25ad42eee7 (diff)
downloadmongo-af3b0c646cc6953ac28a2b0dccd5ed16c263d424.tar.gz
SERVER-26765 move views tests into jsCore
Views tests have been moved to jstests/core/views. YAML configurations for jsCore passthrough suites have been updated to pick up tests in subdirectories of jstests/core. Tasks and variants that previously used the --excludeWithAnyTags resmoke flag now specify task_excluded_tags and variant_excluded_tags, respectively.
Diffstat (limited to 'jstests/core/views/invalid_system_views.js')
-rw-r--r--jstests/core/views/invalid_system_views.js117
1 files changed, 117 insertions, 0 deletions
diff --git a/jstests/core/views/invalid_system_views.js b/jstests/core/views/invalid_system_views.js
new file mode 100644
index 00000000000..14b8b49a2b7
--- /dev/null
+++ b/jstests/core/views/invalid_system_views.js
@@ -0,0 +1,117 @@
+/**
+ * Tests that invalid view definitions in system.views do not impact valid commands on existing
+ * collections.
+ */
+(function() {
+ "use strict";
+
+ function runTest(badViewDefinition) {
+ let viewsDB = db.getSiblingDB("invalid_system_views");
+ assert.commandWorked(viewsDB.dropDatabase());
+
+ // Create a regular collection, then insert an invalid view into system.views.
+ assert.writeOK(viewsDB.collection.insert({x: 1}));
+ assert.commandWorked(viewsDB.runCommand({create: "collection2"}));
+ assert.commandWorked(viewsDB.runCommand({create: "collection3"}));
+ assert.commandWorked(viewsDB.collection.createIndex({x: 1}));
+ assert.writeOK(viewsDB.system.views.insert(badViewDefinition),
+ "failed to insert " + tojson(badViewDefinition));
+
+ // Test that a command involving views properly fails with a views-specific error code.
+ assert.commandFailedWithCode(
+ viewsDB.runCommand({listCollections: 1}),
+ ErrorCodes.InvalidViewDefinition,
+ "listCollections should have failed in the presence of an invalid view");
+
+ // Helper function to create a message to use if an assertion fails.
+ function makeErrorMessage(msg) {
+ return msg +
+ " should work on a valid, existing collection, despite the presence of bad views" +
+ " in system.views";
+ }
+
+ // Commands that run on existing regular collections should not be impacted by the presence
+ // of invalid views.
+ assert.commandWorked(
+ db.adminCommand(
+ {applyOps: [{op: "c", ns: "invalid_system_views.$cmd", o: {drop: "collection3"}}]}),
+ makeErrorMessage("applyOps"));
+
+ assert.writeOK(viewsDB.collection.insert({y: "baz"}), makeErrorMessage("insert"));
+
+ assert.writeOK(viewsDB.collection.update({y: "baz"}, {$set: {y: "qux"}}),
+ makeErrorMessage("update"));
+
+ assert.writeOK(viewsDB.collection.remove({y: "baz"}), makeErrorMessage("remove"));
+
+ assert.commandWorked(
+ viewsDB.runCommand({findAndModify: "collection", query: {x: 1}, update: {x: 2}}),
+ makeErrorMessage("findAndModify with update"));
+
+ assert.commandWorked(
+ viewsDB.runCommand({findAndModify: "collection", query: {x: 2}, remove: true}),
+ makeErrorMessage("findAndModify with remove"));
+
+ const lookup = {
+ $lookup: {from: "collection2", localField: "_id", foreignField: "_id", as: "match"}
+ };
+ assert.commandWorked(viewsDB.runCommand({aggregate: "collection", pipeline: [lookup]}),
+ makeErrorMessage("aggregate with $lookup"));
+
+ const graphLookup = {
+ $graphLookup: {
+ from: "collection2",
+ startWith: "$_id",
+ connectFromField: "_id",
+ connectToField: "_id",
+ as: "match"
+ }
+ };
+ assert.commandWorked(viewsDB.runCommand({aggregate: "collection", pipeline: [graphLookup]}),
+ makeErrorMessage("aggregate with $graphLookup"));
+
+ assert.commandWorked(viewsDB.runCommand({dropIndexes: "collection", index: "x_1"}),
+ makeErrorMessage("dropIndexes"));
+
+ assert.commandWorked(viewsDB.collection.createIndex({x: 1}),
+ makeErrorMessage("createIndexes"));
+
+ assert.commandWorked(viewsDB.collection.reIndex(), makeErrorMessage("reIndex"));
+
+ const storageEngine = jsTest.options().storageEngine;
+ if (storageEngine === "ephemeralForTest" || storageEngine === "inMemory") {
+ print("Not testing compact command on ephemeral storage engine " + storageEngine);
+ } else {
+ assert.commandWorked(viewsDB.runCommand({compact: "collection", force: true}),
+ makeErrorMessage("compact"));
+ }
+
+ assert.commandWorked(
+ viewsDB.runCommand({collMod: "collection", validator: {x: {$type: "string"}}}),
+ makeErrorMessage("collMod"));
+
+ const renameCommand = {
+ renameCollection: "invalid_system_views.collection",
+ to: "invalid_system_views.collection2",
+ dropTarget: true
+ };
+ assert.commandWorked(viewsDB.adminCommand(renameCommand),
+ makeErrorMessage("renameCollection"));
+
+ assert.commandWorked(viewsDB.runCommand({drop: "collection2"}), makeErrorMessage("drop"));
+
+ // Drop the offending view so that the validate hook succeeds.
+ assert.writeOK(viewsDB.system.views.remove(badViewDefinition));
+ }
+
+ let badViews = [
+ {_id: "badViewStringPipeline", pipeline: "bad"},
+ {_id: "badViewEmptyObjectPipeline", pipeline: {}},
+ {_id: "badViewNumericalPipeline", pipeline: 7},
+ {_id: "badViewArrayWithIntegerPipeline", pipeline: [1]},
+ {_id: "badViewArrayWithEmptyObjectPipeline", pipeline: [{}]},
+ {_id: "badViewArrayWithEmptyArrayPipeline", pipeline: [[]]},
+ {_id: 7, pipeline: []},
+ ];
+ badViews.forEach(runTest);
+}());