Add suggest_cache command and tests

This commit is contained in:
Jordan Wages 2025-07-19 02:07:23 -05:00
commit 7f996fe123
2 changed files with 169 additions and 0 deletions

View file

@ -88,3 +88,118 @@ server {
analyze.check_missing_domains(json_output=True)
out_json = json.loads(capsys.readouterr().out.strip())
assert out_json == ["missing.com"]
def test_suggest_cache(tmp_path, monkeypatch, capsys):
db_path = tmp_path / "database" / "ngxstat.db"
db_path.parent.mkdir(parents=True, exist_ok=True)
conn = sqlite3.connect(db_path)
cur = conn.cursor()
cur.execute(
"""
CREATE TABLE logs (
id INTEGER PRIMARY KEY,
ip TEXT,
host TEXT,
time TEXT,
request TEXT,
status INTEGER,
bytes_sent INTEGER,
referer TEXT,
user_agent TEXT,
cache_status TEXT
)
"""
)
entries = [
(
"127.0.0.1",
"example.com",
"2024-01-01 10:00:00",
"GET /foo HTTP/1.1",
200,
100,
"-",
"curl",
"MISS",
),
(
"127.0.0.1",
"example.com",
"2024-01-01 10:01:00",
"GET /foo HTTP/1.1",
200,
100,
"-",
"curl",
"MISS",
),
(
"127.0.0.1",
"example.com",
"2024-01-01 10:02:00",
"GET /foo HTTP/1.1",
200,
100,
"-",
"curl",
"MISS",
),
(
"127.0.0.1",
"cached.com",
"2024-01-01 10:00:00",
"GET /bar HTTP/1.1",
200,
100,
"-",
"curl",
"MISS",
),
(
"127.0.0.1",
"cached.com",
"2024-01-01 10:01:00",
"GET /bar HTTP/1.1",
200,
100,
"-",
"curl",
"MISS",
),
]
cur.executemany(
"INSERT INTO logs (ip, host, time, request, status, bytes_sent, referer, user_agent, cache_status)"
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
entries,
)
conn.commit()
conn.close()
conf = tmp_path / "nginx.conf"
conf.write_text(
"""
server {
listen 80;
server_name example.com;
}
server {
listen 80;
server_name cached.com;
proxy_cache cache1;
}
"""
)
monkeypatch.setattr(analyze, "DB_PATH", db_path)
monkeypatch.setattr(gr, "DB_PATH", db_path)
monkeypatch.setattr(analyze.nginx_config, "DEFAULT_PATHS", [str(conf)])
analyze.suggest_cache(threshold=2, json_output=False)
out = capsys.readouterr().out.strip().splitlines()
assert out == ["example.com /foo 3"]
analyze.suggest_cache(threshold=2, json_output=True)
out_json = json.loads(capsys.readouterr().out.strip())
assert out_json == [{"host": "example.com", "path": "/foo", "misses": 3}]