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.
304 lines
7.7 KiB
Python
304 lines
7.7 KiB
Python
"""Importable libvirt stand-in for real RPC subprocess boundary tests.
|
|
|
|
This module deliberately lives outside ``virsh_panel``. Production requests
|
|
cannot choose it; tests explicitly inject its module name into the supervisor.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
|
|
VIR_ERR_NO_DOMAIN = 42
|
|
VIR_ERR_OPERATION_FAILED = 9
|
|
VIR_ERR_NO_STORAGE_VOL = 50
|
|
|
|
|
|
class libvirtError(Exception):
|
|
def __init__(self, message, err=None):
|
|
super().__init__(message)
|
|
self.err = err or (
|
|
VIR_ERR_NO_DOMAIN, 10, message, 2,
|
|
"domain", "detail", None, 17, 23,
|
|
)
|
|
|
|
def get_error_code(self):
|
|
return self.err[0]
|
|
|
|
def get_error_domain(self):
|
|
return self.err[1]
|
|
|
|
def get_error_message(self):
|
|
return self.err[2]
|
|
|
|
def get_error_level(self):
|
|
return self.err[3]
|
|
|
|
def get_str1(self):
|
|
return self.err[4]
|
|
|
|
def get_str2(self):
|
|
return self.err[5]
|
|
|
|
def get_str3(self):
|
|
return self.err[6]
|
|
|
|
def get_int1(self):
|
|
return self.err[7]
|
|
|
|
def get_int2(self):
|
|
return self.err[8]
|
|
|
|
|
|
class virDomain:
|
|
def __init__(self, connection, uuid="00000000-0000-4000-8000-000000000001"):
|
|
self.connection = connection
|
|
self.uuid = uuid
|
|
|
|
def UUIDString(self):
|
|
return self.uuid
|
|
|
|
def name(self):
|
|
return "rpc-guest"
|
|
|
|
def info(self):
|
|
return [1, 2097152, 1048576, 2, 12345]
|
|
|
|
def state(self, flags=0):
|
|
return (1, 0)
|
|
|
|
def isActive(self):
|
|
return 1
|
|
|
|
def XMLDesc(self, flags=0):
|
|
return f"<domain><name>rpc-guest</name><uuid>{self.uuid}</uuid></domain>"
|
|
|
|
def snapshotLookupByName(self, name, flags=0):
|
|
return virDomainSnapshot(self, name)
|
|
|
|
def destroy(self):
|
|
if self.connection.uri == "test:///write-hangs":
|
|
self.connection.hang()
|
|
return 0
|
|
|
|
|
|
class virDomainSnapshot:
|
|
def __init__(self, domain, name):
|
|
self.domain = domain
|
|
self.snapshot_name = name
|
|
|
|
def getName(self):
|
|
return self.snapshot_name
|
|
|
|
def getDomain(self):
|
|
return self.domain
|
|
|
|
|
|
class virStorageVol:
|
|
def __init__(self, pool, name="disk.qcow2"):
|
|
self.pool = pool
|
|
self.volume_name = name
|
|
self.data = b"initial-volume-bytes\x00\xff"
|
|
|
|
def name(self):
|
|
return self.volume_name
|
|
|
|
def info(self):
|
|
return [0, 4096, len(self.data)]
|
|
|
|
def upload(self, stream, offset, length, flags=0):
|
|
stream.volume = self
|
|
stream.writing = True
|
|
stream.offset = offset
|
|
stream.expected = length
|
|
stream.data = b""
|
|
return 0
|
|
|
|
def download(self, stream, offset, length, flags=0):
|
|
stream.volume = self
|
|
stream.writing = False
|
|
stream.offset = 0
|
|
stream.data = self.data[offset:offset + length]
|
|
return 0
|
|
|
|
|
|
class virStoragePool:
|
|
def __init__(self, connection):
|
|
self.connection = connection
|
|
self.volume = virStorageVol(self)
|
|
|
|
def name(self):
|
|
return "images"
|
|
|
|
def info(self):
|
|
return [2, 1000000, 4096, 995904]
|
|
|
|
def listAllVolumes(self, flags=0):
|
|
return [self.volume]
|
|
|
|
def storageVolLookupByName(self, name):
|
|
if name != self.volume.name():
|
|
raise libvirtError("storage volume is absent", (
|
|
VIR_ERR_NO_STORAGE_VOL, 18, "storage volume is absent", 2,
|
|
name, None, None, 0, 0,
|
|
))
|
|
return self.volume
|
|
|
|
|
|
class virStream:
|
|
def __init__(self, connection):
|
|
self.connection = connection
|
|
self.volume = None
|
|
self.writing = False
|
|
self.offset = 0
|
|
self.data = b""
|
|
|
|
def send(self, data):
|
|
# A short write catches callers that incorrectly assume send-all.
|
|
count = min(len(data), 3)
|
|
self.data += data[:count]
|
|
return count
|
|
|
|
def recv(self, length):
|
|
chunk = self.data[self.offset:self.offset + min(length, 4)]
|
|
self.offset += len(chunk)
|
|
return chunk
|
|
|
|
def finish(self):
|
|
if self.writing:
|
|
self.volume.data = self.data
|
|
return 0
|
|
|
|
def abort(self):
|
|
return 0
|
|
|
|
|
|
class virNWFilter:
|
|
def name(self):
|
|
return "clean-traffic"
|
|
|
|
def XMLDesc(self, flags=0):
|
|
return "<filter name='clean-traffic'/>"
|
|
|
|
|
|
class virConnect:
|
|
def __init__(self, uri):
|
|
self.uri = uri
|
|
self.domain = virDomain(self)
|
|
self.pool = virStoragePool(self)
|
|
|
|
def getURI(self):
|
|
return self.uri
|
|
|
|
def getHostname(self):
|
|
if self.uri == "test:///read-hangs":
|
|
self.hang()
|
|
return "rpc-test-host"
|
|
|
|
def getInfo(self):
|
|
return ["x86_64", 8192, 4, 3000, 1, 1, 4, 1]
|
|
|
|
def getVersion(self):
|
|
return 10000000
|
|
|
|
def lookupByUUIDString(self, uuid):
|
|
if uuid != self.domain.UUIDString():
|
|
raise libvirtError("domain is absent")
|
|
return self.domain
|
|
|
|
def listAllDomains(self, flags=0):
|
|
return [self.domain]
|
|
|
|
def getAllDomainStats(self, stats=0, flags=0):
|
|
return [(self.domain, {"cpu.time": 12345, "nested": [None, True, 1.25]})]
|
|
|
|
def domainListGetStats(self, domains, stats=0, flags=0):
|
|
return [(domain, {"cpu.time": 12345}) for domain in domains]
|
|
|
|
def storagePoolLookupByName(self, name):
|
|
assert name == "images"
|
|
return self.pool
|
|
|
|
def newStream(self, flags=0):
|
|
return virStream(self)
|
|
|
|
def nwfilterLookupByName(self, name):
|
|
assert name == "clean-traffic"
|
|
return virNWFilter()
|
|
|
|
def echo(self, value):
|
|
return value
|
|
|
|
def process_id(self):
|
|
return os.getpid()
|
|
|
|
def sleep(self, seconds):
|
|
time.sleep(seconds)
|
|
return "completed"
|
|
|
|
def hang(self, marker=None, ignore_term=False):
|
|
if ignore_term:
|
|
signal.signal(signal.SIGTERM, signal.SIG_IGN)
|
|
if marker is not None:
|
|
Path(marker).write_text(str(os.getpid()), encoding="ascii")
|
|
while True:
|
|
time.sleep(60)
|
|
|
|
def crash(self, code=17):
|
|
os._exit(code)
|
|
|
|
def start_stubborn_child(self, heartbeat):
|
|
# Model an SSH descendant which ignores TERM after its RPC parent exits.
|
|
program = (
|
|
"import os,signal,sys,time; from pathlib import Path; "
|
|
"signal.signal(signal.SIGTERM, signal.SIG_IGN); "
|
|
"p=Path(sys.argv[1]); "
|
|
"exec('while True:\\n p.write_text(str(time.monotonic_ns()))\\n time.sleep(0.01)')"
|
|
)
|
|
child = subprocess.Popen(
|
|
[sys.executable, "-c", program, heartbeat],
|
|
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL,
|
|
)
|
|
deadline = time.monotonic() + 2.0
|
|
while not Path(heartbeat).exists() and time.monotonic() < deadline:
|
|
time.sleep(0.005)
|
|
if not Path(heartbeat).exists():
|
|
child.kill()
|
|
child.wait(timeout=2.0)
|
|
raise RuntimeError("test descendant did not start")
|
|
return child.pid
|
|
|
|
def ordinary_error(self):
|
|
raise ValueError("backend rejected input")
|
|
|
|
def close(self):
|
|
if self.uri == "test:///close-hangs":
|
|
self.hang()
|
|
return 0
|
|
|
|
|
|
def open(uri):
|
|
if uri == "test:///open-hangs":
|
|
while True:
|
|
time.sleep(60)
|
|
if uri == "test:///open-error":
|
|
raise libvirtError("connection failed", (
|
|
VIR_ERR_OPERATION_FAILED, 10, "connection failed", 2,
|
|
None, None, None, 0, 0,
|
|
))
|
|
return virConnect(uri)
|
|
|
|
|
|
def qemuAgentCommand(domain, command, timeout, flags):
|
|
payload = json.loads(command)
|
|
return json.dumps({"return": {"uuid": domain.UUIDString(),
|
|
"execute": payload["execute"],
|
|
"timeout": timeout, "flags": flags}})
|