clan_cli: Added lazy qmp
This commit is contained in:
@@ -1,10 +1,7 @@
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import traceback
|
||||
from collections.abc import Generator
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
from time import sleep
|
||||
from typing import TYPE_CHECKING
|
||||
@@ -24,34 +21,6 @@ if TYPE_CHECKING:
|
||||
no_kvm = not os.path.exists("/dev/kvm")
|
||||
|
||||
|
||||
@contextmanager
|
||||
def monkeypatch_tempdir_with_custom_path(
|
||||
*, monkeypatch: pytest.MonkeyPatch, custom_path: str, prefix_condition: str
|
||||
) -> Generator[None, None, None]:
|
||||
# Custom wrapper function that checks the prefix and either modifies the behavior or falls back to the original
|
||||
class CustomTemporaryDirectory(tempfile.TemporaryDirectory):
|
||||
def __init__(
|
||||
self,
|
||||
suffix: str | None = None,
|
||||
prefix: str | None = None,
|
||||
dir: str | None = None, # noqa: A002
|
||||
) -> None:
|
||||
if prefix == prefix_condition:
|
||||
self.name = custom_path # Use the custom path
|
||||
self._finalizer = None # Prevent cleanup attempts on the custom path by the original finalizer
|
||||
else:
|
||||
super().__init__(suffix=suffix, prefix=prefix, dir=dir)
|
||||
|
||||
# Use ExitStack to ensure unpatching
|
||||
try:
|
||||
# Patch the TemporaryDirectory with our custom class
|
||||
monkeypatch.setattr(tempfile, "TemporaryDirectory", CustomTemporaryDirectory)
|
||||
yield # This allows the code within the 'with' block of this context manager to run
|
||||
finally:
|
||||
# Unpatch the TemporaryDirectory
|
||||
monkeypatch.undo()
|
||||
|
||||
|
||||
def run_vm_in_thread(machine_name: str) -> None:
|
||||
# runs machine and prints exceptions
|
||||
def run() -> None:
|
||||
@@ -71,14 +40,14 @@ def run_vm_in_thread(machine_name: str) -> None:
|
||||
# wait for qmp socket to exist
|
||||
def wait_vm_up(state_dir: Path) -> None:
|
||||
socket_file = state_dir / "qga.sock"
|
||||
timeout: float = 50
|
||||
while timeout > 0:
|
||||
timeout = 5.0
|
||||
while True:
|
||||
if timeout <= 0:
|
||||
raise TimeoutError(f"qga socket {socket_file} not found")
|
||||
if socket_file.exists():
|
||||
break
|
||||
sleep(0.1)
|
||||
timeout -= 0.1
|
||||
if timeout <= 0:
|
||||
raise TimeoutError(f"{socket_file} did not appear")
|
||||
|
||||
|
||||
# wait for vm to be down by checking if qga socket is down
|
||||
@@ -161,18 +130,18 @@ def test_vm_qmp(
|
||||
monkeypatch.chdir(flake.path)
|
||||
|
||||
# the state dir is a point of reference for qemu interactions as it links to the qga/qmp sockets
|
||||
qmp_state_dir = temporary_home / "vm-tmp"
|
||||
state_dir = vm_state_dir(str(flake.path), "my_machine")
|
||||
|
||||
with monkeypatch_tempdir_with_custom_path(
|
||||
monkeypatch=monkeypatch,
|
||||
custom_path=str(qmp_state_dir),
|
||||
prefix_condition="machine_vm-",
|
||||
):
|
||||
# start the VM
|
||||
run_vm_in_thread("my_machine")
|
||||
# start the VM
|
||||
run_vm_in_thread("my_machine")
|
||||
|
||||
# connect with qmp
|
||||
qmp = qmp_connect(qmp_state_dir)
|
||||
qmp = qmp_connect(state_dir)
|
||||
|
||||
# verify that issuing a command works
|
||||
# result = qmp.cmd_obj({"execute": "query-status"})
|
||||
result = qmp.command("query-status")
|
||||
assert result["status"] == "running", result
|
||||
|
||||
# shutdown machine (prevent zombie qemu processes)
|
||||
qmp.command("system_powerdown")
|
||||
|
||||
Reference in New Issue
Block a user