diff options
| author | Robert Guo <robert.guo@10gen.com> | 2016-10-06 16:34:21 -0400 |
|---|---|---|
| committer | Robert Guo <robert.guo@10gen.com> | 2016-10-13 18:07:24 -0400 |
| commit | 32bd5fcc91071b45173d7fbbdb541f562c767f5f (patch) | |
| tree | e544d37cef8179d205109fff07dd7cb09316197a | |
| parent | 3e13f3c3a984b31b7977e08a5da6c4e91918d5d4 (diff) | |
| download | mongo-32bd5fcc91071b45173d7fbbdb541f562c767f5f.tar.gz | |
SERVER-26503 fix incorrect uses of assert.throws()
| -rw-r--r-- | jstests/aggregation/bugs/server6556.js | 4 | ||||
| -rw-r--r-- | jstests/core/fts_mix.js | 26 | ||||
| -rw-r--r-- | jstests/core/fts_partition1.js | 14 | ||||
| -rw-r--r-- | jstests/core/indexes_on_indexes.js | 8 | ||||
| -rw-r--r-- | jstests/core/ord.js | 58 | ||||
| -rw-r--r-- | jstests/core/push_sort.js | 48 | ||||
| -rw-r--r-- | jstests/noPassthroughWithMongod/log_component_helpers.js | 8 | ||||
| -rw-r--r-- | jstests/sharding/authwhere.js | 12 | ||||
| -rw-r--r-- | jstests/ssl/x509_client.js | 40 |
9 files changed, 134 insertions, 84 deletions
diff --git a/jstests/aggregation/bugs/server6556.js b/jstests/aggregation/bugs/server6556.js index 261dc5a35b5..a6d1a0c4837 100644 --- a/jstests/aggregation/bugs/server6556.js +++ b/jstests/aggregation/bugs/server6556.js @@ -20,4 +20,6 @@ assert.eq(c.aggregate({$project: {_id: 0, matches: {$eq: ["as", "$foo"]}}}).toAr assert.eq(c.aggregate({$project: {_id: 0, matches: {$eq: ["as\0de", "$foo"]}}}).toArray(), [{matches: false}]); // should assert on fieldpaths with a null -assert.throws(c.aggregate, {$project: {_id: 0, matches: {$eq: ["as\0df", "$f\0oo"]}}}); +assert.throws(function() { + c.aggregate({$project: {_id: 0, matches: {$eq: ["as\0df", "$f\0oo"]}}}); +}); diff --git a/jstests/core/fts_mix.js b/jstests/core/fts_mix.js index 5142497fb41..f0098639e87 100644 --- a/jstests/core/fts_mix.js +++ b/jstests/core/fts_mix.js @@ -120,9 +120,31 @@ assert.eq(getIDS(res), getIDS(res2)); // -------------------------------------------- LANGUAGE ------------------------------------------- -assert.throws(tc.find({"$text": {"$search": "member", $language: "spanglish"}})); +assert.throws(function() { + const cursor = tc.find({"$text": {"$search": "member", $language: "spanglish"}}); + if (db.getMongo().readMode() === "legacy") { + // In legacy read mode, calling next() will check if the response to the OP_QUERY message + // has an error. + cursor.next(); + } else { + // In commands read mode, calling hasNext() will check if the find command returned an + // error. We intentionally do not call next() to avoid masking errors caused by the cursor + // exhausting all of its documents. + cursor.hasNext(); + } +}); assert.doesNotThrow(function() { - tc.find({"$text": {"$search": "member", $language: "english"}}); + const cursor = tc.find({"$text": {"$search": "member", $language: "english"}}); + if (db.getMongo().readMode() === "legacy") { + // In legacy read mode, calling next() will check if the response to the OP_QUERY message + // has an error. + cursor.next(); + } else { + // In commands read mode, calling hasNext() will check if the find command returned an + // error. We intentionally do not call next() to avoid masking errors caused by the cursor + // exhausting all of its documents. + cursor.hasNext(); + } }); // -------------------------------------------- LIMIT RESULTS -------------------------------------- diff --git a/jstests/core/fts_partition1.js b/jstests/core/fts_partition1.js index fc32507f430..a2a52a74cdf 100644 --- a/jstests/core/fts_partition1.js +++ b/jstests/core/fts_partition1.js @@ -10,7 +10,19 @@ t.insert({_id: 4, x: 2, y: "bar"}); t.ensureIndex({x: 1, y: "text"}); -assert.throws(t.find({"$text": {"$search": "foo"}})); +assert.throws(function() { + const cursor = t.find({"$text": {"$search": "foo"}}); + if (db.getMongo().readMode() === "legacy") { + // In legacy read mode, calling next() will check if the response to the OP_QUERY message + // has an error. + cursor.next(); + } else { + // In commands read mode, calling hasNext() will check if the find command returned an + // error. We intentionally do not call next() to avoid masking errors caused by the cursor + // exhausting all of its documents. + cursor.hasNext(); + } +}); assert.eq([1], queryIDS(t, "foo", {x: 1})); diff --git a/jstests/core/indexes_on_indexes.js b/jstests/core/indexes_on_indexes.js index 3ebbf1d2f9c..bcf6b860c7c 100644 --- a/jstests/core/indexes_on_indexes.js +++ b/jstests/core/indexes_on_indexes.js @@ -13,13 +13,15 @@ assert.eq(t.system.indexes.getIndexes().length, 0); print("trying via createIndex"); - assert.throws(t.system.indexes.createIndex({_id: 1})); + assert.commandFailed(t.system.indexes.createIndex({_id: 1})); printjson(t.system.indexes.getIndexes()); assert.eq(t.system.indexes.getIndexes().length, 0); print("trying via direct insertion"); - assert.throws(t.system.indexes.insert( - {v: 1, key: {_id: 1}, ns: "indexes_on_indexes.system.indexes", name: "wontwork"})); + assert.writeErrorWithCode( + t.system.indexes.insert( + {v: 1, key: {_id: 1}, ns: "indexes_on_indexes.system.indexes", name: "wontwork"}), + ErrorCodes.CannotCreateIndex); printjson(t.system.indexes.getIndexes()); assert.eq(t.system.indexes.getIndexes().length, 0); }()); diff --git a/jstests/core/ord.js b/jstests/core/ord.js index 46607b539e8..b398c225c6b 100644 --- a/jstests/core/ord.js +++ b/jstests/core/ord.js @@ -5,30 +5,34 @@ // the $or implementation may need minor adjustments (memory ownership) if this // behavior is changed. -t = db.jstests_ord; -t.drop(); - -t.ensureIndex({a: 1}); -t.ensureIndex({b: 1}); - -for (i = 0; i < 80; ++i) { - t.save({a: 1}); -} - -for (i = 0; i < 100; ++i) { - t.save({b: 1}); -} - -c = t.find({$or: [{a: 1}, {b: 1}]}).batchSize(100); -for (i = 0; i < 90; ++i) { - c.next(); -} -// At this point, our initial query has ended and there is a client cursor waiting -// to read additional documents from index {b:1}. Deduping is performed against -// the index key {a:1}. - -t.dropIndex({a: 1}); - -// Dropping an index kills all cursors on the indexed namespace, not just those -// cursors using the dropped index. -assert.throws(c.next()); +(function() { + const t = db.jstests_ord; + t.drop(); + + t.ensureIndex({a: 1}); + t.ensureIndex({b: 1}); + + for (i = 0; i < 80; ++i) { + t.save({a: 1}); + } + + for (i = 0; i < 100; ++i) { + t.save({b: 1}); + } + + const c = t.find({$or: [{a: 1}, {b: 1}]}).batchSize(100); + for (i = 0; i < 100; ++i) { + c.next(); + } + // At this point, our initial query has ended and there is a client cursor waiting + // to read additional documents from index {b:1}. Deduping is performed against + // the index key {a:1}. + + t.dropIndex({a: 1}); + + // Dropping an index kills all cursors on the indexed namespace, not just those + // cursors using the dropped index. + assert.throws(function() { + c.next(); + }); +})(); diff --git a/jstests/core/push_sort.js b/jstests/core/push_sort.js index b9676f6c963..2a518020248 100644 --- a/jstests/core/push_sort.js +++ b/jstests/core/push_sort.js @@ -56,46 +56,56 @@ assert.eq([{a: {b: 2}}, {a: {b: 3}}], t.findOne({_id: 7}).x); var doc8 = {_id: 8, x: [{a: 1}, {a: 2}]}; t.save(doc8); var res = t.update({_id: 8}, {$push: {x: {$sort: {a: -1}}}}); -assert.writeError(res); +assert.writeErrorWithCode(res, ErrorCodes.DollarPrefixedFieldName); assert.docEq(t.findOne({_id: 8}), doc8); // ensure doc was not changed t.save({_id: 100, x: [{a: 1}]}); -// For now, elements of the $each vector need to be objects. In here, '2' is an invalide $each. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [2], $slice: -2, $sort: {a: 1}}}})); +// Elements of the $each vector can be integers. In here, '2' is a valid $each. +assert.writeOK(t.update({_id: 100}, {$push: {x: {$each: [2], $slice: -2, $sort: {a: 1}}}})); -// For the same reason as above, '1' is an invalid $each element. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}, 1], $slice: -2, $sort: {a: 1}}}})); +// For the same reason as above, '1' is an valid $each element. +assert.writeOK(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}, 1], $slice: -2, $sort: {a: 1}}}})); // The sort key pattern cannot be empty. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {}}}})); +assert.writeErrorWithCode( + t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {}}}}), + ErrorCodes.BadValue); -// For now, we do not support positive $slice's (ie, trimming from the array's front). -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: 2, $sort: {a: 1}}}})); +// Support positive $slice's (ie, trimming from the array's front). +assert.writeOK(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: 2, $sort: {a: 1}}}})); // A $slice cannot be a fractional value. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2.1, $sort: {a: 1}}}})); +assert.writeErrorWithCode( + t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2.1, $sort: {a: 1}}}}), + ErrorCodes.BadValue); // The sort key pattern's value must be either 1 or -1. In here, {a:-2} is an invalid value. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {a: -2}}}})); +assert.writeErrorWithCode( + t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {a: -2}}}}), + ErrorCodes.BadValue); -// For now, we are not supporting sorting of basic elements (non-object, non-arrays). In here, -// the $sort clause would need to have a key pattern value rather than 1. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: 1}}})); +// Support sorting array alements that are not documents. +assert.writeOK(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: 1}}})); // The key pattern 'a.' is an invalid value for $sort. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {'a.': 1}}}})); +assert.writeErrorWithCode( + t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {'a.': 1}}}}), + ErrorCodes.BadValue); // An empty key pattern is not a valid $sort value. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {'': 1}}}})); +assert.writeErrorWithCode( + t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {'': 1}}}}), + ErrorCodes.BadValue); // If a $slice is used, the only other $sort clause that's accepted is $sort. In here, $xxx // is not a valid clause. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $xxx: {s: 1}}}})); +assert.writeErrorWithCode( + t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $xxx: {s: 1}}}}), + ErrorCodes.BadValue); t.remove({}); -// Ensure that existing values are validated in the array as objects during a $sort with $each, -// not only the elements in the $each array. +// Existing values are validated in the array do not have to be objects during a $sort with $each. t.save({_id: 100, x: [1, "foo"]}); -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {a: 1}}}})); +assert.writeOK(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {a: 1}}}})); diff --git a/jstests/noPassthroughWithMongod/log_component_helpers.js b/jstests/noPassthroughWithMongod/log_component_helpers.js index dfe6523ac35..405b2bdaca3 100644 --- a/jstests/noPassthroughWithMongod/log_component_helpers.js +++ b/jstests/noPassthroughWithMongod/log_component_helpers.js @@ -32,8 +32,12 @@ assert.eq(mongo.getLogComponents().storage.journal.verbosity, 1); // setLogLevel - invalid argument - assert.throws(mongo.setLogLevel, [2, 24]); - assert.throws(db.setLogLevel, [2, ["array", "not.allowed"]]); + assert.throws(function() { + mongo.setLogLevel(2, 24); + }); + assert.throws(function() { + db.setLogLevel(2, ["array", "not.allowed"]); + }); // Restore originalSettings assert.commandWorked( diff --git a/jstests/sharding/authwhere.js b/jstests/sharding/authwhere.js index 7443a91005a..a47dc5fb601 100644 --- a/jstests/sharding/authwhere.js +++ b/jstests/sharding/authwhere.js @@ -62,13 +62,19 @@ // Sanity check. test1Reader can count (read) test1, but not test2. assert.eq(test1DB.foo.count(), 1); - assert.throws(test2DB.foo.count); + assert.throws(function() { + test2DB.foo.count(); + }); // Cannot examine second database from a where clause. - assert.throws(test1DB.foo.count, ["db.getSiblingDB('test2').foo.count() == 1"]); + assert.throws(function() { + test1DB.foo.count("db.getSiblingDB('test2').foo.count() == 1"); + }); // Cannot write test1 via tricky where clause. - assert.throws(test1DB.foo.count, ["db.foo.insert({b: 1})"]); + assert.throws(function() { + test1DB.foo.count("db.foo.insert({b: 1})"); + }); assert.eq(test1DB.foo.count(), 1); } finally { adminDB.logout(); diff --git a/jstests/ssl/x509_client.js b/jstests/ssl/x509_client.js index 5bcc83edef9..5842438b98a 100644 --- a/jstests/ssl/x509_client.js +++ b/jstests/ssl/x509_client.js @@ -29,22 +29,16 @@ function authAndTest(mongo) { test = mongo.getDB("test"); // It should be impossible to create users with the same name as the server's subject - assert.throws( - function() { - external.createUser( - {user: SERVER_USER, roles: [{'role': 'userAdminAnyDatabase', 'db': 'admin'}]}); - }, - {}, - "Created user with same name as the server's x.509 subject"); + assert.throws(function() { + external.createUser( + {user: SERVER_USER, roles: [{'role': 'userAdminAnyDatabase', 'db': 'admin'}]}); + }, [], "Created user with same name as the server's x.509 subject"); // It should be impossible to create users with names recognized as cluster members - assert.throws( - function() { - external.createUser( - {user: INTERNAL_USER, roles: [{'role': 'userAdminAnyDatabase', 'db': 'admin'}]}); - }, - {}, - "Created user which would be recognized as a cluster member"); + assert.throws(function() { + external.createUser( + {user: INTERNAL_USER, roles: [{'role': 'userAdminAnyDatabase', 'db': 'admin'}]}); + }, [], "Created user which would be recognized as a cluster member"); // Add user using localhost exception external.createUser({ @@ -62,12 +56,9 @@ function authAndTest(mongo) { }); // Localhost exception should not be in place anymore - assert.throws( - function() { - test.foo.findOne(); - }, - {}, - "read without login"); + assert.throws(function() { + test.foo.findOne(); + }, [], "read without login"); assert(!external.auth({user: INVALID_CLIENT_USER, mechanism: 'MONGODB-X509'}), "authentication with invalid user should fail"); @@ -86,12 +77,9 @@ function authAndTest(mongo) { test.foo.findOne(); external.logout(); - assert.throws( - function() { - test.foo.findOne(); - }, - {}, - "read after logout"); + assert.throws(function() { + test.foo.findOne(); + }, [], "read after logout"); } print("1. Testing x.509 auth to mongod"); |
