commit a8017e90c767c2e8c7ca82f95d4cdee41a0705ee Author: Charlie Swanson Date: Wed Sep 11 13:40:18 2019 -0400 WIP towards SERVER-43287 diff --git a/jstests/aggregation/extras/utils.js b/jstests/aggregation/extras/utils.js index 7364ecde54..fd5752df00 100644 --- a/jstests/aggregation/extras/utils.js +++ b/jstests/aggregation/extras/utils.js @@ -357,3 +357,10 @@ function generateCollection({ assert.eq(numDocs, res.nInserted); assert.eq(numDocs, coll.find().itcount()); } + +/** + * Asserts that 'coll' exists. + */ +function collectionExists(coll) { + return Array.contains(coll.getDB().getCollectionNames(), coll.getName()); +} diff --git a/jstests/aggregation/no_output_to_system.js b/jstests/aggregation/no_output_to_system.js new file mode 100644 index 0000000000..3b323ef79e --- /dev/null +++ b/jstests/aggregation/no_output_to_system.js @@ -0,0 +1,18 @@ +// Tests that you cannot use aggregation to output to a system collection. +(function() { +"use strict"; + +load('jstests/aggregation/extras/utils.js'); // For 'assertErrorCode'. + +const input = db.no_output_to_system; +input.drop(); +assert.commandWorked(input.insert({_id: 0})); + +// ensure we cant do dangerous things to system collections +const outputInSystem = db.system.no_output_to_system; +assertErrorCode(input, {$out: outputInSystem.getName()}, 17385); +assert(!collectionExists(outputInSystem)); + +assertErrorCode(input, {$merge: outputInSystem.getName()}, 17385); +assert(!collectionExists(outputInSystem)); +}()); diff --git a/jstests/aggregation/bugs/server3253.js b/jstests/aggregation/sources/out/basic_functionality.js similarity index 79% rename from jstests/aggregation/bugs/server3253.js rename to jstests/aggregation/sources/out/basic_functionality.js index 5df6736e88..e5fa6bfd94 100644 --- a/jstests/aggregation/bugs/server3253.js +++ b/jstests/aggregation/sources/out/basic_functionality.js @@ -1,3 +1,4 @@ +// Basic tests for the $out stage. // Cannot implicitly shard accessed collections because unsupported use of sharded collection // for output collection of aggregation pipeline. // @tags: [ @@ -5,21 +6,19 @@ // assumes_unsharded_collection, // ] -// server-3253 Unsharded support for $out +(function() { +"use strict"; load('jstests/aggregation/extras/utils.js'); -var input = db.server3253_in; -var inputDoesntExist = db.server3253_doesnt_exist; -var output = db.server3253_out; -var cappedOutput = db.server3253_out_capped; +const input = db.server3253_in; +const inputDoesntExist = db.server3253_doesnt_exist; +const output = db.server3253_out; +const cappedOutput = db.server3253_out_capped; input.drop(); inputDoesntExist.drop(); // never created output.drop(); - -function collectionExists(coll) { - return Array.contains(coll.getDB().getCollectionNames(), coll.getName()); -} +cappedOutput.drop(); function getOutputIndexes() { return output.getIndexes().sort(function(a, b) { @@ -53,14 +52,12 @@ function listCollections(name) { return new DBCommandCursor(db, collectionInfosCursor).toArray(); } -input.insert({_id: 1}); -input.insert({_id: 2}); -input.insert({_id: 3}); +assert.commandWorked(input.insert([{_id: 1}, {_id: 2}, {_id: 3}])); -// insert into output so that the index exists and test() does not fail the first time around +// Insert into output so that the index exists and test() does not fail the first time around. output.insert({_id: 1}); -// ensure there are no tmp agg_out collections before we begin +// Ensure there are no tmp agg_out collections before we begin. assert.eq([], listCollections(/tmp\.agg_out/)); // basic test @@ -104,10 +101,6 @@ if (jsTest.options().storageEngine !== "mobile") { // ensure everything works even if input doesn't exist. test(inputDoesntExist, [], []); -// ensure we cant do dangerous things to system collections -var outputInSystem = db.system.server3253_out; -assertErrorCode(input, {$out: outputInSystem.getName()}, 17385); -assert(!collectionExists(outputInSystem)); - // shoudn't leave temp collections laying around assert.eq([], listCollections(/tmp\.agg_out/)); +}());