|
| 1 | +import subprocess |
| 2 | +from unittest.mock import call, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | +from fastapi.testclient import TestClient |
| 6 | + |
| 7 | +from backend.config.paths import get_tmp_thread_files |
| 8 | +from backend.main import app |
| 9 | + |
| 10 | +client = TestClient(app) |
| 11 | + |
| 12 | +TASK_UID = "abc123" |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def mock_cancel_deps(): |
| 17 | + """Patch all three side-effects of the cancel endpoint.""" |
| 18 | + with ( |
| 19 | + patch("backend.main.celery_app") as mock_celery, |
| 20 | + patch("backend.main.subprocess.run") as mock_subproc, |
| 21 | + patch("backend.main.shutil.rmtree") as mock_rmtree, |
| 22 | + ): |
| 23 | + yield { |
| 24 | + "celery": mock_celery, |
| 25 | + "subprocess": mock_subproc, |
| 26 | + "rmtree": mock_rmtree, |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | +def test_cancel_returns_200(mock_cancel_deps): |
| 31 | + response = client.post(f"/api/cancel/{TASK_UID}") |
| 32 | + assert response.status_code == 200 |
| 33 | + |
| 34 | + |
| 35 | +def test_cancel_response_body(mock_cancel_deps): |
| 36 | + response = client.post(f"/api/cancel/{TASK_UID}") |
| 37 | + data = response.json() |
| 38 | + assert data["status"] == "cancelled" |
| 39 | + assert data["task_uid"] == TASK_UID |
| 40 | + |
| 41 | + |
| 42 | +def test_cancel_revokes_celery_task(mock_cancel_deps): |
| 43 | + client.post(f"/api/cancel/{TASK_UID}") |
| 44 | + mock_cancel_deps["celery"].control.revoke.assert_called_once_with(TASK_UID) |
| 45 | + |
| 46 | + |
| 47 | +def test_cancel_stops_docker_container(mock_cancel_deps): |
| 48 | + client.post(f"/api/cancel/{TASK_UID}") |
| 49 | + mock_cancel_deps["subprocess"].assert_called_once_with( |
| 50 | + ["docker", "stop", f"pysymbench-{TASK_UID}"], |
| 51 | + check=False, |
| 52 | + capture_output=True, |
| 53 | + timeout=30, |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def test_cancel_removes_all_tmp_dirs(mock_cancel_deps): |
| 58 | + client.post(f"/api/cancel/{TASK_UID}") |
| 59 | + expected_paths = get_tmp_thread_files(TASK_UID) |
| 60 | + calls = [call(p, ignore_errors=True) for p in expected_paths] |
| 61 | + mock_cancel_deps["rmtree"].assert_has_calls(calls, any_order=True) |
| 62 | + assert mock_cancel_deps["rmtree"].call_count == len(expected_paths) |
| 63 | + |
| 64 | + |
| 65 | +def test_cancel_still_succeeds_when_docker_stop_fails(mock_cancel_deps): |
| 66 | + """docker stop returning non-zero exit (container not running) must not raise.""" |
| 67 | + mock_cancel_deps["subprocess"].return_value = subprocess.CompletedProcess( |
| 68 | + args=[], returncode=1 |
| 69 | + ) |
| 70 | + response = client.post(f"/api/cancel/{TASK_UID}") |
| 71 | + assert response.status_code == 200 |
| 72 | + assert response.json()["status"] == "cancelled" |
| 73 | + |
| 74 | + |
| 75 | +def test_cancel_different_uids_use_correct_container_name(mock_cancel_deps): |
| 76 | + for uid in ["uid-one", "uid-two"]: |
| 77 | + mock_cancel_deps["subprocess"].reset_mock() |
| 78 | + client.post(f"/api/cancel/{uid}") |
| 79 | + args = mock_cancel_deps["subprocess"].call_args[0][0] |
| 80 | + assert args[-1] == f"pysymbench-{uid}" |
0 commit comments