You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/engine/tests/test_app_shutdown.py

115 lines
5.1 KiB
Python

"""Lost isolated execution must retain job evidence and mutation guards."""
import time
import pytest
from test_app import (
VM_ID, configure_host, configure_reinstall_case, configure_snapshot_case,
setup_admin, wait_for_reinstall_job, wait_for_snapshot_state, web,
)
from virsh_panel.app import reinstall_post_commit
from virsh_panel.libvirt_rpc import EXECUTION_ERROR_CODES
from virsh_panel.libvirt_service import VMError
def test_shutdown_rejects_new_http_requests(web):
client, *_ = web
client.app.state.begin_shutdown()
response = client.get("/api/v1/health")
assert response.status_code == 503
@pytest.mark.parametrize("error_code", sorted(EXECUTION_ERROR_CODES))
def test_reinstall_interruption_preserves_password_and_guard(web, error_code):
client, _state, _token, service, _manager, _ = web
csrf, template_id, _volume = configure_reinstall_case(web)
def interrupted(*args, **kwargs):
raise VMError("isolated execution interrupted", 503, error_code=error_code)
service.reinstall_cloud_vm = interrupted
body = {"template_id": template_id, "confirmation_name": "cloud-vm"}
headers = {"X-CSRF-Token": csrf}
response = client.post(f"/api/v1/vms/{VM_ID}/reinstall", json=body, headers=headers)
assert response.status_code == 202, response.text
result = wait_for_reinstall_job(client, response.json()["job_id"], csrf)
assert result["status"] == "uncertain"
assert result["error_code"] == error_code
assert result["generated_password"]
assert not reinstall_post_commit(result)
assert "finished_at" not in result
duplicate = client.post(f"/api/v1/vms/{VM_ID}/reinstall", json=body, headers=headers)
assert duplicate.status_code == 409
power = client.post(f"/api/v1/vms/{VM_ID}/actions",
json={"action": "poweroff"}, headers=headers)
assert power.status_code == 409
@pytest.mark.parametrize("error_code", sorted(EXECUTION_ERROR_CODES))
def test_snapshot_interruption_preserves_catalog_and_guard(web, error_code):
client, service, csrf = configure_snapshot_case(web)
service.snapshot_create_error = VMError(
"isolated execution interrupted", 503, error_code=error_code)
key = "e" * 32
response = client.post(
f"/api/v1/vms/{VM_ID}/snapshots",
json={"snapshot_key": key, "name": "snapshot", "consistency_mode": "crash"},
headers={"X-CSRF-Token": csrf},
)
assert response.status_code == 202, response.text
assert wait_for_snapshot_state(client, key, "uncertain")["state"] == "uncertain"
saved = client.app.state.snapshot_job_state(response.json()["job_id"])
assert saved["status"] == "uncertain"
assert saved["error_code"] == error_code
deleted = client.delete(f"/api/v1/vms/{VM_ID}", headers={"X-CSRF-Token": csrf})
assert deleted.status_code == 409
assert not any(call[0] == "delete_vm" for call in service.calls)
@pytest.mark.parametrize("error_code", sorted(EXECUTION_ERROR_CODES))
def test_rescue_interruption_preserves_original_xml_and_guard(web, error_code):
client, _state, token, service, _manager, _ = web
csrf = setup_admin(client, token)
configure_host(client, csrf)
headers = {"X-CSRF-Token": csrf}
iso = bytearray(1024 * 1024)
for sector, kind in ((16, 1), (17, 0), (18, 255)):
offset = sector * 2048
iso[offset] = kind
iso[offset + 1:offset + 6] = b"CD001"
iso[offset + 6] = 1
iso[17 * 2048 + 7:17 * 2048 + 30] = b"EL TORITO SPECIFICATION"
iso[17 * 2048 + 71:17 * 2048 + 75] = (20).to_bytes(4, "little")
response = client.post("/api/v1/rescue-templates?name=Rescue",
content=bytes(iso), headers=headers)
assert response.status_code == 202, response.text
for _ in range(100):
imported = client.get(
f"/api/v1/rescue-templates/import-jobs/{response.json()['job_id']}").json()
if imported["status"] not in {"queued", "running"}:
break
time.sleep(0.01)
assert imported["status"] == "completed"
template = client.get("/api/v1/rescue-templates").json()["templates"][0]
service.rescue_enter_error = VMError(
"isolated execution interrupted", 503, error_code=error_code)
response = client.post(f"/api/v1/vms/{VM_ID}/rescue", json={
"template_id": template["id"], "confirmation_name": "alma-test",
}, headers=headers)
assert response.status_code == 202, response.text
for _ in range(100):
result = client.get(f"/api/v1/rescue-jobs/{response.json()['job_id']}").json()
if result["status"] not in {"queued", "running"}:
break
time.sleep(0.01)
assert result["status"] == "uncertain"
assert result["error_code"] == error_code
assert client.get(f"/api/v1/vms/{VM_ID}/rescue").json()["status"] == "uncertain"
# The exception path must not run the ordinary failure rollback, which
# compares prepare_rescue_vm XML and can delete the durable original XML.
assert sum(call[0] == "prepare_rescue_vm" for call in service.calls) == 1
power = client.post(f"/api/v1/vms/{VM_ID}/actions",
json={"action": "poweroff"}, headers=headers)
assert power.status_code == 409