From f6e880b19565b29359fcfb39f9cf96cab2454a33 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Wed, 2 Sep 2020 13:38:48 -0400 Subject: [PATCH] uitests: Add connection nodedev and agent event testing Signed-off-by: Cole Robinson --- tests/uitests/test_connection.py | 8 +++++++ virtManager/connection.py | 16 ++++++++++++-- virtManager/lib/testmock.py | 37 ++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/tests/uitests/test_connection.py b/tests/uitests/test_connection.py index ec970c63e..2c0e22ab7 100644 --- a/tests/uitests/test_connection.py +++ b/tests/uitests/test_connection.py @@ -47,3 +47,11 @@ class UITestConnection(uiutils.UITestCase): self.sleep(1) manager.find(r"^test testdriver.xml - Not Connected", "table cell") uiutils.check(lambda: manager.active) + + def testConnectionFakeEvents(self): + self.app.open( + extra_opts=["--test-options=fake-nodedev-event=computer", + "--test-options=fake-agent-event=test-many-devices"]) + manager = self.app.topwin + self.sleep(2.5) + uiutils.check(lambda: manager.active) diff --git a/virtManager/connection.py b/virtManager/connection.py index b23311687..00990e3b1 100644 --- a/virtManager/connection.py +++ b/virtManager/connection.py @@ -16,6 +16,7 @@ from virtinst import log from virtinst import pollhelpers from .lib import connectauth +from .lib import testmock from .baseclass import vmmGObject from .lib.libvirtenummap import LibvirtEnumMap from .object.domain import vmmDomain @@ -754,6 +755,10 @@ class vmmConnection(vmmGObject): self._domain_cb_ids.append( self.get_backend().domainEventRegisterAny( None, eventid, cb, eventname)) + + if (eventname == "VIR_DOMAIN_EVENT_ID_AGENT_LIFECYCLE" and + self.config.CLITestOptions.fake_agent_event): + testmock.schedule_fake_agent_event(self, cb) except Exception as e: # pragma: no cover log.debug("Error registering %s event: %s", eventname, e) @@ -802,12 +807,19 @@ class vmmConnection(vmmGObject): eventid = getattr(libvirt, "VIR_NODE_DEVICE_EVENT_ID_LIFECYCLE", 0) updateid = getattr(libvirt, "VIR_NODE_DEVICE_EVENT_ID_UPDATE", 1) + lifecycle_cb = self._node_device_lifecycle_event + update_cb = self._node_device_update_event + self._node_device_cb_ids.append( self.get_backend().nodeDeviceEventRegisterAny( - None, eventid, self._node_device_lifecycle_event, None)) + None, eventid, lifecycle_cb, None)) self._node_device_cb_ids.append( self.get_backend().nodeDeviceEventRegisterAny( - None, updateid, self._node_device_update_event, None)) + None, updateid, update_cb, None)) + + if self.config.CLITestOptions.fake_nodedev_event: + testmock.schedule_fake_nodedev_event(self, + lifecycle_cb, update_cb) self.using_node_device_events = True log.debug("Using node device events") diff --git a/virtManager/lib/testmock.py b/virtManager/lib/testmock.py index 8edfb61e0..15dfd298c 100644 --- a/virtManager/lib/testmock.py +++ b/virtManager/lib/testmock.py @@ -62,6 +62,39 @@ def fake_dhcp_leases(): return ret +def schedule_fake_agent_event(conn, cb): + import libvirt + vmname = conn.config.CLITestOptions.fake_agent_event + backend = conn.get_backend() + state = libvirt.VIR_CONNECT_DOMAIN_EVENT_AGENT_LIFECYCLE_STATE_CONNECTED + reason = libvirt.VIR_CONNECT_DOMAIN_EVENT_AGENT_LIFECYCLE_REASON_CHANNEL + + def time_cb(): + dom = backend.lookupByName(vmname) + cb(backend, dom, state, reason, None) + + conn.timeout_add(1000, time_cb) + + +def schedule_fake_nodedev_event(conn, lifecycle_cb, update_cb): + import libvirt + nodename = conn.config.CLITestOptions.fake_nodedev_event + backend = conn.get_backend() + + def lifecycle_cb_wrapper(): + nodedev = backend.nodeDeviceLookupByName(nodename) + state = libvirt.VIR_NODE_DEVICE_EVENT_CREATED + reason = 0 + lifecycle_cb(backend, nodedev, state, reason, None) + + def update_cb_wrapper(): + nodedev = backend.nodeDeviceLookupByName(nodename) + update_cb(backend, nodedev, None) + + conn.timeout_add(1000, lifecycle_cb_wrapper) + conn.timeout_add(2000, update_cb_wrapper) + + class CLITestOptionsClass: """ Helper class for parsing and tracking --test-* options. @@ -117,6 +150,8 @@ class CLITestOptionsClass: fail to test some connection code paths * conn-crash: Test connection abruptly closing like when libvirtd is restarted. + * fake-agent-event: Fake a qemu guest agent API event + * fake-nodedev-event: Fake nodedev API events """ def __init__(self, test_options_str): optset = set() @@ -157,6 +192,8 @@ class CLITestOptionsClass: self.fake_systray = _get("fake-systray") self.object_blacklist = _get_value("object-blacklist") self.conn_crash = _get("conn-crash") + self.fake_agent_event = _get_value("fake-agent-event") + self.fake_nodedev_event = _get_value("fake-nodedev-event") if optset: # pragma: no cover raise RuntimeError("Unknown --test-options keys: %s" % optset)