|
| 1 | +from contextlib import contextmanager |
| 2 | +import os |
| 3 | +import signal |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | + |
| 7 | +from ddtrace.internal.utils.retry import RetryError |
| 8 | +from ddtrace.vendor import psutil |
| 9 | +from tests.webclient import Client |
| 10 | + |
| 11 | + |
| 12 | +ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 13 | +ROOT_PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 14 | + |
| 15 | + |
| 16 | +def _build_env(): |
| 17 | + environ = dict(PATH="%s:%s" % (ROOT_PROJECT_DIR, ROOT_DIR), PYTHONPATH="%s:%s" % (ROOT_PROJECT_DIR, ROOT_DIR)) |
| 18 | + if os.environ.get("PATH"): |
| 19 | + environ["PATH"] = "%s:%s" % (os.environ.get("PATH"), environ["PATH"]) |
| 20 | + if os.environ.get("PYTHONPATH"): |
| 21 | + environ["PYTHONPATH"] = "%s:%s" % (os.environ.get("PYTHONPATH"), environ["PYTHONPATH"]) |
| 22 | + return environ |
| 23 | + |
| 24 | + |
| 25 | +@contextmanager |
| 26 | +def gunicorn_server(appsec_enabled="true", remote_configuration_enabled="true", token=None): |
| 27 | + cmd = ["gunicorn", "-w", "3", "-b", "0.0.0.0:8000", "tests.appsec.app:app"] |
| 28 | + for res in appsec_application_server( |
| 29 | + cmd, appsec_enabled=appsec_enabled, remote_configuration_enabled=remote_configuration_enabled, token=token |
| 30 | + ): |
| 31 | + yield res |
| 32 | + |
| 33 | + |
| 34 | +@contextmanager |
| 35 | +def flask_server(appsec_enabled="true", remote_configuration_enabled="true", token=None): |
| 36 | + cmd = ["python", "tests/appsec/app.py", "--no-reload"] |
| 37 | + for res in appsec_application_server( |
| 38 | + cmd, appsec_enabled=appsec_enabled, remote_configuration_enabled=remote_configuration_enabled, token=token |
| 39 | + ): |
| 40 | + yield res |
| 41 | + |
| 42 | + |
| 43 | +def appsec_application_server(cmd, appsec_enabled="true", remote_configuration_enabled="true", token=None): |
| 44 | + env = _build_env() |
| 45 | + env["DD_REMOTE_CONFIG_POLL_INTERVAL_SECONDS"] = "0.5" |
| 46 | + env["DD_REMOTE_CONFIGURATION_ENABLED"] = remote_configuration_enabled |
| 47 | + if token: |
| 48 | + env["_DD_REMOTE_CONFIGURATION_ADDITIONAL_HEADERS"] = "X-Datadog-Test-Session-Token:%s," % (token,) |
| 49 | + if appsec_enabled: |
| 50 | + env["DD_APPSEC_ENABLED"] = appsec_enabled |
| 51 | + env["DD_TRACE_AGENT_URL"] = os.environ.get("DD_TRACE_AGENT_URL", "") |
| 52 | + |
| 53 | + server_process = subprocess.Popen( |
| 54 | + cmd, |
| 55 | + env=env, |
| 56 | + stdout=sys.stdout, |
| 57 | + stderr=sys.stderr, |
| 58 | + preexec_fn=os.setsid, |
| 59 | + ) |
| 60 | + try: |
| 61 | + client = Client("http://0.0.0.0:8000") |
| 62 | + |
| 63 | + try: |
| 64 | + print("Waiting for server to start") |
| 65 | + client.wait(max_tries=100, delay=0.1) |
| 66 | + print("Server started") |
| 67 | + except RetryError: |
| 68 | + raise AssertionError( |
| 69 | + "Server failed to start, see stdout and stderr logs" |
| 70 | + "\n=== Captured STDOUT ===\n%s=== End of captured STDOUT ===" |
| 71 | + "\n=== Captured STDERR ===\n%s=== End of captured STDERR ===" |
| 72 | + % (server_process.stdout, server_process.stderr) |
| 73 | + ) |
| 74 | + # If we run a Gunicorn application, we want to get the child's pid, see test_remoteconfiguration_e2e.py |
| 75 | + parent = psutil.Process(server_process.pid) |
| 76 | + children = parent.children(recursive=True) |
| 77 | + |
| 78 | + yield server_process, client, (children[1].pid if len(children) > 1 else None) |
| 79 | + try: |
| 80 | + client.get_ignored("/shutdown") |
| 81 | + except Exception: |
| 82 | + raise AssertionError( |
| 83 | + "\n=== Captured STDOUT ===\n%s=== End of captured STDOUT ===" |
| 84 | + "\n=== Captured STDERR ===\n%s=== End of captured STDERR ===" |
| 85 | + % (server_process.stdout, server_process.stderr) |
| 86 | + ) |
| 87 | + finally: |
| 88 | + os.killpg(os.getpgid(server_process.pid), signal.SIGTERM) |
| 89 | + server_process.wait() |
0 commit comments