1818import os
1919import socket
2020import sys
21+ import time
2122import warnings
2223
2324try :
@@ -88,14 +89,14 @@ def is_server_resolvable():
8889
8990
9091def _connect (host , port , ** kwargs ):
91- try :
92- client = pymongo . MongoClient (
93- host , port , serverSelectionTimeoutMS = 100 , ** kwargs )
94- client . admin . command ( 'ismaster' ) # Can we connect?
95- # If connected, then return client with default timeout
96- return pymongo . MongoClient ( host , port , ** kwargs )
97- except pymongo . errors . ConnectionFailure :
98- return None
92+ client = pymongo . MongoClient ( host , port , ** kwargs )
93+ start = time . time ()
94+ while not client . nodes :
95+ time . sleep ( 0.05 )
96+ if time . time () - start > 0.1 :
97+ return None
98+
99+ return client
99100
100101
101102class client_knobs (object ):
@@ -147,11 +148,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
147148
148149
149150def _all_users (db ):
150- if Version .from_client (db .client ).at_least (2 , 5 , 3 , - 1 ):
151- return set (u ['user' ]
152- for u in db .command ('usersInfo' ).get ('users' , []))
153- else :
154- return set (u ['user' ] for u in db .system .users .find ())
151+ return set (u ['user' ] for u in db .command ('usersInfo' ).get ('users' , []))
155152
156153
157154class ClientContext (object ):
@@ -190,32 +187,6 @@ def __init__(self):
190187
191188 if self .client :
192189 self .connected = True
193- ismaster = self .client .admin .command ('ismaster' )
194- self .sessions_enabled = 'logicalSessionTimeoutMinutes' in ismaster
195-
196- if 'setName' in ismaster :
197- self .replica_set_name = ismaster ['setName' ]
198- self .is_rs = True
199- # It doesn't matter which member we use as the seed here.
200- self .client = pymongo .MongoClient (
201- host ,
202- port ,
203- replicaSet = self .replica_set_name ,
204- ** self .ssl_client_options )
205- # Get the authoritative ismaster result from the primary.
206- self .ismaster = self .client .admin .command ('ismaster' )
207- nodes = [partition_node (node .lower ())
208- for node in self .ismaster .get ('hosts' , [])]
209- nodes .extend ([partition_node (node .lower ())
210- for node in self .ismaster .get ('passives' , [])])
211- nodes .extend ([partition_node (node .lower ())
212- for node in self .ismaster .get ('arbiters' , [])])
213- self .nodes = set (nodes )
214- else :
215- self .ismaster = ismaster
216- self .nodes = set ([(host , port )])
217- self .w = len (self .ismaster .get ("hosts" , [])) or 1
218- self .version = Version .from_client (self .client )
219190
220191 try :
221192 self .cmd_line = self .client .admin .command ('getCmdLineOpts' )
@@ -232,10 +203,7 @@ def __init__(self):
232203 if self .auth_enabled :
233204 # See if db_user already exists.
234205 if not self ._check_user_provided ():
235- roles = {}
236- if self .version .at_least (2 , 5 , 3 , - 1 ):
237- roles = {'roles' : ['root' ]}
238- self .client .admin .add_user (db_user , db_pwd , ** roles )
206+ self .client .admin .add_user (db_user , db_pwd , roles = ['root' ])
239207
240208 self .client = _connect (host ,
241209 port ,
@@ -247,6 +215,43 @@ def __init__(self):
247215 # May not have this if OperationFailure was raised earlier.
248216 self .cmd_line = self .client .admin .command ('getCmdLineOpts' )
249217
218+ self .ismaster = ismaster = self .client .admin .command ('isMaster' )
219+ self .sessions_enabled = 'logicalSessionTimeoutMinutes' in ismaster
220+
221+ if 'setName' in ismaster :
222+ self .replica_set_name = ismaster ['setName' ]
223+ self .is_rs = True
224+ if self .auth_enabled :
225+ # It doesn't matter which member we use as the seed here.
226+ self .client = pymongo .MongoClient (
227+ host ,
228+ port ,
229+ username = db_user ,
230+ password = db_pwd ,
231+ replicaSet = self .replica_set_name ,
232+ ** self .ssl_client_options )
233+ else :
234+ self .client = pymongo .MongoClient (
235+ host ,
236+ port ,
237+ replicaSet = self .replica_set_name ,
238+ ** self .ssl_client_options )
239+
240+ # Get the authoritative ismaster result from the primary.
241+ self .ismaster = self .client .admin .command ('ismaster' )
242+ nodes = [partition_node (node .lower ())
243+ for node in self .ismaster .get ('hosts' , [])]
244+ nodes .extend ([partition_node (node .lower ())
245+ for node in self .ismaster .get ('passives' , [])])
246+ nodes .extend ([partition_node (node .lower ())
247+ for node in self .ismaster .get ('arbiters' , [])])
248+ self .nodes = set (nodes )
249+ else :
250+ self .ismaster = ismaster
251+ self .nodes = set ([(host , port )])
252+ self .w = len (self .ismaster .get ("hosts" , [])) or 1
253+ self .version = Version .from_client (self .client )
254+
250255 if 'enableTestCommands=1' in self .cmd_line ['argv' ]:
251256 self .test_commands_enabled = True
252257 elif 'parsed' in self .cmd_line :
@@ -487,6 +492,7 @@ def require_sessions(self, func):
487492 "Sessions not supported" ,
488493 func = func )
489494
495+
490496# Reusable client context
491497client_context = ClientContext ()
492498
@@ -499,6 +505,10 @@ class IntegrationTest(unittest.TestCase):
499505 def setUpClass (cls ):
500506 cls .client = client_context .client
501507 cls .db = cls .client .pymongo_test
508+ if client_context .auth_enabled :
509+ cls .credentials = {'username' : db_user , 'password' : db_pwd }
510+ else :
511+ cls .credentials = {}
502512
503513
504514# Use assertRaisesRegex if available, otherwise use Python 2.7's
0 commit comments