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_libvirt_service_rpc.py

90 lines
3.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
@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