|
16 | 16 | """ |
17 | 17 |
|
18 | 18 | import os |
| 19 | +import sys |
19 | 20 | import warnings |
20 | 21 |
|
21 | 22 | import pymongo |
22 | 23 | from nose.plugins.skip import SkipTest |
23 | 24 | from pymongo.errors import ConnectionFailure, OperationFailure |
24 | 25 |
|
25 | | -# hostnames retrieved by MongoReplicaSetClient from isMaster will be of unicode |
| 26 | +# Hostnames retrieved by MongoReplicaSetClient from isMaster will be of unicode |
26 | 27 | # type in Python 2, so ensure these hostnames are unicodes, too. It makes tests |
27 | 28 | # like `test_repr` predictable. |
| 29 | + |
| 30 | +# A standalone mongod, mongos, or a seed for a replica set. |
28 | 31 | host = unicode(os.environ.get("DB_IP", 'localhost')) |
29 | 32 | port = int(os.environ.get("DB_PORT", 27017)) |
30 | | -pair = '%s:%d' % (host, port) |
31 | 33 |
|
| 34 | +# Only used by test_master_slave_connection. |
32 | 35 | host2 = unicode(os.environ.get("DB_IP2", 'localhost')) |
33 | 36 | port2 = int(os.environ.get("DB_PORT2", 27018)) |
34 | 37 |
|
| 38 | +# Only used by test_master_slave_connection. |
35 | 39 | host3 = unicode(os.environ.get("DB_IP3", 'localhost')) |
36 | 40 | port3 = int(os.environ.get("DB_PORT3", 27019)) |
37 | 41 |
|
38 | | -db_user = unicode(os.environ.get("DB_USER", "administrator")) |
| 42 | +db_user = unicode(os.environ.get("DB_USER", "user")) |
39 | 43 | db_pwd = unicode(os.environ.get("DB_PASSWORD", "password")) |
40 | 44 |
|
| 45 | +def _split_host_port(host_port): |
| 46 | + host, port = host_port.split(':', 1) |
| 47 | + return host, int(port) |
| 48 | + |
| 49 | +# When testing against a replica set, 'host' and 'port' need |
| 50 | +# to represent the primary. |
| 51 | +try: |
| 52 | + _ismaster = pymongo.MongoClient(host, port).admin.command('ismaster') |
| 53 | + # Master slave topologies don't use setName. The members also aren't |
| 54 | + # aware of one another (e.g. ismaster doesn't have a 'hosts' field). |
| 55 | + if 'setName' in _ismaster: |
| 56 | + _primary = _ismaster['primary'] |
| 57 | + host, port = _split_host_port(_primary) |
| 58 | + _secondaries = set(_ismaster['hosts']) - set([_primary]) |
| 59 | + for _idx, _host_port in enumerate(_secondaries): |
| 60 | + _host, _port = _split_host_port(_host_port) |
| 61 | + # Enumerate doesn't accept a start argument until python 2.6. |
| 62 | + setattr(sys.modules[__name__], "host%d" % (_idx + 1,), _host) |
| 63 | + setattr(sys.modules[__name__], "port%d" % (_idx + 1,), _port) |
| 64 | +except ConnectionFailure: |
| 65 | + pass |
| 66 | + |
| 67 | +pair = '%s:%d' % (host, port) |
41 | 68 |
|
42 | 69 | class AuthContext(object): |
43 | 70 |
|
|
0 commit comments