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

106 lines
4.6 KiB
Python

"""VPC readback threads inherit the execution that already owns each VM."""
import asyncio
import threading
import time
from types import SimpleNamespace
from test_engine_boundary import MemoryStore
from test_vpc import FakeFabric, INSTANCE_A, INSTANCE_B, payload
from virsh_panel.coordination import (
DurableCoordinator, current_execution, execution_context,
)
from virsh_panel.internal import _DurableAsyncLock
from virsh_panel.libvirt_service import VMService
from virsh_panel.vpc import VpcCoordinator, parse_vpc_plan
from virsh_panel.vpc_dataplane import VpcPrivateHostAdapter
def test_parallel_vpc_readback_inherits_vm_owner_and_can_reenter(monkeypatch):
store = MemoryStore()
admission = DurableCoordinator(store)
parent = admission.claim(operation_id="vpc-readback-parent")
plan = parse_vpc_plan(payload(hosts=[{
"host_id": "host-a", "members": [
{"member_id": "member-a", "instance_uuid": INSTANCE_A,
"private_ip_cidr": "10.20.0.10/32", "subnet_id": "subnet-a"},
{"member_id": "member-b", "instance_uuid": INSTANCE_B,
"private_ip_cidr": "10.20.0.11/32", "subnet_id": "subnet-a"},
],
}]))
vpc = VpcCoordinator(
store, FakeFabric(hosts=("host-a",)),
vm_lock=lambda host, vm_id: _DurableAsyncLock(
admission, (f"vm:{host}:{vm_id}",)),
)
entered_together = threading.Barrier(2)
seen = []
expected = None
monkeypatch.setattr(
"virsh_panel.libvirt_service.get_runtime",
lambda: SimpleNamespace(queue_timeout=0),
)
monkeypatch.setattr(
"virsh_panel.coordination.get_default_coordinator", lambda: admission)
def configure(self, vm_id, **_kwargs):
return {"xml": f"inactive:{vm_id}", "live": f"live:{vm_id}",
"seed_verified": True, "restart_required": False}
def read_pair(self, vm_id, **_kwargs):
context = current_execution()
assert context is not None
assert context.coordinator is admission
assert context.claim.token == expected.claim.token
assert context.claim.execution_id == parent.execution_id
assert threading.current_thread().name.startswith("vpc-guest-probe")
# This is the same production guard used by the precise QGA probe.
# The outer VPC instance locks already own both of these resources.
with self._operation_guard(vm_id):
assert current_execution().claim.execution_id == parent.execution_id
assert current_execution().claim.token != context.claim.token
entered_together.wait(timeout=2)
seen.append(vm_id)
assert current_execution() is context
return {"inactive": f"inactive:{vm_id}", "live": f"live:{vm_id}",
"guest_network": {"state": "confirmed", "verified": True}}
monkeypatch.setattr(VMService, "ensure_private_interface", configure)
monkeypatch.setattr(VMService, "read_private_domain_pair", read_pair)
monkeypatch.setattr(VpcPrivateHostAdapter, "__init__",
lambda self, *_args, **_kwargs: None)
monkeypatch.setattr(VpcPrivateHostAdapter, "apply", lambda self, _plan: {})
observed = {}
def observe(self, _plan, *, xml_by_instance):
observed.update(xml_by_instance)
return {"state": "active"}
monkeypatch.setattr(VpcPrivateHostAdapter, "observe", observe)
async def run():
nonlocal expected
with execution_context(admission, parent):
async with vpc._instance_locks(plan):
expected = current_execution()
result = await asyncio.to_thread(
vpc._run_host_dataplane, plan, "host-a",
SimpleNamespace(manager=object()),
{"libvirt_uri": "qemu:///system", "storage_pool": "default",
"iso_pool": "iso", "default_network": "default"},
[], None, fabric_public_keys={}, fabric_plan_digest="ef" * 32,
request_deadline=time.monotonic() + 10,
)
assert result["private_dataplane_verified"] is True
assert current_execution() is expected
assert all(claim["kind"] != "background"
for claim in admission.snapshot().values())
assert set(seen) == {INSTANCE_A, INSTANCE_B}
assert set(observed) == {INSTANCE_A, INSTANCE_B}
assert all(pair["seed_verified"] for pair in observed.values())
assert set(admission.snapshot()) == {parent.token}
assert admission.release(parent)
assert admission.snapshot() == {}
asyncio.run(run())