Skip to content

Commit cedc355

Browse files
committed
connect to a single mongos
1 parent 424721b commit cedc355

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

test/__init__.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@
106106
db_user = res['username'] or db_user
107107
db_pwd = res['password'] or db_pwd
108108
elif TEST_SERVERLESS:
109-
uri = os.environ["MONGODB_URI"]
110-
host = "mongodb://%s:%s@%s" % (
111-
db_user, db_pwd, str(uri).lstrip("mongodb://"))
112-
port = None
109+
res = parse_uri(os.environ["MONGODB_URI"])
110+
host, port = res['nodelist'].pop(0)
111+
additional_serverless_mongoses = res['nodelist']
112+
db_user = res['username'] or db_user
113+
db_pwd = res['password'] or db_pwd
113114
TLS_OPTIONS = {'tls': True}
114115

115116

@@ -393,6 +394,7 @@ def _init_client(self):
393394
if TEST_SERVERLESS:
394395
self.test_commands_enabled = True
395396
self.has_ipv6 = False
397+
396398
else:
397399
self.server_parameters = self.client.admin.command(
398400
'getParameter', '*')
@@ -407,14 +409,19 @@ def _init_client(self):
407409
if params.get('enableTestCommands') == '1':
408410
self.test_commands_enabled = True
409411
self.has_ipv6 = self._server_started_with_ipv6()
410-
self.is_mongos = (self.ismaster.get('msg') == 'isdbgrid')
411-
if self.is_mongos:
412+
413+
self.is_mongos = (self.ismaster.get('msg') == 'isdbgrid')
414+
if self.is_mongos:
415+
if self.serverless:
416+
self.mongoses.append(self.client.address)
417+
self.mongoses.extend(additional_serverless_mongoses)
418+
else:
412419
# Check for another mongos on the next port.
413420
address = self.client.address
414421
next_address = address[0], address[1] + 1
415422
self.mongoses.append(address)
416-
mongos_client = self._connect(*next_address,
417-
**self.default_client_options)
423+
mongos_client = self._connect(
424+
*next_address, **self.default_client_options)
418425
if mongos_client:
419426
ismaster = mongos_client.admin.command('ismaster')
420427
if ismaster.get('msg') == 'isdbgrid':
@@ -444,10 +451,7 @@ def port(self):
444451

445452
@property
446453
def pair(self):
447-
if self.port:
448-
return "%s:%d" % (self.host, self.port)
449-
else:
450-
return "%s" % (self.host, )
454+
return "%s:%d" % (self.host, self.port)
451455

452456
@property
453457
def has_secondaries(self):

test/test_client.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ def iterate():
224224
self.assertRaises(TypeError, iterate)
225225

226226
def test_get_default_database(self):
227-
c = rs_or_single_client("mongodb://%s/foo" % (client_context.pair, ),
227+
c = rs_or_single_client("mongodb://%s:%d/foo" % (client_context.host,
228+
client_context.port),
228229
connect=False)
229230
self.assertEqual(Database(c, 'foo'), c.get_default_database())
230231
# Test that default doesn't override the URI value.
@@ -239,36 +240,42 @@ def test_get_default_database(self):
239240
self.assertEqual(ReadPreference.SECONDARY, db.read_preference)
240241
self.assertEqual(write_concern, db.write_concern)
241242

242-
c = rs_or_single_client("mongodb://%s/" % (client_context.pair, ),
243+
c = rs_or_single_client("mongodb://%s:%d/" % (client_context.host,
244+
client_context.port),
243245
connect=False)
244246
self.assertEqual(Database(c, 'foo'), c.get_default_database('foo'))
245247

246248
def test_get_default_database_error(self):
247249
# URI with no database.
248-
c = rs_or_single_client("mongodb://%s/" % (client_context.pair, ),
250+
c = rs_or_single_client("mongodb://%s:%d/" % (client_context.host,
251+
client_context.port),
249252
connect=False)
250253
self.assertRaises(ConfigurationError, c.get_default_database)
251254

252255
def test_get_default_database_with_authsource(self):
253256
# Ensure we distinguish database name from authSource.
254-
uri = "mongodb://%s/foo?authSource=src" % (client_context.pair, )
257+
uri = "mongodb://%s:%d/foo?authSource=src" % (
258+
client_context.host, client_context.port)
255259
c = rs_or_single_client(uri, connect=False)
256260
self.assertEqual(Database(c, 'foo'), c.get_default_database())
257261

258262
def test_get_database_default(self):
259-
c = rs_or_single_client("mongodb://%s/foo" % (client_context.pair, ),
263+
c = rs_or_single_client("mongodb://%s:%d/foo" % (client_context.host,
264+
client_context.port),
260265
connect=False)
261266
self.assertEqual(Database(c, 'foo'), c.get_database())
262267

263268
def test_get_database_default_error(self):
264269
# URI with no database.
265-
c = rs_or_single_client("mongodb://%s/" % (client_context.pair, ),
270+
c = rs_or_single_client("mongodb://%s:%d/" % (client_context.host,
271+
client_context.port),
266272
connect=False)
267273
self.assertRaises(ConfigurationError, c.get_database)
268274

269275
def test_get_database_default_with_authsource(self):
270276
# Ensure we distinguish database name from authSource.
271-
uri = "mongodb://%s/foo?authSource=src" % (client_context.pair, )
277+
uri = "mongodb://%s:%d/foo?authSource=src" % (
278+
client_context.host, client_context.port)
272279
c = rs_or_single_client(uri, connect=False)
273280
self.assertEqual(Database(c, 'foo'), c.get_database())
274281

@@ -365,9 +372,10 @@ def test_uri_codec_options(self):
365372
# Ensure codec options are passed in correctly
366373
uuid_representation_label = 'javaLegacy'
367374
unicode_decode_error_handler = 'ignore'
368-
uri = ("mongodb://%s/foo?tz_aware=true&uuidrepresentation="
375+
uri = ("mongodb://%s:%d/foo?tz_aware=true&uuidrepresentation="
369376
"%s&unicode_decode_error_handler=%s" % (
370-
client_context.pair,
377+
client_context.host,
378+
client_context.port,
371379
uuid_representation_label,
372380
unicode_decode_error_handler))
373381
c = MongoClient(uri, connect=False)
@@ -1362,8 +1370,8 @@ def init(self, *args):
13621370
try:
13631371
ServerHeartbeatStartedEvent.__init__ = init
13641372
listener = HeartbeatStartedListener()
1365-
uri = "mongodb://%s/?heartbeatFrequencyMS=500" % (
1366-
client_context.pair, )
1373+
uri = "mongodb://%s:%d/?heartbeatFrequencyMS=500" % (
1374+
client_context.host, client_context.port)
13671375
client = single_client(uri, event_listeners=[listener])
13681376
wait_until(lambda: len(listener.results) >= 2,
13691377
"record two ServerHeartbeatStartedEvents")

0 commit comments

Comments
 (0)