Skip to content

Commit 15c5b30

Browse files
committed
Use pytest for tests.
1 parent 6c1659a commit 15c5b30

File tree

9 files changed

+470
-2046
lines changed

9 files changed

+470
-2046
lines changed

.github/workflows/ubuntu.yml

Lines changed: 16 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,92 +8,40 @@ on:
88

99
jobs:
1010
build-module:
11-
runs-on: ubuntu-latest
11+
runs-on: ubuntu-22.04
1212
steps:
1313
- name: Checkout repository
1414
uses: actions/checkout@v4
1515
- name: Install dependencies
1616
run: |
1717
sudo apt-get update
18-
sudo apt-get install -y cmake libc-ares-dev libre2-dev
18+
sudo apt-get install -y cmake libc-ares-dev
1919
- name: Checkout nginx
2020
uses: actions/checkout@v4
2121
with:
2222
repository: nginx/nginx
2323
path: nginx
24-
- name: Configure nginx
24+
- name: Build nginx
2525
working-directory: nginx
26-
run: auto/configure --with-compat
27-
- name: Create build directory
28-
run: mkdir build
26+
run: |
27+
auto/configure --with-compat --with-debug --with-http_ssl_module \
28+
--with-http_v2_module --with-http_v3_module
29+
make -j $(nproc)
2930
- name: Build module
30-
working-directory: build
3131
run: |
32+
mkdir build
33+
cd build
3234
cmake -DNGX_OTEL_NGINX_BUILD_DIR=${PWD}/../nginx/objs \
3335
-DNGX_OTEL_DEV=ON ..
34-
make -j 4
35-
strip ngx_otel_module.so
36-
- name: Archive module
37-
uses: actions/upload-artifact@v4
38-
with:
39-
name: nginx-otel-module
40-
path: build/ngx_otel_module.so
41-
- name: Archive protoc and opentelemetry-proto
42-
uses: actions/upload-artifact@v4
43-
with:
44-
name: protoc-opentelemetry-proto
45-
path: |
46-
build/_deps/grpc-build/third_party/protobuf/protoc
47-
build/_deps/otelcpp-src/third_party/opentelemetry-proto
48-
test-module:
49-
needs: build-module
50-
runs-on: ubuntu-latest
51-
steps:
52-
- name: Checkout repository
53-
uses: actions/checkout@v4
54-
- name: Download module
55-
uses: actions/download-artifact@v4
56-
with:
57-
name: nginx-otel-module
58-
path: build
59-
- name: Download protoc and opentelemetry-proto
60-
uses: actions/download-artifact@v4
61-
with:
62-
name: protoc-opentelemetry-proto
63-
path: build/_deps
64-
- name: List files
65-
run: ls -laR .
66-
- name: Fix protoc file permissions
67-
run: chmod +x build/_deps/grpc-build/third_party/protobuf/protoc
68-
- name: Install perl modules
69-
run: sudo cpan IO::Socket::SSL Crypt::Misc
36+
make -j $(nproc)
7037
- name: Download otelcol
7138
run: |
72-
curl -LO https://github.com/\
39+
curl -sLo - https://github.com/\
7340
open-telemetry/opentelemetry-collector-releases/releases/download/\
74-
v0.76.1/otelcol_0.76.1_linux_amd64.tar.gz
75-
tar -xzf otelcol_0.76.1_linux_amd64.tar.gz
76-
- name: Checkout nginx
77-
uses: actions/checkout@v4
78-
with:
79-
repository: nginx/nginx
80-
path: nginx
81-
- name: Build nginx
82-
working-directory: nginx
83-
run: |
84-
auto/configure --with-compat --with-debug --with-http_ssl_module \
85-
--with-http_v2_module --with-http_v3_module
86-
make -j 4
87-
- name: Checkout lib from nginx-tests
88-
uses: actions/checkout@v4
89-
with:
90-
repository: nginx/nginx-tests
91-
sparse-checkout: |
92-
lib
93-
path: nginx-tests
41+
v0.115.1/otelcol_0.115.1_linux_amd64.tar.gz | tar -xzv
42+
- name: Install test dependencies
43+
run: pip install -r tests/requirements.txt
9444
- name: Run tests
95-
working-directory: tests
9645
run: |
97-
PERL5LIB=../nginx-tests/lib TEST_NGINX_UNSAFE=1 \
98-
TEST_NGINX_VERBOSE=1 TEST_NGINX_GLOBALS="load_module \
99-
${PWD}/../build/ngx_otel_module.so;" prove -v .
46+
pytest tests --maxfail=10 --nginx=nginx/objs/nginx \
47+
--module=build/ngx_otel_module.so --otelcol=./otelcol

tests/conftest.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)