Skip to content

Commit 08faf91

Browse files
authored
Use environmental variables and Docker container (#1)
1 parent f36fb5d commit 08faf91

File tree

6 files changed

+74
-21
lines changed

6 files changed

+74
-21
lines changed

ferret.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ services:
1515
environment:
1616
- FERRETDB_POSTGRESQL_URL=postgres://postgres:5432/ferretdb
1717

18+
legacy-mongo:
19+
image: mongodb/mongodb-community-server:5.0.30-ubuntu2004
20+
container_name: legacy-mongo
21+
command: sleep 86400
22+
extra_hosts:
23+
- "host.docker.internal:host-gateway"
24+
volumes:
25+
- ${TEST_DIRECTORY:-mongo/jstests}:/jstests
26+
1827
networks:
1928
default:
2029
name: ferretdb

maxscale.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ services:
1616
volumes:
1717
- ./config/maxscale.cnf:/etc/maxscale.cnf
1818

19+
legacy-mongo:
20+
image: mongodb/mongodb-community-server:5.0.30-ubuntu2004
21+
container_name: legacy-mongo
22+
command: sleep 86400
23+
extra_hosts:
24+
- "host.docker.internal:host-gateway"
25+
volumes:
26+
- ${TEST_DIRECTORY:-./mongo/jstests}:/jstests
27+
1928
networks:
2029
default:
2130
name: mariadb

mongo5.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ services:
1010
- MONGO_INITDB_ROOT_USERNAME=admin
1111
- MONGO_INITDB_ROOT_PASSWORD=password
1212

13+
legacy-mongo:
14+
image: mongodb/mongodb-community-server:5.0.30-ubuntu2004
15+
container_name: legacy-mongo
16+
command: sleep 86400
17+
extra_hosts:
18+
- "host.docker.internal:host-gateway"
19+
volumes:
20+
- ${TEST_DIRECTORY:-./mongo/jstests}:/jstests
21+
1322
networks:
1423
default:
1524
name: mongo

mongo7.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ services:
1010
- MONGO_INITDB_ROOT_USERNAME=admin
1111
- MONGO_INITDB_ROOT_PASSWORD=password
1212

13+
legacy-mongo:
14+
image: mongodb/mongodb-community-server:5.0.30-ubuntu2004
15+
container_name: legacy-mongo
16+
command: sleep 86400
17+
extra_hosts:
18+
- "host.docker.internal:host-gateway"
19+
volumes:
20+
- ${TEST_DIRECTORY:-./mongo/jstests}:/jstests
21+
1322
networks:
1423
default:
1524
name: mongo

mongo8.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ services:
1010
- MONGO_INITDB_ROOT_USERNAME=admin
1111
- MONGO_INITDB_ROOT_PASSWORD=password
1212

13+
legacy-mongo:
14+
image: mongodb/mongodb-community-server:5.0.30-ubuntu2004
15+
container_name: legacy-mongo
16+
command: sleep 86400
17+
extra_hosts:
18+
- "host.docker.internal:host-gateway"
19+
volumes:
20+
- ${TEST_DIRECTORY:-./mongo/jstests}:/jstests
21+
1322
networks:
1423
default:
1524
name: mongo

run.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import subprocess
55
import sys
66
import time
7-
from pymongo import MongoClient
8-
from pymongo.errors import ConnectionFailure, OperationFailure
97

108
# Constants
119
VERSIONS = ["5", "7", "8"]
@@ -14,19 +12,21 @@
1412
SKIP_DIRECTORY = "skip"
1513
COMMON_SKIP_FILE = "common.txt"
1614
LOG_FILE_PATH = os.path.join(LOG_DIRECTORY, LOG_FILENAME)
17-
TEST_DIRECTORY = os.path.join(os.getcwd(), "mongo/jstests")
1815
COMMON_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")
2624
AUTHENTICATION_DATABASE = "admin"
2725
AUTHENTICATION_MECHANISM = "SCRAM-SHA-1"
2826
USE_SSL = "false"
2927
LOAD_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

3131
if 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.
5151
def 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.
7277
def 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.
7984
def 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.
96102
def 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

Comments
 (0)