9494COMPRESSORS = os .environ .get ("COMPRESSORS" )
9595MONGODB_API_VERSION = os .environ .get ("MONGODB_API_VERSION" )
9696TEST_LOADBALANCER = bool (os .environ .get ("TEST_LOADBALANCER" ))
97+ TEST_SERVERLESS = bool (os .environ .get ("TEST_SERVERLESS" ))
9798SINGLE_MONGOS_LB_URI = os .environ .get ("SINGLE_MONGOS_LB_URI" )
9899MULTI_MONGOS_LB_URI = os .environ .get ("MULTI_MONGOS_LB_URI" )
99100if TEST_LOADBALANCER :
104105 host , port = res ['nodelist' ][0 ]
105106 db_user = res ['username' ] or db_user
106107 db_pwd = res ['password' ] or db_pwd
108+ elif TEST_SERVERLESS :
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
114+ TLS_OPTIONS = {'tls' : True }
107115
108116
109117def is_server_resolvable ():
@@ -231,6 +239,7 @@ def __init__(self):
231239 self .conn_lock = threading .Lock ()
232240 self .is_data_lake = False
233241 self .load_balancer = TEST_LOADBALANCER
242+ self .serverless = TEST_SERVERLESS
234243 if self .load_balancer :
235244 self .default_client_options ["loadBalanced" ] = True
236245 if COMPRESSORS :
@@ -309,22 +318,26 @@ def _init_client(self):
309318 if self .client :
310319 self .connected = True
311320
312- try :
313- self .cmd_line = self .client .admin .command ('getCmdLineOpts' )
314- except pymongo .errors .OperationFailure as e :
315- msg = e .details .get ('errmsg' , '' )
316- if e .code == 13 or 'unauthorized' in msg or 'login' in msg :
317- # Unauthorized.
318- self .auth_enabled = True
319- else :
320- raise
321+ if self .serverless :
322+ self .auth_enabled = True
321323 else :
322- self .auth_enabled = self ._server_started_with_auth ()
324+ try :
325+ self .cmd_line = self .client .admin .command ('getCmdLineOpts' )
326+ except pymongo .errors .OperationFailure as e :
327+ msg = e .details .get ('errmsg' , '' )
328+ if e .code == 13 or 'unauthorized' in msg or 'login' in msg :
329+ # Unauthorized.
330+ self .auth_enabled = True
331+ else :
332+ raise
333+ else :
334+ self .auth_enabled = self ._server_started_with_auth ()
323335
324336 if self .auth_enabled :
325- # See if db_user already exists.
326- if not self ._check_user_provided ():
327- _create_user (self .client .admin , db_user , db_pwd )
337+ if not self .serverless :
338+ # See if db_user already exists.
339+ if not self ._check_user_provided ():
340+ _create_user (self .client .admin , db_user , db_pwd )
328341
329342 self .client = self ._connect (
330343 host , port , username = db_user , password = db_pwd ,
@@ -334,10 +347,13 @@ def _init_client(self):
334347 # May not have this if OperationFailure was raised earlier.
335348 self .cmd_line = self .client .admin .command ('getCmdLineOpts' )
336349
337- self .server_status = self .client .admin .command ('serverStatus' )
338- if self .storage_engine == "mmapv1" :
339- # MMAPv1 does not support retryWrites=True.
340- self .default_client_options ['retryWrites' ] = False
350+ if self .serverless :
351+ self .server_status = {}
352+ else :
353+ self .server_status = self .client .admin .command ('serverStatus' )
354+ if self .storage_engine == "mmapv1" :
355+ # MMAPv1 does not support retryWrites=True.
356+ self .default_client_options ['retryWrites' ] = False
341357
342358 ismaster = self .ismaster
343359 self .sessions_enabled = 'logicalSessionTimeoutMinutes' in ismaster
@@ -374,33 +390,41 @@ def _init_client(self):
374390 self .nodes = set ([(host , port )])
375391 self .w = len (ismaster .get ("hosts" , [])) or 1
376392 self .version = Version .from_client (self .client )
377- self .server_parameters = self .client .admin .command (
378- 'getParameter' , '*' )
379393
380- if 'enableTestCommands=1' in self . cmd_line [ 'argv' ] :
394+ if TEST_SERVERLESS :
381395 self .test_commands_enabled = True
382- elif 'parsed' in self .cmd_line :
383- params = self .cmd_line ['parsed' ].get ('setParameter' , [])
384- if 'enableTestCommands=1' in params :
396+ self .has_ipv6 = False
397+ else :
398+ self .server_parameters = self .client .admin .command (
399+ 'getParameter' , '*' )
400+ if 'enableTestCommands=1' in self .cmd_line ['argv' ]:
385401 self .test_commands_enabled = True
386- else :
387- params = self .cmd_line ['parsed' ].get ('setParameter' , {} )
388- if params . get ( 'enableTestCommands' ) == '1' :
402+ elif 'parsed' in self . cmd_line :
403+ params = self .cmd_line ['parsed' ].get ('setParameter' , [] )
404+ if 'enableTestCommands=1' in params :
389405 self .test_commands_enabled = True
406+ else :
407+ params = self .cmd_line ['parsed' ].get ('setParameter' , {})
408+ if params .get ('enableTestCommands' ) == '1' :
409+ self .test_commands_enabled = True
410+ self .has_ipv6 = self ._server_started_with_ipv6 ()
390411
391412 self .is_mongos = (self .ismaster .get ('msg' ) == 'isdbgrid' )
392- self .has_ipv6 = self ._server_started_with_ipv6 ()
393413 if self .is_mongos :
394- # Check for another mongos on the next port.
395- address = self .client .address
396- next_address = address [0 ], address [1 ] + 1
397- self .mongoses .append (address )
398- mongos_client = self ._connect (* next_address ,
399- ** self .default_client_options )
400- if mongos_client :
401- ismaster = mongos_client .admin .command ('ismaster' )
402- if ismaster .get ('msg' ) == 'isdbgrid' :
403- self .mongoses .append (next_address )
414+ if self .serverless :
415+ self .mongoses .append (self .client .address )
416+ self .mongoses .extend (additional_serverless_mongoses )
417+ else :
418+ # Check for another mongos on the next port.
419+ address = self .client .address
420+ next_address = address [0 ], address [1 ] + 1
421+ self .mongoses .append (address )
422+ mongos_client = self ._connect (
423+ * next_address , ** self .default_client_options )
424+ if mongos_client :
425+ ismaster = mongos_client .admin .command ('ismaster' )
426+ if ismaster .get ('msg' ) == 'isdbgrid' :
427+ self .mongoses .append (next_address )
404428
405429 def init (self ):
406430 with self .conn_lock :
@@ -891,6 +915,9 @@ def setUpClass(cls):
891915 if (client_context .load_balancer and
892916 not getattr (cls , 'RUN_ON_LOAD_BALANCER' , False )):
893917 raise SkipTest ('this test does not support load balancers' )
918+ if (client_context .serverless and
919+ not getattr (cls , 'RUN_ON_SERVERLESS' , False )):
920+ raise SkipTest ('this test does not support serverless' )
894921 cls .client = client_context .client
895922 cls .db = cls .client .pymongo_test
896923 if client_context .auth_enabled :
0 commit comments