diff options
author | Reo Kimura <reo.kimura@mongodb.com> | 2021-09-24 17:31:16 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-09-24 18:23:16 +0000 |
commit | 2eff178ab31e80a4842b9b2c8d967540c27c3fbc (patch) | |
tree | 7d632ec11d98507995e5bf122c7fd465b176b330 /jstests/noPassthroughWithMongod | |
parent | 4c34d58b373a202bcdc7649d28943ba5b4ba4382 (diff) | |
download | mongo-2eff178ab31e80a4842b9b2c8d967540c27c3fbc.tar.gz |
SERVER-58170 Prohibit dropping system.views if timeseries collections are present
Diffstat (limited to 'jstests/noPassthroughWithMongod')
3 files changed, 134 insertions, 0 deletions
diff --git a/jstests/noPassthroughWithMongod/timeseries_list_collections_invalid_view.js b/jstests/noPassthroughWithMongod/timeseries_list_collections_invalid_view.js new file mode 100644 index 00000000000..08e2159cbe1 --- /dev/null +++ b/jstests/noPassthroughWithMongod/timeseries_list_collections_invalid_view.js @@ -0,0 +1,56 @@ +/** + * Tests the behavior of listCollections in the presence of both a time-series collection and an + * invalid view definition. + * + * @tags: [ + * assumes_against_mongod_not_mongos, + * does_not_support_transactions, + * requires_getmore, + * ] + */ +(function() { +'use strict'; + +const testDB = db.getSiblingDB(jsTestName()); +assert.commandWorked(testDB.dropDatabase()); + +const timeFieldName = 'time'; +const coll = testDB.getCollection('t'); + +assert.commandWorked( + testDB.createCollection(coll.getName(), {timeseries: {timeField: timeFieldName}})); +assert.commandWorked(testDB.adminCommand({ + applyOps: [ + {op: 'i', ns: testDB.getName() + '.system.views', o: {_id: 'invalid', pipeline: 'invalid'}} + ] +})); + +assert.commandFailedWithCode(testDB.runCommand({listCollections: 1}), + ErrorCodes.InvalidViewDefinition); +assert.commandFailedWithCode(testDB.runCommand({listCollections: 1, filter: {type: 'timeseries'}}), + ErrorCodes.InvalidViewDefinition); +assert.commandFailedWithCode( + testDB.runCommand({listCollections: 1, filter: {name: coll.getName()}}), + ErrorCodes.InvalidViewDefinition); + +// TODO (SERVER-25493): Change filter to {type: 'collection'}. +const collections = + assert + .commandWorked(testDB.runCommand( + {listCollections: 1, filter: {$or: [{type: 'collection'}, {type: {$exists: false}}]}})) + .cursor.firstBatch; +jsTestLog('Checking listCollections result: ' + tojson(collections)); +assert.eq(collections.length, 2); +assert(collections.find(entry => entry.name === 'system.views')); +assert(collections.find(entry => entry.name === 'system.buckets.' + coll.getName())); + +// Users are prohibited from dropping system.views when there are time-series collections present. +// However, this restriction isn't in place on earlier versions and its possible for users to +// upgrade to this version with a dropped system.views collection while having time-series +// collections present. This allows us to continue to test the behaviour of this scenario. +assert.commandWorked( + testDB.adminCommand({configureFailPoint: "allowSystemViewsDrop", mode: "alwaysOn"})); +assert(testDB.system.views.drop()); +assert.commandWorked( + testDB.adminCommand({configureFailPoint: "allowSystemViewsDrop", mode: "off"})); +})(); diff --git a/jstests/noPassthroughWithMongod/timeseries_list_collections_missing_view.js b/jstests/noPassthroughWithMongod/timeseries_list_collections_missing_view.js new file mode 100644 index 00000000000..3e1f865918e --- /dev/null +++ b/jstests/noPassthroughWithMongod/timeseries_list_collections_missing_view.js @@ -0,0 +1,36 @@ +/** + * Tests that listCollections shows the time-series buckets collection, but not the view, if the + * time-series view is missing. + * + * @tags: [ + * does_not_support_transactions, + * requires_getmore, + * ] + */ +(function() { +'use strict'; + +const testDB = db.getSiblingDB(jsTestName()); +assert.commandWorked(testDB.dropDatabase()); + +const timeFieldName = 'time'; +const coll = testDB.getCollection('t'); + +assert.commandWorked( + testDB.createCollection(coll.getName(), {timeseries: {timeField: timeFieldName}})); + +// Users are prohibited from dropping system.views when there are time-series collections present. +// However, this restriction isn't in place on earlier versions and its possible for users to +// upgrade to this version with a dropped system.views collection while having time-series +// collections present. This allows us to continue to test the behaviour of this scenario. +assert.commandWorked( + testDB.adminCommand({configureFailPoint: "allowSystemViewsDrop", mode: "alwaysOn"})); +assert(testDB.system.views.drop()); +assert.commandWorked( + testDB.adminCommand({configureFailPoint: "allowSystemViewsDrop", mode: "off"})); + +const collections = assert.commandWorked(testDB.runCommand({listCollections: 1})).cursor.firstBatch; +jsTestLog('Checking listCollections result: ' + tojson(collections)); +assert.eq(collections.length, 1); +assert(collections.find(entry => entry.name === 'system.buckets.' + coll.getName())); +})(); diff --git a/jstests/noPassthroughWithMongod/timeseries_system_views_drop.js b/jstests/noPassthroughWithMongod/timeseries_system_views_drop.js new file mode 100644 index 00000000000..8adf9ad391c --- /dev/null +++ b/jstests/noPassthroughWithMongod/timeseries_system_views_drop.js @@ -0,0 +1,42 @@ +/** + * Tests that the system.views collection cannot be dropped if time-series collections are present. + * + * @tags: [ + * assumes_no_implicit_collection_creation_after_drop, + * does_not_support_stepdowns, + * does_not_support_transactions, + * requires_getmore, + * ] + */ +(function() { +"use strict"; + +load("jstests/core/timeseries/libs/timeseries.js"); + +const testDB = db.getSiblingDB("timeseries_system_views_drop"); + +TimeseriesTest.run((insert) => { + const tsColl = testDB.timeseries_system_views_drop; + const timeFieldName = 'time'; + + tsColl.drop(); + assert.commandWorked( + testDB.createCollection(tsColl.getName(), {timeseries: {timeField: timeFieldName}})); + + assert.eq(testDB.system.views.find().toArray().length, 1); + assert.throws(() => { + testDB.system.views.drop(); + }); + + assert(tsColl.drop()); + assert.eq(testDB.system.views.find().toArray().length, 0); + + const coll = testDB.my_coll; + + coll.drop(); + assert.commandWorked(testDB.createCollection(coll.getName())); + assert.commandWorked(testDB.createView("myView", coll.getName(), [])); + + assert(testDB.system.views.drop()); +}); +})(); |