mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-24 17:06:18 -05:00
Controller logs: an unparseable since is a 400, not empty logs (#953)
journalctl exits 1 with nothing on stdout when it cannot parse --since (a bare "1h" is one such value); the controller returned that empty stdout as the logs, with a 200. A failed journalctl is now an error: 400 when it could not parse since, 500 otherwise. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d25ebb9e63
commit
f3f762ef1b
4 changed files with 51 additions and 3 deletions
|
|
@ -159,6 +159,8 @@ async def service_logs(
|
|||
result = get_service_logs(name, lines, since)
|
||||
|
||||
if "error" in result:
|
||||
raise HTTPException(status_code=500, detail=result["error"])
|
||||
# A since journalctl cannot parse is the caller's mistake.
|
||||
status_code = 400 if result.get("invalid_since") else 500
|
||||
raise HTTPException(status_code=status_code, detail=result["error"])
|
||||
|
||||
return ServiceLogsResponse(**result)
|
||||
|
|
|
|||
|
|
@ -528,10 +528,11 @@ def get_service_logs(name: str, lines: int = 100, since: Optional[str] = None) -
|
|||
Args:
|
||||
name: Service name from config
|
||||
lines: Number of lines to return
|
||||
since: Time specification (e.g., "1h", "30m", "2024-01-01")
|
||||
since: journalctl time specification (e.g., "-1h", "1 hour ago", "2024-01-01 09:00"). A bare "1h" is
|
||||
not one.
|
||||
|
||||
Returns:
|
||||
dict with logs and metadata
|
||||
dict with logs and metadata, or an "error" (with "invalid_since" when journalctl could not parse `since`)
|
||||
"""
|
||||
systemd_name = _get_systemd_name(name)
|
||||
if not systemd_name:
|
||||
|
|
@ -543,6 +544,10 @@ def get_service_logs(name: str, lines: int = 100, since: Optional[str] = None) -
|
|||
|
||||
try:
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
|
||||
if result.returncode != 0:
|
||||
# journalctl prints nothing to stdout when it fails; that must not read as "no logs".
|
||||
error = (result.stderr or "").strip() or f"journalctl exited with {result.returncode}"
|
||||
return {"error": error, "invalid_since": bool(since) and "Failed to parse timestamp" in error}
|
||||
return {
|
||||
"service": name,
|
||||
"lines": lines,
|
||||
|
|
|
|||
|
|
@ -261,3 +261,27 @@ class TestCertDownloadReadiness:
|
|||
assert response.status_code == 200
|
||||
assert body.encode() in response.content
|
||||
assert "attachment" in response.headers.get("content-disposition", "")
|
||||
|
||||
|
||||
class TestServiceLogsEndpoint:
|
||||
"""A since journalctl cannot parse is the caller's mistake (400), not empty logs."""
|
||||
|
||||
def test_unparseable_since_is_a_bad_request(self, test_client):
|
||||
with mock.patch("subprocess.run") as mock_run:
|
||||
mock_run.return_value = mock.Mock(returncode=1, stdout="", stderr="Failed to parse timestamp: 1h\n")
|
||||
response = test_client.get("/api/services/wrolpi-api/logs", params={"since": "1h"})
|
||||
assert response.status_code == 400
|
||||
assert "Failed to parse timestamp: 1h" in response.json()["detail"]
|
||||
|
||||
def test_journalctl_failure_is_a_server_error(self, test_client):
|
||||
with mock.patch("subprocess.run") as mock_run:
|
||||
mock_run.return_value = mock.Mock(returncode=1, stdout="", stderr="Failed to open journal\n")
|
||||
response = test_client.get("/api/services/wrolpi-api/logs")
|
||||
assert response.status_code == 500
|
||||
|
||||
def test_logs(self, test_client):
|
||||
with mock.patch("subprocess.run") as mock_run:
|
||||
mock_run.return_value = mock.Mock(returncode=0, stdout="log line\n", stderr="")
|
||||
response = test_client.get("/api/services/wrolpi-api/logs", params={"since": "-1h"})
|
||||
assert response.status_code == 200
|
||||
assert response.json()["logs"] == "log line\n"
|
||||
|
|
|
|||
|
|
@ -342,6 +342,23 @@ class TestGetServiceLogs:
|
|||
assert "1h" in call_args
|
||||
|
||||
|
||||
def test_unparseable_since_is_an_error(self):
|
||||
"""journalctl exits 1 and prints nothing to stdout for a since it cannot parse (like a bare "1h");
|
||||
that is an invalid since, not an empty log."""
|
||||
with mock.patch("subprocess.run") as mock_run:
|
||||
mock_run.return_value = mock.Mock(returncode=1, stdout="", stderr="Failed to parse timestamp: 1h\n")
|
||||
result = get_service_logs("wrolpi-api", lines=50, since="1h")
|
||||
assert result["error"] == "Failed to parse timestamp: 1h"
|
||||
assert result["invalid_since"] is True
|
||||
|
||||
def test_journalctl_failure_is_an_error(self):
|
||||
"""Any other failure of journalctl is reported, not returned as empty logs."""
|
||||
with mock.patch("subprocess.run") as mock_run:
|
||||
mock_run.return_value = mock.Mock(returncode=1, stdout="", stderr="Failed to open journal\n")
|
||||
result = get_service_logs("wrolpi-api", lines=50)
|
||||
assert result["error"] == "Failed to open journal"
|
||||
assert not result.get("invalid_since")
|
||||
|
||||
class TestDiscoverRunningWrolpiServices:
|
||||
"""Tests for discover_wrolpi_services function."""
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue