Skip to content

Commit f37be74

Browse files
committed
Remove test assumptions about primary host
1 parent c7c352f commit f37be74

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

test/__init__.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,55 @@
1616
"""
1717

1818
import os
19+
import sys
1920
import warnings
2021

2122
import pymongo
2223
from nose.plugins.skip import SkipTest
2324
from pymongo.errors import ConnectionFailure, OperationFailure
2425

25-
# hostnames retrieved by MongoReplicaSetClient from isMaster will be of unicode
26+
# Hostnames retrieved by MongoReplicaSetClient from isMaster will be of unicode
2627
# type in Python 2, so ensure these hostnames are unicodes, too. It makes tests
2728
# like `test_repr` predictable.
29+
30+
# A standalone mongod, mongos, or a seed for a replica set.
2831
host = unicode(os.environ.get("DB_IP", 'localhost'))
2932
port = int(os.environ.get("DB_PORT", 27017))
30-
pair = '%s:%d' % (host, port)
3133

34+
# Only used by test_master_slave_connection.
3235
host2 = unicode(os.environ.get("DB_IP2", 'localhost'))
3336
port2 = int(os.environ.get("DB_PORT2", 27018))
3437

38+
# Only used by test_master_slave_connection.
3539
host3 = unicode(os.environ.get("DB_IP3", 'localhost'))
3640
port3 = int(os.environ.get("DB_PORT3", 27019))
3741

38-
db_user = unicode(os.environ.get("DB_USER", "administrator"))
42+
db_user = unicode(os.environ.get("DB_USER", "user"))
3943
db_pwd = unicode(os.environ.get("DB_PASSWORD", "password"))
4044

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)
4168

4269
class AuthContext(object):
4370

test/test_auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ def test_unix_socket(self):
561561
not version.at_least(authed_client, (2, 7, 1))):
562562
raise SkipTest("SERVER-8492")
563563

564-
mongodb_socket = '/tmp/mongodb-27017.sock'
564+
mongodb_socket = '/tmp/mongodb-%d.sock' % (port,)
565565
if not os.access(mongodb_socket, os.R_OK):
566566
raise SkipTest("Socket file is not accessable")
567567

test/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ def test_unix_socket(self):
499499
not version.at_least(client, (2, 7, 1))):
500500
raise SkipTest("SERVER-8492")
501501

502-
mongodb_socket = '/tmp/mongodb-27017.sock'
502+
mongodb_socket = '/tmp/mongodb-%d.sock' % (port,)
503503
if not os.access(mongodb_socket, os.R_OK):
504504
raise SkipTest("Socket file is not accessable")
505505

0 commit comments

Comments
 (0)