A failed upgrade is shown on the upgrade page instead of redirecting home (#955)

* A failed upgrade is shown on the upgrade page instead of redirecting home

A failed upgrade restarts the services it stopped, so the upgrade page saw
them come back and redirected home as if the upgrade had worked.  The
Controller now reports how the last run of a script ended
(GET /api/scripts/{name}/result, from systemd's Result and exit status),
and the upgrade page reads it before redirecting.  A failure turns the
banner red, gives the exit code, opens the output, and offers a link back.

Only a run the page has seen start counts, because the result of an
earlier run is still reported until the new run begins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* An upgrade's result survives the daemon-reload it does

The upgrade reinstalls its unit and reloads systemd, which clears the run's
timestamps but keeps Result and the exit status.  A failure is now known
from those alone, and the result reports whether the script is running, so
the upgrade page waits on that instead of on a timestamp that may never come.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-11-08 11:11:41 -07:00 • committed by Roland
parent d88db90ce9
commit 19ff59d5e0
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
6 changed files with 317 additions and 11 deletions

View file

@ -13,6 +13,7 @@ from controller.lib.scripts import (
AVAILABLE_SCRIPTS,
get_current_branch,
get_script_output,
get_script_result,
get_script_status,
list_available_scripts,
start_script,
@ -368,3 +369,89 @@ class TestGetServiceTiming:
completed = subprocess.CompletedProcess(args=[], returncode=0, stdout="ExecMainStartTimestamp=\n")
with mock.patch("controller.lib.scripts.subprocess.run", return_value=completed):
assert _get_service_timing("wrolpi-upgrade.service") == (None, None)
def _systemctl_show(stdout: str):
return mock.Mock(returncode=0, stdout=stdout)
class TestGetScriptResult:
"""How the last run of a script ended, read from systemd."""
def test_failed_run(self):
"""An upgrade that exited non-zero is reported as failed, with its exit status and when it finished."""
stdout = "Result=exit-code\nExecMainStatus=5\nActiveState=failed\nExecMainExitTimestamp=@1794151352.5\n"
with mock.patch("controller.lib.scripts.is_docker_mode", return_value=False), \
mock.patch("controller.lib.scripts.subprocess.run", return_value=_systemctl_show(stdout)) as run:
result = get_script_result("upgrade")
assert result == {
"script_name": "upgrade",
"running": False,
"finished": True,
"success": False,
"result": "exit-code",
"exit_status": 5,
"finished_at": "2026-11-08T15:22:32.500000+00:00",
}
assert "wrolpi-upgrade.service" in run.call_args[0][0]
def test_successful_run(self):
stdout = "Result=success\nExecMainStatus=0\nActiveState=inactive\nExecMainExitTimestamp=@1762615352\n"
with mock.patch("controller.lib.scripts.is_docker_mode", return_value=False), \
mock.patch("controller.lib.scripts.subprocess.run", return_value=_systemctl_show(stdout)):
result = get_script_result("upgrade")
assert result["finished"] is True
assert result["success"] is True
assert result["exit_status"] == 0
def test_running(self):
"""A run in progress has not finished, so it has neither succeeded nor failed."""
stdout = "Result=success\nExecMainStatus=0\nActiveState=activating\nExecMainExitTimestamp=\n"
with mock.patch("controller.lib.scripts.is_docker_mode", return_value=False), \
mock.patch("controller.lib.scripts.subprocess.run", return_value=_systemctl_show(stdout)):
result = get_script_result("upgrade")
assert result["running"] is True
assert result["finished"] is False
assert result["success"] is None
assert result["finished_at"] is None
def test_failed_run_after_daemon_reload(self):
"""An upgrade reloads systemd, which clears the run's timestamps but keeps its Result and exit status."""
stdout = "Result=exit-code\nExecMainStatus=5\nActiveState=failed\nExecMainExitTimestamp=\n"
with mock.patch("controller.lib.scripts.is_docker_mode", return_value=False), \
mock.patch("controller.lib.scripts.subprocess.run", return_value=_systemctl_show(stdout)):
result = get_script_result("upgrade")
assert result["running"] is False
assert result["finished"] is True
assert result["success"] is False
assert result["exit_status"] == 5
assert result["finished_at"] is None
def test_successful_run_after_daemon_reload(self):
"""A clean run whose timestamps were cleared is not running and not failed; the upgrade page redirects."""
stdout = "Result=success\nExecMainStatus=0\nActiveState=inactive\nExecMainExitTimestamp=\n"
with mock.patch("controller.lib.scripts.is_docker_mode", return_value=False), \
mock.patch("controller.lib.scripts.subprocess.run", return_value=_systemctl_show(stdout)):
result = get_script_result("upgrade")
assert result["running"] is False
assert result["success"] is not False
def test_never_run(self):
"""A service that has never run has no result."""
stdout = "Result=success\nExecMainStatus=0\nActiveState=inactive\nExecMainExitTimestamp=\n"
with mock.patch("controller.lib.scripts.is_docker_mode", return_value=False), \
mock.patch("controller.lib.scripts.subprocess.run", return_value=_systemctl_show(stdout)):
result = get_script_result("upgrade")
assert result["finished"] is False
assert result["success"] is None
def test_systemctl_missing(self):
with mock.patch("controller.lib.scripts.is_docker_mode", return_value=False), \
mock.patch("controller.lib.scripts.subprocess.run", side_effect=FileNotFoundError):
result = get_script_result("upgrade")
assert result["finished"] is False
assert result["success"] is None
def test_unknown_script(self):
with mock.patch("controller.lib.scripts.is_docker_mode", return_value=False):
assert get_script_result("nonexistent") is None

View file

@ -13,6 +13,7 @@ class TestDockerModeRejectsScriptsEndpoints:
@pytest.mark.parametrize("method,endpoint", [
("post", "/api/scripts/repair/start"),
("get", "/api/scripts/repair/output"),
("get", "/api/scripts/upgrade/result"),
])
def test_action_rejected_in_docker(self, test_client_docker_mode, method, endpoint):
response = getattr(test_client_docker_mode, method)(endpoint)
@ -166,3 +167,22 @@ class TestScriptsOutputEndpoint:
# OpenAPI endpoint-presence tests are consolidated in test_api.py::TestOpenAPI.
class TestScriptResultEndpoint:
"""Tests for /api/scripts/{name}/result, which the upgrade page reads before calling an upgrade done."""
def test_reports_failed_upgrade(self, test_client):
result = {
"script_name": "upgrade", "running": False, "finished": True, "success": False, "result": "exit-code", "exit_status": 5,
"finished_at": "2026-11-08T15:22:32+00:00",
}
with mock.patch("controller.api.scripts.get_script_result", return_value=result):
response = test_client.get("/api/scripts/upgrade/result")
assert response.status_code == 200
assert response.json() == result
def test_unknown_script_is_404(self, test_client):
with mock.patch("controller.api.scripts.get_script_result", return_value=None):
response = test_client.get("/api/scripts/nonexistent/result")
assert response.status_code == 404