Skip to content

Commit 4353278

Browse files
authored
PYTHON-5207 Convert mod_wsgi tests to use the new test runner (#2202)
1 parent 72ed102 commit 4353278

File tree

12 files changed

+195
-120
lines changed

12 files changed

+195
-120
lines changed

.evergreen/config.yml

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,6 @@ functions:
227227
args:
228228
- ${DRIVERS_TOOLS}/.evergreen/atlas_data_lake/run-mongohouse-image.sh
229229

230-
"run mod_wsgi tests":
231-
- command: subprocess.exec
232-
type: test
233-
params:
234-
include_expansions_in_env: [MOD_WSGI_VERSION, MOD_WSGI_EMBEDDED, "PYTHON_BINARY"]
235-
working_dir: "src"
236-
binary: bash
237-
args:
238-
- .evergreen/scripts/run-with-env.sh
239-
- .evergreen/scripts/run-mod-wsgi-tests.sh
240-
241230
"run doctests":
242231
- command: subprocess.exec
243232
type: test
@@ -412,40 +401,6 @@ tasks:
412401
TEST_NAME: index_management
413402
AUTH: "auth"
414403

415-
- name: "mod-wsgi-standalone"
416-
tags: ["mod_wsgi"]
417-
commands:
418-
- func: "run server"
419-
vars:
420-
TOPOLOGY: "server"
421-
- func: "run mod_wsgi tests"
422-
423-
- name: "mod-wsgi-replica-set"
424-
tags: ["mod_wsgi"]
425-
commands:
426-
- func: "run server"
427-
vars:
428-
TOPOLOGY: "replica_set"
429-
- func: "run mod_wsgi tests"
430-
431-
- name: "mod-wsgi-embedded-mode-standalone"
432-
tags: ["mod_wsgi"]
433-
commands:
434-
- func: "run server"
435-
- func: "run mod_wsgi tests"
436-
vars:
437-
MOD_WSGI_EMBEDDED: "1"
438-
439-
- name: "mod-wsgi-embedded-mode-replica-set"
440-
tags: ["mod_wsgi"]
441-
commands:
442-
- func: "run server"
443-
vars:
444-
TOPOLOGY: "replica_set"
445-
- func: "run mod_wsgi tests"
446-
vars:
447-
MOD_WSGI_EMBEDDED: "1"
448-
449404
- name: "no-server"
450405
tags: ["no-server"]
451406
commands:

.evergreen/generated_configs/tasks.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,48 @@ tasks:
775775
TEST_NAME: load_balancer
776776
tags: [load-balancer, noauth, nossl]
777777

778+
# Mod wsgi tests
779+
- name: mod-wsgi-standalone
780+
commands:
781+
- func: run server
782+
vars:
783+
TOPOLOGY: standalone
784+
- func: run tests
785+
vars:
786+
TEST_NAME: mod_wsgi
787+
SUB_TEST_NAME: standalone
788+
tags: [mod_wsgi]
789+
- name: mod-wsgi-replica-set
790+
commands:
791+
- func: run server
792+
vars:
793+
TOPOLOGY: replica_set
794+
- func: run tests
795+
vars:
796+
TEST_NAME: mod_wsgi
797+
SUB_TEST_NAME: standalone
798+
tags: [mod_wsgi]
799+
- name: mod-wsgi-embedded-mode-standalone
800+
commands:
801+
- func: run server
802+
vars:
803+
TOPOLOGY: standalone
804+
- func: run tests
805+
vars:
806+
TEST_NAME: mod_wsgi
807+
SUB_TEST_NAME: embedded
808+
tags: [mod_wsgi]
809+
- name: mod-wsgi-embedded-mode-replica-set
810+
commands:
811+
- func: run server
812+
vars:
813+
TOPOLOGY: replica_set
814+
- func: run tests
815+
vars:
816+
TEST_NAME: mod_wsgi
817+
SUB_TEST_NAME: embedded
818+
tags: [mod_wsgi]
819+
778820
# Ocsp tests
779821
- name: test-ocsp-ecdsa-valid-cert-server-does-not-staple
780822
commands:

.evergreen/generated_configs/variants.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -696,10 +696,7 @@ buildvariants:
696696
# Mod wsgi tests
697697
- name: mod_wsgi-ubuntu-22-python3.9
698698
tasks:
699-
- name: mod-wsgi-standalone
700-
- name: mod-wsgi-replica-set
701-
- name: mod-wsgi-embedded-mode-standalone
702-
- name: mod-wsgi-embedded-mode-replica-set
699+
- name: .mod_wsgi
703700
display_name: mod_wsgi Ubuntu-22 Python3.9
704701
run_on:
705702
- ubuntu2204-small
@@ -708,10 +705,7 @@ buildvariants:
708705
PYTHON_BINARY: /opt/python/3.9/bin/python3
709706
- name: mod_wsgi-ubuntu-22-python3.13
710707
tasks:
711-
- name: mod-wsgi-standalone
712-
- name: mod-wsgi-replica-set
713-
- name: mod-wsgi-embedded-mode-standalone
714-
- name: mod-wsgi-embedded-mode-replica-set
708+
- name: .mod_wsgi
715709
display_name: mod_wsgi Ubuntu-22 Python3.13
716710
run_on:
717711
- ubuntu2204-small

.evergreen/scripts/generate_config.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -614,12 +614,7 @@ def create_atlas_data_lake_variants():
614614
def create_mod_wsgi_variants():
615615
variants = []
616616
host = HOSTS["ubuntu22"]
617-
tasks = [
618-
"mod-wsgi-standalone",
619-
"mod-wsgi-replica-set",
620-
"mod-wsgi-embedded-mode-standalone",
621-
"mod-wsgi-embedded-mode-replica-set",
622-
]
617+
tasks = [".mod_wsgi"]
623618
expansions = dict(MOD_WSGI_VERSION="4")
624619
for python in MIN_MAX_PYTHON:
625620
display_name = get_display_name("mod_wsgi", host, python=python)
@@ -892,6 +887,24 @@ def create_oidc_tasks():
892887
return tasks
893888

894889

890+
def create_mod_wsgi_tasks():
891+
tasks = []
892+
for test, topology in product(["standalone", "embedded-mode"], ["standalone", "replica_set"]):
893+
if test == "standalone":
894+
task_name = "mod-wsgi-"
895+
else:
896+
task_name = "mod-wsgi-embedded-mode-"
897+
task_name += topology.replace("_", "-")
898+
server_vars = dict(TOPOLOGY=topology)
899+
server_func = FunctionCall(func="run server", vars=server_vars)
900+
vars = dict(TEST_NAME="mod_wsgi", SUB_TEST_NAME=test.split("-")[0])
901+
test_func = FunctionCall(func="run tests", vars=vars)
902+
tags = ["mod_wsgi"]
903+
commands = [server_func, test_func]
904+
tasks.append(EvgTask(name=task_name, tags=tags, commands=commands))
905+
return tasks
906+
907+
895908
def _create_ocsp_task(algo, variant, server_type, base_task_name):
896909
file_name = f"{algo}-basic-tls-ocsp-{variant}.json"
897910

.evergreen/scripts/mod_wsgi_tester.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
from __future__ import annotations
2+
3+
import os
4+
import sys
5+
import time
6+
import urllib.error
7+
import urllib.request
8+
from pathlib import Path
9+
from shutil import which
10+
11+
from utils import LOGGER, ROOT, run_command, write_env
12+
13+
14+
def make_request(url, timeout=10):
15+
for _ in range(int(timeout)):
16+
try:
17+
urllib.request.urlopen(url) # noqa: S310
18+
return
19+
except urllib.error.HTTPError:
20+
pass
21+
time.sleep(1)
22+
raise TimeoutError(f"Failed to access {url}")
23+
24+
25+
def setup_mod_wsgi(sub_test_name: str) -> None:
26+
env = os.environ.copy()
27+
if sub_test_name == "embedded":
28+
env["MOD_WSGI_CONF"] = "mod_wsgi_test_embedded.conf"
29+
elif sub_test_name == "standalone":
30+
env["MOD_WSGI_CONF"] = "mod_wsgi_test.conf"
31+
else:
32+
raise ValueError("mod_wsgi sub test must be either 'standalone' or 'embedded'")
33+
write_env("MOD_WSGI_CONF", env["MOD_WSGI_CONF"])
34+
apache = which("apache2")
35+
if not apache and Path("/usr/lib/apache2/mpm-prefork/apache2").exists():
36+
apache = "/usr/lib/apache2/mpm-prefork/apache2"
37+
if apache:
38+
apache_config = "apache24ubuntu161404.conf"
39+
else:
40+
apache = which("httpd")
41+
if not apache:
42+
raise ValueError("Could not find apache2 or httpd")
43+
apache_config = "apache22amazon.conf"
44+
python_version = ".".join(str(val) for val in sys.version_info[:2])
45+
mod_wsgi_version = 4
46+
so_file = f"/opt/python/mod_wsgi/python_version/{python_version}/mod_wsgi_version/{mod_wsgi_version}/mod_wsgi.so"
47+
write_env("MOD_WSGI_SO", so_file)
48+
env["MOD_WSGI_SO"] = so_file
49+
env["PYTHONHOME"] = f"/opt/python/{python_version}"
50+
env["PROJECT_DIRECTORY"] = project_directory = str(ROOT)
51+
write_env("APACHE_BINARY", apache)
52+
write_env("APACHE_CONFIG", apache_config)
53+
uri1 = f"http://localhost:8080/interpreter1{project_directory}"
54+
write_env("TEST_URI1", uri1)
55+
uri2 = f"http://localhost:8080/interpreter2{project_directory}"
56+
write_env("TEST_URI2", uri2)
57+
run_command(f"{apache} -k start -f {ROOT}/test/mod_wsgi_test/{apache_config}", env=env)
58+
59+
# Wait for the endpoints to be available.
60+
try:
61+
make_request(uri1, 10)
62+
make_request(uri2, 10)
63+
except Exception as e:
64+
LOGGER.error(Path("error_log").read_text())
65+
raise e
66+
67+
68+
def test_mod_wsgi() -> None:
69+
sys.path.insert(0, ROOT)
70+
from test.mod_wsgi_test.test_client import main, parse_args
71+
72+
uri1 = os.environ["TEST_URI1"]
73+
uri2 = os.environ["TEST_URI2"]
74+
args = f"-n 25000 -t 100 parallel {uri1} {uri2}"
75+
try:
76+
main(*parse_args(args.split()))
77+
78+
args = f"-n 25000 serial {uri1} {uri2}"
79+
main(*parse_args(args.split()))
80+
except Exception as e:
81+
LOGGER.error(Path("error_log").read_text())
82+
raise e
83+
84+
85+
def teardown_mod_wsgi() -> None:
86+
apache = os.environ["APACHE_BINARY"]
87+
apache_config = os.environ["APACHE_CONFIG"]
88+
89+
run_command(f"{apache} -k stop -f {ROOT}/test/mod_wsgi_test/{apache_config}")
90+
91+
92+
if __name__ == "__main__":
93+
setup_mod_wsgi()

.evergreen/scripts/run-mod-wsgi-tests.sh

Lines changed: 0 additions & 53 deletions
This file was deleted.

.evergreen/scripts/run_tests.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ def run() -> None:
100100
if TEST_PERF:
101101
start_time = datetime.now()
102102

103+
# Run mod_wsgi tests using the helper.
104+
if TEST_NAME == "mod_wsgi":
105+
from mod_wsgi_tester import test_mod_wsgi
106+
107+
test_mod_wsgi()
108+
return
109+
103110
# Send kms tests to run remotely.
104111
if TEST_NAME == "kms" and SUB_TEST_NAME in ["azure", "gcp"]:
105112
from kms_tester import test_kms_send_to_remote

.evergreen/scripts/setup_tests.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ def handle_test_env() -> None:
251251
cmd = f'bash "{DRIVERS_TOOLS}/.evergreen/run-load-balancer.sh" start'
252252
run_command(cmd)
253253

254+
if test_name == "mod_wsgi":
255+
from mod_wsgi_tester import setup_mod_wsgi
256+
257+
setup_mod_wsgi(sub_test_name)
258+
254259
if test_name == "ocsp":
255260
if sub_test_name:
256261
os.environ["OCSP_SERVER_TYPE"] = sub_test_name
@@ -381,7 +386,7 @@ def handle_test_env() -> None:
381386
# Use --capture=tee-sys so pytest prints test output inline:
382387
# https://docs.pytest.org/en/stable/how-to/capture-stdout-stderr.html
383388
TEST_ARGS = f"-v --capture=tee-sys --durations=5 {TEST_ARGS}"
384-
TEST_SUITE = TEST_SUITE_MAP[test_name]
389+
TEST_SUITE = TEST_SUITE_MAP.get(test_name)
385390
if TEST_SUITE:
386391
TEST_ARGS = f"-m {TEST_SUITE} {TEST_ARGS}"
387392

.evergreen/scripts/teardown_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,10 @@
4444
elif TEST_NAME == "auth_aws" and sys.platform != "darwin":
4545
run_command(f"bash {DRIVERS_TOOLS}/.evergreen/auth_aws/teardown.sh")
4646

47+
# Tear down mog_wsgi if applicable.
48+
elif TEST_NAME == "mod_wsgi":
49+
from mod_wsgi_tester import teardown_mod_wsgi
50+
51+
teardown_mod_wsgi()
52+
4753
LOGGER.info(f"Tearing down tests of type '{TEST_NAME}'... done.")

.evergreen/scripts/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ class Distro:
5050
}
5151

5252
# Tests that require a sub test suite.
53-
SUB_TEST_REQUIRED = ["auth_aws", "auth_oidc", "kms"]
53+
SUB_TEST_REQUIRED = ["auth_aws", "auth_oidc", "kms", "mod_wsgi"]
54+
55+
EXTRA_TESTS = ["mod_wsgi"]
5456

5557

5658
def get_test_options(
@@ -62,7 +64,7 @@ def get_test_options(
6264
if require_sub_test_name:
6365
parser.add_argument(
6466
"test_name",
65-
choices=sorted(TEST_SUITE_MAP),
67+
choices=sorted(list(TEST_SUITE_MAP) + EXTRA_TESTS),
6668
nargs="?",
6769
default="default",
6870
help="The optional name of the test suite to set up, typically the same name as a pytest marker.",

0 commit comments

Comments
 (0)