/*
 * Test to ensure that (dump/restore/export/import/oplog) works with a replica set connection string
 * 1. Start a replica set.
 * 2. Add data to a collection.
 * 3. Take a dump of the database.
 * 4. Drop the db.
 * 5. Restore the db.
 * 6. Export a collection.
 * 7. Drop the collection.
 * 8. Import the collection.
*/

// Load utility methods for replica set tests
load("jstests/replsets/rslib.js");

print("starting the replica set")

var replTest = new ReplSetTest({ name: 'tool_replset', nodes: 2, oplogSize: 5 });
var nodes = replTest.startSet();
replTest.initiate();
var master = replTest.getPrimary();
for (var i = 0; i < 100; i++) {
    master.getDB("foo").bar.insert({ a: i });
}
replTest.awaitReplication();

var replSetConnString = "tool_replset/127.0.0.1:" + replTest.ports[0] +
                        ",127.0.0.1:" + replTest.ports[1];

// Test with mongodump/mongorestore
print("dump the db");
var data = MongoRunner.dataDir + "/tool_replset-dump1/";
runMongoProgram("mongodump", "--host", replSetConnString, "--out", data);

print("db successfully dumped, dropping now");
master.getDB("foo").dropDatabase();
replTest.awaitReplication();

print("restore the db");
runMongoProgram("mongorestore", "--host", replSetConnString, "--dir", data);

print("db successfully restored, checking count")
var x = master.getDB("foo").getCollection("bar").count();
assert.eq(x, 100, "mongorestore should have successfully restored the collection");

replTest.awaitReplication();

// 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");

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");

var x = master.getDB("foo").getCollection("bar").count();
assert.eq(x, 100, "mongoimport should have successfully imported the collection");

print("all tests successful, stopping replica set")

replTest.stopSet();

print("replica set stopped, test complete")
