wrolpi/controller/test/test_scripts_api.py
lrnselfreliance 19ff59d5e0
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>
2026-11-08 11:11:59 -07:00

188 lines
8.3 KiB
Python

"""
Integration tests for Controller scripts API endpoints.
"""
from unittest import mock
import pytest
class TestDockerModeRejectsScriptsEndpoints:
"""Scripts action endpoints should return 501 in Docker mode."""
@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)
assert response.status_code == 501
assert "Docker" in response.json()["detail"]
class TestScriptsListEndpoint:
"""Tests for /api/scripts endpoint."""
def test_scripts_list_returns_200(self, test_client):
"""Scripts list endpoint should return 200 OK."""
response = test_client.get("/api/scripts")
assert response.status_code == 200
def test_scripts_list_returns_list(self, test_client):
"""Scripts list should return a list."""
response = test_client.get("/api/scripts")
data = response.json()
assert isinstance(data, list)
def test_scripts_list_includes_repair(self, test_client):
"""Scripts list should include repair script."""
response = test_client.get("/api/scripts")
data = response.json()
names = [s["name"] for s in data]
assert "repair" in names
def test_scripts_list_has_required_fields(self, test_client):
"""Each script should have required fields."""
response = test_client.get("/api/scripts")
data = response.json()
for script in data:
assert "name" in script
assert "display_name" in script
assert "description" in script
assert "warnings" in script
assert "available" in script
def test_scripts_unavailable_in_docker_mode(self, test_client_docker_mode):
"""Scripts should be marked unavailable in Docker mode."""
response = test_client_docker_mode.get("/api/scripts")
data = response.json()
for script in data:
assert script["available"] is False
class TestScriptsStatusEndpoint:
"""Tests for /api/scripts/status endpoint."""
def test_scripts_status_returns_200(self, test_client):
"""Scripts status endpoint should return 200 OK."""
response = test_client.get("/api/scripts/status")
assert response.status_code == 200
def test_scripts_status_has_running_field(self, test_client):
"""Scripts status should have running field."""
response = test_client.get("/api/scripts/status")
data = response.json()
assert "running" in data
def test_scripts_status_not_running_in_docker(self, test_client_docker_mode):
"""Scripts should not be running in Docker mode."""
response = test_client_docker_mode.get("/api/scripts/status")
data = response.json()
assert data["running"] is False
class TestScriptsStartEndpoint:
"""Tests for /api/scripts/{name}/start endpoint."""
def test_scripts_start_returns_400_for_unknown_script(self, test_client):
"""Scripts start should return 400 for unknown script."""
with mock.patch("controller.lib.scripts.is_docker_mode", return_value=False):
response = test_client.post("/api/scripts/nonexistent/start")
assert response.status_code == 400
def test_scripts_start_returns_400_if_already_running(self, test_client):
"""Scripts start should return 400 if script already running."""
with mock.patch("controller.lib.scripts.is_docker_mode", return_value=False):
with mock.patch("controller.lib.scripts.get_script_status", return_value={
"running": True,
"script_name": "repair",
}):
response = test_client.post("/api/scripts/repair/start")
assert response.status_code == 400
assert "already running" in response.json()["detail"].lower()
def test_scripts_start_rejects_unknown_body_keys(self, test_client):
"""Parameters go in `params`; a body like {"branch": ...} is an error, not an upgrade of some other
branch."""
with mock.patch("controller.lib.scripts.is_docker_mode", return_value=False), \
mock.patch("controller.lib.scripts.get_script_status", return_value={"running": False}), \
mock.patch("subprocess.Popen") as popen:
response = test_client.post("/api/scripts/upgrade/start", json={"branch": "master"})
assert response.status_code == 422
popen.assert_not_called()
class TestScriptsOutputEndpoint:
"""Tests for /api/scripts/{name}/output endpoint."""
def test_scripts_output_returns_200(self, test_client):
"""Scripts output should return 200 when not in Docker mode."""
with mock.patch("controller.lib.scripts.is_docker_mode", return_value=False):
mock_result = mock.Mock()
mock_result.stdout = "Log output"
mock_result.returncode = 0
with mock.patch("subprocess.run", return_value=mock_result):
response = test_client.get("/api/scripts/repair/output")
assert response.status_code == 200
def test_scripts_output_has_expected_fields(self, test_client):
"""Scripts output should have expected fields."""
with mock.patch("controller.lib.scripts.is_docker_mode", return_value=False):
mock_result = mock.Mock()
mock_result.stdout = "Log output"
mock_result.returncode = 0
with mock.patch("subprocess.run", return_value=mock_result):
response = test_client.get("/api/scripts/repair/output")
data = response.json()
assert "output" in data
assert "lines" in data
assert "script_name" in data
def test_scripts_output_respects_lines_param(self, test_client):
"""Scripts output should respect lines parameter."""
with mock.patch("controller.lib.scripts.is_docker_mode", return_value=False):
mock_result = mock.Mock()
mock_result.stdout = "Log output"
mock_result.returncode = 0
with mock.patch("subprocess.run", return_value=mock_result) as mock_run:
response = test_client.get("/api/scripts/repair/output?lines=500")
assert response.status_code == 200
# Verify journalctl was called with -n 500
call_args = mock_run.call_args[0][0]
assert "500" in call_args
def test_scripts_output_limits_lines(self, test_client):
"""Scripts output should limit lines to 5000."""
with mock.patch("controller.lib.scripts.is_docker_mode", return_value=False):
mock_result = mock.Mock()
mock_result.stdout = "Log output"
mock_result.returncode = 0
with mock.patch("subprocess.run", return_value=mock_result) as mock_run:
response = test_client.get("/api/scripts/repair/output?lines=10000")
assert response.status_code == 200
# Verify journalctl was called with -n 5000 (max)
call_args = mock_run.call_args[0][0]
assert "5000" in call_args
# 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