diff options
Diffstat (limited to 'src/mongo/dbtests/mock_replica_set_test.cpp')
-rw-r--r-- | src/mongo/dbtests/mock_replica_set_test.cpp | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/mongo/dbtests/mock_replica_set_test.cpp b/src/mongo/dbtests/mock_replica_set_test.cpp index d82c7ff8d30..1a59ddb4718 100644 --- a/src/mongo/dbtests/mock_replica_set_test.cpp +++ b/src/mongo/dbtests/mock_replica_set_test.cpp @@ -243,6 +243,113 @@ namespace mongo_test { ASSERT(expectedMembers == memberList); } + TEST(MockReplicaSetTest, IsMasterReconfigNodeRemoved) { + MockReplicaSet replSet("n", 3); + + MockReplicaSet::ReplConfigMap config = replSet.getReplConfig(); + const string hostToRemove("$n1:27017"); + config.erase(hostToRemove); + replSet.setConfig(config); + + { + // Check isMaster for node still in set + BSONObj cmdResponse; + MockRemoteDBServer* node = replSet.getNode("$n0:27017"); + const MockRemoteDBServer::InstanceID id = node->getInstanceID(); + bool ok = node->runCommand(id, "foo.bar", BSON("ismaster" << 1), cmdResponse); + ASSERT(ok); + + ASSERT(cmdResponse["ismaster"].trueValue()); + ASSERT(!cmdResponse["secondary"].trueValue()); + ASSERT_EQUALS("$n0:27017", cmdResponse["me"].str()); + ASSERT_EQUALS("$n0:27017", cmdResponse["primary"].str()); + ASSERT_EQUALS("n", cmdResponse["setName"].str()); + + set<string> expectedHosts; + expectedHosts.insert("$n0:27017"); + expectedHosts.insert("$n2:27017"); + + set<string> hostList; + BSONObjIterator iter(cmdResponse["hosts"].embeddedObject()); + while (iter.more()) { + hostList.insert(iter.next().str()); + } + + ASSERT(expectedHosts == hostList); + ASSERT(hostList.count(hostToRemove) == 0); + } + + { + // Check isMaster for node still not in set anymore + BSONObj cmdResponse; + MockRemoteDBServer* node = replSet.getNode(hostToRemove); + const MockRemoteDBServer::InstanceID id = node->getInstanceID(); + bool ok = node->runCommand(id, "foo.bar", BSON("ismaster" << 1), cmdResponse); + ASSERT(ok); + + ASSERT(!cmdResponse["ismaster"].trueValue()); + ASSERT(!cmdResponse["secondary"].trueValue()); + ASSERT_EQUALS(hostToRemove, cmdResponse["me"].str()); + ASSERT_EQUALS("n", cmdResponse["setName"].str()); + } + } + + TEST(MockReplicaSetTest, replSetGetStatusReconfigNodeRemoved) { + MockReplicaSet replSet("n", 3); + + MockReplicaSet::ReplConfigMap config = replSet.getReplConfig(); + const string hostToRemove("$n1:27017"); + config.erase(hostToRemove); + replSet.setConfig(config); + + { + // Check replSetGetStatus for node still in set + BSONObj cmdResponse; + MockRemoteDBServer* node = replSet.getNode("$n2:27017"); + const MockRemoteDBServer::InstanceID id = node->getInstanceID(); + bool ok = node->runCommand(id, "foo.bar", BSON("replSetGetStatus" << 1), cmdResponse); + ASSERT(ok); + + ASSERT_EQUALS("n", cmdResponse["set"].str()); + ASSERT_EQUALS(2, cmdResponse["myState"].numberInt()); + + set<string> memberList; + BSONObjIterator iter(cmdResponse["members"].embeddedObject()); + while (iter.more()) { + BSONElement member(iter.next()); + memberList.insert(member["name"].str()); + + if (member["self"].trueValue()) { + ASSERT_EQUALS(2, member["state"].numberInt()); + ASSERT_EQUALS("$n2:27017", member["name"].str()); + } + else if (member["name"].str() == "$n0:27017") { + ASSERT_EQUALS(1, member["state"].numberInt()); + } + else { + ASSERT_EQUALS(2, member["state"].numberInt()); + } + } + + set<string> expectedMembers; + expectedMembers.insert("$n0:27017"); + expectedMembers.insert("$n2:27017"); + ASSERT(expectedMembers == memberList); + } + + { + // Check replSetGetStatus for node still not in set anymore + BSONObj cmdResponse; + MockRemoteDBServer* node = replSet.getNode(hostToRemove); + const MockRemoteDBServer::InstanceID id = node->getInstanceID(); + bool ok = node->runCommand(id, "foo.bar", BSON("replSetGetStatus" << 1), cmdResponse); + ASSERT(ok); + + ASSERT_EQUALS("n", cmdResponse["set"].str()); + ASSERT_EQUALS(10, cmdResponse["myState"].numberInt()); + } + } + TEST(MockReplicaSetTest, KillNode) { MockReplicaSet replSet("n", 3); const string priHostName(replSet.getPrimary()); |