From f3f762ef1b8b107d1b5d966e02a4bf958a571477 Mon Sep 17 00:00:00 2001 From: lrnselfreliance <33263708+lrnselfreliance@users.noreply.github.com> Date: Sun, 8 Nov 2026 08:01:27 -0700 Subject: [PATCH] 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 --- controller/api/services.py | 4 +++- controller/lib/systemd.py | 9 +++++++-- controller/test/test_api.py | 24 ++++++++++++++++++++++++ controller/test/test_systemd.py | 17 +++++++++++++++++ 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/controller/api/services.py b/controller/api/services.py index c154d7ba..bae9c0ac 100644 --- a/controller/api/services.py +++ b/controller/api/services.py @@ -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) diff --git a/controller/lib/systemd.py b/controller/lib/systemd.py index 5e63d98b..c5ed99cb 100644 --- a/controller/lib/systemd.py +++ b/controller/lib/systemd.py @@ -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, diff --git a/controller/test/test_api.py b/controller/test/test_api.py index 50501694..5f99dbb0 100644 --- a/controller/test/test_api.py +++ b/controller/test/test_api.py @@ -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" diff --git a/controller/test/test_systemd.py b/controller/test/test_systemd.py index 0d28758a..7b4188e0 100644 --- a/controller/test/test_systemd.py +++ b/controller/test/test_systemd.py @@ -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."""