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:
lrnselfreliance 2026-11-08 08:01:27 -07:00 • committed by Roland
parent d25ebb9e63
commit f3f762ef1b
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
4 changed files with 51 additions and 3 deletions

View file

@ -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"

View file

@ -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."""