Skip to content

Commit

Permalink
test: switch to pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
jimf5 committed Dec 6, 2023
1 parent fc7e69a commit 777bb84
Show file tree
Hide file tree
Showing 10 changed files with 901 additions and 2,007 deletions.
37 changes: 8 additions & 29 deletions .github/workflows/nginx-otel-module-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ jobs:
with:
name: nginx-otel-module
path: build/ngx_otel_module.so
- name: Archive protoc and opentelemetry-proto
uses: actions/upload-artifact@v3
with:
name: protoc-opentelemetry-proto
path: |
build/_deps/grpc-build/third_party/protobuf/protoc
build/_deps/otelcpp-src/third_party/opentelemetry-proto
test-module:
needs: build-module
runs-on: ubuntu-latest
Expand All @@ -47,36 +40,22 @@ jobs:
with:
name: nginx-otel-module
path: build
- name: Download protoc and opentelemetry-proto
uses: actions/download-artifact@v3
with:
name: protoc-opentelemetry-proto
path: build/_deps
- name: List files
run: ls -laR .
- name: Fix protoc file permissions
run: chmod +x build/_deps/grpc-build/third_party/protobuf/protoc
- name: Install perl modules
run: sudo cpan IO::Socket::SSL Crypt::Misc
- name: Download otelcol
run: |
curl -LO https://github.com/\
open-telemetry/opentelemetry-collector-releases/releases/download/\
v0.76.1/otelcol_0.76.1_linux_amd64.tar.gz
tar -xzf otelcol_0.76.1_linux_amd64.tar.gz
- name: Checkout nginx and nginx-test
- name: Checkout nginx
run: |
hg clone http://hg.nginx.org/nginx/
hg clone http://hg.nginx.org/nginx-tests/
- name: Build nginx
working-directory: nginx
run: |
auto/configure --with-compat --with-debug --with-http_ssl_module \
--with-http_v2_module --with-http_v3_module
make -j 4
- name: Install test dependecies
working-directory: tests
run: |
pip install -r requirements.txt
- name: Run tests
working-directory: tests
run: |
PERL5LIB=../nginx-tests/lib TEST_NGINX_UNSAFE=1 \
TEST_NGINX_VERBOSE=1 TEST_NGINX_GLOBALS="load_module \
${PWD}/../build/ngx_otel_module.so;" prove -v .
PYTHONPATH=${PWD} TEST_NGINX_GLOBALS=\
"load_module ${PWD}/../build/ngx_otel_module.so;" \
pytest --log-cli-level=debug
107 changes: 107 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from jinja2 import Environment
import logging
from OpenSSL import crypto
import os
import pytest
import subprocess
import time


pytest_plugins = [
"otelcol_fixtures",
]

NGINX_BINARY = os.getenv("TEST_NGINX_BINARY", "../nginx/objs/nginx")
CAPABILITIES = subprocess.check_output(
[NGINX_BINARY, "-V"], stderr=subprocess.STDOUT
).decode("utf-8")


def self_signed_cert(test_dir):
name = "localhost"
k = crypto.PKey()
k.generate_key(crypto.TYPE_RSA, 2048)
cert = crypto.X509()
cert.get_subject().CN = name
cert.set_issuer(cert.get_subject())
cert.gmtime_adj_notBefore(0)
cert.gmtime_adj_notAfter(365 * 86400) # 365 days
cert.set_pubkey(k)
cert.sign(k, "sha512")
(test_dir / f"{name}.key").write_text(
crypto.dump_privatekey(crypto.FILETYPE_PEM, k).decode("utf-8")
)
(test_dir / f"{name}.crt").write_text(
crypto.dump_certificate(crypto.FILETYPE_PEM, cert).decode("utf-8")
)


@pytest.fixture(scope="session")
def logger():
logging.basicConfig(level=logging.INFO)
return logging.getLogger(__name__)


@pytest.fixture(scope="module")
def testdir(tmp_path_factory):
return tmp_path_factory.mktemp("nginx")


@pytest.fixture(scope="module")
def nginx_config(request, testdir, logger):
tmpl = Environment().from_string(request.module.NGINX_CONFIG)
settings = request.param
test_globals = (
f"pid {testdir}/nginx.pid;\nerror_log {testdir}/error.log debug;\n"
)
test_globals += os.getenv("TEST_NGINX_GLOBALS", "")
settings["test_globals"] = test_globals
test_globals_http = f"root {testdir};\naccess_log {testdir}/access.log;\n"
test_globals_http += os.getenv("TEST_NGINX_GLOBALS_HTTP", "")
settings["test_globals_http"] = test_globals_http
test_globals_stream = os.getenv("TEST_NGINX_GLOBALS_STREAM", "")
settings["test_globals_stream"] = test_globals_stream
conf = tmpl.render(settings)
logger.debug(conf)
return conf


@pytest.fixture(scope="module")
def nginx(testdir, nginx_config, certs, logger):
# logger.debug(CAPABILITIES)
(testdir / "nginx.conf").write_text(nginx_config)
args = [
NGINX_BINARY,
"-p",
f"{testdir}",
"-c",
"nginx.conf",
"-e",
"error.log",
]
logger.info("Starting nginx...")
_nginx = subprocess.Popen(args)
logger.debug(f"path={NGINX_BINARY}")
logger.debug(f"args={' '.join(_nginx.args[1:])}")
logger.debug(f"pid={_nginx.pid}")
while not (testdir / "nginx.pid").exists():
time.sleep(0.1)
if _nginx.poll() is not None:
raise subprocess.SubprocessError("Can't start nginx")
yield _nginx
logger.info("Stopping nginx...")
_nginx.terminate()
try:
_nginx.wait(timeout=15)
except subprocess.TimeoutExpired:
_nginx.kill()
_log = (testdir / "error.log").read_text()
assert "[alert]" not in _log
if os.getenv("TEST_NGINX_CATLOG", "0") in ["1", "true"]:
logger.debug(_log)


@pytest.fixture(scope="module")
def certs(request, testdir):
if getattr(request.module, "CERT_GEN", None) is not None:
return eval(request.module.CERT_GEN)(testdir)
Loading

0 comments on commit 777bb84

Please sign in to comment.