44import subprocess
55import sys
66import time
7- from pymongo import MongoClient
8- from pymongo .errors import ConnectionFailure , OperationFailure
97
108# Constants
119VERSIONS = ["5" , "7" , "8" ]
1412SKIP_DIRECTORY = "skip"
1513COMMON_SKIP_FILE = "common.txt"
1614LOG_FILE_PATH = os .path .join (LOG_DIRECTORY , LOG_FILENAME )
17- TEST_DIRECTORY = os .path .join (os .getcwd (), "mongo/jstests" )
1815COMMON_LIST = os .path .join (SKIP_DIRECTORY , COMMON_SKIP_FILE )
16+ DOCKER_DIRECTORY = "/jstests"
1917
2018# User Variables (Consider moving these to a config file or environment variables)
2119# Note: No username/password required for MaxScale test
22- MONGO_HOST = "127.0.0.1"
23- MONGO_PORT = 27017
24- MONGO_USERNAME = " admin"
25- MONGO_PASSWORD = " password"
20+ MONGO_HOST = os . environ . get ( "MONGO_HOST" , "host.docker.internal" )
21+ MONGO_PORT = int ( os . environ . get ( "MONGO_PORT" , 27017 ))
22+ MONGO_USERNAME = os . environ . get ( "MONGO_USERNAME" , " admin")
23+ MONGO_PASSWORD = os . environ . get ( "MONGO_PASSWORD" , " password")
2624AUTHENTICATION_DATABASE = "admin"
2725AUTHENTICATION_MECHANISM = "SCRAM-SHA-1"
2826USE_SSL = "false"
2927LOAD_BALANCE = "false"
28+ TEST_DIRECTORY = os .environ .get ("TEST_DIRECTORY" , os .path .join (os .getcwd (), "mongo/jstests" ))
29+ DOCKER_COMMAND = f"docker compose exec legacy-mongo mongo" # requires envVar set for compose file such as `export COMPOSE_FILE=mongo5.yml`
3030
3131if MONGO_USERNAME and MONGO_PASSWORD :
3232 creds = f"{ MONGO_USERNAME } :{ MONGO_PASSWORD } @"
@@ -49,17 +49,22 @@ def check_list_existence(file_path, file_description):
4949
5050# Validate MongoDB connectivity and credentials.
5151def validate_connection (uri ):
52+ ping_command = DOCKER_COMMAND .split () + ["--eval" , "db.runCommand({ping: 1})" , uri ]
5253 try :
53- client = MongoClient (uri , serverSelectionTimeoutMS = 5000 )
54- client [AUTHENTICATION_DATABASE ].command ("ping" )
54+ subprocess .run (
55+ ping_command ,
56+ check = True ,
57+ stdout = subprocess .PIPE ,
58+ stderr = subprocess .STDOUT ,
59+ timeout = 30 ,
60+ )
5561 print ("Connected successfully to database" )
56-
57- except ConnectionFailure :
58- print ("\033 [91mFailed to connect to database\033 [0m" )
62+ except subprocess .TimeoutExpired :
63+ print ("\033 [91mFailed to connect to database: timeout \033 [0m" )
5964 sys .exit (1 )
60-
61- except OperationFailure :
62- print ("\033 [91mAuthentication failed for database \033 [0m" )
65+ except subprocess . CalledProcessError as e :
66+ msg = f"Failed to connect to database: { e . output } "
67+ print ("\033 [91m" + msg + " \033 [0m" )
6368 sys .exit (1 )
6469
6570
@@ -70,13 +75,14 @@ def create_mongo_uri():
7075
7176# Build the mongo command with the necessary arguments.
7277def build_mongo_command (script_path ):
73- mongo_command = [ "mongo" , f"mongodb://{ creds } { MONGO_HOST } :{ MONGO_PORT } /{ auth_part } " ]
78+ mongo_command = DOCKER_COMMAND . split () + [ f"mongodb://{ creds } { MONGO_HOST } :{ MONGO_PORT } /{ auth_part } " ]
7479 mongo_command .append (script_path )
7580 return mongo_command
7681
7782
7883# Run an individual script using subprocess.
7984def run_script (script_path ):
85+ script_path = os .path .join (DOCKER_DIRECTORY , script_path )
8086 mongo_command = build_mongo_command (script_path )
8187 # print(mongo_command)
8288 try :
@@ -92,25 +98,27 @@ def run_script(script_path):
9298 return False
9399
94100
95- # Find all JS files that are not in the exclusion lists.
101+ # Find all JS files that are not in the exclusion lists relative to the given directory .
96102def find_js_files (directory , mongo_version ):
97103 exclusions = set ()
98104 with open (COMMON_LIST , "r" ) as f :
99- exclusions .update ([TEST_DIRECTORY + line .strip () for line in f ])
105+ exclusions .update ([line .strip () for line in f ])
100106
101107 # Add specific mongo version exclusions.
102108 mongo_exclusions = f"{ SKIP_DIRECTORY } /mongo{ mongo_version } .txt"
103109 check_list_existence (mongo_exclusions , mongo_exclusions )
104110 with open (mongo_exclusions , "r" ) as f :
105- exclusions .update ([TEST_DIRECTORY + line .strip () for line in f ])
111+ exclusions .update ([line .strip () for line in f ])
106112
107113 js_files = []
108114 for root , dirs , files in os .walk (directory ):
109115 for file in files :
110116 if file .endswith (".js" ):
111117 full_path = os .path .join (root , file )
112- if full_path not in exclusions :
113- js_files .append (full_path )
118+ relative_path = os .path .relpath (full_path , directory )
119+ # exclusions files such as `skip/common.txt` contain path prefixed with "/"
120+ if os .path .join ("/" , relative_path ) not in exclusions :
121+ js_files .append (relative_path )
114122
115123 if not js_files :
116124 print (f"No JS files found in { directory } " )
0 commit comments