|
| 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() |
0 commit comments