diff options
Diffstat (limited to 'jstests/core')
| -rw-r--r-- | jstests/core/apply_ops_invalid_index_spec.js | 107 | ||||
| -rw-r--r-- | jstests/core/collation_with_reverse_index.js | 12 | ||||
| -rw-r--r-- | jstests/core/set_param1.js | 66 | ||||
| -rw-r--r-- | jstests/core/update_numeric_field_name.js | 29 |
4 files changed, 196 insertions, 18 deletions
diff --git a/jstests/core/apply_ops_invalid_index_spec.js b/jstests/core/apply_ops_invalid_index_spec.js new file mode 100644 index 00000000000..2ca88081fa7 --- /dev/null +++ b/jstests/core/apply_ops_invalid_index_spec.js @@ -0,0 +1,107 @@ +/** + * Tests how applyOps handles index specs with unknown fields. + * + * We subject index specs with version 2 or later to stricter validation than version 1 index specs. + * When given an index spec with an unrecognized field, applyOps will reject v:2 indexes with an + * InvalidIndexSpecificationOption error while v:1 indexes are accepted as-is. + * + * @tags: [ + * requires_non_retryable_commands, + * requires_fastcount, + * + * # applyOps uses the oplog which requires replication support. + * requires_replication, + * ] + */ + +(function() { + 'use strict'; + + const t = db.apply_ops_invalid_index_spec; + t.drop(); + + const collNs = t.getFullName(); + const cmdNs = db.getName() + '.$cmd'; + const systemIndexesNs = db.getCollection('system.indexes').getFullName(); + + assert.commandWorked(db.createCollection(t.getName())); + assert.writeOK(t.save({_id: 100, a: 100})); + + // Tests that db.collection.createIndex() fails when given an index spec containing an unknown + // field. + assert.commandFailedWithCode(t.createIndex({a: 1}, {v: 2, name: 'a_1_base_v2', unknown: 1}), + ErrorCodes.InvalidIndexSpecificationOption); + assert.commandFailedWithCode(t.createIndex({a: 1}, {v: 1, name: 'a_1_base_v1', unknown: 1}), + ErrorCodes.InvalidIndexSpecificationOption); + + // Inserting a v:2 index directly into system.indexes with an unknown field in the index + // spec should return an error. + assert.commandFailedWithCode(db.adminCommand({ + applyOps: [{ + op: 'i', + ns: systemIndexesNs, + o: {v: 2, key: {a: 1}, name: 'a_1_system_v2', ns: collNs, unknown: 1}, + }], + }), + ErrorCodes.InvalidIndexSpecificationOption); + + // Inserting a v:1 index directly into system.indexes with an unknown field in the index spec + // should ignore the unrecognized field and create the index. + assert.commandWorked(db.adminCommand({ + applyOps: [{ + op: 'i', + ns: systemIndexesNs, + o: {v: 1, key: {a: 1}, name: 'a_1_system_v1', ns: collNs, unknown: 1}, + }], + })); + + // + // Background indexes should be subject to the same level of validation as foreground indexes. + // + + // Inserting a background index directly into system.indexes with a bad index key pattern should + // return an error. + assert.commandFailedWithCode(db.adminCommand({ + applyOps: [{ + op: 'i', + ns: systemIndexesNs, + o: {key: {b: 'sideways'}, name: 'b_1_bg_system_v2', ns: collNs, background: true}, + }], + }), + ErrorCodes.CannotCreateIndex); + + // Inserting a v:2 background index directly into system.indexes with an unknown field in the + // index spec should return an error. + assert.commandFailedWithCode(db.adminCommand({ + applyOps: [{ + op: 'i', + ns: systemIndexesNs, + o: { + v: 2, + key: {b: 1}, + name: 'b_1_bg_system_v2', + ns: collNs, + background: true, + unknown: true, + }, + }], + }), + ErrorCodes.InvalidIndexSpecificationOption); + + // Inserting a background v:1 index directly into system.indexes with an unknown field in the + // index spec should work. + assert.commandWorked(db.adminCommand({ + applyOps: [{ + op: 'i', + ns: systemIndexesNs, + o: { + v: 1, + key: {b: 1}, + name: 'b_1_bg_system_v1', + ns: collNs, + background: true, + unknown: true, + }, + }], + })); +})(); diff --git a/jstests/core/collation_with_reverse_index.js b/jstests/core/collation_with_reverse_index.js new file mode 100644 index 00000000000..af246187348 --- /dev/null +++ b/jstests/core/collation_with_reverse_index.js @@ -0,0 +1,12 @@ +// Regression test for SERVER-34846. +(function() { + const coll = db.collation_with_reverse_index; + coll.drop(); + + coll.insertOne({int: 1, text: "hello world"}); + coll.createIndex({int: -1, text: -1}, {collation: {locale: "en", strength: 1}}); + const res = coll.find({int: 1}, {_id: 0, int: 1, text: 1}).toArray(); + + assert.eq(res.length, 1); + assert.eq(res[0].text, "hello world"); +})(); diff --git a/jstests/core/set_param1.js b/jstests/core/set_param1.js index 51b13ae87cc..4faa312808b 100644 --- a/jstests/core/set_param1.js +++ b/jstests/core/set_param1.js @@ -115,29 +115,59 @@ assert.commandFailed( assert.commandWorked( db.adminCommand({"setParameter": 1, logComponentVerbosity: old.logComponentVerbosity})); -// -// oplogFetcherMaxFetcherRestarts -// - var isMongos = (db.isMaster().msg === 'isdbgrid'); if (!isMongos) { - var origRestarts = - assert.commandWorked(db.adminCommand({getParameter: 1, oplogFetcherMaxFetcherRestarts: 1})) - .oplogFetcherMaxFetcherRestarts; - assert.gte( - origRestarts, 0, 'default value of oplogFetcherMaxFetcherRestarts cannot be negative'); + // + // oplogFetcherSteadyStateMaxFetcherRestarts + // + var origRestarts = assert + .commandWorked(db.adminCommand( + {getParameter: 1, oplogFetcherSteadyStateMaxFetcherRestarts: 1})) + .oplogFetcherSteadyStateMaxFetcherRestarts; + assert.gte(origRestarts, + 0, + 'default value of oplogFetcherSteadyStateMaxFetcherRestarts cannot be negative'); assert.commandFailedWithCode( - db.adminCommand({setParameter: 1, oplogFetcherMaxFetcherRestarts: -1}), + db.adminCommand({setParameter: 1, oplogFetcherSteadyStateMaxFetcherRestarts: -1}), ErrorCodes.BadValue, - 'server should reject negative values for oplogFetcherMaxFetcherRestarts'); - assert.commandWorked(db.adminCommand({setParameter: 1, oplogFetcherMaxFetcherRestarts: 0})); + 'server should reject negative values for oplogFetcherSteadyStateMaxFetcherRestarts'); assert.commandWorked( - db.adminCommand({setParameter: 1, oplogFetcherMaxFetcherRestarts: origRestarts + 20})); - assert.eq( - origRestarts + 20, - assert.commandWorked(db.adminCommand({getParameter: 1, oplogFetcherMaxFetcherRestarts: 1})) - .oplogFetcherMaxFetcherRestarts); + db.adminCommand({setParameter: 1, oplogFetcherSteadyStateMaxFetcherRestarts: 0})); + assert.commandWorked(db.adminCommand( + {setParameter: 1, oplogFetcherSteadyStateMaxFetcherRestarts: origRestarts + 20})); + assert.eq(origRestarts + 20, + assert + .commandWorked(db.adminCommand( + {getParameter: 1, oplogFetcherSteadyStateMaxFetcherRestarts: 1})) + .oplogFetcherSteadyStateMaxFetcherRestarts); // Restore original value. + assert.commandWorked(db.adminCommand( + {setParameter: 1, oplogFetcherSteadyStateMaxFetcherRestarts: origRestarts})); + + // + // oplogFetcherInitialSyncStateMaxFetcherRestarts + // + origRestarts = assert + .commandWorked(db.adminCommand( + {getParameter: 1, oplogFetcherInitialSyncMaxFetcherRestarts: 1})) + .oplogFetcherInitialSyncMaxFetcherRestarts; + assert.gte(origRestarts, + 0, + 'default value of oplogFetcherInitialSyncMaxFetcherRestarts cannot be negative'); + assert.commandFailedWithCode( + db.adminCommand({setParameter: 1, oplogFetcherInitialSyncMaxFetcherRestarts: -1}), + ErrorCodes.BadValue, + 'server should reject negative values for oplogFetcherInitialSyncMaxFetcherRestarts'); assert.commandWorked( - db.adminCommand({setParameter: 1, oplogFetcherMaxFetcherRestarts: origRestarts})); + db.adminCommand({setParameter: 1, oplogFetcherInitialSyncMaxFetcherRestarts: 0})); + assert.commandWorked(db.adminCommand( + {setParameter: 1, oplogFetcherInitialSyncMaxFetcherRestarts: origRestarts + 20})); + assert.eq(origRestarts + 20, + assert + .commandWorked(db.adminCommand( + {getParameter: 1, oplogFetcherInitialSyncMaxFetcherRestarts: 1})) + .oplogFetcherInitialSyncMaxFetcherRestarts); + // Restore original value. + assert.commandWorked(db.adminCommand( + {setParameter: 1, oplogFetcherInitialSyncMaxFetcherRestarts: origRestarts})); } diff --git a/jstests/core/update_numeric_field_name.js b/jstests/core/update_numeric_field_name.js new file mode 100644 index 00000000000..4adb7eee8be --- /dev/null +++ b/jstests/core/update_numeric_field_name.js @@ -0,0 +1,29 @@ +// Test that update operations correctly fail if they violate the "ambiguous field name in array" +// constraint for indexes. This is designed to reproduce SERVER-37058. +(function() { + "use strict"; + + const coll = db.update_numeric_field_name; + coll.drop(); + + assert.writeOK(coll.insert({_id: 0, 'a': [{}]})); + assert.commandWorked(coll.createIndex({'a.0.c': 1})); + + // Attempt to insert a field name '0'. The first '0' refers to the first element of the array + // 'a'. + assert.writeErrorWithCode(coll.update({_id: 0}, {$set: {'a.0.0': 1}}), 16746); + + // Verify that the indexes were not affected. + let res = assert.commandWorked(coll.validate(true)); + assert(res.valid, tojson(res)); + + assert.writeErrorWithCode(coll.update({_id: 0}, {$set: {'a.0.0.b': 1}}), 16746); + res = assert.commandWorked(coll.validate(true)); + assert(res.valid, tojson(res)); + + // An update which does not violate the ambiguous field name in array constraint should succeed. + assert.writeOK(coll.update({_id: 0}, {$set: {'a.1.b.0.0': 1}})); + + res = assert.commandWorked(coll.validate(true)); + assert(res.valid, tojson(res)); +})(); |
