ngxstat/tests/test_nginx_config.py
ngxstat-bot a8f7ac9b7a
Some checks failed
CI / Lint, test, and build (push) Failing after 45s
lint: remove unused Path import in tests/test_nginx_config.py
2025-08-16 05:27:02 -05:00

63 lines
1.5 KiB
Python

from scripts import nginx_config as nc
def test_discover_configs(tmp_path, monkeypatch):
root = tmp_path / "nginx"
root.mkdir()
conf_d = root / "conf.d"
conf_d.mkdir()
subdir = root / "sub"
subdir.mkdir()
main = root / "nginx.conf"
site = conf_d / "site.conf"
extra = root / "extra.conf"
nested = subdir / "foo.conf"
main.write_text("include conf.d/*.conf;\ninclude extra.conf;\n")
site.write_text("# site config\n")
extra.write_text("include sub/foo.conf;\n")
nested.write_text("# nested config\n")
monkeypatch.setattr(nc, "DEFAULT_PATHS", [str(main)])
found = nc.discover_configs()
assert found == {main, site, extra, nested}
def test_parse_servers(tmp_path):
conf1 = tmp_path / "site.conf"
conf2 = tmp_path / "other.conf"
conf1.write_text(
"""
server {
listen 80;
server_name example.com;
root /srv/example;
proxy_cache cache1;
}
"""
)
conf2.write_text(
"""
server {
listen 443 ssl;
server_name example.org;
}
"""
)
servers = nc.parse_servers({conf1, conf2, tmp_path / "missing.conf"})
servers = sorted(servers, key=lambda s: s.get("server_name"))
assert len(servers) == 2
assert servers[0]["server_name"] == "example.com"
assert servers[0]["listen"] == "80"
assert servers[0]["root"] == "/srv/example"
assert servers[0]["proxy_cache"] == "cache1"
assert servers[1]["server_name"] == "example.org"
assert servers[1]["listen"] == "443 ssl"
assert "proxy_cache" not in servers[1]