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_rpc_native_error_recei...

92 lines
3.3 KiB
Python

"""Native error replies retain the existing same-session recovery contract."""
import os
import pytest
from test_libvirt_rpc import VM_ID, runtime_factory # noqa: F401
from virsh_panel.libvirt_rpc import ExecutionFailure
@pytest.fixture
def native_error_runtime(runtime_factory, tmp_path, monkeypatch):
# Model the real storage-pool fallback: build can report an existing
# directory, after which create and readback must remain callable.
name = "libvirt_native_error_receipt_backend"
(tmp_path / (name + ".py")).write_text('''
import libvirt_rpc_backend as base
libvirtError = base.libvirtError
class virStoragePool(base.virStoragePool):
def build(self, flags=0):
raise libvirtError("directory already exists", (
9, 18, "directory already exists", 2, None, None, None, 0, 0))
def create(self, flags=0):
return 0
class virConnect(base.virConnect):
def __init__(self, uri):
super().__init__(uri)
self.pool = virStoragePool(self)
def open(uri):
return virConnect(uri)
''', encoding="utf-8")
monkeypatch.syspath_prepend(str(tmp_path))
monkeypatch.setenv("PYTHONPATH", str(tmp_path) + os.pathsep
+ os.environ.get("PYTHONPATH", ""))
return runtime_factory(backend_module=name)
def build_error(session):
pool = session.connection.storagePoolLookupByName("images")
with pytest.raises(Exception) as raised:
pool.build(0)
assert not isinstance(raised.value, ExecutionFailure)
assert raised.value.get_error_code() == 9
assert session.failure is None
return pool
def test_native_error_still_allows_existing_pool_start_and_readback(native_error_runtime):
runtime = native_error_runtime
session = runtime.open("test:///default", "host-a", timeout=5)
pool = build_error(session)
assert pool.create(0) == 0
assert pool.info()[0] == 2
# The error receipt is not a success ACK. A later successful write must
# preserve its cleanup obligation until this healthy session closes.
assert session.intent.is_file()
session.close()
assert session.failure is None
assert not list(runtime.state_dir.rglob("*.json"))
later = runtime.open("test:///default", "host-a", timeout=5)
assert later.connection.lookupByUUIDString(VM_ID).destroy() == 0
later.close()
@pytest.mark.parametrize("failure", ["read", "close"])
def test_native_error_obligation_survives_later_success_then_connection_failure(
native_error_runtime, failure):
runtime = native_error_runtime
session = runtime.open("test:///" + failure + "-hangs", "host-a", timeout=5)
assert build_error(session).create(0) == 0
if failure == "read":
with pytest.raises(ExecutionFailure) as raised:
session.connection.getHostname()
assert raised.value.outcome_unknown is True
session.close()
assert session.failure.error_code == "libvirt_execution_uncertain"
assert session.failure.outcome_unknown is True
assert session.intent.is_file()
observer = runtime.open("test:///default", "host-a", timeout=5)
assert observer.connection.getVersion() > 0
with pytest.raises(ExecutionFailure) as blocked:
observer.connection.lookupByUUIDString(VM_ID).destroy()
assert blocked.value.error_code == "libvirt_execution_blocked"
observer.close()