1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
import asyncio
import libvirt
import libvirtaio
import sys
import unittest
import pytest
class TestLibvirtAio(unittest.TestCase):
async def _run(self, register):
def lifecycleCallback(conn, dom, event, detail, domainChangedEvent):
if (event == libvirt.VIR_DOMAIN_EVENT_STOPPED or
event == libvirt.VIR_DOMAIN_EVENT_STARTED):
domainChangedEvent.set()
if register:
libvirtEvents = libvirtaio.virEventRegisterAsyncIOImpl()
else:
libvirtEvents = libvirtaio.getCurrentImpl()
conn = libvirt.open("test:///default")
dom = conn.lookupByName("test")
eventRegistered = False
domainStopped = False
try:
# Ensure the VM is running.
self.assertEqual([libvirt.VIR_DOMAIN_RUNNING, libvirt.VIR_DOMAIN_RUNNING_UNKNOWN], dom.state())
self.assertTrue(libvirtEvents.is_idle())
# Register VM start/stopped event handler.
domainChangedEvent = asyncio.Event()
conn.domainEventRegisterAny(dom, libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE, lifecycleCallback, domainChangedEvent)
eventRegistered = True
self.assertFalse(libvirtEvents.is_idle())
# Stop the VM.
dom.destroy()
domainStopped = True
# Ensure domain stopped event is received.
await asyncio.wait_for(domainChangedEvent.wait(), 2)
self.assertEqual([libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_SHUTOFF_DESTROYED], dom.state())
# Start the VM.
domainChangedEvent.clear()
domainStopped = False
dom.create()
# Ensure domain started event is received.
await asyncio.wait_for(domainChangedEvent.wait(), 2)
self.assertEqual([libvirt.VIR_DOMAIN_RUNNING, libvirt.VIR_DOMAIN_RUNNING_BOOTED], dom.state())
self.assertFalse(libvirtEvents.is_idle())
# Deregister the VM start/stopped event handler.
eventRegistered = False
conn.domainEventDeregisterAny(libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE)
# Wait for event queue to clear.
await libvirtEvents.drain()
# Make sure event queue is cleared.
self.assertTrue(libvirtEvents.is_idle())
finally:
if eventRegistered:
conn.domainEventDeregisterAny(libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE)
if domainStopped:
dom.create()
@pytest.mark.separate_process
def testEventsWithManualLoopSetup(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self._run(register=True))
loop.close()
asyncio.set_event_loop(None)
@pytest.mark.separate_process
@unittest.skipIf(sys.version_info < (3,7), "test requires Python 3.7+")
def testEventsWithAsyncioRun(self):
asyncio.run(self._run(register=True))
@pytest.mark.separate_process
@unittest.skipIf(sys.version_info >= (3,10), "test not compatible with Python 3.10+")
def testEventsPreInit(self):
# Initialize libvirt events before setting the event loop. This is not recommended.
# But is supported in older version of Python for the sake of back-compat.
loop = asyncio.new_event_loop()
libvirtaio.virEventRegisterAsyncIOImpl(loop)
asyncio.set_event_loop(loop)
loop.run_until_complete(self._run(register=False))
loop.close()
asyncio.set_event_loop(None)
@pytest.mark.separate_process
def testEventsImplicitLoopInit(self):
# Allow virEventRegisterAsyncIOImpl() to init the event loop by calling
# asyncio.get_event_loop(). This is not recommended and probably only works by
# accident. But is supported for now for the sake of back-compat. For Python
# 3.10+, asyncio will report deprecation warnings.
libvirtaio.virEventRegisterAsyncIOImpl()
loop = asyncio.get_event_loop()
loop.run_until_complete(self._run(register=False))
loop.close()
asyncio.set_event_loop(None)
|