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 Feb 7, 2024
1 parent aac5678 commit d869139
Show file tree
Hide file tree
Showing 9 changed files with 899 additions and 2,007 deletions.
44 changes: 15 additions & 29 deletions .github/workflows/nginx-otel-module-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ jobs:
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 @@ -50,36 +43,29 @@ 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
- 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: Download otelcol
uses: robinraju/release-downloader@v1.9
with:
repository: open-telemetry/opentelemetry-collector-releases
tag: v0.93.0
fileName: otelcol_0.93.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 .
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)
_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...")
_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 d869139

Please sign in to comment.