|
| 1 | +import jinja2 |
| 2 | +import logging |
| 3 | +from OpenSSL import crypto |
| 4 | +import os |
| 5 | +import pytest |
| 6 | +import subprocess |
| 7 | +import time |
| 8 | + |
| 9 | + |
| 10 | +pytest_plugins = [ |
| 11 | + "trace_service", |
| 12 | +] |
| 13 | + |
| 14 | + |
| 15 | +def pytest_addoption(parser): |
| 16 | + parser.addoption("--nginx", required=True) |
| 17 | + parser.addoption("--module", required=True) |
| 18 | + parser.addoption("--otelcol") |
| 19 | + parser.addoption("--globals", default="") |
| 20 | + |
| 21 | + |
| 22 | +def self_signed_cert(test_dir, name): |
| 23 | + k = crypto.PKey() |
| 24 | + k.generate_key(crypto.TYPE_RSA, 2048) |
| 25 | + cert = crypto.X509() |
| 26 | + cert.get_subject().CN = name |
| 27 | + cert.set_issuer(cert.get_subject()) |
| 28 | + cert.gmtime_adj_notBefore(0) |
| 29 | + cert.gmtime_adj_notAfter(365 * 86400) # 365 days |
| 30 | + cert.set_pubkey(k) |
| 31 | + cert.sign(k, "sha512") |
| 32 | + (test_dir / f"{name}.key").write_text( |
| 33 | + crypto.dump_privatekey(crypto.FILETYPE_PEM, k).decode("utf-8") |
| 34 | + ) |
| 35 | + (test_dir / f"{name}.crt").write_text( |
| 36 | + crypto.dump_certificate(crypto.FILETYPE_PEM, cert).decode("utf-8") |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture(scope="session") |
| 41 | +def logger(): |
| 42 | + logging.basicConfig(level=logging.INFO) |
| 43 | + return logging.getLogger(__name__) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture(scope="module") |
| 47 | +def testdir(tmp_path_factory): |
| 48 | + return tmp_path_factory.mktemp("nginx") |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture(scope="module") |
| 52 | +def nginx_config(request, pytestconfig, testdir, logger): |
| 53 | + tmpl = jinja2.Environment().from_string(request.module.NGINX_CONFIG) |
| 54 | + params = getattr(request, "param", {}) |
| 55 | + params["globals"] = ( |
| 56 | + f"pid {testdir}/nginx.pid;\n" |
| 57 | + + "error_log stderr info;\n" |
| 58 | + + f"error_log {testdir}/error.log info;\n" |
| 59 | + + f"load_module {os.path.abspath(pytestconfig.option.module)};\n" |
| 60 | + + pytestconfig.option.globals |
| 61 | + ) |
| 62 | + params["http_globals"] = f"root {testdir};\n" + "access_log off;\n" |
| 63 | + conf = tmpl.render(params) |
| 64 | + logger.debug(conf) |
| 65 | + return conf |
| 66 | + |
| 67 | + |
| 68 | +@pytest.fixture(scope="module") |
| 69 | +def nginx(testdir, pytestconfig, nginx_config, certs, logger, otelcol): |
| 70 | + (testdir / "nginx.conf").write_text(nginx_config) |
| 71 | + logger.info("Starting nginx...") |
| 72 | + proc = subprocess.Popen( |
| 73 | + [ |
| 74 | + pytestconfig.option.nginx, |
| 75 | + "-p", |
| 76 | + str(testdir), |
| 77 | + "-c", |
| 78 | + "nginx.conf", |
| 79 | + "-e", |
| 80 | + "error.log", |
| 81 | + ] |
| 82 | + ) |
| 83 | + logger.debug(f"args={' '.join(proc.args)}") |
| 84 | + logger.debug(f"pid={proc.pid}") |
| 85 | + while not (testdir / "nginx.pid").exists(): |
| 86 | + time.sleep(0.1) |
| 87 | + assert proc.poll() is None, "Can't start nginx" |
| 88 | + yield proc |
| 89 | + logger.info("Stopping nginx...") |
| 90 | + proc.terminate() |
| 91 | + try: |
| 92 | + proc.wait(timeout=5) |
| 93 | + except subprocess.TimeoutExpired: |
| 94 | + proc.kill() |
| 95 | + assert "[alert]" not in (testdir / "error.log").read_text() |
| 96 | + |
| 97 | + |
| 98 | +@pytest.fixture(scope="module") |
| 99 | +def certs(testdir): |
| 100 | + self_signed_cert(testdir, "localhost") |
0 commit comments