Add nginx config parsing utilities
This commit is contained in:
parent
84e3e0e7de
commit
97ad5bc998
2 changed files with 158 additions and 0 deletions
70
tests/test_nginx_config.py
Normal file
70
tests/test_nginx_config.py
Normal file
|
@ -0,0 +1,70 @@
|
|||
import sys
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.append(str(REPO_ROOT))
|
||||
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]
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue