Debian Codegraph @

ansible_collections/community/libvirt/tests/unit/modules/test_virt_install.py

raw
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.modules.virt_install import core
10 from ansible_collections.community.libvirt.plugins.module_utils.libvirt import (
11 VMNotFound, VIRT_SUCCESS, VIRT_FAILED)
12
13
14 class TestCoreFunction(unittest.TestCase):
15 """Test cases for the core() function"""
16
17 def setUp(self):
18 """Set up test fixtures"""
19 self.mock_module = mock.Mock()
20 self.mock_module.params = {
21 'name': 'test-vm',
22 'state': 'present',
23 'uri': 'qemu:///system',
24 'recreate': False
25 }
26 self.mock_module.check_mode = False
27 # Mock run_command to return proper tuple for LibvirtConnection initialization
28 self.mock_module.run_command.return_value = (0, 'linux-kernel', '')
29 self.mock_module.fail_json = mock.Mock(
30 side_effect=Exception("fail_json called"))
31
32 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
33 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
34 def test_core_missing_vm_name(
35 self,
36 mock_libvirt_wrapper,
37 mock_virt_install_tool):
38 """Test core() when VM name is missing"""
39 self.mock_module.params['name'] = None
40
41 with self.assertRaises(Exception) as context:
42 core(self.mock_module)
43
44 self.mock_module.fail_json.assert_called_once_with(
45 msg="virtual machine name is missing")
46
47 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
48 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
49 def test_core_present_vm_not_exists_create_success(
50 self, mock_libvirt_wrapper, mock_virt_install_tool):
51 """Test core() when VM doesn't exist and should be created successfully"""
52 # Setup mocks
53 mock_virt_conn = mock.Mock()
54 mock_virt_install = mock.Mock()
55 mock_libvirt_wrapper.return_value = mock_virt_conn
56 mock_virt_install_tool.return_value = mock_virt_install
57
58 # VM doesn't exist - should raise VMNotFound exception
59 mock_virt_conn.find_vm.side_effect = VMNotFound("VM not found")
60
61 # VirtInstallTool.execute returns success
62 mock_virt_install.execute.return_value = (
63 True, VIRT_SUCCESS, {"msg": "VM created successfully"})
64 mock_virt_install.get_commands.return_value = []
65
66 # Call core function
67 rc, result = core(self.mock_module)
68
69 # Assertions
70 self.assertEqual(rc, VIRT_SUCCESS)
71 self.assertTrue(result['changed'])
72 self.assertEqual(result['msg'], "VM created successfully")
73 mock_virt_install.execute.assert_called_once_with(dryrun=False)
74
75 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
76 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
77 def test_core_present_vm_not_exists_create_failure(
78 self, mock_libvirt_wrapper, mock_virt_install_tool):
79 """Test core() when VM doesn't exist and creation fails"""
80 # Setup mocks
81 mock_virt_conn = mock.Mock()
82 mock_virt_install = mock.Mock()
83 mock_libvirt_wrapper.return_value = mock_virt_conn
84 mock_virt_install_tool.return_value = mock_virt_install
85
86 # VM doesn't exist - should raise VMNotFound exception
87 mock_virt_conn.find_vm.side_effect = VMNotFound("VM not found")
88
89 # VirtInstallTool.execute returns failure
90 mock_virt_install.execute.return_value = (
91 False, VIRT_FAILED, {"msg": "VM creation failed"})
92 mock_virt_install.get_commands.return_value = []
93
94 # Call core function
95 rc, result = core(self.mock_module)
96
97 # Assertions
98 self.assertEqual(rc, VIRT_FAILED)
99 self.assertFalse(result['changed'])
100 self.assertEqual(result['msg'], "VM creation failed")
101
102 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
103 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
104 def test_core_present_vm_exists_no_recreate(
105 self, mock_libvirt_wrapper, mock_virt_install_tool):
106 """Test core() when VM exists and recreate=False"""
107 # Setup mocks
108 mock_virt_conn = mock.Mock()
109 mock_virt_install = mock.Mock()
110 mock_libvirt_wrapper.return_value = mock_virt_conn
111 mock_virt_install_tool.return_value = mock_virt_install
112
113 # VM exists
114 mock_vm = mock.Mock()
115 mock_virt_conn.find_vm.return_value = mock_vm
116
117 # Call core function
118 rc, result = core(self.mock_module)
119
120 # Assertions
121 self.assertEqual(rc, VIRT_SUCCESS)
122 self.assertFalse(result['changed'])
123 self.assertEqual(
124 result['message'],
125 "virtual machine 'test-vm' already exists")
126 mock_virt_install.execute.assert_not_called()
127
128 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
129 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
130 def test_core_present_vm_exists_recreate_inactive(
131 self, mock_libvirt_wrapper, mock_virt_install_tool):
132 """Test core() when VM exists, recreate=True, and VM is inactive"""
133 # Setup mocks
134 mock_virt_conn = mock.Mock()
135 mock_virt_install = mock.Mock()
136 mock_libvirt_wrapper.return_value = mock_virt_conn
137 mock_virt_install_tool.return_value = mock_virt_install
138
139 self.mock_module.params['recreate'] = True
140
141 # VM exists and is inactive
142 mock_vm = mock.Mock()
143 mock_vm.isActive.return_value = False
144 mock_virt_conn.find_vm.return_value = mock_vm
145
146 # VirtInstallTool.execute returns success
147 mock_virt_install.execute.return_value = (
148 True, VIRT_SUCCESS, {"msg": "VM recreated successfully"})
149 mock_virt_install.get_commands.return_value = []
150
151 # Call core function
152 rc, result = core(self.mock_module)
153
154 # Assertions
155 self.assertEqual(rc, VIRT_SUCCESS)
156 self.assertTrue(result['changed'])
157 self.assertEqual(result['msg'], "VM recreated successfully")
158 # Should not be called if VM is inactive
159 mock_virt_conn.destroy.assert_not_called()
160 mock_virt_conn.undefine.assert_called_once_with('test-vm')
161 mock_virt_install.execute.assert_called_once_with(dryrun=False)
162
163 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
164 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
165 def test_core_present_vm_exists_recreate_active(
166 self, mock_libvirt_wrapper, mock_virt_install_tool):
167 """Test core() when VM exists, recreate=True, and VM is active"""
168 # Setup mocks
169 mock_virt_conn = mock.Mock()
170 mock_virt_install = mock.Mock()
171 mock_libvirt_wrapper.return_value = mock_virt_conn
172 mock_virt_install_tool.return_value = mock_virt_install
173
174 self.mock_module.params['recreate'] = True
175
176 # VM exists and is active
177 mock_vm = mock.Mock()
178 mock_vm.isActive.return_value = True
179 mock_virt_conn.find_vm.return_value = mock_vm
180
181 # VirtInstallTool.execute returns success
182 mock_virt_install.execute.return_value = (
183 True, VIRT_SUCCESS, {"msg": "VM recreated successfully"})
184 mock_virt_install.get_commands.return_value = []
185
186 # Call core function
187 rc, result = core(self.mock_module)
188
189 # Assertions
190 self.assertEqual(rc, VIRT_SUCCESS)
191 self.assertTrue(result['changed'])
192 self.assertEqual(result['msg'], "VM recreated successfully")
193 mock_virt_conn.destroy.assert_called_once_with('test-vm')
194 mock_virt_conn.undefine.assert_called_once_with('test-vm')
195 mock_virt_install.execute.assert_called_once_with(dryrun=False)
196
197 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
198 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
199 def test_core_absent_vm_not_exists(
200 self,
201 mock_libvirt_wrapper,
202 mock_virt_install_tool):
203 """Test core() when state=absent and VM doesn't exist"""
204 # Setup mocks
205 mock_virt_conn = mock.Mock()
206 mock_virt_install = mock.Mock()
207 mock_libvirt_wrapper.return_value = mock_virt_conn
208 mock_virt_install_tool.return_value = mock_virt_install
209
210 self.mock_module.params['state'] = 'absent'
211
212 # VM doesn't exist - should raise VMNotFound exception
213 mock_virt_conn.find_vm.side_effect = VMNotFound("VM not found")
214
215 # Call core function
216 rc, result = core(self.mock_module)
217
218 # Assertions
219 self.assertEqual(rc, VIRT_SUCCESS)
220 self.assertFalse(result['changed'])
221 self.assertEqual(
222 result['message'],
223 "virtual machine 'test-vm' is already absent")
224 mock_virt_conn.destroy.assert_not_called()
225 mock_virt_conn.undefine.assert_not_called()
226
227 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
228 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
229 def test_core_absent_vm_exists_inactive(
230 self, mock_libvirt_wrapper, mock_virt_install_tool):
231 """Test core() when state=absent, VM exists, and VM is inactive"""
232 # Setup mocks
233 mock_virt_conn = mock.Mock()
234 mock_virt_install = mock.Mock()
235 mock_libvirt_wrapper.return_value = mock_virt_conn
236 mock_virt_install_tool.return_value = mock_virt_install
237
238 self.mock_module.params['state'] = 'absent'
239
240 # VM exists and is inactive
241 mock_vm = mock.Mock()
242 mock_vm.isActive.return_value = False
243 mock_virt_conn.find_vm.return_value = mock_vm
244
245 # Call core function
246 rc, result = core(self.mock_module)
247
248 # Assertions
249 self.assertEqual(rc, VIRT_SUCCESS)
250 self.assertTrue(result['changed'])
251 # Should not be called if VM is inactive
252 mock_virt_conn.destroy.assert_not_called()
253 mock_virt_conn.undefine.assert_called_once_with('test-vm')
254
255 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
256 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
257 def test_core_absent_vm_exists_active(
258 self, mock_libvirt_wrapper, mock_virt_install_tool):
259 """Test core() when state=absent, VM exists, and VM is active"""
260 # Setup mocks
261 mock_virt_conn = mock.Mock()
262 mock_virt_install = mock.Mock()
263 mock_libvirt_wrapper.return_value = mock_virt_conn
264 mock_virt_install_tool.return_value = mock_virt_install
265
266 self.mock_module.params['state'] = 'absent'
267
268 # VM exists and is active
269 mock_vm = mock.Mock()
270 mock_vm.isActive.return_value = True
271 mock_virt_conn.find_vm.return_value = mock_vm
272
273 # Call core function
274 rc, result = core(self.mock_module)
275
276 # Assertions
277 self.assertEqual(rc, VIRT_SUCCESS)
278 self.assertTrue(result['changed'])
279 mock_virt_conn.destroy.assert_called_once_with('test-vm')
280 mock_virt_conn.undefine.assert_called_once_with('test-vm')
281
282 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
283 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
284 def test_core_unsupported_state(
285 self,
286 mock_libvirt_wrapper,
287 mock_virt_install_tool):
288 """Test core() with unsupported state"""
289 # Setup mocks
290 mock_virt_conn = mock.Mock()
291 mock_virt_install = mock.Mock()
292 mock_libvirt_wrapper.return_value = mock_virt_conn
293 mock_virt_install_tool.return_value = mock_virt_install
294
295 self.mock_module.params['state'] = 'invalid_state'
296
297 # VM doesn't exist (doesn't matter for this test) - should raise
298 # VMNotFound exception
299 mock_virt_conn.find_vm.side_effect = VMNotFound("VM not found")
300
301 # Call core function and expect failure
302 with self.assertRaises(Exception) as context:
303 core(self.mock_module)
304
305 self.mock_module.fail_json.assert_called_once_with(
306 msg="unsupported state 'invalid_state'")
307
308 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
309 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
310 def test_core_check_mode_enabled(
311 self,
312 mock_libvirt_wrapper,
313 mock_virt_install_tool):
314 """Test core() with check_mode enabled"""
315 # Setup mocks
316 mock_virt_conn = mock.Mock()
317 mock_virt_install = mock.Mock()
318 mock_libvirt_wrapper.return_value = mock_virt_conn
319 mock_virt_install_tool.return_value = mock_virt_install
320
321 self.mock_module.check_mode = True
322
323 # VM doesn't exist - should raise VMNotFound exception
324 mock_virt_conn.find_vm.side_effect = VMNotFound("VM not found")
325
326 # VirtInstallTool.execute returns success
327 mock_virt_install.execute.return_value = (
328 True, VIRT_SUCCESS, {"msg": "VM created successfully"})
329 mock_virt_install.get_commands.return_value = []
330
331 # Call core function
332 rc, result = core(self.mock_module)
333
334 # Assertions
335 self.assertEqual(rc, VIRT_SUCCESS)
336 mock_virt_install.execute.assert_called_once_with(dryrun=True)
337
338 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
339 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
340 def test_core_result_structure(
341 self,
342 mock_libvirt_wrapper,
343 mock_virt_install_tool):
344 """Test core() returns proper result structure"""
345 # Setup mocks
346 mock_virt_conn = mock.Mock()
347 mock_virt_install = mock.Mock()
348 mock_libvirt_wrapper.return_value = mock_virt_conn
349 mock_virt_install_tool.return_value = mock_virt_install
350
351 # VM exists and no recreate
352 mock_vm = mock.Mock()
353 mock_virt_conn.find_vm.return_value = mock_vm
354
355 # Call core function
356 rc, result = core(self.mock_module)
357
358 # Assertions - check result structure
359 self.assertIsInstance(result, dict)
360 self.assertIn('changed', result)
361 self.assertIn('original_message', result)
362 self.assertIn('message', result)
363 self.assertIsInstance(result['changed'], bool)
364
365 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
366 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
367 def test_core_libvirt_wrapper_initialization(
368 self, mock_libvirt_wrapper, mock_virt_install_tool):
369 """Test core() properly initializes LibvirtWrapper"""
370 # Setup mocks
371 mock_virt_conn = mock.Mock()
372 mock_virt_install = mock.Mock()
373 mock_libvirt_wrapper.return_value = mock_virt_conn
374 mock_virt_install_tool.return_value = mock_virt_install
375
376 # VM doesn't exist - should raise VMNotFound exception
377 mock_virt_conn.find_vm.side_effect = VMNotFound("VM not found")
378 mock_virt_install.execute.return_value = (
379 True, VIRT_SUCCESS, {"msg": "VM created"})
380 mock_virt_install.get_commands.return_value = []
381
382 # Call core function
383 rc, result = core(self.mock_module)
384
385 # Verify LibvirtWrapper was initialized with the module
386 mock_libvirt_wrapper.assert_called_once_with(self.mock_module)
387
388 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
389 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
390 def test_core_virt_install_tool_initialization(
391 self, mock_libvirt_wrapper, mock_virt_install_tool):
392 """Test core() properly initializes VirtInstallTool"""
393 # Setup mocks
394 mock_virt_conn = mock.Mock()
395 mock_virt_install = mock.Mock()
396 mock_libvirt_wrapper.return_value = mock_virt_conn
397 mock_virt_install_tool.return_value = mock_virt_install
398
399 # VM doesn't exist - should raise VMNotFound exception
400 mock_virt_conn.find_vm.side_effect = VMNotFound("VM not found")
401 mock_virt_install.execute.return_value = (
402 True, VIRT_SUCCESS, {"msg": "VM created"})
403 mock_virt_install.get_commands.return_value = []
404
405 # Call core function
406 rc, result = core(self.mock_module)
407
408 # Verify VirtInstallTool was initialized with the module
409 mock_virt_install_tool.assert_called_once_with(self.mock_module)
410
411 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
412 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
413 def test_core_find_vm_called_with_correct_name(
414 self, mock_libvirt_wrapper, mock_virt_install_tool):
415 """Test core() calls find_vm with correct VM name"""
416 # Setup mocks
417 mock_virt_conn = mock.Mock()
418 mock_virt_install = mock.Mock()
419 mock_libvirt_wrapper.return_value = mock_virt_conn
420 mock_virt_install_tool.return_value = mock_virt_install
421
422 # VM doesn't exist - should raise VMNotFound exception
423 mock_virt_conn.find_vm.side_effect = VMNotFound("VM not found")
424 mock_virt_install.execute.return_value = (
425 True, VIRT_SUCCESS, {"msg": "VM created"})
426 mock_virt_install.get_commands.return_value = []
427
428 # Call core function
429 rc, result = core(self.mock_module)
430
431 # Verify find_vm was called with correct name
432 mock_virt_conn.find_vm.assert_called_once_with('test-vm')
433
434 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
435 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
436 def test_core_different_vm_names(
437 self,
438 mock_libvirt_wrapper,
439 mock_virt_install_tool):
440 """Test core() with different VM names"""
441 # Setup mocks
442 mock_virt_conn = mock.Mock()
443 mock_virt_install = mock.Mock()
444 mock_libvirt_wrapper.return_value = mock_virt_conn
445 mock_virt_install_tool.return_value = mock_virt_install
446
447 # Test with different VM name
448 self.mock_module.params['name'] = 'different-vm-name'
449
450 # VM doesn't exist - should raise VMNotFound exception
451 mock_virt_conn.find_vm.side_effect = VMNotFound("VM not found")
452 mock_virt_install.execute.return_value = (
453 True, VIRT_SUCCESS, {"msg": "VM created"})
454 mock_virt_install.get_commands.return_value = []
455
456 # Call core function
457 rc, result = core(self.mock_module)
458
459 # Verify find_vm was called with correct name
460 mock_virt_conn.find_vm.assert_called_once_with('different-vm-name')
461
462 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
463 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
464 def test_core_empty_vm_name(
465 self,
466 mock_libvirt_wrapper,
467 mock_virt_install_tool):
468 """Test core() with empty VM name"""
469 # Setup mocks
470 mock_virt_conn = mock.Mock()
471 mock_virt_install = mock.Mock()
472 mock_libvirt_wrapper.return_value = mock_virt_conn
473 mock_virt_install_tool.return_value = mock_virt_install
474
475 self.mock_module.params['name'] = ''
476
477 # Call core function and expect failure
478 with self.assertRaises(Exception) as context:
479 core(self.mock_module)
480
481 self.mock_module.fail_json.assert_called_once_with(
482 msg="virtual machine name is missing")
483
484 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.VirtInstallTool')
485 @mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_install.LibvirtWrapper')
486 def test_core_execute_extra_result_merging(
487 self, mock_libvirt_wrapper, mock_virt_install_tool):
488 """Test core() properly merges extra results from execute"""
489 # Setup mocks
490 mock_virt_conn = mock.Mock()
491 mock_virt_install = mock.Mock()
492 mock_libvirt_wrapper.return_value = mock_virt_conn
493 mock_virt_install_tool.return_value = mock_virt_install
494
495 # VM doesn't exist - should raise VMNotFound exception
496 mock_virt_conn.find_vm.side_effect = VMNotFound("VM not found")
497
498 # VirtInstallTool.execute returns success with extra data
499 extra_data = {
500 "msg": "VM created successfully",
501 "extra_field": "extra_value",
502 "command": "virt-install --name test-vm"
503 }
504 mock_virt_install.execute.return_value = (
505 True, VIRT_SUCCESS, extra_data)
506 mock_virt_install.get_commands.return_value = []
507
508 # Call core function
509 rc, result = core(self.mock_module)
510
511 # Assertions - check that extra data is merged
512 self.assertEqual(rc, VIRT_SUCCESS)
513 self.assertTrue(result['changed'])
514 self.assertEqual(result['msg'], "VM created successfully")
515 self.assertEqual(result['extra_field'], "extra_value")
516 self.assertEqual(result['command'], "virt-install --name test-vm")
517
518
519 if __name__ == '__main__':
520 unittest.main(verbosity=2)