1- from importlib .util import find_spec
21import signal
32import sys
3+ from importlib .util import find_spec
44
55if find_spec ("docker" ) == None :
66 raise SystemExit (
7- f'ERROR (Missing dependency docker SDK): test script dependent on docker sdk for python \n run pip install docker to install' )
7+ f"ERROR (Missing dependency docker SDK): test script dependent on docker sdk for python \n run pip install docker to install"
8+ )
89
910import docker
1011from docker .errors import APIError , NotFound
1415
1516def handle_container_running_or_name_conflict (client , container_name ):
1617 container = client .containers .get (container_name )
17- state = container .attrs [' State' ]
18- status = state [' Status' ]
19- if status == ' exited' or status == ' running' :
20- print (f' Detected existing container: Removing { container_name } ' )
18+ state = container .attrs [" State" ]
19+ status = state [" Status" ]
20+ if status == " exited" or status == " running" :
21+ print (f" Detected existing container: Removing { container_name } " )
2122 container .remove (v = True , force = True )
2223
2324
2425def run_mongodb_container (client , container_name ):
26+ ulimits = [docker .types .Ulimit (name = "nofile" , soft = 65535 , hard = 65535 )]
2527 # starts up a mongodb container for test, must follow config setup in config.py
2628 return client .containers .run (
27- image = ' mongo:latest' ,
29+ image = " mongo:latest" ,
2830 command = "mongod --port 27018" ,
2931 network = "docker_backend" ,
3032 name = container_name ,
3133 detach = True ,
3234 hostname = "test_mongodb" ,
3335 auto_remove = True ,
34- remove = True
36+ remove = True ,
37+ ulimits = ulimits ,
3538 )
39+
40+
3641# function to start up a container for running test
3742def start_mongodb_container (client ):
3843 # handle error types
39- responses = {
40- 409 : handle_container_running_or_name_conflict
41- }
44+ responses = {409 : handle_container_running_or_name_conflict }
4245
4346 try :
44- print (f' Starting { container_name } ' )
47+ print (f" Starting { container_name } " )
4548 return run_mongodb_container (client , container_name )
4649 except APIError as e :
4750 # does not handle errors in handle func
4851 responses [e .status_code ](client , container_name )
49- print (f' Starting { container_name } ' )
52+ print (f" Starting { container_name } " )
5053 # do not handle error here, want program to stop on error
5154 return run_mongodb_container (client , container_name )
5255
@@ -57,12 +60,14 @@ def check_api_running(client):
5760 container = client .containers .get (api_container_name )
5861 except NotFound :
5962 raise SystemExit (
60- f'ERROR(Could not find { container_name } container): API container must be running to perform tests' )
63+ f"ERROR(Could not find { container_name } container): API container must be running to perform tests"
64+ )
6165
62- status = container .attrs [' State' ][ ' Status' ]
66+ status = container .attrs [" State" ][ " Status" ]
6367 if status != "running" :
6468 raise SystemExit (
65- f'ERROR (container status : { status } ): API container must be running to perform tests' )
69+ f"ERROR (container status : { status } ): API container must be running to perform tests"
70+ )
6671 return container
6772
6873
@@ -73,7 +78,7 @@ def setup_test_env(args):
7378 # run pytest without description warnings
7479 _ , stream = api_container .exec_run (f'pytest { " " .join (args )} ' , stream = True )
7580 for data in stream :
76- print (data .decode (), end = '' )
81+ print (data .decode (), end = "" )
7782
7883 # with auto_remove, local volumes should be pruned on stop
7984 mongodb_container .stop ()
@@ -84,7 +89,7 @@ def signal_handler(signum, frame):
8489 print ("\n Signal interrupt detected: Cleaning up container" )
8590 try :
8691 container = client .containers .get (container_name )
87- status = container .attrs [' State' ][ ' Status' ]
92+ status = container .attrs [" State" ][ " Status" ]
8893 if status == "running" :
8994 container .stop ()
9095 except :
0 commit comments