Skip to content

Commit

Permalink
SERVER-14140 Disallow setting both --host and --dbpath in tools
Browse files Browse the repository at this point in the history
  • Loading branch information
sverch committed Jun 9, 2014
1 parent 0974eb3 commit cd57c77
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
73 changes: 73 additions & 0 deletions jstests/tool/incompatible_options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Tests to ensure that incompatible options cause a failure to startup.



// Make sure --host and --dbpath options are incompatible.

// The base name to use for various things in the test, including the dbpath and the database name
var testBaseName = "jstests_tool_incompatible_options";

// Paths to external directories to be used to store dump files
var dumpDir = MongoRunner.dataPath + testBaseName + "_dump_external/";
var dumpFile = MongoRunner.dataPath + testBaseName + "_export_external.json";
var testDbpath = MongoRunner.dataPath + testBaseName + "_dbpath_external/";

resetDbpath(dumpDir);
resetDbpath(testDbpath);

// First, start and stop the mongod we are using for the direct dump
var mongodDirect = MongoRunner.runMongod({ dbpath : testDbpath });
mongodDirect.getDB(testBaseName).getCollection("test").insert({x:1});
MongoRunner.stopMongod(mongodDirect.port);

// Next, start the mongod we are using for the network dump
var mongodSource = MongoRunner.runMongod();
var sourceDB = mongodSource.getDB(testBaseName);

// Test that mongodump with both --host and --dbpath fails
var ret = MongoRunner.runMongoTool("mongodump", { out : dumpDir,
dbpath : testDbpath,
host : mongodSource.host });
assert.neq(ret, 0, "mongodump started successfully with both --host and --dbpath");

// Test that mongorestore with both --host and --dbpath fails, but succeeds otherwise
ret = MongoRunner.runMongoTool("mongodump", { out : dumpDir, dbpath : testDbpath });
assert.eq(ret, 0, "failed to run mongorestore on expected successful call");
ret = MongoRunner.runMongoTool("mongorestore", { dir : dumpDir, host : mongodSource.host });
assert.eq(ret, 0, "failed to run mongodump on expected successful call");
mongodSource.getDB(testBaseName).dropDatabase();
ret = MongoRunner.runMongoTool("mongorestore", { dir : dumpDir,
dbpath : testDbpath,
host : mongodSource.host });
assert.neq(ret, 0, "mongorestore started successfully with both --host and --dbpath");

// Test that mongoexport with both --host and --dbpath fails
ret = MongoRunner.runMongoTool("mongoexport", { out : dumpFile,
db : testBaseName,
collection : "test",
dbpath : testDbpath,
host : mongodSource.host });
assert.neq(ret, 0, "mongoexport started successfully with both --host and --dbpath");

// Test that mongoimport with both --host and --dbpath fails, but succeeds otherwise
ret = MongoRunner.runMongoTool("mongoexport", { out : dumpFile,
db : testBaseName,
collection : "test",
dbpath : testDbpath });
assert.eq(ret, 0, "failed to run mongoexport on expected successful call");
ret = MongoRunner.runMongoTool("mongoimport", { file : dumpFile,
db : testBaseName,
collection : "test",
host : mongodSource.host });
assert.eq(ret, 0, "failed to run mongoimport on expected successful call");
mongodSource.getDB(testBaseName).dropDatabase();
ret = MongoRunner.runMongoTool("mongoimport", { file : dumpFile,
db : testBaseName,
collection : "test",
dbpath : testDbpath,
host : mongodSource.host });
assert.neq(ret, 0, "mongoimport started successfully with both --host and --dbpath");
MongoRunner.stopMongod(mongodSource.port);


print(testBaseName + " success!");
6 changes: 4 additions & 2 deletions src/mongo/tools/tool_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ namespace mongo {

Status addRemoteServerToolOptions(moe::OptionSection* options) {
options->addOptionChaining("host", "host,h", moe::String,
"mongo host to connect to ( <set name>/s1,s2 for sets)");
"mongo host to connect to ( <set name>/s1,s2 for sets)")
.incompatibleWith("dbpath");

options->addOptionChaining("port", "port", moe::Int,
"server port. Can also use --host hostname:port")
Expand Down Expand Up @@ -130,7 +131,8 @@ namespace mongo {
options->addOptionChaining("dbpath", "dbpath", moe::String,
"directly access mongod database files in the given path, instead of "
"connecting to a mongod server - needs to lock the data directory, "
"so cannot be used if a mongod is currently accessing the same path");
"so cannot be used if a mongod is currently accessing the same path")
.incompatibleWith("host");

options->addOptionChaining("directoryperdb", "directoryperdb", moe::Switch,
"each db is in a separate directory (relevant only if dbpath specified)");
Expand Down

0 comments on commit cd57c77

Please sign in to comment.