11import json
22import os
3+ from pymongo .mongo_client import MongoClient
34import pytest
45import requests
56import subprocess
89
910# settings come from the docker-compose file
1011
12+ MONGO_HOST = "localhost:27017"
13+ MONGO_DB = 'auth2_python_client_test'
14+
1115_COMPOSE_FILE = "docker-compose.yaml"
1216_COMPOSE_PROJECT_NAME = "auth_client_tests"
1317_AUTH_SERVICE_NAME = "auth"
@@ -61,13 +65,24 @@ def _run_dc(env, *args):
6165 )
6266
6367
68+ def _clear_auth_db ():
69+ mc = MongoClient (MONGO_HOST )
70+ db = mc [MONGO_DB ]
71+ # don't drop db since that drops indexes
72+ for name in db .list_collection_names ():
73+ if not name .startswith ("system." ):
74+ # don't drop collection since that drops indexes
75+ db .get_collection (name ).delete_many ({})
76+
77+
6478@pytest .fixture (scope = "session" , autouse = True )
6579def docker_compose ():
6680 env = os .environ .copy ()
6781 print ("Starting docker-compose..." )
6882 try :
6983 _run_dc (env , "up" , "-d" , "--build" )
7084 _wait_for_services ()
85+ _clear_auth_db () # in case the compose was left up
7186 yield # run the tests
7287 logarg = os .environ .get ("AUTH_TEST_DUMP_LOGS" )
7388 if logarg :
@@ -76,9 +91,9 @@ def docker_compose():
7691 else :
7792 _run_dc (env , "logs" )
7893 finally :
79- print ( "Stopping docker-compose..." )
80- # TODO TEST add a way to keep things running and be able to rerun tests
81- _run_dc (env , "down" )
94+ if not os . environ . get ( "AUTH_TEST_LEAVE_COMPOSE_UP" ):
95+ print ( "Stopping docker-compose..." )
96+ _run_dc (env , "down" )
8297
8398
8499@pytest .fixture (scope = "session" , autouse = True )
0 commit comments