Skip to content

Commit

Permalink
Use pytest for tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimf5 committed Apr 3, 2024
1 parent d381713 commit bf736d9
Show file tree
Hide file tree
Showing 9 changed files with 901 additions and 2,014 deletions.
56 changes: 20 additions & 36 deletions .github/workflows/nginx-otel-module-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,15 @@ jobs:
- name: Build module
working-directory: build
run: |
cmake -DNGX_OTEL_NGINX_BUILD_DIR=${PWD}/../nginx/objs \
-DNGX_OTEL_DEV=ON ..
make -j 4
cmake -DNGX_OTEL_NGINX_BUILD_DIR=${GITHUB_WORKSPACE}/nginx/objs \
-DNGX_OTEL_DEV=ON ${GITHUB_WORKSPACE}
make -j $(nproc)
strip ngx_otel_module.so
- name: Archive module
uses: actions/upload-artifact@v4
with:
name: nginx-otel-module
path: build/ngx_otel_module.so
- name: Archive protoc and opentelemetry-proto
uses: actions/upload-artifact@v4
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 @@ -51,36 +44,27 @@ jobs:
with:
name: nginx-otel-module
path: build
- name: Download protoc and opentelemetry-proto
uses: actions/download-artifact@v4
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
run: |
hg clone http://hg.nginx.org/nginx/
hg clone http://hg.nginx.org/nginx-tests/
- name: Checkout nginx
run: hg clone http://hg.nginx.org/nginx/
- 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
make -j $(nproc)
- name: Install test dependecies
working-directory: tests
run: pip install -r requirements.txt
- name: Download otelcol
uses: robinraju/release-downloader@v1.9
with:
repository: open-telemetry/opentelemetry-collector-releases
tag: v0.97.0
fileName: otelcol_0.97.0_linux_amd64.tar.gz
extract: true
- 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 .
env:
TEST_NGINX_GLOBALS: |
load_module ${{ github.workspace }}/build/ngx_otel_module.so;
run: 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)
params = request.param
params["test_globals"] = (
f"pid {testdir}/nginx.pid;\nerror_log {testdir}/error.log debug;\n"
+ os.getenv("TEST_NGINX_GLOBALS", "")
)
params[
"test_globals_http"
] = f"root {testdir};\naccess_log {testdir}/access.log;\n" + os.getenv(
"TEST_NGINX_GLOBALS_HTTP", ""
)
params["test_globals_stream"] = os.getenv("TEST_NGINX_GLOBALS_STREAM", "")
conf = tmpl.render(params)
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...")
proc = subprocess.Popen(args)
logger.debug(f"path={NGINX_BINARY}")
logger.debug(f"args={' '.join(proc.args[1:])}")
logger.debug(f"pid={proc.pid}")
while not (testdir / "nginx.pid").exists():
time.sleep(0.1)
if proc.poll() is not None:
raise subprocess.SubprocessError("Can't start nginx")
yield proc
logger.info("Stopping nginx...")
proc.terminate()
try:
proc.wait(timeout=15)
except subprocess.TimeoutExpired:
proc.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 bf736d9

Please sign in to comment.