| 1 |
import unittest |
| 2 |
import uuid |
| 3 |
import libvirt |
| 4 |
|
| 5 |
|
| 6 |
class TestLibvirtDomainLifecycle(unittest.TestCase): |
| 7 |
"""Domain lifecycle operations test coverage""" |
| 8 |
|
| 9 |
def setUp(self): |
| 10 |
self.conn = libvirt.open("test:///default") |
| 11 |
self.dom = self.conn.lookupByName("test") |
| 12 |
|
| 13 |
def tearDown(self): |
| 14 |
self.dom = None |
| 15 |
self.conn = None |
| 16 |
|
| 17 |
def testDomainStateReturnsValidTuple(self): |
| 18 |
"""check state() returns values""" |
| 19 |
state_info = self.dom.state() |
| 20 |
|
| 21 |
self.assertIsInstance(state_info, (tuple, list)) |
| 22 |
self.assertEqual(len(state_info), 2) |
| 23 |
|
| 24 |
state, reason = state_info |
| 25 |
self.assertIsInstance(state, int) |
| 26 |
self.assertIsInstance(reason, int) |
| 27 |
|
| 28 |
self.assertGreaterEqual(state, 0) |
| 29 |
self.assertLessEqual(state, 7) |
| 30 |
|
| 31 |
def testDomainIsActiveReturnsBool(self): |
| 32 |
"""test if isActive() returns a boolean value""" |
| 33 |
result = self.dom.isActive() |
| 34 |
self.assertIsInstance(result, (bool, int)) |
| 35 |
self.assertIn(result, [0, 1, True, False]) |
| 36 |
|
| 37 |
def testDomainIsPersistentReturnsBool(self): |
| 38 |
result = self.dom.isPersistent() |
| 39 |
self.assertIsInstance(result, (bool, int)) |
| 40 |
self.assertIn(result, [0, 1, True, False]) |
| 41 |
|
| 42 |
|
| 43 |
def testDomainInfoReturnsValidTuple(self): |
| 44 |
"""check info() returns value, 5-tuple with types""" |
| 45 |
info = self.dom.info() |
| 46 |
|
| 47 |
|
| 48 |
self.assertIsInstance(info, (tuple, list)) |
| 49 |
self.assertEqual(len(info), 5) |
| 50 |
|
| 51 |
state, maxMem, memory, nrVirtCpu, cpuTime = info |
| 52 |
|
| 53 |
self.assertIsInstance(state, int) |
| 54 |
self.assertIsInstance(maxMem, int) |
| 55 |
self.assertIsInstance(memory, int) |
| 56 |
self.assertIsInstance(nrVirtCpu, int) |
| 57 |
self.assertIsInstance(cpuTime, int) |
| 58 |
|
| 59 |
self.assertGreaterEqual(state, 0) |
| 60 |
self.assertGreater(maxMem, 0) |
| 61 |
self.assertGreater(memory, 0) |
| 62 |
self.assertGreater(nrVirtCpu, 0) |
| 63 |
self.assertGreaterEqual(cpuTime, 0) |
| 64 |
|
| 65 |
def testDomainNameReturnsString(self): |
| 66 |
name = self.dom.name() |
| 67 |
|
| 68 |
self.assertIsInstance(name, str) |
| 69 |
self.assertEqual(name, "test") |
| 70 |
self.assertGreater(len(name), 0) |
| 71 |
|
| 72 |
def testDomainUUIDStringReturnsValidFormat(self): |
| 73 |
uuid_str = self.dom.UUIDString() |
| 74 |
|
| 75 |
self.assertIsInstance(uuid_str, str) |
| 76 |
self.assertEqual(len(uuid_str), 36) |
| 77 |
|
| 78 |
try: |
| 79 |
uuid_obj = uuid.UUID(uuid_str) |
| 80 |
self.assertEqual(str(uuid_obj), uuid_str) |
| 81 |
except ValueError: |
| 82 |
self.fail(f"Invalid UUID format: {uuid_str}") |
| 83 |
|
| 84 |
def testDomainIDReturnsValidValue(self): |
| 85 |
dom_id = self.dom.ID() |
| 86 |
|
| 87 |
|
| 88 |
self.assertIsInstance(dom_id, int) |
| 89 |
self.assertGreaterEqual(dom_id, 0) |
| 90 |
|
| 91 |
|
| 92 |
def testDomainCreateDestroyCycle(self): |
| 93 |
if self.dom.isActive(): |
| 94 |
ret = self.dom.destroy() |
| 95 |
self.assertEqual(ret, 0) |
| 96 |
self.assertFalse(self.dom.isActive()) |
| 97 |
|
| 98 |
ret = self.dom.create() |
| 99 |
self.assertEqual(ret, 0) |
| 100 |
|
| 101 |
|
| 102 |
self.assertTrue(self.dom.isActive()) |
| 103 |
state, _ = self.dom.state() |
| 104 |
self.assertEqual(state, libvirt.VIR_DOMAIN_RUNNING) |
| 105 |
|
| 106 |
ret = self.dom.destroy() |
| 107 |
self.assertEqual(ret, 0) |
| 108 |
|
| 109 |
|
| 110 |
self.assertFalse(self.dom.isActive()) |
| 111 |
|
| 112 |
def testDomainDestroyWhenInactive(self): |
| 113 |
"""Test that destroying an inactive domain raises an error.""" |
| 114 |
if self.dom.isActive(): |
| 115 |
self.dom.destroy() |
| 116 |
|
| 117 |
|
| 118 |
with self.assertRaises(libvirt.libvirtError): |
| 119 |
self.dom.destroy() |
| 120 |
|
| 121 |
def testDomainCreateWhenActive(self): |
| 122 |
if not self.dom.isActive(): |
| 123 |
self.dom.create() |
| 124 |
|
| 125 |
with self.assertRaises(libvirt.libvirtError): |
| 126 |
self.dom.create() |
| 127 |
|
| 128 |
self.dom.destroy() |
| 129 |
|
| 130 |
def testDomainShutdown(self): |
| 131 |
"""We can't verify the domain is actually shut down because shutdown |
| 132 |
asynchronous nature of shutdown() |
| 133 |
""" |
| 134 |
if not self.dom.isActive(): |
| 135 |
self.dom.create() |
| 136 |
|
| 137 |
ret = self.dom.shutdown() |
| 138 |
self.assertEqual(ret, 0) |
| 139 |
|
| 140 |
if self.dom.isActive(): |
| 141 |
self.dom.destroy() |
| 142 |
|
| 143 |
def testDomainReboot(self): |
| 144 |
"""Test reboot() sends reboot request.""" |
| 145 |
if not self.dom.isActive(): |
| 146 |
self.dom.create() |
| 147 |
|
| 148 |
|
| 149 |
ret = self.dom.reboot(0) |
| 150 |
self.assertEqual(ret, 0) |
| 151 |
|
| 152 |
if self.dom.isActive(): |
| 153 |
self.dom.destroy() |
| 154 |
|
| 155 |
|
| 156 |
def testDomainStateWithIsActive(self): |
| 157 |
if self.dom.isActive(): |
| 158 |
self.dom.destroy() |
| 159 |
|
| 160 |
self.assertFalse(self.dom.isActive()) |
| 161 |
state, _ = self.dom.state() |
| 162 |
self.assertIn(state, [libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_SHUTDOWN]) |
| 163 |
|
| 164 |
self.dom.create() |
| 165 |
self.assertTrue(self.dom.isActive()) |
| 166 |
state, _ = self.dom.state() |
| 167 |
self.assertEqual(state, libvirt.VIR_DOMAIN_RUNNING) |
| 168 |
|
| 169 |
self.dom.destroy() |
| 170 |
|
| 171 |
def testDomainInfoStateConsistentWithState(self): |
| 172 |
"""check if info()[0] matches state()[0]""" |
| 173 |
state_from_state, _ = self.dom.state() |
| 174 |
state_from_info = self.dom.info()[0] |
| 175 |
|
| 176 |
self.assertEqual(state_from_state, state_from_info) |
| 177 |
|
| 178 |
|
| 179 |
def testDomainIDChangesWithActivation(self): |
| 180 |
"""domain ID changes from -1 when domain becomes active |
| 181 |
expected states are as follow: -1 -> id>0 -> -1 |
| 182 |
""" |
| 183 |
if self.dom.isActive(): |
| 184 |
self.dom.destroy() |
| 185 |
|
| 186 |
self.assertEqual(self.dom.ID(), -1) |
| 187 |
|
| 188 |
self.dom.create() |
| 189 |
self.assertGreaterEqual(self.dom.ID(), 0) |
| 190 |
|
| 191 |
self.dom.destroy() |
| 192 |
self.assertEqual(self.dom.ID(), -1) |
| 193 |
|
| 194 |
|
| 195 |
class TestLibvirtDomainPersistence(unittest.TestCase): |
| 196 |
def setUp(self): |
| 197 |
self.conn = libvirt.open("test:///default") |
| 198 |
self.dom = self.conn.lookupByName("test") |
| 199 |
self.dom_xml = self.dom.XMLDesc(0) |
| 200 |
|
| 201 |
def tearDown(self): |
| 202 |
try: |
| 203 |
self.conn.lookupByName("test") |
| 204 |
except libvirt.libvirtError: |
| 205 |
self.conn.defineXML(self.dom_xml) |
| 206 |
|
| 207 |
self.dom = None |
| 208 |
self.conn = None |
| 209 |
|
| 210 |
def testDomainIsPersistent(self): |
| 211 |
self.assertTrue(self.dom.isPersistent()) |
| 212 |
|
| 213 |
def testDomainXMLDescReturnsValidXML(self): |
| 214 |
"""check if XMLDesc() returns valid XML string""" |
| 215 |
xml = self.dom.XMLDesc(0) |
| 216 |
|
| 217 |
self.assertIsInstance(xml, str) |
| 218 |
self.assertGreater(len(xml), 0) |
| 219 |
|
| 220 |
self.assertIn("<domain", xml) |
| 221 |
self.assertIn("</domain>", xml) |
| 222 |
self.assertIn("test", xml) |
| 223 |
|
| 224 |
def testDomainUndefineRemovesPersistence(self): |
| 225 |
"""this test modifies the test domain, but tearDown will restore |
| 226 |
it to original state |
| 227 |
""" |
| 228 |
|
| 229 |
if self.dom.isActive(): |
| 230 |
self.dom.destroy() |
| 231 |
|
| 232 |
ret = self.dom.undefine() |
| 233 |
self.assertEqual(ret, 0) |
| 234 |
|
| 235 |
|
| 236 |
with self.assertRaises(libvirt.libvirtError): |
| 237 |
self.conn.lookupByName("test") |
| 238 |
|
| 239 |
def testDomainDefineCreatesDefinition(self): |
| 240 |
"""create domain from XML and check its persistance""" |
| 241 |
new_dom_xml = """ |
| 242 |
<domain type='test'> |
| 243 |
<name>test-define-temp</name> |
| 244 |
<uuid>12345678-1234-1234-1234-123456789abc</uuid> |
| 245 |
<memory unit='KiB'>524288</memory> |
| 246 |
<vcpu>1</vcpu> |
| 247 |
<os> |
| 248 |
<type>hvm</type> |
| 249 |
</os> |
| 250 |
</domain> |
| 251 |
""" |
| 252 |
|
| 253 |
new_dom = self.conn.defineXML(new_dom_xml) |
| 254 |
self.assertIsNotNone(new_dom) |
| 255 |
self.assertEqual(new_dom.name(), "test-define-temp") |
| 256 |
self.assertTrue(new_dom.isPersistent()) |
| 257 |
|
| 258 |
self.assertFalse(new_dom.isActive()) |
| 259 |
|
| 260 |
new_dom.undefine() |
| 261 |
|
| 262 |
|
| 263 |
class TestLibvirtDomainLookup(unittest.TestCase): |
| 264 |
|
| 265 |
def setUp(self): |
| 266 |
self.conn = libvirt.open("test:///default") |
| 267 |
self.dom = self.conn.lookupByName("test") |
| 268 |
|
| 269 |
def tearDown(self): |
| 270 |
self.dom = None |
| 271 |
self.conn = None |
| 272 |
|
| 273 |
def testLookupDomainByName(self): |
| 274 |
dom = self.conn.lookupByName("test") |
| 275 |
|
| 276 |
self.assertIsNotNone(dom) |
| 277 |
self.assertEqual(dom.name(), "test") |
| 278 |
self.assertIsInstance(dom, libvirt.virDomain) |
| 279 |
|
| 280 |
def testLookupByNameWithInvalidName(self): |
| 281 |
with self.assertRaises(libvirt.libvirtError) as ctx: |
| 282 |
self.conn.lookupByName("nonexistent-domain-12345") |
| 283 |
err = ctx.exception |
| 284 |
self.assertIsNotNone(err) |
| 285 |
|
| 286 |
def testLookupDomainByUUID(self): |
| 287 |
"""libvirt C API function virDomainLookupByUUID() expects a raw 16-byte UUID, |
| 288 |
not a UUID string. UUIDs are 128 bits = 16 bytes after '-' removal. |
| 289 |
""" |
| 290 |
uuid_str = self.dom.UUIDString() |
| 291 |
|
| 292 |
|
| 293 |
uuid_bytes = bytes.fromhex(uuid_str.replace("-", "")) |
| 294 |
|
| 295 |
dom = self.conn.lookupByUUID(uuid_bytes) |
| 296 |
self.assertIsNotNone(dom) |
| 297 |
self.assertEqual(dom.name(), "test") |
| 298 |
self.assertEqual(dom.UUIDString(), uuid_str) |
| 299 |
|
| 300 |
def testLookupDomainByUUIDString(self): |
| 301 |
uuid_str = self.dom.UUIDString() |
| 302 |
|
| 303 |
dom = self.conn.lookupByUUIDString(uuid_str) |
| 304 |
self.assertIsNotNone(dom) |
| 305 |
self.assertEqual(dom.name(), "test") |
| 306 |
self.assertEqual(dom.UUIDString(), uuid_str) |
| 307 |
|
| 308 |
def testLookupDomainByID(self): |
| 309 |
if not self.dom.isActive(): |
| 310 |
self.dom.create() |
| 311 |
|
| 312 |
dom_id = self.dom.ID() |
| 313 |
self.assertGreaterEqual(dom_id, 0) |
| 314 |
|
| 315 |
dom = self.conn.lookupByID(dom_id) |
| 316 |
self.assertIsNotNone(dom) |
| 317 |
self.assertEqual(dom.name(), "test") |
| 318 |
self.assertEqual(dom.ID(), dom_id) |
| 319 |
|
| 320 |
if self.dom.isActive(): |
| 321 |
self.dom.destroy() |
| 322 |
|
| 323 |
def testLookupByIDForInactiveDomain(self): |
| 324 |
"""lookupByID() should fail for inactive domain""" |
| 325 |
if self.dom.isActive(): |
| 326 |
self.dom.destroy() |
| 327 |
|
| 328 |
|
| 329 |
self.assertEqual(self.dom.ID(), -1) |
| 330 |
|
| 331 |
|
| 332 |
with self.assertRaises(libvirt.libvirtError): |
| 333 |
self.conn.lookupByID(-1) |
| 334 |
|
| 335 |
|
| 336 |
class TestLibvirtDomainListOperations(unittest.TestCase): |
| 337 |
|
| 338 |
def setUp(self): |
| 339 |
self.conn = libvirt.open("test:///default") |
| 340 |
|
| 341 |
def tearDown(self): |
| 342 |
self.conn = None |
| 343 |
|
| 344 |
def testListAllDomainsReturnsList(self): |
| 345 |
domains = self.conn.listAllDomains() |
| 346 |
|
| 347 |
self.assertIsInstance(domains, list) |
| 348 |
self.assertGreater(len(domains), 0) |
| 349 |
|
| 350 |
|
| 351 |
for dom in domains: |
| 352 |
self.assertIsInstance(dom, libvirt.virDomain) |
| 353 |
self.assertIsInstance(dom.name(), str) |
| 354 |
|
| 355 |
def testListAllDomains(self): |
| 356 |
"""it is expected that "test" domain will be within the list""" |
| 357 |
domains = self.conn.listAllDomains() |
| 358 |
names = [dom.name() for dom in domains] |
| 359 |
|
| 360 |
self.assertIn("test", names) |
| 361 |
|
| 362 |
def testListAllActiveDomains(self): |
| 363 |
dom = self.conn.lookupByName("test") |
| 364 |
|
| 365 |
if not dom.isActive(): |
| 366 |
dom.create() |
| 367 |
|
| 368 |
|
| 369 |
active_domains = self.conn.listAllDomains( |
| 370 |
libvirt.VIR_CONNECT_LIST_DOMAINS_ACTIVE |
| 371 |
) |
| 372 |
|
| 373 |
self.assertIsInstance(active_domains, list) |
| 374 |
self.assertGreater(len(active_domains), 0) |
| 375 |
|
| 376 |
for d in active_domains: |
| 377 |
self.assertTrue(d.isActive()) |
| 378 |
|
| 379 |
names = [d.name() for d in active_domains] |
| 380 |
self.assertIn("test", names) |
| 381 |
|
| 382 |
if dom.isActive(): |
| 383 |
dom.destroy() |
| 384 |
|
| 385 |
def testListAllInactiveDomains(self): |
| 386 |
dom = self.conn.lookupByName("test") |
| 387 |
|
| 388 |
if dom.isActive(): |
| 389 |
dom.destroy() |
| 390 |
|
| 391 |
|
| 392 |
inactive_domains = self.conn.listAllDomains( |
| 393 |
libvirt.VIR_CONNECT_LIST_DOMAINS_INACTIVE |
| 394 |
) |
| 395 |
|
| 396 |
self.assertIsInstance(inactive_domains, list) |
| 397 |
self.assertGreater(len(inactive_domains), 0) |
| 398 |
|
| 399 |
for d in inactive_domains: |
| 400 |
self.assertFalse(d.isActive()) |
| 401 |
|
| 402 |
names = [d.name() for d in inactive_domains] |
| 403 |
self.assertIn("test", names) |
| 404 |
|
| 405 |
def testListAllPersistentDomains(self): |
| 406 |
persistent_domains = self.conn.listAllDomains( |
| 407 |
libvirt.VIR_CONNECT_LIST_DOMAINS_PERSISTENT |
| 408 |
) |
| 409 |
|
| 410 |
self.assertIsInstance(persistent_domains, list) |
| 411 |
self.assertGreater(len(persistent_domains), 0) |
| 412 |
|
| 413 |
for d in persistent_domains: |
| 414 |
self.assertTrue(d.isPersistent()) |
| 415 |
|
| 416 |
names = [d.name() for d in persistent_domains] |
| 417 |
self.assertIn("test", names) |
| 418 |
|
| 419 |
|
| 420 |
if __name__ == "__main__": |
| 421 |
unittest.main() |