|
| 1 | +# Copyright 2013 MongoDB, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Test MongoReplicaSetClients and replica set configuration changes.""" |
| 16 | + |
| 17 | +import socket |
| 18 | +import sys |
| 19 | +import unittest |
| 20 | + |
| 21 | +sys.path[0:0] = [""] |
| 22 | + |
| 23 | +from pymongo import MongoClient |
| 24 | +from pymongo.errors import AutoReconnect |
| 25 | +from pymongo.mongo_replica_set_client import MongoReplicaSetClient |
| 26 | +from pymongo.pool import Pool |
| 27 | +from test import host as default_host, port as default_port |
| 28 | + |
| 29 | + |
| 30 | +class MockPool(Pool): |
| 31 | + def __init__(self, pair, *args, **kwargs): |
| 32 | + if pair: |
| 33 | + # RS client passes 'pair' to Pool's constructor. |
| 34 | + self.mock_host, self.mock_port = pair |
| 35 | + else: |
| 36 | + # MongoClient passes pair to get_socket() instead. |
| 37 | + self.mock_host, self.mock_port = None, None |
| 38 | + |
| 39 | + Pool.__init__( |
| 40 | + self, |
| 41 | + pair=(default_host, default_port), |
| 42 | + max_size=None, |
| 43 | + net_timeout=None, |
| 44 | + conn_timeout=20, |
| 45 | + use_ssl=False, |
| 46 | + use_greenlets=False) |
| 47 | + |
| 48 | + def get_socket(self, pair=None, force=False): |
| 49 | + sock_info = Pool.get_socket(self, (default_host, default_port), force) |
| 50 | + sock_info.host = self.mock_host or pair[0] |
| 51 | + return sock_info |
| 52 | + |
| 53 | + |
| 54 | +MOCK_HOSTS = ['a:27017', 'b:27017', 'c:27017'] |
| 55 | +MOCK_PRIMARY = MOCK_HOSTS[0] |
| 56 | +MOCK_RS_NAME = 'rs' |
| 57 | + |
| 58 | + |
| 59 | +class MockClientBase(object): |
| 60 | + def __init__(self): |
| 61 | + self.mock_hosts = MOCK_HOSTS |
| 62 | + |
| 63 | + # Hosts that should raise socket errors. |
| 64 | + self.mock_down_hosts = [] |
| 65 | + |
| 66 | + # Hosts that should respond to ismaster as if they're standalone. |
| 67 | + self.mock_standalone_hosts = [] |
| 68 | + |
| 69 | + def mock_is_master(self, host): |
| 70 | + if host in self.mock_down_hosts: |
| 71 | + raise socket.timeout('timed out') |
| 72 | + |
| 73 | + if host in self.mock_standalone_hosts: |
| 74 | + return {'ismaster': True} |
| 75 | + |
| 76 | + if host not in self.mock_hosts: |
| 77 | + # Host removed from set by a reconfig. |
| 78 | + return {'ismaster': False, 'secondary': False} |
| 79 | + |
| 80 | + ismaster = host == MOCK_PRIMARY |
| 81 | + |
| 82 | + # Simulate a replica set member. |
| 83 | + return { |
| 84 | + 'ismaster': ismaster, |
| 85 | + 'secondary': not ismaster, |
| 86 | + 'setName': MOCK_RS_NAME, |
| 87 | + 'hosts': self.mock_hosts} |
| 88 | + |
| 89 | + def simple_command(self, sock_info, dbname, spec): |
| 90 | + # __simple_command is also used for authentication, but in this |
| 91 | + # test it's only used for ismaster. |
| 92 | + assert spec == {'ismaster': 1} |
| 93 | + response = self.mock_is_master('%s:%s' % (sock_info.host, 27017)) |
| 94 | + ping_time = 10 |
| 95 | + return response, ping_time |
| 96 | + |
| 97 | + |
| 98 | +class MockClient(MockClientBase, MongoClient): |
| 99 | + def __init__(self, hosts): |
| 100 | + MockClientBase.__init__(self) |
| 101 | + MongoClient.__init__( |
| 102 | + self, |
| 103 | + hosts, |
| 104 | + replicaSet=MOCK_RS_NAME, |
| 105 | + _pool_class=MockPool) |
| 106 | + |
| 107 | + def _MongoClient__simple_command(self, sock_info, dbname, spec): |
| 108 | + return self.simple_command(sock_info, dbname, spec) |
| 109 | + |
| 110 | + |
| 111 | +class MockReplicaSetClient(MockClientBase, MongoReplicaSetClient): |
| 112 | + def __init__(self, hosts): |
| 113 | + MockClientBase.__init__(self) |
| 114 | + MongoReplicaSetClient.__init__( |
| 115 | + self, |
| 116 | + hosts, |
| 117 | + replicaSet=MOCK_RS_NAME) |
| 118 | + |
| 119 | + def _MongoReplicaSetClient__is_master(self, host): |
| 120 | + response = self.mock_is_master('%s:%s' % host) |
| 121 | + connection_pool = MockPool(host) |
| 122 | + ping_time = 10 |
| 123 | + return response, connection_pool, ping_time |
| 124 | + |
| 125 | + def _MongoReplicaSetClient__simple_command(self, sock_info, dbname, spec): |
| 126 | + return self.simple_command(sock_info, dbname, spec) |
| 127 | + |
| 128 | + |
| 129 | +class TestSecondaryBecomesStandalone(unittest.TestCase): |
| 130 | + # An administrator removes a secondary from a 3-node set and |
| 131 | + # brings it back up as standalone, without updating the other |
| 132 | + # members' config. Verify we don't continue using it. |
| 133 | + def test_client(self): |
| 134 | + c = MockClient(','.join(MOCK_HOSTS)) |
| 135 | + |
| 136 | + # MongoClient connects to primary by default. |
| 137 | + self.assertEqual('a', c.host) |
| 138 | + self.assertEqual(27017, c.port) |
| 139 | + |
| 140 | + # C is brought up as a standalone. |
| 141 | + c.mock_standalone_hosts.append('c:27017') |
| 142 | + |
| 143 | + # Fail over. |
| 144 | + c.mock_down_hosts = ['a:27017', 'b:27017'] |
| 145 | + |
| 146 | + # Force reconnect. |
| 147 | + c.disconnect() |
| 148 | + |
| 149 | + try: |
| 150 | + c.db.collection.find_one() |
| 151 | + except AutoReconnect, e: |
| 152 | + self.assertTrue('not a member of replica set' in str(e)) |
| 153 | + else: |
| 154 | + self.fail("MongoClient didn't raise AutoReconnect") |
| 155 | + |
| 156 | + self.assertEqual(None, c.host) |
| 157 | + self.assertEqual(None, c.port) |
| 158 | + |
| 159 | + def test_replica_set_client(self): |
| 160 | + c = MockReplicaSetClient(','.join(MOCK_HOSTS)) |
| 161 | + self.assertTrue(('c', 27017) in c.secondaries) |
| 162 | + |
| 163 | + # C is brought up as a standalone. |
| 164 | + c.mock_standalone_hosts.append('c:27017') |
| 165 | + c.refresh() |
| 166 | + |
| 167 | + self.assertEqual(('a', 27017), c.primary) |
| 168 | + self.assertEqual(set([('b', 27017)]), c.secondaries) |
| 169 | + |
| 170 | + |
| 171 | +class TestSecondaryRemoved(unittest.TestCase): |
| 172 | + # An administrator removes a secondary from a 3-node set *without* |
| 173 | + # restarting it as standalone. |
| 174 | + def test_replica_set_client(self): |
| 175 | + c = MockReplicaSetClient(','.join(MOCK_HOSTS)) |
| 176 | + self.assertTrue(('c', 27017) in c.secondaries) |
| 177 | + |
| 178 | + # C is removed. |
| 179 | + c.mock_hosts.remove('c:27017') |
| 180 | + c.refresh() |
| 181 | + |
| 182 | + self.assertEqual(('a', 27017), c.primary) |
| 183 | + self.assertEqual(set([('b', 27017)]), c.secondaries) |
| 184 | + |
| 185 | + |
| 186 | +if __name__ == "__main__": |
| 187 | + unittest.main() |
0 commit comments