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.
136 lines
5.4 KiB
Python
136 lines
5.4 KiB
Python
"""Exercise VMService's real spawned connection, rather than a service mock."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib
|
|
import os
|
|
import sys
|
|
import types
|
|
|
|
import pytest
|
|
|
|
from test_libvirt_rpc import VM_ID, runtime_factory # noqa: F401
|
|
from test_durable_coordination import coordinator # noqa: F401
|
|
|
|
|
|
@pytest.fixture
|
|
def scoped_service(runtime_factory, monkeypatch):
|
|
backend = importlib.import_module("libvirt_rpc_backend")
|
|
# Service import needs the extension's exception type even on macOS. The
|
|
# scoped path itself must never call an in-process libvirt connection.
|
|
if "libvirt" not in sys.modules:
|
|
stub = types.ModuleType("libvirt")
|
|
stub.libvirtError = backend.libvirtError
|
|
monkeypatch.setitem(sys.modules, "libvirt", stub)
|
|
module = importlib.import_module("virsh_panel.libvirt_service")
|
|
monkeypatch.setattr(module, "libvirt", backend)
|
|
runtime = runtime_factory()
|
|
monkeypatch.setattr(module, "get_runtime", lambda: runtime)
|
|
sessions = []
|
|
real_open = runtime.open
|
|
|
|
def observed_open(*args, **kwargs):
|
|
session = real_open(*args, **kwargs)
|
|
sessions.append(session)
|
|
return session
|
|
|
|
monkeypatch.setattr(runtime, "open", observed_open)
|
|
|
|
def create(uri="test:///default"):
|
|
return module.VMService(
|
|
uri, "images", "iso", "bridge:br0", execution_scope="host-a")
|
|
|
|
return module, runtime, sessions, create
|
|
|
|
|
|
def test_scoped_vmservice_uses_real_worker_and_preserves_native_error_mapping(scoped_service):
|
|
module, runtime, sessions, create = scoped_service
|
|
service = create()
|
|
assert service.get_vm_state(VM_ID) == {
|
|
"uuid": VM_ID, "name": "rpc-guest", "state": "running",
|
|
}
|
|
assert sessions[-1].pid != os.getpid()
|
|
assert not sessions[-1].process.is_alive()
|
|
with pytest.raises(module.VMError) as absent:
|
|
service.get_vm_state("00000000-0000-4000-8000-000000000002")
|
|
assert absent.value.status == 404
|
|
assert absent.value.__cause__.get_error_code() == 42
|
|
with pytest.raises(module.VMError) as unavailable:
|
|
create("test:///open-error").get_vm_state(VM_ID)
|
|
assert unavailable.value.status == 502
|
|
assert unavailable.value.__cause__.get_error_code() == 9
|
|
assert not list(runtime.state_dir.rglob("*.json"))
|
|
|
|
|
|
@pytest.mark.parametrize("write", [False, True], ids=["read-timeout", "write-unknown"])
|
|
def test_scoped_vmservice_cannot_swallow_a_poisoned_rpc_session(scoped_service, write):
|
|
module, runtime, sessions, create = scoped_service
|
|
service = create("test:///write-hangs" if write else "test:///read-hangs")
|
|
|
|
def helper_that_catches_remote_failure():
|
|
with service._connect(timeout=3.0) as conn:
|
|
try:
|
|
if write:
|
|
conn.lookupByUUIDString(VM_ID).destroy()
|
|
else:
|
|
conn.getHostname()
|
|
except Exception:
|
|
# Model existing rollback/metadata helpers which catch broad
|
|
# exceptions. The connection context must veto this success.
|
|
return {"status": "succeeded"}
|
|
|
|
with pytest.raises(module.LibvirtExecutionError) as raised:
|
|
helper_that_catches_remote_failure()
|
|
assert raised.value.status == 504
|
|
assert raised.value.error_code == (
|
|
"libvirt_execution_uncertain" if write else "libvirt_execution_timeout")
|
|
assert raised.value.outcome_unknown is write
|
|
assert sessions[-1].pid != os.getpid()
|
|
assert not sessions[-1].process.is_alive()
|
|
assert bool(list(runtime.state_dir.rglob("*.json"))) is write
|
|
|
|
|
|
def test_acknowledged_power_action_survives_connection_close_timeout(
|
|
scoped_service, coordinator, monkeypatch, caplog):
|
|
from virsh_panel import coordination
|
|
|
|
monkeypatch.setattr(coordination, "_default_coordinator", coordinator)
|
|
_module, _runtime, sessions, create = scoped_service
|
|
result = create("test:///close-hangs").action(VM_ID, "poweroff")
|
|
|
|
assert result == {"uuid": VM_ID, "action": "poweroff", "command_accepted": True}
|
|
session = sessions[-1]
|
|
assert session.failure is None
|
|
assert session.cleanup_failure.error_code == "libvirt_execution_timeout"
|
|
assert session.cleanup_failure.outcome_unknown is False
|
|
assert not session.process.is_alive()
|
|
assert coordinator.snapshot() == {}
|
|
assert "libvirt connection cleanup failed" in caplog.text
|
|
assert "test:///close-hangs" not in caplog.text
|
|
|
|
# Both durable VM admission and the host RPC admission remain usable.
|
|
assert create().action(VM_ID, "poweroff")["command_accepted"] is True
|
|
assert coordinator.snapshot() == {}
|
|
|
|
|
|
def test_missing_power_write_receipt_retains_both_protections(
|
|
scoped_service, coordinator, monkeypatch):
|
|
from virsh_panel import coordination
|
|
|
|
monkeypatch.setattr(coordination, "_default_coordinator", coordinator)
|
|
module, runtime, sessions, create = scoped_service
|
|
|
|
with pytest.raises(module.LibvirtExecutionError) as raised:
|
|
create("test:///write-hangs").action(VM_ID, "poweroff")
|
|
assert raised.value.error_code == "libvirt_execution_uncertain"
|
|
assert raised.value.outcome_unknown is True
|
|
assert sessions[-1].failure.outcome_unknown is True
|
|
claims = coordinator.snapshot()
|
|
assert len(claims) == 1
|
|
assert next(iter(claims.values()))["state"] == "uncertain"
|
|
assert list(runtime.state_dir.rglob("*.json"))
|
|
|
|
with pytest.raises(module.VMError) as blocked:
|
|
create().action(VM_ID, "poweroff")
|
|
assert blocked.value.error_code == "coordination_uncertain"
|