summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Hirschhorn <max.hirschhorn@mongodb.com>2016-06-07 16:01:44 -0400
committerMax Hirschhorn <max.hirschhorn@mongodb.com>2016-06-07 16:01:44 -0400
commit01dfe61a6c03307f0384406bfbde486f8de15425 (patch)
tree907e50e42f26e987af8b7a792d0b628ef2728264
parent032424437d50eceafc5eb7c9b2c24c73da6ff1fc (diff)
downloadmongo-01dfe61a6c03307f0384406bfbde486f8de15425.tar.gz
SERVER-23741 Use MongoRunner.runMongoTool() to run tools from JS tests.
Changes all usages of runMongoProgram(<mongo tool>, ...) to MongoRunner.runMongoTool(<mongo tool>, {...}) to take advantage of the --dialTimeout command line option supported by the mongo tools. Adds support for specifying positional arguments to MongoRunner.runMongoTool() in order to support all invocations of the bsondump and mongofiles tools.
-rw-r--r--jstests/sharding/auth.js20
-rw-r--r--jstests/ssl/ssl_cert_password.js127
-rw-r--r--jstests/tool/dumpauth.js27
-rw-r--r--jstests/tool/dumprestore10.js15
-rw-r--r--jstests/tool/dumprestore3.js25
-rw-r--r--jstests/tool/dumprestore7.js26
-rw-r--r--jstests/tool/dumprestore9.js20
-rw-r--r--jstests/tool/dumpsecondary.js45
-rw-r--r--jstests/tool/gridfs.js9
-rw-r--r--jstests/tool/oplog_all_ops.js6
-rw-r--r--jstests/tool/restorewithauth.js61
-rw-r--r--jstests/tool/stat1.js40
-rw-r--r--jstests/tool/tool1.js53
-rw-r--r--jstests/tool/tool_replset.js39
-rw-r--r--src/mongo/shell/servers.js19
15 files changed, 296 insertions, 236 deletions
diff --git a/jstests/sharding/auth.js b/jstests/sharding/auth.js
index f3fe71a5950..e8521a3b0d8 100644
--- a/jstests/sharding/auth.js
+++ b/jstests/sharding/auth.js
@@ -280,18 +280,14 @@
assert.commandWorked(res);
// Check that dump doesn't get stuck with auth
- var x = runMongoProgram("mongodump",
- "--host",
- s.s.host,
- "-d",
- testUser.db,
- "-u",
- testUser.username,
- "-p",
- testUser.password,
- "--authenticationMechanism",
- "SCRAM-SHA-1");
- print("result: " + x);
+ var exitCode = MongoRunner.runMongoTool("mongodump", {
+ host: s.s.host,
+ db: testUser.db,
+ username: testUser.username,
+ password: testUser.password,
+ authenticationMechanism: "SCRAM-SHA-1",
+ });
+ assert.eq(0, exitCode, "mongodump failed to run with authentication enabled");
// Test read only users
print("starting read only tests");
diff --git a/jstests/ssl/ssl_cert_password.js b/jstests/ssl/ssl_cert_password.js
index 772038f9970..733c304cec7 100644
--- a/jstests/ssl/ssl_cert_password.js
+++ b/jstests/ssl/ssl_cert_password.js
@@ -41,32 +41,26 @@ assert.eq(0, c.count(), "dumprestore_ssl.foo collection is not initially empty")
c.save({a: 22});
assert.eq(1, c.count(), "failed to insert document into dumprestore_ssl.foo collection");
-exit_code = runMongoProgram("mongodump",
- "--out",
- external_scratch_dir,
- "--port",
- md.port,
- "--ssl",
- "--sslPEMKeyFile",
- "jstests/libs/password_protected.pem",
- "--sslPEMKeyPassword",
- "qwerty");
+exit_code = MongoRunner.runMongoTool("mongodump", {
+ out: external_scratch_dir,
+ port: md.port,
+ ssl: "",
+ sslPEMKeyFile: "jstests/libs/password_protected.pem",
+ sslPEMKeyPassword: "qwerty",
+});
assert.eq(exit_code, 0, "Failed to start mongodump with ssl");
c.drop();
assert.eq(0, c.count(), "dumprestore_ssl.foo collection is not empty after drop");
-exit_code = runMongoProgram("mongorestore",
- "--dir",
- external_scratch_dir,
- "--port",
- md.port,
- "--ssl",
- "--sslPEMKeyFile",
- "jstests/libs/password_protected.pem",
- "--sslPEMKeyPassword",
- "qwerty");
+exit_code = MongoRunner.runMongoTool("mongorestore", {
+ dir: external_scratch_dir,
+ port: md.port,
+ ssl: "",
+ sslPEMKeyFile: "jstests/libs/password_protected.pem",
+ sslPEMKeyPassword: "qwerty",
+});
assert.eq(exit_code, 0, "Failed to start mongorestore with ssl");
@@ -85,40 +79,30 @@ assert.eq(1, c.count(), "failed to insert document into exportimport_ssl.foo col
var exportimport_file = "data.json";
-exit_code = runMongoProgram("mongoexport",
- "--out",
- external_scratch_dir + exportimport_file,
- "-d",
- exportimport_ssl_dbname,
- "-c",
- "foo",
- "--port",
- md.port,
- "--ssl",
- "--sslPEMKeyFile",
- "jstests/libs/password_protected.pem",
- "--sslPEMKeyPassword",
- "qwerty");
+exit_code = MongoRunner.runMongoTool("mongoexport", {
+ out: external_scratch_dir + exportimport_file,
+ db: exportimport_ssl_dbname,
+ collection: "foo",
+ port: md.port,
+ ssl: "",
+ sslPEMKeyFile: "jstests/libs/password_protected.pem",
+ sslPEMKeyPassword: "qwerty",
+});
assert.eq(exit_code, 0, "Failed to start mongoexport with ssl");
c.drop();
assert.eq(0, c.count(), "afterdrop", "-d", exportimport_ssl_dbname, "-c", "foo");
-exit_code = runMongoProgram("mongoimport",
- "--file",
- external_scratch_dir + exportimport_file,
- "-d",
- exportimport_ssl_dbname,
- "-c",
- "foo",
- "--port",
- md.port,
- "--ssl",
- "--sslPEMKeyFile",
- "jstests/libs/password_protected.pem",
- "--sslPEMKeyPassword",
- "qwerty");
+exit_code = MongoRunner.runMongoTool("mongoimport", {
+ file: external_scratch_dir + exportimport_file,
+ db: exportimport_ssl_dbname,
+ collection: "foo",
+ port: md.port,
+ ssl: "",
+ sslPEMKeyFile: "jstests/libs/password_protected.pem",
+ sslPEMKeyPassword: "qwerty",
+});
assert.eq(exit_code, 0, "Failed to start mongoimport with ssl");
@@ -135,18 +119,16 @@ mongofiles_db = md.getDB(mongofiles_ssl_dbname);
source_filename = 'jstests/ssl/ssl_cert_password.js';
filename = 'ssl_cert_password.js';
-exit_code = runMongoProgram("mongofiles",
- "-d",
- mongofiles_ssl_dbname,
- "put",
- source_filename,
- "--port",
- md.port,
- "--ssl",
- "--sslPEMKeyFile",
- "jstests/libs/password_protected.pem",
- "--sslPEMKeyPassword",
- "qwerty");
+exit_code = MongoRunner.runMongoTool("mongofiles",
+ {
+ db: mongofiles_ssl_dbname,
+ port: md.port,
+ ssl: "",
+ sslPEMKeyFile: "jstests/libs/password_protected.pem",
+ sslPEMKeyPassword: "qwerty",
+ },
+ "put",
+ source_filename);
assert.eq(exit_code, 0, "Failed to start mongofiles with ssl");
@@ -159,20 +141,17 @@ md5_computed = mongofiles_db.runCommand({filemd5: file_obj._id}).md5;
assert.eq(md5, md5_stored, "md5 incorrect for file");
assert.eq(md5, md5_computed, "md5 computed incorrectly by server");
-exit_code = runMongoProgram("mongofiles",
- "-d",
- mongofiles_ssl_dbname,
- "get",
- source_filename,
- "-l",
- external_scratch_dir + filename,
- "--port",
- md.port,
- "--ssl",
- "--sslPEMKeyFile",
- "jstests/libs/password_protected.pem",
- "--sslPEMKeyPassword",
- "qwerty");
+exit_code = MongoRunner.runMongoTool("mongofiles",
+ {
+ db: mongofiles_ssl_dbname,
+ local: external_scratch_dir + filename,
+ port: md.port,
+ ssl: "",
+ sslPEMKeyFile: "jstests/libs/password_protected.pem",
+ sslPEMKeyPassword: "qwerty",
+ },
+ "get",
+ source_filename);
assert.eq(exit_code, 0, "Failed to start mongofiles with ssl");
diff --git a/jstests/tool/dumpauth.js b/jstests/tool/dumpauth.js
index 7be119a9f54..795e63f4378 100644
--- a/jstests/tool/dumpauth.js
+++ b/jstests/tool/dumpauth.js
@@ -30,20 +30,17 @@ assert.eq(t.count(), 100, "testcol should have documents");
db.createUser({user: "backup", pwd: "password", roles: ["backup"]});
// Backup the database with the backup user
-x = runMongoProgram("mongodump",
- "--db",
- dbName,
- "--out",
- dumpDir,
- "--authenticationDatabase=admin",
- "-u",
- "backup",
- "-p",
- "password",
- "-h",
- "127.0.0.1:" + m.port);
-assert.eq(x, 0, "mongodump should succeed with authentication");
+var exitCode = MongoRunner.runMongoTool("mongodump", {
+ db: dbName,
+ out: dumpDir,
+ authenticationDatabase: "admin",
+ username: "backup",
+ password: "password",
+ host: "127.0.0.1:" + m.port,
+});
+assert.eq(exitCode, 0, "mongodump should succeed with authentication");
// Assert that a BSON document for admin.system.profile has been produced
-x = runMongoProgram("bsondump", dumpDir + "/" + dbName + "/" + profileName + ".bson");
-assert.eq(x, 0, "bsondump should succeed parsing the profile data");
+exitCode =
+ MongoRunner.runMongoTool("bsondump", {}, dumpDir + "/" + dbName + "/" + profileName + ".bson");
+assert.eq(exitCode, 0, "bsondump should succeed parsing the profile data");
diff --git a/jstests/tool/dumprestore10.js b/jstests/tool/dumprestore10.js
index 7c8cc0ada58..dd0b0116a73 100644
--- a/jstests/tool/dumprestore10.js
+++ b/jstests/tool/dumprestore10.js
@@ -33,7 +33,11 @@ step("mongodump from replset");
var data = MongoRunner.dataDir + "/dumprestore10-dump1/";
-runMongoProgram("mongodump", "--host", "127.0.0.1:" + master.port, "--out", data);
+var exitCode = MongoRunner.runMongoTool("mongodump", {
+ host: "127.0.0.1:" + master.port,
+ out: data,
+});
+assert.eq(0, exitCode, "mongodump failed to dump data from the replica set");
{
step("remove data after dumping");
@@ -47,8 +51,13 @@ runMongoProgram("mongodump", "--host", "127.0.0.1:" + master.port, "--out", data
step("try mongorestore with write concern");
-runMongoProgram(
- "mongorestore", "--writeConcern", "2", "--host", "127.0.0.1:" + master.port, "--dir", data);
+exitCode = MongoRunner.runMongoTool("mongorestore", {
+ writeConcern: "2",
+ host: "127.0.0.1:" + master.port,
+ dir: data,
+});
+assert.eq(
+ 0, exitCode, "mongorestore failed to restore the data to a replica set while using w=2 writes");
var x = 0;
diff --git a/jstests/tool/dumprestore3.js b/jstests/tool/dumprestore3.js
index 6ac6ae76c3f..f29aa3576c4 100644
--- a/jstests/tool/dumprestore3.js
+++ b/jstests/tool/dumprestore3.js
@@ -21,22 +21,35 @@ replTest.awaitReplication();
jsTestLog("mongodump from primary");
var data = MongoRunner.dataDir + "/dumprestore3-other1/";
resetDbpath(data);
-var ret = runMongoProgram("mongodump", "--host", primary.host, "--out", data);
+var ret = MongoRunner.runMongoTool("mongodump", {
+ host: primary.host,
+ out: data,
+});
assert.eq(ret, 0, "mongodump should exit w/ 0 on primary");
jsTestLog("try mongorestore to secondary");
-ret = runMongoProgram("mongorestore", "--host", secondary.host, "--dir", data);
+ret = MongoRunner.runMongoTool("mongorestore", {
+ host: secondary.host,
+ dir: data,
+});
assert.neq(ret, 0, "mongorestore should exit w/ 1 on secondary");
jsTestLog("mongoexport from primary");
dataFile = MongoRunner.dataDir + "/dumprestore3-other2.json";
-ret = runMongoProgram(
- "mongoexport", "--host", primary.host, "--out", dataFile, "--db", "foo", "--collection", "bar");
+ret = MongoRunner.runMongoTool("mongoexport", {
+ host: primary.host,
+ out: dataFile,
+ db: "foo",
+ collection: "bar",
+});
assert.eq(ret, 0, "mongoexport should exit w/ 0 on primary");
jsTestLog("mongoimport from secondary");
-ret = runMongoProgram("mongoimport", "--host", secondary.host, "--file", dataFile);
-assert.neq(ret, 0, "mongoreimport should exit w/ 1 on secondary");
+ret = MongoRunner.runMongoTool("mongoimport", {
+ host: secondary.host,
+ file: dataFile,
+});
+assert.neq(ret, 0, "mongoimport should exit w/ 1 on secondary");
jsTestLog("stopSet");
replTest.stopSet();
diff --git a/jstests/tool/dumprestore7.js b/jstests/tool/dumprestore7.js
index 9235dd9c450..5f5cee7001a 100644
--- a/jstests/tool/dumprestore7.js
+++ b/jstests/tool/dumprestore7.js
@@ -46,24 +46,30 @@ var master = replTest.getPrimary();
step("try mongodump with $timestamp");
var data = MongoRunner.dataDir + "/dumprestore7-dump1/";
-var query = "{\"ts\":{\"$gt\":{\"$timestamp\":{\"t\":" + time.ts.t + ",\"i\":" + time.ts.i + "}}}}";
+var query = {ts: {$gt: {$timestamp: {t: time.ts.t, i: time.ts.i}}}};
-MongoRunner.runMongoTool("mongodump", {
- "host": "127.0.0.1:" + replTest.ports[0],
- "db": "local",
- "collection": "oplog.rs",
- "query": query,
- "out": data
+var exitCode = MongoRunner.runMongoTool("mongodump", {
+ host: "127.0.0.1:" + replTest.ports[0],
+ db: "local",
+ collection: "oplog.rs",
+ query: tojson(query),
+ out: data,
});
+assert.eq(0, exitCode, "monogdump failed to dump the oplog");
step("try mongorestore from $timestamp");
-runMongoProgram(
- "mongorestore", "--host", "127.0.0.1:" + conn.port, "--dir", data, "--writeConcern", 1);
+exitCode = MongoRunner.runMongoTool("mongorestore", {
+ host: "127.0.0.1:" + conn.port,
+ dir: data,
+ writeConcern: 1,
+});
+assert.eq(0, exitCode, "mongorestore failed to restore the oplog");
+
var x = 9;
x = conn.getDB("local").getCollection("oplog.rs").count();
-assert.eq(x, 20, "mongorestore should only have the latter 20 entries");
+assert.eq(x, 20, "mongorestore should only have inserted the latter 20 entries");
step("stopSet");
replTest.stopSet();
diff --git a/jstests/tool/dumprestore9.js b/jstests/tool/dumprestore9.js
index 7db1f817b24..dfb8f2d0979 100644
--- a/jstests/tool/dumprestore9.js
+++ b/jstests/tool/dumprestore9.js
@@ -53,7 +53,11 @@ if (0) {
dumpdir = MongoRunner.dataDir + "/dumprestore9-dump1/";
resetDbpath(dumpdir);
- runMongoProgram("mongodump", "--host", s._mongos[0].host, "--out", dumpdir);
+ var exitCode = MongoRunner.runMongoTool("mongodump", {
+ host: s.s0.host,
+ out: dumpdir,
+ });
+ assert.eq(0, exitCode, "mongodump failed to dump data through one of the mongos processes");
step("Shutting down cluster");
@@ -69,12 +73,14 @@ if (0) {
step("Restore data and config");
- runMongoProgram("mongorestore",
- dumpdir,
- "--host",
- s._mongos[1].host,
- "--restoreShardingConfig",
- "--forceConfigRestore");
+ exitCode = MongoRunner.runMongoTool("mongorestore", {
+ dir: dumpdir,
+ host: s.s1.host,
+ restoreShardingConfig: "",
+ forceConfigRestore: "",
+ });
+ assert.eq(
+ 0, exitCode, "mongorestore failed to restore data through the other mongos process");
config = s.getDB("config");
assert(config.databases.findOne({_id: 'aaa'}).partitioned,
diff --git a/jstests/tool/dumpsecondary.js b/jstests/tool/dumpsecondary.js
index 9abe8d7476e..15bd25f4745 100644
--- a/jstests/tool/dumpsecondary.js
+++ b/jstests/tool/dumpsecondary.js
@@ -15,32 +15,33 @@ var slaves = replTest.liveNodes.slaves;
assert(slaves.length == 1, "Expected 1 slave but length was " + slaves.length);
slave = slaves[0];
-var args = [
- 'mongodump',
- '-h',
- slave.host,
- '--out',
- MongoRunner.dataDir + '/jstests_tool_dumpsecondary_external/'
-];
-var authargs =
- ['--username', jsTest.options().authUser, '--password', jsTest.options().authPassword];
+var commonOptions = {};
if (jsTest.options().keyFile) {
- args = args.concat(authargs);
+ commonOptions.username = jsTest.options().authUser;
+ commonOptions.password = jsTest.options().authPassword;
}
-runMongoProgram.apply(null, args);
-db.foo.drop();
+var exitCode =
+ MongoRunner.runMongoTool("mongodump",
+ Object.extend({
+ host: slave.host,
+ out: MongoRunner.dataDir + "/jstests_tool_dumpsecondary_external/",
+ },
+ commonOptions));
+assert.eq(0, exitCode, "mongodump failed to dump data from the secondary");
+
+db.foo.drop();
assert.eq(0, db.foo.count(), "after drop");
-args = [
- 'mongorestore',
- '-h',
- master.host,
- MongoRunner.dataDir + '/jstests_tool_dumpsecondary_external/'
-];
-if (jsTest.options().keyFile) {
- args = args.concat(authargs);
-}
-runMongoProgram.apply(null, args);
+
+exitCode =
+ MongoRunner.runMongoTool("mongorestore",
+ Object.extend({
+ host: master.host,
+ dir: MongoRunner.dataDir + "/jstests_tool_dumpsecondary_external/",
+ },
+ commonOptions));
+assert.eq(0, exitCode, "mongorestore failed to restore data to the primary");
+
assert.soon("db.foo.findOne()", "no data after sleep");
assert.eq(1, db.foo.count(), "after restore");
assert.eq(1000, db.foo.findOne().a, "after restore 2");
diff --git a/jstests/tool/gridfs.js b/jstests/tool/gridfs.js
index 5fbf6e6036b..147a255d93e 100644
--- a/jstests/tool/gridfs.js
+++ b/jstests/tool/gridfs.js
@@ -18,7 +18,14 @@ function testGridFS(name) {
var rawmd5 = md5sumFile(filename);
// upload file (currently calls filemd5 internally)
- runMongoProgram("mongofiles", "--port", mongos.port, "put", filename, '--db', name);
+ var exitCode = MongoRunner.runMongoTool("mongofiles",
+ {
+ port: mongos.port,
+ db: name,
+ },
+ "put",
+ filename);
+ assert.eq(0, exitCode, "mongofiles failed to upload '" + filename + "' into a sharded cluster");
assert.eq(d.fs.files.count(), 1);
var fileObj = d.fs.files.findOne();
diff --git a/jstests/tool/oplog_all_ops.js b/jstests/tool/oplog_all_ops.js
index fb988174d24..06e6abe29fd 100644
--- a/jstests/tool/oplog_all_ops.js
+++ b/jstests/tool/oplog_all_ops.js
@@ -44,7 +44,11 @@ repl2.initiate();
repl2.awaitSecondaryNodes();
var srcConn = repl1.getPrimary();
-runMongoProgram('mongooplog', '--from', repl1.getPrimary().host, '--host', repl2.getPrimary().host);
+var exitCode = MongoRunner.runMongoTool('mongooplog', {
+ from: repl1.getPrimary().host,
+ host: repl2.getPrimary().host,
+});
+assert.eq(0, exitCode, 'mongooplog failed to poll operations from rs1 and apply them to rs2');
var repl1Hash = testDB.runCommand({dbhash: 1});
diff --git a/jstests/tool/restorewithauth.js b/jstests/tool/restorewithauth.js
index 0fd29706ee1..4a44933fdcb 100644
--- a/jstests/tool/restorewithauth.js
+++ b/jstests/tool/restorewithauth.js
@@ -39,7 +39,13 @@ assert.eq(foo.baz.getIndexes().length, 1);
// get data dump
var dumpdir = MongoRunner.dataDir + "/restorewithauth-dump1/";
resetDbpath(dumpdir);
-x = runMongoProgram("mongodump", "--db", "foo", "-h", "127.0.0.1:" + conn.port, "--out", dumpdir);
+
+var exitCode = MongoRunner.runMongoTool("mongodump", {
+ db: "foo",
+ host: "127.0.0.1:" + conn.port,
+ out: dumpdir,
+});
+assert.eq(0, exitCode, "mongodump failed to dump data from mongod without auth enabled");
// now drop the db
foo.dropDatabase();
@@ -63,7 +69,14 @@ assert.eq(-1, collNames.indexOf("bar"), "bar collection already exists");
assert.eq(-1, collNames.indexOf("baz"), "baz collection already exists");
// now try to restore dump
-x = runMongoProgram("mongorestore", "-h", "127.0.0.1:" + conn.port, "--dir", dumpdir, "-vvvvv");
+exitCode = MongoRunner.runMongoTool("mongorestore", {
+ host: "127.0.0.1:" + conn.port,
+ dir: dumpdir,
+ verbose: 5,
+});
+assert.neq(0,
+ exitCode,
+ "mongorestore shouldn't have been able to restore data to mongod with auth enabled");
// make sure that the collection isn't restored
collNames = foo.getCollectionNames();
@@ -71,19 +84,16 @@ assert.eq(-1, collNames.indexOf("bar"), "bar collection was restored");
assert.eq(-1, collNames.indexOf("baz"), "baz collection was restored");
// now try to restore dump with correct credentials
-x = runMongoProgram("mongorestore",
- "-h",
- "127.0.0.1:" + conn.port,
- "-d",
- "foo",
- "--authenticationDatabase=admin",
- "-u",
- "admin",
- "-p",
- "admin",
- "--dir",
- dumpdir + "foo/",
- "-vvvvv");
+exitCode = MongoRunner.runMongoTool("mongorestore", {
+ host: "127.0.0.1:" + conn.port,
+ db: "foo",
+ authenticationDatabase: "admin",
+ username: "admin",
+ password: "admin",
+ dir: dumpdir + "foo/",
+ verbose: 5,
+});
+assert.eq(0, exitCode, "mongorestore failed to restore data to mongod with auth enabled");
// make sure that the collection was restored
collNames = foo.getCollectionNames();
@@ -99,18 +109,15 @@ foo.dropDatabase();
foo.createUser({user: 'user', pwd: 'password', roles: jsTest.basicUserRoles});
// now try to restore dump with foo database credentials
-x = runMongoProgram("mongorestore",
- "-h",
- "127.0.0.1:" + conn.port,
- "-d",
- "foo",
- "-u",
- "user",
- "-p",
- "password",
- "--dir",
- dumpdir + "foo/",
- "-vvvvv");
+exitCode = MongoRunner.runMongoTool("mongorestore", {
+ host: "127.0.0.1:" + conn.port,
+ db: "foo",
+ username: "user",
+ password: "password",
+ dir: dumpdir + "foo/",
+ verbose: 5,
+});
+assert.eq(0, exitCode, "mongorestore failed to restore the 'foo' database");
// make sure that the collection was restored
collNames = foo.getCollectionNames();
diff --git a/jstests/tool/stat1.js b/jstests/tool/stat1.js
index efdbcb0f376..534f21e0367 100644
--- a/jstests/tool/stat1.js
+++ b/jstests/tool/stat1.js
@@ -8,28 +8,20 @@ db = m.getDB("admin");
db.createUser({user: "eliot", pwd: "eliot", roles: jsTest.adminUserRoles});
assert(db.auth("eliot", "eliot"), "auth failed");
-x = runMongoProgram("mongostat",
- "--host",
- "127.0.0.1:" + m.port,
- "--username",
- "eliot",
- "--password",
- "eliot",
- "--rowcount",
- "1",
- "--authenticationDatabase",
- "admin");
-assert.eq(x, 0, "mongostat should exit successfully with eliot:eliot");
+var exitCode = MongoRunner.runMongoTool("mongostat", {
+ host: "127.0.0.1:" + m.port,
+ username: "eliot",
+ password: "eliot",
+ rowcount: "1",
+ authenticationDatabase: "admin",
+});
+assert.eq(exitCode, 0, "mongostat should exit successfully with eliot:eliot");
-x = runMongoProgram("mongostat",
- "--host",
- "127.0.0.1:" + m.port,
- "--username",
- "eliot",
- "--password",
- "wrong",
- "--rowcount",
- "1",
- "--authenticationDatabase",
- "admin");
-assert.neq(x, 0, "mongostat should exit with -1 with eliot:wrong");
+exitCode = MongoRunner.runMongoTool("mongostat", {
+ host: "127.0.0.1:" + m.port,
+ username: "eliot",
+ password: "wrong",
+ rowcount: "1",
+ authenticationDatabase: "admin",
+});
+assert.neq(exitCode, 0, "mongostat should exit with -1 with eliot:wrong");
diff --git a/jstests/tool/tool1.js b/jstests/tool/tool1.js
index ce5e880b4ba..c968d746742 100644
--- a/jstests/tool/tool1.js
+++ b/jstests/tool/tool1.js
@@ -22,9 +22,20 @@ c = m.getDB(baseName).getCollection(baseName);
c.save({a: 1});
assert(c.findOne());
-runMongoProgram("mongodump", "--host", "127.0.0.1:" + m.port, "--out", externalPath);
+var exitCode = MongoRunner.runMongoTool("mongodump", {
+ host: "127.0.0.1:" + m.port,
+ out: externalPath,
+});
+assert.eq(0, exitCode, "mongodump failed to dump data from mongod");
+
c.drop();
-runMongoProgram("mongorestore", "--host", "127.0.0.1:" + m.port, "--dir", externalPath);
+
+exitCode = MongoRunner.runMongoTool("mongorestore", {
+ host: "127.0.0.1:" + m.port,
+ dir: externalPath,
+});
+assert.eq(0, exitCode, "mongorestore failed to restore data to mongod");
+
assert.soon("c.findOne()", "mongodump then restore has no data w/sleep");
assert(c.findOne(), "mongodump then restore has no data");
assert.eq(1, c.findOne().a, "mongodump then restore has no broken data");
@@ -32,26 +43,28 @@ assert.eq(1, c.findOne().a, "mongodump then restore has no broken data");
resetDbpath(externalPath);
assert.eq(-1, fileSize(), "mongoexport prep invalid");
-runMongoProgram("mongoexport",
- "--host",
- "127.0.0.1:" + m.port,
- "-d",
- baseName,
- "-c",
- baseName,
- "--out",
- externalFile);
+
+exitCode = MongoRunner.runMongoTool("mongoexport", {
+ host: "127.0.0.1:" + m.port,
+ db: baseName,
+ collection: baseName,
+ out: externalFile,
+});
+assert.eq(
+ 0, exitCode, "mongoexport failed to export collection '" + c.getFullName() + "' from mongod");
+
assert.lt(10, fileSize(), "file size changed");
c.drop();
-runMongoProgram("mongoimport",
- "--host",
- "127.0.0.1:" + m.port,
- "-d",
- baseName,
- "-c",
- baseName,
- "--file",
- externalFile);
+
+exitCode = MongoRunner.runMongoTool("mongoimport", {
+ host: "127.0.0.1:" + m.port,
+ db: baseName,
+ collection: baseName,
+ file: externalFile,
+});
+assert.eq(
+ 0, exitCode, "mongoimport failed to import collection '" + c.getFullName() + "' into mongod");
+
assert.soon("c.findOne()", "mongo import json A");
assert(c.findOne() && 1 == c.findOne().a, "mongo import json B");
diff --git a/jstests/tool/tool_replset.js b/jstests/tool/tool_replset.js
index 3b0338a02d4..1608acbe992 100644
--- a/jstests/tool/tool_replset.js
+++ b/jstests/tool/tool_replset.js
@@ -36,14 +36,22 @@
// Test with mongodump/mongorestore
print("dump the db");
var data = MongoRunner.dataDir + "/tool_replset-dump1/";
- runMongoProgram("mongodump", "--host", replSetConnString, "--out", data);
+ var exitCode = MongoRunner.runMongoTool("mongodump", {
+ host: replSetConnString,
+ out: data,
+ });
+ assert.eq(0, exitCode, "mongodump failed to dump from the replica set");
print("db successfully dumped, dropping now");
master.getDB("foo").dropDatabase();
replTest.awaitReplication();
print("restore the db");
- runMongoProgram("mongorestore", "--host", replSetConnString, "--dir", data);
+ exitCode = MongoRunner.runMongoTool("mongorestore", {
+ host: replSetConnString,
+ dir: data,
+ });
+ assert.eq(0, exitCode, "mongorestore failed to restore data to the replica set");
print("db successfully restored, checking count");
var x = master.getDB("foo").getCollection("bar").count();
@@ -54,16 +62,28 @@
// Test with mongoexport/mongoimport
print("export the collection");
var extFile = MongoRunner.dataDir + "/tool_replset/export";
- runMongoProgram(
- "mongoexport", "--host", replSetConnString, "--out", extFile, "-d", "foo", "-c", "bar");
+ exitCode = MongoRunner.runMongoTool("mongoexport", {
+ host: replSetConnString,
+ out: extFile,
+ db: "foo",
+ collection: "bar",
+ });
+ assert.eq(
+ 0, exitCode, "mongoexport failed to export collection 'foo.bar' from the replica set");
print("collection successfully exported, dropping now");
master.getDB("foo").getCollection("bar").drop();
replTest.awaitReplication();
print("import the collection");
- runMongoProgram(
- "mongoimport", "--host", replSetConnString, "--file", extFile, "-d", "foo", "-c", "bar");
+ exitCode = MongoRunner.runMongoTool("mongoimport", {
+ host: replSetConnString,
+ file: extFile,
+ db: "foo",
+ collection: "bar",
+ });
+ assert.eq(
+ 0, exitCode, "mongoimport failed to import collection 'foo.bar' into the replica set");
var x = master.getDB("foo").getCollection("bar").count();
assert.eq(x, 100, "mongoimport should have successfully imported the collection");
@@ -75,8 +95,11 @@
master.getDB("foo").getCollection("bar").count(),
"count before running " + "mongooplog was not 100 as expected");
- runMongoProgram(
- "mongooplog", "--from", "127.0.0.1:" + replTest.ports[0], "--host", replSetConnString);
+ exitCode = MongoRunner.runMongoTool("mongooplog", {
+ from: "127.0.0.1:" + replTest.ports[0],
+ host: replSetConnString,
+ });
+ assert.eq(0, exitCode, "mongooplog failed to replay the oplog");
print("finished running mongooplog to replay the oplog");
diff --git a/src/mongo/shell/servers.js b/src/mongo/shell/servers.js
index fc536a3cd82..eb00f2eaeeb 100644
--- a/src/mongo/shell/servers.js
+++ b/src/mongo/shell/servers.js
@@ -806,15 +806,18 @@ var MongoRunner, _startMongod, startMongoProgram, runMongoProgram, startMongoPro
/**
* Starts an instance of the specified mongo tool
*
- * @param {String} binaryName The name of the tool to run
- * @param {Object} opts options to pass to the tool
- * {
- * binVersion {string}: version of tool to run
- * }
+ * @param {String} binaryName - The name of the tool to run.
+ * @param {Object} [opts={}] - Options of the form --flag or --key=value to pass to the tool.
+ * @param {string} [opts.binVersion] - The version of the tool to run.
+ *
+ * @param {...string} positionalArgs - Positional arguments to pass to the tool after all
+ * options have been specified. For example,
+ * MongoRunner.runMongoTool("executable", {key: value}, arg1, arg2) would invoke
+ * ./executable --key value arg1 arg2.
*
* @see MongoRunner.arrOptions
*/
- MongoRunner.runMongoTool = function(binaryName, opts) {
+ MongoRunner.runMongoTool = function(binaryName, opts, ...positionalArgs) {
var opts = opts || {};
// Normalize and get the binary version to use
@@ -829,8 +832,12 @@ var MongoRunner, _startMongod, startMongoProgram, runMongoProgram, startMongoPro
opts['dialTimeout'] = '30';
}
+ // Convert 'opts' into an array of arguments.
var argsArray = MongoRunner.arrOptions(binaryName, opts);
+ // Append any positional arguments that were specified.
+ argsArray.push(...positionalArgs);
+
return runMongoProgram.apply(null, argsArray);
};