| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# (c) 2025, Joey Zhang <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
import unittest |
| 7 |
|
| 8 |
from ansible_collections.community.libvirt.tests.unit.compat import mock |
| 9 |
from ansible_collections.community.libvirt.plugins.module_utils.libvirt import ( |
| 10 |
LibvirtConnection, |
| 11 |
VMNotFound, |
| 12 |
VIRT_FAILED, |
| 13 |
VIRT_SUCCESS, |
| 14 |
VIRT_UNAVAILABLE, |
| 15 |
VIRT_STATE_NAME_MAP, |
| 16 |
HAS_VIRT, |
| 17 |
HAS_XML |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
class TestLibvirtConnectionInit(unittest.TestCase): |
| 22 |
"""Test LibvirtConnection initialization""" |
| 23 |
|
| 24 |
def setUp(self): |
| 25 |
"""Set up test fixtures""" |
| 26 |
self.mock_module = mock.Mock() |
| 27 |
self.mock_module.run_command.return_value = (0, 'Linux test 5.10.0', '') |
| 28 |
|
| 29 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 30 |
def test_init_normal_connection(self, mock_libvirt): |
| 31 |
"""Test initialization with normal libvirt connection""" |
| 32 |
mock_conn = mock.Mock() |
| 33 |
mock_libvirt.open.return_value = mock_conn |
| 34 |
|
| 35 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 36 |
|
| 37 |
self.assertEqual(conn.module, self.mock_module) |
| 38 |
self.assertEqual(conn.conn, mock_conn) |
| 39 |
mock_libvirt.open.assert_called_once_with('qemu:///system') |
| 40 |
self.mock_module.run_command.assert_called_once_with('uname -r') |
| 41 |
|
| 42 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 43 |
def test_init_xen_connection(self, mock_libvirt): |
| 44 |
"""Test initialization with Xen hypervisor detection""" |
| 45 |
mock_conn = mock.Mock() |
| 46 |
mock_libvirt.open.return_value = mock_conn |
| 47 |
self.mock_module.run_command.return_value = (0, 'Linux test 5.10.0-xen', '') |
| 48 |
|
| 49 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 50 |
|
| 51 |
self.assertEqual(conn.conn, mock_conn) |
| 52 |
mock_libvirt.open.assert_called_once_with(None) |
| 53 |
|
| 54 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 55 |
def test_init_esx_connection(self, mock_libvirt): |
| 56 |
"""Test initialization with ESX connection""" |
| 57 |
mock_conn = mock.Mock() |
| 58 |
mock_libvirt.VIR_CRED_AUTHNAME = 1 |
| 59 |
mock_libvirt.VIR_CRED_NOECHOPROMPT = 2 |
| 60 |
mock_libvirt.openAuth.return_value = mock_conn |
| 61 |
|
| 62 |
conn = LibvirtConnection('esx://host/system', self.mock_module) |
| 63 |
|
| 64 |
self.assertEqual(conn.conn, mock_conn) |
| 65 |
# Verify the call was made with expected structure |
| 66 |
args, kwargs = mock_libvirt.openAuth.call_args |
| 67 |
self.assertEqual(args[0], 'esx://host/system') |
| 68 |
self.assertEqual(args[1], [[1, 2], [], None]) |
| 69 |
|
| 70 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 71 |
def test_init_connection_failure(self, mock_libvirt): |
| 72 |
"""Test initialization when connection fails""" |
| 73 |
mock_libvirt.open.return_value = None |
| 74 |
|
| 75 |
with self.assertRaises(Exception) as context: |
| 76 |
LibvirtConnection('qemu:///system', self.mock_module) |
| 77 |
|
| 78 |
self.assertEqual(str(context.exception), "hypervisor connection failure") |
| 79 |
|
| 80 |
|
| 81 |
class TestLibvirtConnectionVMFinding(unittest.TestCase): |
| 82 |
"""Test VM finding functionality""" |
| 83 |
|
| 84 |
def setUp(self): |
| 85 |
"""Set up test fixtures""" |
| 86 |
self.mock_module = mock.Mock() |
| 87 |
self.mock_module.run_command.return_value = (0, 'Linux test 5.10.0', '') |
| 88 |
|
| 89 |
# Create mock VM objects |
| 90 |
self.mock_vm1 = mock.Mock() |
| 91 |
self.mock_vm1.name.return_value = 'test-vm1' |
| 92 |
self.mock_vm2 = mock.Mock() |
| 93 |
self.mock_vm2.name.return_value = 'test-vm2' |
| 94 |
self.mock_vms = [self.mock_vm1, self.mock_vm2] |
| 95 |
|
| 96 |
# Create mock connection |
| 97 |
self.mock_conn = mock.Mock() |
| 98 |
self.mock_conn.listAllDomains.return_value = self.mock_vms |
| 99 |
|
| 100 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 101 |
def test_find_vm_by_name(self, mock_libvirt): |
| 102 |
"""Test finding VM by name""" |
| 103 |
mock_libvirt.open.return_value = self.mock_conn |
| 104 |
|
| 105 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 106 |
result = conn.find_vm('test-vm1') |
| 107 |
|
| 108 |
self.assertEqual(result, self.mock_vm1) |
| 109 |
self.mock_conn.listAllDomains.assert_called_once() |
| 110 |
|
| 111 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 112 |
def test_find_vm_not_found(self, mock_libvirt): |
| 113 |
"""Test finding non-existent VM""" |
| 114 |
mock_libvirt.open.return_value = self.mock_conn |
| 115 |
|
| 116 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 117 |
|
| 118 |
with self.assertRaises(VMNotFound) as context: |
| 119 |
conn.find_vm('nonexistent-vm') |
| 120 |
|
| 121 |
self.assertEqual(str(context.exception), "virtual machine nonexistent-vm not found") |
| 122 |
|
| 123 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 124 |
def test_find_vm_list_all(self, mock_libvirt): |
| 125 |
"""Test finding all VMs with vmid=-1""" |
| 126 |
mock_libvirt.open.return_value = self.mock_conn |
| 127 |
|
| 128 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 129 |
result = conn.find_vm(-1) |
| 130 |
|
| 131 |
self.assertEqual(result, self.mock_vms) |
| 132 |
self.mock_conn.listAllDomains.assert_called_once() |
| 133 |
|
| 134 |
|
| 135 |
class TestLibvirtConnectionLifecycleOperations(unittest.TestCase): |
| 136 |
"""Test VM lifecycle operations""" |
| 137 |
|
| 138 |
def setUp(self): |
| 139 |
"""Set up test fixtures""" |
| 140 |
self.mock_module = mock.Mock() |
| 141 |
self.mock_module.run_command.return_value = (0, 'Linux test 5.10.0', '') |
| 142 |
|
| 143 |
self.mock_vm = mock.Mock() |
| 144 |
self.mock_vm.name.return_value = 'test-vm' |
| 145 |
self.mock_vm.shutdown.return_value = 0 |
| 146 |
self.mock_vm.suspend.return_value = 0 |
| 147 |
self.mock_vm.resume.return_value = 0 |
| 148 |
self.mock_vm.create.return_value = 0 |
| 149 |
self.mock_vm.destroy.return_value = 0 |
| 150 |
self.mock_vm.undefineFlags.return_value = 0 |
| 151 |
|
| 152 |
self.mock_conn = mock.Mock() |
| 153 |
self.mock_conn.listAllDomains.return_value = [self.mock_vm] |
| 154 |
|
| 155 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 156 |
def test_shutdown(self, mock_libvirt): |
| 157 |
"""Test VM shutdown""" |
| 158 |
mock_libvirt.open.return_value = self.mock_conn |
| 159 |
|
| 160 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 161 |
result = conn.shutdown('test-vm') |
| 162 |
|
| 163 |
self.assertEqual(result, 0) |
| 164 |
self.mock_vm.shutdown.assert_called_once() |
| 165 |
|
| 166 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 167 |
def test_pause(self, mock_libvirt): |
| 168 |
"""Test VM pause (alias for suspend)""" |
| 169 |
mock_libvirt.open.return_value = self.mock_conn |
| 170 |
|
| 171 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 172 |
result = conn.pause('test-vm') |
| 173 |
|
| 174 |
self.assertEqual(result, 0) |
| 175 |
self.mock_vm.suspend.assert_called_once() |
| 176 |
|
| 177 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 178 |
def test_unpause(self, mock_libvirt): |
| 179 |
"""Test VM unpause (alias for resume)""" |
| 180 |
mock_libvirt.open.return_value = self.mock_conn |
| 181 |
|
| 182 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 183 |
result = conn.unpause('test-vm') |
| 184 |
|
| 185 |
self.assertEqual(result, 0) |
| 186 |
self.mock_vm.resume.assert_called_once() |
| 187 |
|
| 188 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 189 |
def test_suspend(self, mock_libvirt): |
| 190 |
"""Test VM suspend""" |
| 191 |
mock_libvirt.open.return_value = self.mock_conn |
| 192 |
|
| 193 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 194 |
result = conn.suspend('test-vm') |
| 195 |
|
| 196 |
self.assertEqual(result, 0) |
| 197 |
self.mock_vm.suspend.assert_called_once() |
| 198 |
|
| 199 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 200 |
def test_resume(self, mock_libvirt): |
| 201 |
"""Test VM resume""" |
| 202 |
mock_libvirt.open.return_value = self.mock_conn |
| 203 |
|
| 204 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 205 |
result = conn.resume('test-vm') |
| 206 |
|
| 207 |
self.assertEqual(result, 0) |
| 208 |
self.mock_vm.resume.assert_called_once() |
| 209 |
|
| 210 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 211 |
def test_create(self, mock_libvirt): |
| 212 |
"""Test VM create""" |
| 213 |
mock_libvirt.open.return_value = self.mock_conn |
| 214 |
|
| 215 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 216 |
result = conn.create('test-vm') |
| 217 |
|
| 218 |
self.assertEqual(result, 0) |
| 219 |
self.mock_vm.create.assert_called_once() |
| 220 |
|
| 221 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 222 |
def test_destroy(self, mock_libvirt): |
| 223 |
"""Test VM destroy""" |
| 224 |
mock_libvirt.open.return_value = self.mock_conn |
| 225 |
|
| 226 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 227 |
result = conn.destroy('test-vm') |
| 228 |
|
| 229 |
self.assertEqual(result, 0) |
| 230 |
self.mock_vm.destroy.assert_called_once() |
| 231 |
|
| 232 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 233 |
def test_undefine_without_flag(self, mock_libvirt): |
| 234 |
"""Test VM undefine without deletion flag""" |
| 235 |
mock_libvirt.open.return_value = self.mock_conn |
| 236 |
|
| 237 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 238 |
result = conn.undefine('test-vm', 0) |
| 239 |
|
| 240 |
self.assertEqual(result, 0) |
| 241 |
self.mock_vm.undefineFlags.assert_called_once_with(0) |
| 242 |
|
| 243 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 244 |
def test_undefine_with_volume_flag(self, mock_libvirt): |
| 245 |
"""Test VM undefine with volume deletion flag""" |
| 246 |
mock_libvirt.open.return_value = self.mock_conn |
| 247 |
|
| 248 |
# Mock delete_domain_volumes method |
| 249 |
with mock.patch.object(LibvirtConnection, 'delete_domain_volumes') as mock_delete: |
| 250 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 251 |
result = conn.undefine('test-vm', 32) # 32 = flag with volume deletion bit |
| 252 |
|
| 253 |
self.assertEqual(result, 0) |
| 254 |
mock_delete.assert_called_once_with('test-vm') |
| 255 |
self.mock_vm.undefineFlags.assert_called_once_with(32) |
| 256 |
|
| 257 |
|
| 258 |
class TestLibvirtConnectionStatusAndInfo(unittest.TestCase): |
| 259 |
"""Test VM status and information methods""" |
| 260 |
|
| 261 |
def setUp(self): |
| 262 |
"""Set up test fixtures""" |
| 263 |
self.mock_module = mock.Mock() |
| 264 |
self.mock_module.run_command.return_value = (0, 'Linux test 5.10.0', '') |
| 265 |
|
| 266 |
self.mock_vm = mock.Mock() |
| 267 |
self.mock_vm.name.return_value = 'test-vm' |
| 268 |
self.mock_vm.info.return_value = [1, 0, 0, 0, 0] # State 1 = running |
| 269 |
self.mock_vm.XMLDesc.return_value = '<domain>...</domain>' |
| 270 |
self.mock_vm.maxVcpus.return_value = 4 |
| 271 |
self.mock_vm.maxMemory.return_value = 4194304 |
| 272 |
self.mock_vm.autostart.return_value = True |
| 273 |
self.mock_vm.setAutostart.return_value = 0 |
| 274 |
self.mock_vm.UUIDString.return_value = '12345678-1234-5678-9012-123456789abc' |
| 275 |
|
| 276 |
self.mock_conn = mock.Mock() |
| 277 |
self.mock_conn.listAllDomains.return_value = [self.mock_vm] |
| 278 |
self.mock_conn.lookupByName.return_value = self.mock_vm |
| 279 |
self.mock_conn.getInfo.return_value = (1, 2048, 8, 2500, 1, 2, 4, 1) |
| 280 |
self.mock_conn.getType.return_value = 'QEMU' |
| 281 |
self.mock_conn.getFreeMemory.return_value = 1073741824 |
| 282 |
self.mock_conn.defineXML.return_value = self.mock_vm |
| 283 |
|
| 284 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 285 |
def test_get_status2(self, mock_libvirt): |
| 286 |
"""Test get_status2 method with VM object""" |
| 287 |
mock_libvirt.open.return_value = self.mock_conn |
| 288 |
|
| 289 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 290 |
result = conn.get_status2(self.mock_vm) |
| 291 |
|
| 292 |
self.assertEqual(result, 'running') |
| 293 |
self.mock_vm.info.assert_called_once() |
| 294 |
|
| 295 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 296 |
def test_get_status(self, mock_libvirt): |
| 297 |
"""Test get_status method with VM name""" |
| 298 |
mock_libvirt.open.return_value = self.mock_conn |
| 299 |
|
| 300 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 301 |
result = conn.get_status('test-vm') |
| 302 |
|
| 303 |
self.assertEqual(result, 'running') |
| 304 |
|
| 305 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 306 |
def test_get_status_unknown_state(self, mock_libvirt): |
| 307 |
"""Test get_status with unknown state""" |
| 308 |
mock_libvirt.open.return_value = self.mock_conn |
| 309 |
self.mock_vm.info.return_value = [99, 0, 0, 0, 0] # Unknown state |
| 310 |
|
| 311 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 312 |
result = conn.get_status('test-vm') |
| 313 |
|
| 314 |
self.assertEqual(result, 'unknown') |
| 315 |
|
| 316 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 317 |
def test_nodeinfo(self, mock_libvirt): |
| 318 |
"""Test nodeinfo method""" |
| 319 |
mock_libvirt.open.return_value = self.mock_conn |
| 320 |
|
| 321 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 322 |
result = conn.nodeinfo() |
| 323 |
|
| 324 |
self.assertEqual(result, (1, 2048, 8, 2500, 1, 2, 4, 1)) |
| 325 |
self.mock_conn.getInfo.assert_called_once() |
| 326 |
|
| 327 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 328 |
def test_get_type(self, mock_libvirt): |
| 329 |
"""Test get_type method""" |
| 330 |
mock_libvirt.open.return_value = self.mock_conn |
| 331 |
|
| 332 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 333 |
result = conn.get_type() |
| 334 |
|
| 335 |
self.assertEqual(result, 'QEMU') |
| 336 |
self.mock_conn.getType.assert_called_once() |
| 337 |
|
| 338 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 339 |
def test_getFreeMemory(self, mock_libvirt): |
| 340 |
"""Test getFreeMemory method""" |
| 341 |
mock_libvirt.open.return_value = self.mock_conn |
| 342 |
|
| 343 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 344 |
result = conn.getFreeMemory() |
| 345 |
|
| 346 |
self.assertEqual(result, 1073741824) |
| 347 |
self.mock_conn.getFreeMemory.assert_called_once() |
| 348 |
|
| 349 |
|
| 350 |
class TestLibvirtConnectionXMLOperations(unittest.TestCase): |
| 351 |
"""Test XML operations and VM configuration methods""" |
| 352 |
|
| 353 |
def setUp(self): |
| 354 |
"""Set up test fixtures""" |
| 355 |
self.mock_module = mock.Mock() |
| 356 |
self.mock_module.run_command.return_value = (0, 'Linux test 5.10.0', '') |
| 357 |
|
| 358 |
self.mock_vm = mock.Mock() |
| 359 |
self.mock_vm.name.return_value = 'test-vm' |
| 360 |
self.mock_vm.XMLDesc.return_value = '''<domain> |
| 361 |
<name>test-vm</name> |
| 362 |
<devices> |
| 363 |
<interface type='network'> |
| 364 |
<mac address='52:54:00:12:34:56'/> |
| 365 |
<source network='default'/> |
| 366 |
<address type='pci' bus='0x00'/> |
| 367 |
</interface> |
| 368 |
<interface type='bridge'> |
| 369 |
<mac address='52:54:00:12:34:57'/> |
| 370 |
<source bridge='br0'/> |
| 371 |
<address type='pci' bus='0x01'/> |
| 372 |
</interface> |
| 373 |
<disk type='file' device='disk'> |
| 374 |
<source file='/var/lib/libvirt/images/test.qcow2'/> |
| 375 |
</disk> |
| 376 |
<disk type='file' device='disk'> |
| 377 |
<source file='/var/lib/libvirt/images/data.qcow2'/> |
| 378 |
</disk> |
| 379 |
</devices> |
| 380 |
</domain>''' |
| 381 |
self.mock_vm.maxVcpus.return_value = 4 |
| 382 |
self.mock_vm.maxMemory.return_value = 4194304 |
| 383 |
self.mock_vm.UUIDString.return_value = '12345678-1234-5678-9012-123456789abc' |
| 384 |
|
| 385 |
self.mock_conn = mock.Mock() |
| 386 |
self.mock_conn.lookupByName.return_value = self.mock_vm |
| 387 |
self.mock_conn.defineXML.return_value = self.mock_vm |
| 388 |
|
| 389 |
# Mock storage volumes for disk deletion test |
| 390 |
self.mock_volume1 = mock.Mock() |
| 391 |
self.mock_volume2 = mock.Mock() |
| 392 |
self.mock_conn.storageVolLookupByPath.side_effect = [ |
| 393 |
self.mock_volume1, self.mock_volume2 |
| 394 |
] |
| 395 |
|
| 396 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 397 |
def test_get_xml(self, mock_libvirt): |
| 398 |
"""Test get_xml method""" |
| 399 |
mock_libvirt.open.return_value = self.mock_conn |
| 400 |
|
| 401 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 402 |
result = conn.get_xml('test-vm') |
| 403 |
|
| 404 |
self.assertIn('<domain>', result) |
| 405 |
self.assertIn('<name>test-vm</name>', result) |
| 406 |
self.mock_conn.lookupByName.assert_called_once_with('test-vm') |
| 407 |
self.mock_vm.XMLDesc.assert_called_once_with(0) |
| 408 |
|
| 409 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 410 |
def test_get_maxVcpus(self, mock_libvirt): |
| 411 |
"""Test get_maxVcpus method""" |
| 412 |
mock_libvirt.open.return_value = self.mock_conn |
| 413 |
|
| 414 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 415 |
result = conn.get_maxVcpus('test-vm') |
| 416 |
|
| 417 |
self.assertEqual(result, 4) |
| 418 |
self.mock_conn.lookupByName.assert_called_once_with('test-vm') |
| 419 |
self.mock_vm.maxVcpus.assert_called_once() |
| 420 |
|
| 421 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 422 |
def test_get_maxMemory(self, mock_libvirt): |
| 423 |
"""Test get_maxMemory method""" |
| 424 |
mock_libvirt.open.return_value = self.mock_conn |
| 425 |
|
| 426 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 427 |
result = conn.get_maxMemory('test-vm') |
| 428 |
|
| 429 |
self.assertEqual(result, 4194304) |
| 430 |
self.mock_conn.lookupByName.assert_called_once_with('test-vm') |
| 431 |
self.mock_vm.maxMemory.assert_called_once() |
| 432 |
|
| 433 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 434 |
def test_get_uuid(self, mock_libvirt): |
| 435 |
"""Test get_uuid method""" |
| 436 |
mock_libvirt.open.return_value = self.mock_conn |
| 437 |
|
| 438 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 439 |
result = conn.get_uuid('test-vm') |
| 440 |
|
| 441 |
self.assertEqual(result, '12345678-1234-5678-9012-123456789abc') |
| 442 |
self.mock_conn.lookupByName.assert_called_once_with('test-vm') |
| 443 |
self.mock_vm.UUIDString.assert_called_once() |
| 444 |
|
| 445 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 446 |
def test_define_from_xml(self, mock_libvirt): |
| 447 |
"""Test define_from_xml method""" |
| 448 |
mock_libvirt.open.return_value = self.mock_conn |
| 449 |
test_xml = '<domain><name>new-vm</name></domain>' |
| 450 |
|
| 451 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 452 |
result = conn.define_from_xml(test_xml) |
| 453 |
|
| 454 |
self.assertEqual(result, self.mock_vm) |
| 455 |
self.mock_conn.defineXML.assert_called_once_with(test_xml) |
| 456 |
|
| 457 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.etree', create=True) |
| 458 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 459 |
def test_get_interfaces(self, mock_libvirt, mock_etree): |
| 460 |
"""Test get_interfaces method""" |
| 461 |
mock_libvirt.open.return_value = self.mock_conn |
| 462 |
|
| 463 |
# Mock etree parsing |
| 464 |
mock_root = mock.Mock() |
| 465 |
mock_etree.fromstring.return_value = mock_root |
| 466 |
|
| 467 |
# Mock interface elements |
| 468 |
mock_interface1 = mock.Mock() |
| 469 |
mock_interface1.get.return_value = 'network' |
| 470 |
mock_source1 = mock.Mock() |
| 471 |
mock_source1.get.return_value = 'default' |
| 472 |
mock_mac1 = mock.Mock() |
| 473 |
mock_mac1.get.return_value = '52:54:00:12:34:56' |
| 474 |
mock_address1 = mock.Mock() |
| 475 |
mock_address1.get.return_value = '0x00' |
| 476 |
mock_interface1.find.side_effect = [mock_source1, mock_mac1, mock_address1] |
| 477 |
|
| 478 |
mock_interface2 = mock.Mock() |
| 479 |
mock_interface2.get.return_value = 'bridge' |
| 480 |
mock_source2 = mock.Mock() |
| 481 |
mock_source2.get.return_value = 'br0' |
| 482 |
mock_mac2 = mock.Mock() |
| 483 |
mock_mac2.get.return_value = '52:54:00:12:34:57' |
| 484 |
mock_address2 = mock.Mock() |
| 485 |
mock_address2.get.return_value = '0x01' |
| 486 |
mock_interface2.find.side_effect = [mock_source2, mock_mac2, mock_address2] |
| 487 |
|
| 488 |
mock_root.findall.return_value = [mock_interface1, mock_interface2] |
| 489 |
|
| 490 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 491 |
result = conn.get_interfaces('test-vm') |
| 492 |
|
| 493 |
expected = { |
| 494 |
'network_interfaces': { |
| 495 |
'interface_1': { |
| 496 |
'type': 'NAT', |
| 497 |
'mac': '52:54:00:12:34:56', |
| 498 |
'pci_bus': '0x00', |
| 499 |
'source': 'default' |
| 500 |
}, |
| 501 |
'interface_2': { |
| 502 |
'type': 'bridge', |
| 503 |
'mac': '52:54:00:12:34:57', |
| 504 |
'pci_bus': '0x01', |
| 505 |
'source': 'br0' |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
self.assertEqual(result, expected) |
| 511 |
|
| 512 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.etree', create=True) |
| 513 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 514 |
def test_delete_domain_volumes(self, mock_libvirt, mock_etree): |
| 515 |
"""Test delete_domain_volumes method""" |
| 516 |
mock_libvirt.open.return_value = self.mock_conn |
| 517 |
|
| 518 |
# Mock etree parsing |
| 519 |
mock_root = mock.Mock() |
| 520 |
mock_etree.fromstring.return_value = mock_root |
| 521 |
|
| 522 |
# Mock disk elements |
| 523 |
mock_disk1 = mock.Mock() |
| 524 |
mock_disk1.get.return_value = '/var/lib/libvirt/images/test.qcow2' |
| 525 |
mock_disk2 = mock.Mock() |
| 526 |
mock_disk2.get.return_value = '/var/lib/libvirt/images/data.qcow2' |
| 527 |
mock_root.findall.return_value = [mock_disk1, mock_disk2] |
| 528 |
|
| 529 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 530 |
conn.delete_domain_volumes('test-vm') |
| 531 |
|
| 532 |
# Verify volumes were looked up and deleted |
| 533 |
expected_calls = [ |
| 534 |
mock.call('/var/lib/libvirt/images/test.qcow2'), |
| 535 |
mock.call('/var/lib/libvirt/images/data.qcow2') |
| 536 |
] |
| 537 |
self.mock_conn.storageVolLookupByPath.assert_has_calls(expected_calls) |
| 538 |
self.mock_volume1.delete.assert_called_once() |
| 539 |
self.mock_volume2.delete.assert_called_once() |
| 540 |
|
| 541 |
|
| 542 |
class TestLibvirtConnectionAutostart(unittest.TestCase): |
| 543 |
"""Test autostart functionality""" |
| 544 |
|
| 545 |
def setUp(self): |
| 546 |
"""Set up test fixtures""" |
| 547 |
self.mock_module = mock.Mock() |
| 548 |
self.mock_module.run_command.return_value = (0, 'Linux test 5.10.0', '') |
| 549 |
|
| 550 |
self.mock_vm = mock.Mock() |
| 551 |
self.mock_vm.autostart.return_value = True |
| 552 |
self.mock_vm.setAutostart.return_value = 0 |
| 553 |
|
| 554 |
self.mock_conn = mock.Mock() |
| 555 |
self.mock_conn.lookupByName.return_value = self.mock_vm |
| 556 |
|
| 557 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 558 |
def test_get_autostart(self, mock_libvirt): |
| 559 |
"""Test get_autostart method""" |
| 560 |
mock_libvirt.open.return_value = self.mock_conn |
| 561 |
|
| 562 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 563 |
result = conn.get_autostart('test-vm') |
| 564 |
|
| 565 |
self.assertTrue(result) |
| 566 |
self.mock_conn.lookupByName.assert_called_once_with('test-vm') |
| 567 |
self.mock_vm.autostart.assert_called_once() |
| 568 |
|
| 569 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 570 |
def test_set_autostart_enable(self, mock_libvirt): |
| 571 |
"""Test set_autostart method to enable autostart""" |
| 572 |
mock_libvirt.open.return_value = self.mock_conn |
| 573 |
|
| 574 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 575 |
result = conn.set_autostart('test-vm', True) |
| 576 |
|
| 577 |
self.assertEqual(result, 0) |
| 578 |
self.mock_conn.lookupByName.assert_called_once_with('test-vm') |
| 579 |
self.mock_vm.setAutostart.assert_called_once_with(True) |
| 580 |
|
| 581 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 582 |
def test_set_autostart_disable(self, mock_libvirt): |
| 583 |
"""Test set_autostart method to disable autostart""" |
| 584 |
mock_libvirt.open.return_value = self.mock_conn |
| 585 |
|
| 586 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 587 |
result = conn.set_autostart('test-vm', False) |
| 588 |
|
| 589 |
self.assertEqual(result, 0) |
| 590 |
self.mock_conn.lookupByName.assert_called_once_with('test-vm') |
| 591 |
self.mock_vm.setAutostart.assert_called_once_with(False) |
| 592 |
|
| 593 |
|
| 594 |
class TestLibvirtConnectionErrorHandling(unittest.TestCase): |
| 595 |
"""Test error handling and exception scenarios""" |
| 596 |
|
| 597 |
def setUp(self): |
| 598 |
"""Set up test fixtures""" |
| 599 |
self.mock_module = mock.Mock() |
| 600 |
self.mock_module.run_command.return_value = (0, 'Linux test 5.10.0', '') |
| 601 |
|
| 602 |
self.mock_conn = mock.Mock() |
| 603 |
|
| 604 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 605 |
def test_vm_operations_with_vm_not_found(self, mock_libvirt): |
| 606 |
"""Test various operations when VM is not found""" |
| 607 |
mock_libvirt.open.return_value = self.mock_conn |
| 608 |
self.mock_conn.listAllDomains.return_value = [] |
| 609 |
|
| 610 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 611 |
|
| 612 |
operations = [ |
| 613 |
('shutdown', 'nonexistent-vm'), |
| 614 |
('pause', 'nonexistent-vm'), |
| 615 |
('unpause', 'nonexistent-vm'), |
| 616 |
('suspend', 'nonexistent-vm'), |
| 617 |
('resume', 'nonexistent-vm'), |
| 618 |
('create', 'nonexistent-vm'), |
| 619 |
('destroy', 'nonexistent-vm'), |
| 620 |
('get_status', 'nonexistent-vm'), |
| 621 |
] |
| 622 |
|
| 623 |
for operation, vm_name in operations: |
| 624 |
with self.subTest(operation=operation): |
| 625 |
with self.assertRaises(VMNotFound): |
| 626 |
getattr(conn, operation)(vm_name) |
| 627 |
|
| 628 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 629 |
def test_xml_operations_with_missing_vm(self, mock_libvirt): |
| 630 |
"""Test XML operations when VM is not found""" |
| 631 |
mock_libvirt.open.return_value = self.mock_conn |
| 632 |
|
| 633 |
# Simulate VM not found exception from libvirt |
| 634 |
mock_libvirt.libvirtError = Exception |
| 635 |
self.mock_conn.lookupByName.side_effect = Exception("Domain not found") |
| 636 |
|
| 637 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 638 |
|
| 639 |
operations = [ |
| 640 |
('get_xml', 'nonexistent-vm'), |
| 641 |
('get_maxVcpus', 'nonexistent-vm'), |
| 642 |
('get_maxMemory', 'nonexistent-vm'), |
| 643 |
('get_uuid', 'nonexistent-vm'), |
| 644 |
('get_autostart', 'nonexistent-vm'), |
| 645 |
] |
| 646 |
|
| 647 |
for operation, vm_name in operations: |
| 648 |
with self.subTest(operation=operation): |
| 649 |
with self.assertRaises(Exception): |
| 650 |
getattr(conn, operation)(vm_name) |
| 651 |
|
| 652 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 653 |
def test_undefine_with_volume_deletion_error(self, mock_libvirt): |
| 654 |
"""Test undefine when volume deletion fails""" |
| 655 |
mock_libvirt.open.return_value = self.mock_conn |
| 656 |
|
| 657 |
mock_vm = mock.Mock() |
| 658 |
mock_vm.name.return_value = 'test-vm' |
| 659 |
self.mock_conn.listAllDomains.return_value = [mock_vm] |
| 660 |
|
| 661 |
# Mock delete_domain_volumes to raise an exception |
| 662 |
with mock.patch.object(LibvirtConnection, 'delete_domain_volumes', side_effect=Exception("Volume not found")): |
| 663 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 664 |
|
| 665 |
with self.assertRaises(Exception): |
| 666 |
conn.undefine('test-vm', 32) # Flag with volume deletion bit |
| 667 |
|
| 668 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.etree', create=True) |
| 669 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 670 |
def test_delete_domain_volumes_with_missing_volume(self, mock_libvirt, mock_etree): |
| 671 |
"""Test delete_domain_volumes when storage volume is not found""" |
| 672 |
mock_libvirt.open.return_value = self.mock_conn |
| 673 |
|
| 674 |
# Mock VM with disk |
| 675 |
mock_vm = mock.Mock() |
| 676 |
mock_vm.XMLDesc.return_value = '<domain><devices><disk type="file"><source file="/path/to/disk.qcow2"/></disk></devices></domain>' |
| 677 |
self.mock_conn.lookupByName.return_value = mock_vm |
| 678 |
|
| 679 |
# Mock etree parsing |
| 680 |
mock_root = mock.Mock() |
| 681 |
mock_etree.fromstring.return_value = mock_root |
| 682 |
mock_disk = mock.Mock() |
| 683 |
mock_disk.get.return_value = '/path/to/disk.qcow2' |
| 684 |
mock_root.findall.return_value = [mock_disk] |
| 685 |
|
| 686 |
# Simulate volume not found |
| 687 |
self.mock_conn.storageVolLookupByPath.side_effect = Exception("Volume not found") |
| 688 |
|
| 689 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 690 |
|
| 691 |
# Should raise exception when volume lookup fails |
| 692 |
with self.assertRaises(Exception): |
| 693 |
conn.delete_domain_volumes('test-vm') |
| 694 |
|
| 695 |
|
| 696 |
class TestLibvirtConnectionConstants(unittest.TestCase): |
| 697 |
"""Test constants and state mappings""" |
| 698 |
|
| 699 |
def test_constants_defined(self): |
| 700 |
"""Test that constants are properly defined""" |
| 701 |
self.assertEqual(VIRT_FAILED, 1) |
| 702 |
self.assertEqual(VIRT_SUCCESS, 0) |
| 703 |
self.assertEqual(VIRT_UNAVAILABLE, 2) |
| 704 |
|
| 705 |
def test_state_name_map(self): |
| 706 |
"""Test VIRT_STATE_NAME_MAP mappings""" |
| 707 |
expected_mappings = { |
| 708 |
0: 'running', |
| 709 |
1: 'running', |
| 710 |
2: 'running', |
| 711 |
3: 'paused', |
| 712 |
4: 'shutdown', |
| 713 |
5: 'shutdown', |
| 714 |
6: 'crashed', |
| 715 |
} |
| 716 |
|
| 717 |
self.assertEqual(VIRT_STATE_NAME_MAP, expected_mappings) |
| 718 |
|
| 719 |
def test_has_virt_and_xml_flags(self): |
| 720 |
"""Test HAS_VIRT and HAS_XML flags""" |
| 721 |
# These should be boolean values |
| 722 |
self.assertIsInstance(HAS_VIRT, bool) |
| 723 |
self.assertIsInstance(HAS_XML, bool) |
| 724 |
|
| 725 |
|
| 726 |
class TestLibvirtConnectionIntegration(unittest.TestCase): |
| 727 |
"""Integration tests combining multiple methods""" |
| 728 |
|
| 729 |
def setUp(self): |
| 730 |
"""Set up test fixtures""" |
| 731 |
self.mock_module = mock.Mock() |
| 732 |
self.mock_module.run_command.return_value = (0, 'Linux test 5.10.0', '') |
| 733 |
|
| 734 |
self.mock_vm = mock.Mock() |
| 735 |
self.mock_vm.name.return_value = 'test-vm' |
| 736 |
self.mock_vm.info.return_value = [1, 2097152, 2097152, 2, 1000000000] |
| 737 |
self.mock_vm.shutdown.return_value = 0 |
| 738 |
self.mock_vm.XMLDesc.return_value = '<domain><name>test-vm</name></domain>' |
| 739 |
|
| 740 |
self.mock_conn = mock.Mock() |
| 741 |
self.mock_conn.listAllDomains.return_value = [self.mock_vm] |
| 742 |
self.mock_conn.lookupByName.return_value = self.mock_vm |
| 743 |
|
| 744 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 745 |
def test_vm_lifecycle_workflow(self, mock_libvirt): |
| 746 |
"""Test a complete VM lifecycle workflow""" |
| 747 |
mock_libvirt.open.return_value = self.mock_conn |
| 748 |
|
| 749 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 750 |
|
| 751 |
# Find VM |
| 752 |
vm = conn.find_vm('test-vm') |
| 753 |
self.assertEqual(vm, self.mock_vm) |
| 754 |
|
| 755 |
# Check status |
| 756 |
status = conn.get_status('test-vm') |
| 757 |
self.assertEqual(status, 'running') |
| 758 |
|
| 759 |
# Get VM details |
| 760 |
xml = conn.get_xml('test-vm') |
| 761 |
self.assertIn('<domain>', xml) |
| 762 |
|
| 763 |
# Shutdown VM |
| 764 |
result = conn.shutdown('test-vm') |
| 765 |
self.assertEqual(result, 0) |
| 766 |
|
| 767 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 768 |
def test_multiple_vm_operations(self, mock_libvirt): |
| 769 |
"""Test operations on multiple VMs""" |
| 770 |
# Create multiple mock VMs |
| 771 |
mock_vm1 = mock.Mock() |
| 772 |
mock_vm1.name.return_value = 'vm1' |
| 773 |
mock_vm2 = mock.Mock() |
| 774 |
mock_vm2.name.return_value = 'vm2' |
| 775 |
|
| 776 |
self.mock_conn.listAllDomains.return_value = [mock_vm1, mock_vm2] |
| 777 |
mock_libvirt.open.return_value = self.mock_conn |
| 778 |
|
| 779 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 780 |
|
| 781 |
# Get all VMs |
| 782 |
all_vms = conn.find_vm(-1) |
| 783 |
self.assertEqual(len(all_vms), 2) |
| 784 |
self.assertEqual(all_vms, [mock_vm1, mock_vm2]) |
| 785 |
|
| 786 |
# Find specific VM |
| 787 |
vm1 = conn.find_vm('vm1') |
| 788 |
self.assertEqual(vm1, mock_vm1) |
| 789 |
|
| 790 |
vm2 = conn.find_vm('vm2') |
| 791 |
self.assertEqual(vm2, mock_vm2) |
| 792 |
|
| 793 |
@mock.patch('ansible_collections.community.libvirt.plugins.module_utils.libvirt.libvirt', create=True) |
| 794 |
def test_hypervisor_info_operations(self, mock_libvirt): |
| 795 |
"""Test hypervisor information operations""" |
| 796 |
mock_libvirt.open.return_value = self.mock_conn |
| 797 |
self.mock_conn.getInfo.return_value = (1, 8192, 16, 2400, 2, 4, 2, 1) |
| 798 |
self.mock_conn.getType.return_value = 'QEMU' |
| 799 |
self.mock_conn.getFreeMemory.return_value = 2147483648 |
| 800 |
|
| 801 |
conn = LibvirtConnection('qemu:///system', self.mock_module) |
| 802 |
|
| 803 |
# Get node info |
| 804 |
node_info = conn.nodeinfo() |
| 805 |
self.assertEqual(node_info, (1, 8192, 16, 2400, 2, 4, 2, 1)) |
| 806 |
|
| 807 |
# Get hypervisor type |
| 808 |
hv_type = conn.get_type() |
| 809 |
self.assertEqual(hv_type, 'QEMU') |
| 810 |
|
| 811 |
# Get free memory |
| 812 |
free_mem = conn.getFreeMemory() |
| 813 |
self.assertEqual(free_mem, 2147483648) |
| 814 |
|
| 815 |
|
| 816 |
if __name__ == '__main__': |
| 817 |
unittest.main(verbosity=2) |