function runTool(toolName, mongod, shutdownServer, options) {
if (shutdownServer) {
MongoRunner.stopMongod(mongod);
var opts = {dbpath: mongod.fullOptions.pathOpts.dbpath};
Object.extend(opts, options);
assert(!MongoRunner.runMongoTool(toolName, opts));
mongod.fullOptions.restart = true;
return MongoRunner.runMongod(mongod.fullOptions);
} else {
var opts = {host: mongod.host};
Object.extend(opts, options);
assert(!MongoRunner.runMongoTool(toolName, opts));
return mongod;
}
}
var shutdownServer = true;
var mongod = MongoRunner.runMongod();
var admindb = mongod.getDB("admin");
var db = mongod.getDB("foo");
admindb.createUser({user: 'root', pwd: 'pass', roles: ['root']});
admindb.createRole({role: 'adminrole', roles: [], privileges:[]});
db.createUser({user: 'user', pwd: 'pass', roles: jsTest.basicUserRoles});
db.createRole({role: 'role', roles: [], privileges:[]});
admindb.system.users.find().forEach( function(myDoc) {print( "user: "+myDoc._id);});
admindb.system.roles.find().forEach( function(myDoc) {print( "role: "+myDoc._id);});
var dumpDir = MongoRunner.getAndPrepareDumpDirectory("restoreTest");
mongod = runTool("mongodump", mongod, shutdownServer, {out: dumpDir, db: "foo", dumpDbUsersAndRoles: ""});
print("After mongodump");
db = mongod.getDB('foo');
db.dropUser('user')
db.createUser({user: 'user2', pwd: 'password2', roles: jsTest.basicUserRoles});
db.dropRole('role')
db.createRole({role: 'role2', roles: [], privileges:[]});
print("After creating temp users");
mongod = runTool("mongorestore", mongod, shutdownServer, {dir: dumpDir + "foo/", db: 'foo'});
db = mongod.getDB('foo');
admindb = mongod.getDB("admin");
print("After mongorestore without drop");
admindb.system.users.find().forEach( function(myDoc) {print( "user: "+myDoc._id);});
admindb.system.roles.find().forEach( function(myDoc) {print( "role: "+myDoc._id);});
mongod = runTool("mongorestore", mongod, shutdownServer, {dir: dumpDir + "foo/", db: 'foo',
restoreDbUsersAndRoles: ""});
db = mongod.getDB('foo');
admindb = mongod.getDB("admin");
print("After mongorestore without drop & restoreDbUsersAndRoles");
admindb.system.users.find().forEach( function(myDoc) {print( "user: "+myDoc._id);});
admindb.system.roles.find().forEach( function(myDoc) {print( "role: "+myDoc._id);});
mongod = runTool("mongorestore", mongod, shutdownServer, {dir: dumpDir + "foo/", db: 'foo',
drop: "", restoreDbUsersAndRoles: ""});
db = mongod.getDB('foo');
admindb = mongod.getDB("admin");
print("After mongorestore with drop");
admindb.system.users.find().forEach( function(myDoc) {print( "user: "+myDoc._id);});
admindb.system.roles.find().forEach( function(myDoc) {print( "role: "+myDoc._id);});
MongoRunner.stopMongod(mongod);