| 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 |
import os |
| 9 |
|
| 10 |
from ansible_collections.community.libvirt.tests.unit.compat import mock |
| 11 |
from ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance import BaseImageOperator |
| 12 |
|
| 13 |
|
| 14 |
class TestBaseImageOperatorInit(unittest.TestCase): |
| 15 |
"""Test BaseImageOperator initialization""" |
| 16 |
|
| 17 |
def setUp(self): |
| 18 |
"""Set up test fixtures""" |
| 19 |
self.mock_module = mock.Mock() |
| 20 |
self.mock_module.tmpdir = '/tmp/ansible_test' |
| 21 |
self.mock_module.check_mode = False |
| 22 |
|
| 23 |
def test_init_with_required_params(self): |
| 24 |
"""Test initialization with required parameters only""" |
| 25 |
operator = BaseImageOperator(self.mock_module, '/path/to/image.qcow2') |
| 26 |
|
| 27 |
self.assertEqual(operator.module, self.mock_module) |
| 28 |
self.assertEqual(operator.base_image, '/path/to/image.qcow2') |
| 29 |
self.assertIsNone(operator.image_cache_dir) |
| 30 |
self.assertIsNone(operator.image_checksum) |
| 31 |
self.assertIsNone(operator.base_image_path) |
| 32 |
self.assertIsNone(operator.system_disk_path) |
| 33 |
|
| 34 |
def test_init_with_all_params(self): |
| 35 |
"""Test initialization with all parameters""" |
| 36 |
operator = BaseImageOperator( |
| 37 |
self.mock_module, |
| 38 |
'https://example.com/image.qcow2', |
| 39 |
image_cache_dir='/var/cache/images', |
| 40 |
image_checksum='sha256:abc123' |
| 41 |
) |
| 42 |
|
| 43 |
self.assertEqual(operator.module, self.mock_module) |
| 44 |
self.assertEqual(operator.base_image, |
| 45 |
'https://example.com/image.qcow2') |
| 46 |
self.assertEqual(operator.image_cache_dir, '/var/cache/images') |
| 47 |
self.assertEqual(operator.image_checksum, 'sha256:abc123') |
| 48 |
self.assertIsNone(operator.base_image_path) |
| 49 |
self.assertIsNone(operator.system_disk_path) |
| 50 |
|
| 51 |
|
| 52 |
class TestBaseImageOperatorFetchImage(unittest.TestCase): |
| 53 |
"""Test BaseImageOperator fetch_image method""" |
| 54 |
|
| 55 |
def setUp(self): |
| 56 |
"""Set up test fixtures""" |
| 57 |
self.mock_module = mock.Mock() |
| 58 |
self.mock_module.tmpdir = '/tmp/ansible_test' |
| 59 |
self.mock_module.check_mode = False |
| 60 |
self.mock_module.atomic_move = mock.Mock() |
| 61 |
|
| 62 |
def test_fetch_image_local_file_exists(self): |
| 63 |
"""Test fetching local file that exists""" |
| 64 |
operator = BaseImageOperator( |
| 65 |
self.mock_module, '/path/to/local/image.qcow2') |
| 66 |
|
| 67 |
with mock.patch('os.path.exists', return_value=True): |
| 68 |
result = operator.fetch_image(force_pull=False) |
| 69 |
|
| 70 |
self.assertEqual(result, '/path/to/local/image.qcow2') |
| 71 |
self.assertEqual(operator.base_image_path, |
| 72 |
'/path/to/local/image.qcow2') |
| 73 |
self.mock_module.fail_json.assert_not_called() |
| 74 |
|
| 75 |
def test_fetch_image_local_file_not_exists(self): |
| 76 |
"""Test fetching local file that doesn't exist""" |
| 77 |
operator = BaseImageOperator( |
| 78 |
self.mock_module, '/path/to/nonexistent.qcow2') |
| 79 |
|
| 80 |
with mock.patch('os.path.exists', return_value=False): |
| 81 |
operator.fetch_image(force_pull=False) |
| 82 |
|
| 83 |
self.mock_module.fail_json.assert_called_once_with( |
| 84 |
msg="Base image file does not exist: /path/to/nonexistent.qcow2" |
| 85 |
) |
| 86 |
|
| 87 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.fetch_file') |
| 88 |
def test_fetch_image_remote_url_no_cache(self, mock_fetch_file): |
| 89 |
"""Test fetching remote URL without cache directory""" |
| 90 |
operator = BaseImageOperator( |
| 91 |
self.mock_module, 'https://example.com/image.qcow2') |
| 92 |
mock_fetch_file.return_value = '/tmp/downloaded_file' |
| 93 |
|
| 94 |
result = operator.fetch_image(force_pull=False, timeout=30) |
| 95 |
|
| 96 |
expected_path = os.path.join(self.mock_module.tmpdir, 'image.qcow2') |
| 97 |
self.assertEqual(result, expected_path) |
| 98 |
self.assertEqual(operator.base_image_path, expected_path) |
| 99 |
mock_fetch_file.assert_called_once_with( |
| 100 |
self.mock_module, 'https://example.com/image.qcow2', timeout=30 |
| 101 |
) |
| 102 |
self.mock_module.atomic_move.assert_called_once_with( |
| 103 |
'/tmp/downloaded_file', expected_path) |
| 104 |
|
| 105 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.fetch_file') |
| 106 |
def test_fetch_image_remote_url_with_cache_not_exists(self, mock_fetch_file): |
| 107 |
"""Test fetching remote URL with cache directory, file not cached""" |
| 108 |
operator = BaseImageOperator( |
| 109 |
self.mock_module, |
| 110 |
'https://example.com/image.qcow2', |
| 111 |
image_cache_dir='/var/cache' |
| 112 |
) |
| 113 |
mock_fetch_file.return_value = '/tmp/downloaded_file' |
| 114 |
|
| 115 |
with mock.patch('os.path.exists', return_value=False): |
| 116 |
result = operator.fetch_image(force_pull=False) |
| 117 |
|
| 118 |
expected_path = '/var/cache/image.qcow2' |
| 119 |
self.assertEqual(result, expected_path) |
| 120 |
self.assertEqual(operator.base_image_path, expected_path) |
| 121 |
mock_fetch_file.assert_called_once() |
| 122 |
self.mock_module.atomic_move.assert_called_once_with( |
| 123 |
'/tmp/downloaded_file', expected_path) |
| 124 |
|
| 125 |
def test_fetch_image_remote_url_with_cache_exists_no_force(self): |
| 126 |
"""Test fetching remote URL with cache directory, file exists, no force pull""" |
| 127 |
operator = BaseImageOperator( |
| 128 |
self.mock_module, |
| 129 |
'https://example.com/image.qcow2', |
| 130 |
image_cache_dir='/var/cache' |
| 131 |
) |
| 132 |
|
| 133 |
with mock.patch('os.path.exists', return_value=True): |
| 134 |
result = operator.fetch_image(force_pull=False) |
| 135 |
|
| 136 |
expected_path = '/var/cache/image.qcow2' |
| 137 |
self.assertEqual(result, expected_path) |
| 138 |
self.assertEqual(operator.base_image_path, expected_path) |
| 139 |
self.mock_module.atomic_move.assert_not_called() |
| 140 |
|
| 141 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.fetch_file') |
| 142 |
def test_fetch_image_remote_url_with_cache_exists_force_pull(self, mock_fetch_file): |
| 143 |
"""Test fetching remote URL with cache directory, file exists, force pull enabled""" |
| 144 |
operator = BaseImageOperator( |
| 145 |
self.mock_module, |
| 146 |
'https://example.com/image.qcow2', |
| 147 |
image_cache_dir='/var/cache' |
| 148 |
) |
| 149 |
mock_fetch_file.return_value = '/tmp/downloaded_file' |
| 150 |
|
| 151 |
with mock.patch('os.path.exists', return_value=True): |
| 152 |
result = operator.fetch_image(force_pull=True) |
| 153 |
|
| 154 |
expected_path = '/var/cache/image.qcow2' |
| 155 |
self.assertEqual(result, expected_path) |
| 156 |
self.assertEqual(operator.base_image_path, expected_path) |
| 157 |
mock_fetch_file.assert_called_once() |
| 158 |
self.mock_module.atomic_move.assert_called_once_with( |
| 159 |
'/tmp/downloaded_file', expected_path) |
| 160 |
|
| 161 |
def test_fetch_image_url_without_filename(self): |
| 162 |
"""Test fetching URL without filename""" |
| 163 |
operator = BaseImageOperator(self.mock_module, 'https://example.com/') |
| 164 |
|
| 165 |
# Make fail_json raise an exception to simulate real behavior |
| 166 |
self.mock_module.fail_json.side_effect = Exception("Module failed") |
| 167 |
|
| 168 |
with self.assertRaises(Exception): |
| 169 |
operator.fetch_image(force_pull=False) |
| 170 |
|
| 171 |
self.mock_module.fail_json.assert_called_once_with( |
| 172 |
msg="Failed to determine filename from base image URL: https://example.com/" |
| 173 |
) |
| 174 |
|
| 175 |
def test_fetch_image_unsupported_scheme(self): |
| 176 |
"""Test fetching with unsupported URL scheme (should be treated as local file)""" |
| 177 |
operator = BaseImageOperator( |
| 178 |
self.mock_module, 'file:///path/to/image.qcow2') |
| 179 |
|
| 180 |
with mock.patch('os.path.exists', return_value=True): |
| 181 |
result = operator.fetch_image(force_pull=False) |
| 182 |
|
| 183 |
self.assertEqual(result, 'file:///path/to/image.qcow2') |
| 184 |
self.assertEqual(operator.base_image_path, |
| 185 |
'file:///path/to/image.qcow2') |
| 186 |
|
| 187 |
|
| 188 |
class TestBaseImageOperatorResolveSystemDisk(unittest.TestCase): |
| 189 |
"""Test BaseImageOperator _resolve_system_disk method""" |
| 190 |
|
| 191 |
def setUp(self): |
| 192 |
"""Set up test fixtures""" |
| 193 |
self.mock_module = mock.Mock() |
| 194 |
self.mock_module.check_mode = False |
| 195 |
self.operator = BaseImageOperator( |
| 196 |
self.mock_module, '/path/to/image.qcow2') |
| 197 |
|
| 198 |
def test_resolve_system_disk_with_path(self): |
| 199 |
"""Test resolving system disk with path parameter""" |
| 200 |
disk_param = {'path': '/var/lib/libvirt/images/vm.qcow2', 'size': 20} |
| 201 |
|
| 202 |
result = self.operator._resolve_system_disk(disk_param) |
| 203 |
|
| 204 |
self.assertEqual(result, disk_param) |
| 205 |
self.assertEqual(self.operator.system_disk_path, |
| 206 |
'/var/lib/libvirt/images/vm.qcow2') |
| 207 |
|
| 208 |
def test_resolve_system_disk_without_path(self): |
| 209 |
"""Test resolving system disk without path parameter""" |
| 210 |
disk_param = {'size': 20} |
| 211 |
|
| 212 |
with self.assertRaises(ValueError) as context: |
| 213 |
self.operator._resolve_system_disk(disk_param) |
| 214 |
|
| 215 |
self.assertEqual(str(context.exception), |
| 216 |
"The first disk must have a path to import the cloud image") |
| 217 |
|
| 218 |
def test_resolve_system_disk_empty_path(self): |
| 219 |
"""Test resolving system disk with empty path""" |
| 220 |
disk_param = {'path': '', 'size': 20} |
| 221 |
|
| 222 |
with self.assertRaises(ValueError) as context: |
| 223 |
self.operator._resolve_system_disk(disk_param) |
| 224 |
|
| 225 |
self.assertEqual(str(context.exception), |
| 226 |
"The first disk must have a path to import the cloud image") |
| 227 |
|
| 228 |
def test_resolve_system_disk_none_path(self): |
| 229 |
"""Test resolving system disk with None path""" |
| 230 |
disk_param = {'path': None, 'size': 20} |
| 231 |
|
| 232 |
with self.assertRaises(ValueError) as context: |
| 233 |
self.operator._resolve_system_disk(disk_param) |
| 234 |
|
| 235 |
self.assertEqual(str(context.exception), |
| 236 |
"The first disk must have a path to import the cloud image") |
| 237 |
|
| 238 |
|
| 239 |
class TestBaseImageOperatorValidateChecksum(unittest.TestCase): |
| 240 |
"""Test BaseImageOperator validate_checksum method""" |
| 241 |
|
| 242 |
def setUp(self): |
| 243 |
"""Set up test fixtures""" |
| 244 |
self.mock_module = mock.Mock() |
| 245 |
self.mock_module.check_mode = False |
| 246 |
self.mock_module.digest_from_file = mock.Mock() |
| 247 |
|
| 248 |
def test_validate_checksum_no_checksum(self): |
| 249 |
"""Test validation when no checksum is specified""" |
| 250 |
operator = BaseImageOperator(self.mock_module, '/path/to/image.qcow2') |
| 251 |
|
| 252 |
result = operator.validate_checksum() |
| 253 |
|
| 254 |
self.assertTrue(result) |
| 255 |
|
| 256 |
def test_validate_checksum_no_base_image_path(self): |
| 257 |
"""Test validation when base image path is not set""" |
| 258 |
operator = BaseImageOperator( |
| 259 |
self.mock_module, |
| 260 |
'/path/to/image.qcow2', |
| 261 |
image_checksum='sha256:abc123' |
| 262 |
) |
| 263 |
|
| 264 |
operator.validate_checksum() |
| 265 |
|
| 266 |
self.mock_module.fail_json.assert_called_once_with( |
| 267 |
msg="Base image path not found for checksum validation" |
| 268 |
) |
| 269 |
|
| 270 |
@mock.patch('os.path.exists', return_value=False) |
| 271 |
def test_validate_checksum_file_not_exists(self, mock_exists): |
| 272 |
"""Test validation when base image file doesn't exist""" |
| 273 |
operator = BaseImageOperator( |
| 274 |
self.mock_module, |
| 275 |
'/path/to/image.qcow2', |
| 276 |
image_checksum='sha256:abc123' |
| 277 |
) |
| 278 |
operator.base_image_path = '/path/to/image.qcow2' |
| 279 |
|
| 280 |
operator.validate_checksum() |
| 281 |
|
| 282 |
self.mock_module.fail_json.assert_called_once_with( |
| 283 |
msg="Base image path not found for checksum validation" |
| 284 |
) |
| 285 |
|
| 286 |
def test_validate_checksum_invalid_format(self): |
| 287 |
"""Test validation with invalid checksum format""" |
| 288 |
operator = BaseImageOperator( |
| 289 |
self.mock_module, |
| 290 |
'/path/to/image.qcow2', |
| 291 |
image_checksum='invalidformat' |
| 292 |
) |
| 293 |
operator.base_image_path = '/path/to/image.qcow2' |
| 294 |
|
| 295 |
# Make fail_json raise an exception to simulate real behavior |
| 296 |
self.mock_module.fail_json.side_effect = Exception("Module failed") |
| 297 |
|
| 298 |
with mock.patch('os.path.exists', return_value=True): |
| 299 |
with self.assertRaises(Exception): |
| 300 |
operator.validate_checksum() |
| 301 |
|
| 302 |
self.mock_module.fail_json.assert_called_once_with( |
| 303 |
msg="The image_checksum parameter has to be in format <algorithm>:<checksum>" |
| 304 |
) |
| 305 |
|
| 306 |
@mock.patch('os.path.exists', return_value=True) |
| 307 |
def test_validate_checksum_invalid_hex(self, mock_exists): |
| 308 |
"""Test validation with invalid hex checksum""" |
| 309 |
operator = BaseImageOperator( |
| 310 |
self.mock_module, |
| 311 |
'/path/to/image.qcow2', |
| 312 |
image_checksum='sha256:invalid_hex_xyz' |
| 313 |
) |
| 314 |
operator.base_image_path = '/path/to/image.qcow2' |
| 315 |
|
| 316 |
operator.validate_checksum() |
| 317 |
|
| 318 |
self.mock_module.fail_json.assert_called_once_with( |
| 319 |
msg='The checksum format is invalid' |
| 320 |
) |
| 321 |
|
| 322 |
@mock.patch('os.path.exists', return_value=True) |
| 323 |
def test_validate_checksum_successful_match(self, mock_exists): |
| 324 |
"""Test successful checksum validation""" |
| 325 |
expected_checksum = 'abc123def456' |
| 326 |
operator = BaseImageOperator( |
| 327 |
self.mock_module, |
| 328 |
'/path/to/image.qcow2', |
| 329 |
image_checksum=f'sha256:{expected_checksum}' |
| 330 |
) |
| 331 |
operator.base_image_path = '/path/to/image.qcow2' |
| 332 |
self.mock_module.digest_from_file.return_value = expected_checksum |
| 333 |
|
| 334 |
result = operator.validate_checksum() |
| 335 |
|
| 336 |
self.assertTrue(result) |
| 337 |
self.mock_module.digest_from_file.assert_called_once_with( |
| 338 |
'/path/to/image.qcow2', 'sha256' |
| 339 |
) |
| 340 |
|
| 341 |
@mock.patch('os.path.exists', return_value=True) |
| 342 |
def test_validate_checksum_mismatch(self, mock_exists): |
| 343 |
"""Test checksum validation with mismatch""" |
| 344 |
operator = BaseImageOperator( |
| 345 |
self.mock_module, |
| 346 |
'/path/to/image.qcow2', |
| 347 |
image_checksum='sha256:abc123def456' |
| 348 |
) |
| 349 |
operator.base_image_path = '/path/to/image.qcow2' |
| 350 |
self.mock_module.digest_from_file.return_value = 'different_checksum' |
| 351 |
|
| 352 |
result = operator.validate_checksum() |
| 353 |
|
| 354 |
self.assertFalse(result) |
| 355 |
|
| 356 |
@mock.patch('os.path.exists', return_value=True) |
| 357 |
def test_validate_checksum_digest_error(self, mock_exists): |
| 358 |
"""Test checksum validation with digest calculation error""" |
| 359 |
operator = BaseImageOperator( |
| 360 |
self.mock_module, |
| 361 |
'/path/to/image.qcow2', |
| 362 |
image_checksum='sha256:abc123def456' |
| 363 |
) |
| 364 |
operator.base_image_path = '/path/to/image.qcow2' |
| 365 |
self.mock_module.digest_from_file.side_effect = ValueError( |
| 366 |
"Unsupported algorithm") |
| 367 |
|
| 368 |
# Make fail_json raise an exception to simulate real behavior |
| 369 |
self.mock_module.fail_json.side_effect = Exception("Module failed") |
| 370 |
|
| 371 |
with self.assertRaises(Exception): |
| 372 |
operator.validate_checksum() |
| 373 |
|
| 374 |
self.mock_module.fail_json.assert_called_once_with( |
| 375 |
msg="Failed to calculate the actual checksum of the file: Unsupported algorithm" |
| 376 |
) |
| 377 |
|
| 378 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.fetch_url') |
| 379 |
@mock.patch('os.path.exists', return_value=True) |
| 380 |
def test_validate_checksum_from_url(self, mock_exists, mock_fetch_url): |
| 381 |
"""Test checksum validation from URL""" |
| 382 |
# Mock the response object |
| 383 |
mock_response = mock.Mock() |
| 384 |
mock_response.read.return_value = b'abc123def456 image.qcow2\n' |
| 385 |
mock_fetch_url.return_value = (mock_response, {}) |
| 386 |
|
| 387 |
operator = BaseImageOperator( |
| 388 |
self.mock_module, |
| 389 |
'https://example.com/image.qcow2', |
| 390 |
image_checksum='sha256:https://example.com/checksums.sha256' |
| 391 |
) |
| 392 |
operator.base_image_path = '/path/to/image.qcow2' |
| 393 |
self.mock_module.digest_from_file.return_value = 'abc123def456' |
| 394 |
|
| 395 |
result = operator.validate_checksum() |
| 396 |
|
| 397 |
self.assertTrue(result) |
| 398 |
mock_fetch_url.assert_called_once_with( |
| 399 |
self.mock_module, 'https://example.com/checksums.sha256', timeout=60 |
| 400 |
) |
| 401 |
|
| 402 |
@mock.patch('os.path.exists', return_value=True) |
| 403 |
def test_validate_checksum_whitespace_handling(self, mock_exists): |
| 404 |
"""Test checksum validation with whitespace in checksum""" |
| 405 |
operator = BaseImageOperator( |
| 406 |
self.mock_module, |
| 407 |
'/path/to/image.qcow2', |
| 408 |
image_checksum='sha256: abc 123 def 456 ' |
| 409 |
) |
| 410 |
operator.base_image_path = '/path/to/image.qcow2' |
| 411 |
self.mock_module.digest_from_file.return_value = 'abc123def456' |
| 412 |
|
| 413 |
result = operator.validate_checksum() |
| 414 |
|
| 415 |
self.assertTrue(result) |
| 416 |
|
| 417 |
|
| 418 |
class TestBaseImageOperatorFetchChecksumFromUrl(unittest.TestCase): |
| 419 |
"""Test BaseImageOperator _fetch_checksum_from_url method""" |
| 420 |
|
| 421 |
def setUp(self): |
| 422 |
"""Set up test fixtures""" |
| 423 |
self.mock_module = mock.Mock() |
| 424 |
self.mock_module.check_mode = False |
| 425 |
self.operator = BaseImageOperator( |
| 426 |
self.mock_module, |
| 427 |
'https://example.com/image.qcow2' |
| 428 |
) |
| 429 |
|
| 430 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.fetch_url') |
| 431 |
def test_fetch_checksum_single_line_format(self, mock_fetch_url): |
| 432 |
"""Test fetching checksum from single-line format""" |
| 433 |
mock_response = mock.Mock() |
| 434 |
mock_response.read.return_value = b'abc123def456' |
| 435 |
mock_fetch_url.return_value = (mock_response, {}) |
| 436 |
|
| 437 |
result = self.operator._fetch_checksum_from_url( |
| 438 |
'https://example.com/checksum.txt', 'sha256' |
| 439 |
) |
| 440 |
|
| 441 |
self.assertEqual(result, 'abc123def456') |
| 442 |
|
| 443 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.fetch_url') |
| 444 |
def test_fetch_checksum_multi_line_format(self, mock_fetch_url): |
| 445 |
"""Test fetching checksum from multi-line format""" |
| 446 |
checksum_content = ( |
| 447 |
'abc123def456 image.qcow2\n' |
| 448 |
'def456ghi789 other.qcow2\n' |
| 449 |
) |
| 450 |
mock_response = mock.Mock() |
| 451 |
mock_response.read.return_value = checksum_content.encode() |
| 452 |
mock_fetch_url.return_value = (mock_response, {}) |
| 453 |
|
| 454 |
result = self.operator._fetch_checksum_from_url( |
| 455 |
'https://example.com/checksums.txt', 'sha256' |
| 456 |
) |
| 457 |
|
| 458 |
self.assertEqual(result, 'abc123def456') |
| 459 |
|
| 460 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.fetch_url') |
| 461 |
def test_fetch_checksum_binary_mode_marker(self, mock_fetch_url): |
| 462 |
"""Test fetching checksum with binary mode marker""" |
| 463 |
checksum_content = 'abc123def456 *image.qcow2\n' |
| 464 |
mock_response = mock.Mock() |
| 465 |
mock_response.read.return_value = checksum_content.encode() |
| 466 |
mock_fetch_url.return_value = (mock_response, {}) |
| 467 |
|
| 468 |
result = self.operator._fetch_checksum_from_url( |
| 469 |
'https://example.com/checksums.txt', 'sha256' |
| 470 |
) |
| 471 |
|
| 472 |
self.assertEqual(result, 'abc123def456') |
| 473 |
|
| 474 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.fetch_url') |
| 475 |
def test_fetch_checksum_with_path_prefix(self, mock_fetch_url): |
| 476 |
"""Test fetching checksum with path prefix""" |
| 477 |
checksum_content = 'abc123def456 ./image.qcow2\n' |
| 478 |
mock_response = mock.Mock() |
| 479 |
mock_response.read.return_value = checksum_content.encode() |
| 480 |
mock_fetch_url.return_value = (mock_response, {}) |
| 481 |
|
| 482 |
result = self.operator._fetch_checksum_from_url( |
| 483 |
'https://example.com/checksums.txt', 'sha256' |
| 484 |
) |
| 485 |
|
| 486 |
self.assertEqual(result, 'abc123def456') |
| 487 |
|
| 488 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.fetch_url') |
| 489 |
def test_fetch_checksum_file_not_found(self, mock_fetch_url): |
| 490 |
"""Test fetching checksum when file is not found in checksum file""" |
| 491 |
checksum_content = 'abc123def456 other.qcow2\n' |
| 492 |
mock_response = mock.Mock() |
| 493 |
mock_response.read.return_value = checksum_content.encode() |
| 494 |
mock_fetch_url.return_value = (mock_response, {}) |
| 495 |
|
| 496 |
self.operator._fetch_checksum_from_url( |
| 497 |
'https://example.com/checksums.txt', 'sha256' |
| 498 |
) |
| 499 |
|
| 500 |
self.mock_module.fail_json.assert_called_once_with( |
| 501 |
msg="Unable to find a checksum for file 'image.qcow2' in 'https://example.com/checksums.txt'" |
| 502 |
) |
| 503 |
|
| 504 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.fetch_url') |
| 505 |
def test_fetch_checksum_download_failure(self, mock_fetch_url): |
| 506 |
"""Test fetching checksum when download fails""" |
| 507 |
mock_fetch_url.return_value = (None, {'msg': 'Connection failed'}) |
| 508 |
|
| 509 |
# Make fail_json raise an exception to simulate real behavior |
| 510 |
self.mock_module.fail_json.side_effect = Exception("Module failed") |
| 511 |
|
| 512 |
with self.assertRaises(Exception): |
| 513 |
self.operator._fetch_checksum_from_url( |
| 514 |
'https://example.com/checksums.txt', 'sha256' |
| 515 |
) |
| 516 |
|
| 517 |
self.mock_module.fail_json.assert_called_once_with( |
| 518 |
msg="Failed to download checksum from https://example.com/checksums.txt: Connection failed" |
| 519 |
) |
| 520 |
|
| 521 |
|
| 522 |
class TestBaseImageOperatorBuildSystemDisk(unittest.TestCase): |
| 523 |
"""Test BaseImageOperator build_system_disk method""" |
| 524 |
|
| 525 |
def setUp(self): |
| 526 |
"""Set up test fixtures""" |
| 527 |
self.mock_module = mock.Mock() |
| 528 |
self.mock_module.check_mode = False |
| 529 |
self.mock_module.preserved_copy = mock.Mock() |
| 530 |
self.operator = BaseImageOperator( |
| 531 |
self.mock_module, '/path/to/image.qcow2') |
| 532 |
self.operator.base_image_path = '/path/to/base.qcow2' |
| 533 |
|
| 534 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.QemuImgTool') |
| 535 |
@mock.patch('os.path.exists') |
| 536 |
def test_build_system_disk_basic(self, mock_exists, mock_qemu_tool_class): |
| 537 |
"""Test basic system disk building""" |
| 538 |
mock_exists.return_value = False # System disk doesn't exist |
| 539 |
|
| 540 |
# Configure the mocked QemuImgTool |
| 541 |
mock_qemu_instance = mock.Mock() |
| 542 |
mock_qemu_tool_class.return_value = mock_qemu_instance |
| 543 |
mock_qemu_instance.info.return_value = (0, { |
| 544 |
'format': 'qcow2', |
| 545 |
'virtual-size': 1073741824 # 1GB |
| 546 |
}, '') |
| 547 |
mock_qemu_instance.resize.return_value = (0, '', '') |
| 548 |
mock_qemu_instance.get_commands.return_value = [] |
| 549 |
|
| 550 |
disk_param = { |
| 551 |
'path': '/var/lib/libvirt/images/vm.qcow2', |
| 552 |
'size': 20, |
| 553 |
'format': 'qcow2' |
| 554 |
} |
| 555 |
|
| 556 |
result = self.operator.build_system_disk(disk_param) |
| 557 |
|
| 558 |
self.assertEqual(result, disk_param) |
| 559 |
self.assertEqual(self.operator.system_disk_path, |
| 560 |
'/var/lib/libvirt/images/vm.qcow2') |
| 561 |
mock_qemu_instance.info.assert_called_once_with('/path/to/base.qcow2') |
| 562 |
self.mock_module.preserved_copy.assert_called_once_with( |
| 563 |
'/path/to/base.qcow2', '/var/lib/libvirt/images/vm.qcow2' |
| 564 |
) |
| 565 |
mock_qemu_instance.resize.assert_called_once_with( |
| 566 |
'/var/lib/libvirt/images/vm.qcow2', '20G' |
| 567 |
) |
| 568 |
|
| 569 |
@mock.patch('os.path.exists') |
| 570 |
def test_build_system_disk_already_exists(self, mock_exists): |
| 571 |
"""Test building system disk when target already exists""" |
| 572 |
mock_exists.return_value = True |
| 573 |
|
| 574 |
# Make fail_json raise an exception to simulate real behavior |
| 575 |
self.mock_module.fail_json.side_effect = Exception("Module failed") |
| 576 |
|
| 577 |
disk_param = { |
| 578 |
'path': '/var/lib/libvirt/images/vm.qcow2', |
| 579 |
'size': 20 |
| 580 |
} |
| 581 |
|
| 582 |
with self.assertRaises(Exception): |
| 583 |
self.operator.build_system_disk(disk_param) |
| 584 |
|
| 585 |
self.mock_module.fail_json.assert_called_once_with( |
| 586 |
msg="The system disk file already exists: /var/lib/libvirt/images/vm.qcow2" |
| 587 |
) |
| 588 |
|
| 589 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.QemuImgTool') |
| 590 |
@mock.patch('os.path.exists') |
| 591 |
def test_build_system_disk_info_failure(self, mock_exists, mock_qemu_tool_class): |
| 592 |
"""Test building system disk when qemu-img info fails""" |
| 593 |
mock_exists.return_value = False |
| 594 |
|
| 595 |
# Configure the mocked QemuImgTool to return failure |
| 596 |
mock_qemu_instance = mock.Mock() |
| 597 |
mock_qemu_tool_class.return_value = mock_qemu_instance |
| 598 |
mock_qemu_instance.info.return_value = ( |
| 599 |
1, {}, 'Failed to get image info') |
| 600 |
|
| 601 |
# Make fail_json raise an exception to simulate real behavior |
| 602 |
self.mock_module.fail_json.side_effect = Exception("Module failed") |
| 603 |
|
| 604 |
disk_param = { |
| 605 |
'path': '/var/lib/libvirt/images/vm.qcow2', |
| 606 |
'size': 20 |
| 607 |
} |
| 608 |
|
| 609 |
with self.assertRaises(Exception): |
| 610 |
self.operator.build_system_disk(disk_param) |
| 611 |
|
| 612 |
self.mock_module.fail_json.assert_called_once_with( |
| 613 |
msg="Failed to get base image info: Failed to get image info" |
| 614 |
) |
| 615 |
|
| 616 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.QemuImgTool') |
| 617 |
@mock.patch('os.path.exists') |
| 618 |
def test_build_system_disk_no_format(self, mock_exists, mock_qemu_tool_class): |
| 619 |
"""Test building system disk when base image has no format""" |
| 620 |
mock_exists.return_value = False |
| 621 |
|
| 622 |
# Configure the mocked QemuImgTool to return no format |
| 623 |
mock_qemu_instance = mock.Mock() |
| 624 |
mock_qemu_tool_class.return_value = mock_qemu_instance |
| 625 |
mock_qemu_instance.info.return_value = (0, { |
| 626 |
'virtual-size': 1073741824 |
| 627 |
}, '') |
| 628 |
|
| 629 |
# Make fail_json raise an exception to simulate real behavior |
| 630 |
self.mock_module.fail_json.side_effect = Exception("Module failed") |
| 631 |
|
| 632 |
disk_param = { |
| 633 |
'path': '/var/lib/libvirt/images/vm.qcow2', |
| 634 |
'size': 20 |
| 635 |
} |
| 636 |
|
| 637 |
with self.assertRaises(Exception): |
| 638 |
self.operator.build_system_disk(disk_param) |
| 639 |
|
| 640 |
self.mock_module.fail_json.assert_called_once_with( |
| 641 |
msg="No valid format found for the base image: /path/to/base.qcow2" |
| 642 |
) |
| 643 |
|
| 644 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.QemuImgTool') |
| 645 |
@mock.patch('os.path.exists') |
| 646 |
def test_build_system_disk_invalid_size(self, mock_exists, mock_qemu_tool_class): |
| 647 |
"""Test building system disk with invalid size""" |
| 648 |
mock_exists.return_value = False |
| 649 |
|
| 650 |
# Configure the mocked QemuImgTool |
| 651 |
mock_qemu_instance = mock.Mock() |
| 652 |
mock_qemu_tool_class.return_value = mock_qemu_instance |
| 653 |
mock_qemu_instance.info.return_value = (0, { |
| 654 |
'format': 'qcow2', |
| 655 |
'virtual-size': 1073741824 |
| 656 |
}, '') |
| 657 |
|
| 658 |
# Make fail_json raise an exception to simulate real behavior |
| 659 |
self.mock_module.fail_json.side_effect = Exception("Module failed") |
| 660 |
|
| 661 |
disk_param = { |
| 662 |
'path': '/var/lib/libvirt/images/vm.qcow2', |
| 663 |
'size': 'invalid' |
| 664 |
} |
| 665 |
|
| 666 |
with self.assertRaises(Exception): |
| 667 |
self.operator.build_system_disk(disk_param) |
| 668 |
|
| 669 |
self.mock_module.fail_json.assert_called_once_with( |
| 670 |
msg="The system disk size must be an integer" |
| 671 |
) |
| 672 |
|
| 673 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.QemuImgTool') |
| 674 |
@mock.patch('os.path.exists') |
| 675 |
def test_build_system_disk_size_too_small(self, mock_exists, mock_qemu_tool_class): |
| 676 |
"""Test building system disk when size is too small""" |
| 677 |
mock_exists.return_value = False |
| 678 |
|
| 679 |
# Configure the mocked QemuImgTool |
| 680 |
mock_qemu_instance = mock.Mock() |
| 681 |
mock_qemu_tool_class.return_value = mock_qemu_instance |
| 682 |
mock_qemu_instance.info.return_value = (0, { |
| 683 |
'format': 'qcow2', |
| 684 |
'virtual-size': 21474836480 # 20GB |
| 685 |
}, '') |
| 686 |
|
| 687 |
# Make fail_json raise an exception to simulate real behavior |
| 688 |
self.mock_module.fail_json.side_effect = Exception("Module failed") |
| 689 |
|
| 690 |
disk_param = { |
| 691 |
'path': '/var/lib/libvirt/images/vm.qcow2', |
| 692 |
'size': 10 # 10GB, smaller than base image |
| 693 |
} |
| 694 |
|
| 695 |
with self.assertRaises(Exception): |
| 696 |
self.operator.build_system_disk(disk_param) |
| 697 |
|
| 698 |
expected_system_size = 10 * 1024 * 1024 * 1024 |
| 699 |
self.mock_module.fail_json.assert_called_once_with( |
| 700 |
msg=f"The system disk size is too small to import the base image: {expected_system_size} < 21474836480" |
| 701 |
) |
| 702 |
|
| 703 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.QemuImgTool') |
| 704 |
@mock.patch('os.path.exists') |
| 705 |
@mock.patch('os.remove') |
| 706 |
def test_build_system_disk_force_disk_removes_existing(self, mock_remove, mock_exists, mock_qemu_tool_class): |
| 707 |
"""Test building system disk with force_disk=True removes existing file""" |
| 708 |
mock_exists.return_value = True # System disk exists |
| 709 |
|
| 710 |
# Configure the mocked QemuImgTool |
| 711 |
mock_qemu_instance = mock.Mock() |
| 712 |
mock_qemu_tool_class.return_value = mock_qemu_instance |
| 713 |
mock_qemu_instance.info.return_value = (0, { |
| 714 |
'format': 'qcow2', |
| 715 |
'virtual-size': 1073741824 # 1GB |
| 716 |
}, '') |
| 717 |
mock_qemu_instance.resize.return_value = (0, '', '') |
| 718 |
mock_qemu_instance.get_commands.return_value = [] |
| 719 |
|
| 720 |
disk_param = { |
| 721 |
'path': '/var/lib/libvirt/images/vm.qcow2', |
| 722 |
'size': 20, |
| 723 |
'format': 'qcow2' |
| 724 |
} |
| 725 |
|
| 726 |
result = self.operator.build_system_disk(disk_param, force_disk=True) |
| 727 |
|
| 728 |
# Verify the existing file was removed |
| 729 |
mock_remove.assert_called_once_with('/var/lib/libvirt/images/vm.qcow2') |
| 730 |
self.assertEqual(result, disk_param) |
| 731 |
|
| 732 |
# Verify disk was created after removal |
| 733 |
self.mock_module.preserved_copy.assert_called_once_with( |
| 734 |
'/path/to/base.qcow2', '/var/lib/libvirt/images/vm.qcow2' |
| 735 |
) |
| 736 |
mock_qemu_instance.resize.assert_called_once() |
| 737 |
|
| 738 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.QemuImgTool') |
| 739 |
@mock.patch('os.path.exists') |
| 740 |
@mock.patch('os.remove') |
| 741 |
def test_build_system_disk_force_disk_check_mode(self, mock_remove, mock_exists, mock_qemu_tool_class): |
| 742 |
"""Test building system disk with force_disk=True in check_mode doesn't remove file""" |
| 743 |
self.mock_module.check_mode = True |
| 744 |
# System disk exists, base image doesn't exist |
| 745 |
mock_exists.side_effect = lambda path: path == '/var/lib/libvirt/images/vm.qcow2' |
| 746 |
|
| 747 |
# Configure the mocked QemuImgTool |
| 748 |
mock_qemu_instance = mock.Mock() |
| 749 |
mock_qemu_tool_class.return_value = mock_qemu_instance |
| 750 |
|
| 751 |
disk_param = { |
| 752 |
'path': '/var/lib/libvirt/images/vm.qcow2', |
| 753 |
'size': 20, |
| 754 |
'format': 'qcow2' |
| 755 |
} |
| 756 |
|
| 757 |
result = self.operator.build_system_disk(disk_param, force_disk=True) |
| 758 |
|
| 759 |
# In check_mode, file should NOT be removed |
| 760 |
mock_remove.assert_not_called() |
| 761 |
|
| 762 |
# Should return disk config without creating files |
| 763 |
self.assertEqual(result, disk_param) |
| 764 |
self.mock_module.preserved_copy.assert_not_called() |
| 765 |
mock_qemu_instance.resize.assert_not_called() |
| 766 |
|
| 767 |
@mock.patch('os.path.exists') |
| 768 |
def test_build_system_disk_force_disk_false_existing_fails(self, mock_exists): |
| 769 |
"""Test building system disk with force_disk=False fails when disk exists""" |
| 770 |
mock_exists.return_value = True # System disk exists |
| 771 |
|
| 772 |
# Make fail_json raise an exception to simulate real behavior |
| 773 |
self.mock_module.fail_json.side_effect = Exception("Module failed") |
| 774 |
|
| 775 |
disk_param = { |
| 776 |
'path': '/var/lib/libvirt/images/vm.qcow2', |
| 777 |
'size': 20 |
| 778 |
} |
| 779 |
|
| 780 |
with self.assertRaises(Exception): |
| 781 |
self.operator.build_system_disk(disk_param, force_disk=False) |
| 782 |
|
| 783 |
self.mock_module.fail_json.assert_called_once_with( |
| 784 |
msg="The system disk file already exists: /var/lib/libvirt/images/vm.qcow2" |
| 785 |
) |
| 786 |
|
| 787 |
|
| 788 |
class TestBaseImageOperatorCheckMode(unittest.TestCase): |
| 789 |
"""Test BaseImageOperator check_mode support""" |
| 790 |
|
| 791 |
def setUp(self): |
| 792 |
"""Set up test fixtures""" |
| 793 |
self.mock_module = mock.Mock() |
| 794 |
self.mock_module.tmpdir = '/tmp/ansible_test' |
| 795 |
|
| 796 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.fetch_file') |
| 797 |
@mock.patch('os.path.exists') |
| 798 |
def test_fetch_image_check_mode_local_file(self, mock_exists, mock_fetch_file): |
| 799 |
"""Test fetch_image with check_mode for local file""" |
| 800 |
self.mock_module.check_mode = True |
| 801 |
mock_exists.return_value = True |
| 802 |
|
| 803 |
operator = BaseImageOperator( |
| 804 |
self.mock_module, '/path/to/local/image.qcow2') |
| 805 |
result = operator.fetch_image(force_pull=False) |
| 806 |
|
| 807 |
# Local files should still be processed in check_mode |
| 808 |
self.assertEqual(result, '/path/to/local/image.qcow2') |
| 809 |
mock_fetch_file.assert_not_called() |
| 810 |
|
| 811 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.fetch_file') |
| 812 |
@mock.patch('os.path.exists') |
| 813 |
def test_fetch_image_check_mode_url(self, mock_exists, mock_fetch_file): |
| 814 |
"""Test fetch_image with check_mode for URL""" |
| 815 |
self.mock_module.check_mode = True |
| 816 |
mock_exists.return_value = False |
| 817 |
|
| 818 |
operator = BaseImageOperator( |
| 819 |
self.mock_module, 'https://example.com/image.qcow2') |
| 820 |
result = operator.fetch_image(force_pull=False) |
| 821 |
|
| 822 |
# URL downloads should be skipped in check_mode |
| 823 |
expected_path = '/tmp/ansible_test/image.qcow2' |
| 824 |
self.assertEqual(result, expected_path) |
| 825 |
self.assertEqual(operator.base_image_path, expected_path) |
| 826 |
mock_fetch_file.assert_not_called() |
| 827 |
self.mock_module.atomic_move.assert_not_called() |
| 828 |
|
| 829 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.fetch_file') |
| 830 |
@mock.patch('os.path.exists') |
| 831 |
def test_fetch_image_normal_mode_url(self, mock_exists, mock_fetch_file): |
| 832 |
"""Test fetch_image with normal mode for URL""" |
| 833 |
self.mock_module.check_mode = False |
| 834 |
mock_exists.return_value = False |
| 835 |
mock_fetch_file.return_value = '/tmp/temp_image' |
| 836 |
|
| 837 |
operator = BaseImageOperator( |
| 838 |
self.mock_module, 'https://example.com/image.qcow2') |
| 839 |
result = operator.fetch_image(force_pull=False) |
| 840 |
|
| 841 |
# Normal mode should download the file |
| 842 |
expected_path = '/tmp/ansible_test/image.qcow2' |
| 843 |
self.assertEqual(result, expected_path) |
| 844 |
mock_fetch_file.assert_called_once() |
| 845 |
self.mock_module.atomic_move.assert_called_once_with( |
| 846 |
'/tmp/temp_image', expected_path) |
| 847 |
|
| 848 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.QemuImgTool') |
| 849 |
@mock.patch('os.path.exists') |
| 850 |
def test_build_system_disk_check_mode(self, mock_exists, mock_qemu_tool_class): |
| 851 |
"""Test build_system_disk with check_mode""" |
| 852 |
self.mock_module.check_mode = True |
| 853 |
mock_exists.side_effect = lambda path: False # System disk doesn't exist |
| 854 |
|
| 855 |
# Mock QemuImgTool |
| 856 |
mock_qemu_tool = mock.Mock() |
| 857 |
mock_qemu_tool_class.return_value = mock_qemu_tool |
| 858 |
|
| 859 |
operator = BaseImageOperator(self.mock_module, '/path/to/image.qcow2') |
| 860 |
operator.base_image_path = '/path/to/base.qcow2' |
| 861 |
|
| 862 |
disk_param = { |
| 863 |
'path': '/var/lib/libvirt/images/vm.qcow2', |
| 864 |
'size': 20, |
| 865 |
'format': 'qcow2' |
| 866 |
} |
| 867 |
|
| 868 |
result = operator.build_system_disk(disk_param) |
| 869 |
|
| 870 |
# Should return disk config without creating files |
| 871 |
self.assertEqual(result, disk_param) |
| 872 |
|
| 873 |
# QemuImgTool operations should not be called for file creation |
| 874 |
mock_qemu_tool.convert.assert_not_called() |
| 875 |
mock_qemu_tool.resize.assert_not_called() |
| 876 |
self.mock_module.preserved_copy.assert_not_called() |
| 877 |
|
| 878 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.QemuImgTool') |
| 879 |
@mock.patch('os.path.exists') |
| 880 |
def test_build_system_disk_check_mode_with_nonexistent_base_image(self, mock_exists, mock_qemu_tool_class): |
| 881 |
"""Test build_system_disk with check_mode when base image doesn't exist""" |
| 882 |
self.mock_module.check_mode = True |
| 883 |
mock_exists.side_effect = lambda path: False # No files exist |
| 884 |
|
| 885 |
# Mock QemuImgTool |
| 886 |
mock_qemu_tool = mock.Mock() |
| 887 |
mock_qemu_tool_class.return_value = mock_qemu_tool |
| 888 |
|
| 889 |
operator = BaseImageOperator(self.mock_module, '/path/to/image.qcow2') |
| 890 |
operator.base_image_path = '/path/to/nonexistent/base.qcow2' |
| 891 |
|
| 892 |
disk_param = { |
| 893 |
'path': '/var/lib/libvirt/images/vm.qcow2', |
| 894 |
'size': 20, |
| 895 |
'format': 'qcow2' |
| 896 |
} |
| 897 |
|
| 898 |
result = operator.build_system_disk(disk_param) |
| 899 |
|
| 900 |
# Should use mock values for validation when base image doesn't exist |
| 901 |
self.assertEqual(result, disk_param) |
| 902 |
|
| 903 |
# QemuImgTool.info should not be called since base image doesn't exist |
| 904 |
mock_qemu_tool.info.assert_not_called() |
| 905 |
|
| 906 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.QemuImgTool') |
| 907 |
@mock.patch('os.path.exists') |
| 908 |
def test_build_system_disk_normal_mode(self, mock_exists, mock_qemu_tool_class): |
| 909 |
"""Test build_system_disk with normal mode""" |
| 910 |
self.mock_module.check_mode = False |
| 911 |
self.mock_module.preserved_copy = mock.Mock() |
| 912 |
# System disk doesn't exist, base image exists |
| 913 |
mock_exists.side_effect = lambda path: path != '/var/lib/libvirt/images/vm.qcow2' |
| 914 |
|
| 915 |
# Mock QemuImgTool |
| 916 |
mock_qemu_tool = mock.Mock() |
| 917 |
mock_qemu_tool.info.return_value = (0, { |
| 918 |
'format': 'qcow2', |
| 919 |
'virtual-size': 5 * 1024 * 1024 * 1024 # 5GB |
| 920 |
}, '') |
| 921 |
mock_qemu_tool.resize.return_value = (0, '', '') |
| 922 |
mock_qemu_tool.get_commands.return_value = [] |
| 923 |
mock_qemu_tool_class.return_value = mock_qemu_tool |
| 924 |
|
| 925 |
operator = BaseImageOperator(self.mock_module, '/path/to/image.qcow2') |
| 926 |
operator.base_image_path = '/path/to/base.qcow2' |
| 927 |
|
| 928 |
disk_param = { |
| 929 |
'path': '/var/lib/libvirt/images/vm.qcow2', |
| 930 |
'size': 20, |
| 931 |
'format': 'qcow2' |
| 932 |
} |
| 933 |
|
| 934 |
result = operator.build_system_disk(disk_param) |
| 935 |
|
| 936 |
# Should create files in normal mode |
| 937 |
self.assertEqual(result, disk_param) |
| 938 |
|
| 939 |
# File operations should be called |
| 940 |
self.mock_module.preserved_copy.assert_called_once() |
| 941 |
mock_qemu_tool.resize.assert_called_once() |
| 942 |
|
| 943 |
def test_validate_checksum_check_mode_no_file(self): |
| 944 |
"""Test validate_checksum with check_mode when file doesn't exist""" |
| 945 |
self.mock_module.check_mode = True |
| 946 |
|
| 947 |
operator = BaseImageOperator( |
| 948 |
self.mock_module, |
| 949 |
'https://example.com/image.qcow2', |
| 950 |
image_checksum='sha256:abc123def456' |
| 951 |
) |
| 952 |
operator.base_image_path = '/path/to/nonexistent/image.qcow2' |
| 953 |
|
| 954 |
# Should return True in check_mode when file doesn't exist |
| 955 |
result = operator.validate_checksum() |
| 956 |
self.assertTrue(result) |
| 957 |
|
| 958 |
@mock.patch('os.path.exists') |
| 959 |
def test_validate_checksum_check_mode_file_exists(self, mock_exists): |
| 960 |
"""Test validate_checksum with check_mode when file exists locally""" |
| 961 |
self.mock_module.check_mode = True |
| 962 |
mock_exists.return_value = True |
| 963 |
self.mock_module.digest_from_file.return_value = 'abc123def456' |
| 964 |
|
| 965 |
operator = BaseImageOperator( |
| 966 |
self.mock_module, |
| 967 |
'/path/to/local/image.qcow2', |
| 968 |
image_checksum='sha256:abc123def456' |
| 969 |
) |
| 970 |
operator.base_image_path = '/path/to/local/image.qcow2' |
| 971 |
|
| 972 |
# Should perform actual validation in check_mode when file exists |
| 973 |
result = operator.validate_checksum() |
| 974 |
self.assertTrue(result) |
| 975 |
self.mock_module.digest_from_file.assert_called_once() |
| 976 |
|
| 977 |
@mock.patch('os.path.exists') |
| 978 |
def test_validate_checksum_normal_mode(self, mock_exists): |
| 979 |
"""Test validate_checksum with normal mode""" |
| 980 |
self.mock_module.check_mode = False |
| 981 |
mock_exists.return_value = False # File doesn't exist |
| 982 |
|
| 983 |
# Make fail_json raise an exception |
| 984 |
self.mock_module.fail_json.side_effect = Exception( |
| 985 |
"Base image path not found for checksum validation") |
| 986 |
|
| 987 |
operator = BaseImageOperator( |
| 988 |
self.mock_module, |
| 989 |
'https://example.com/image.qcow2', |
| 990 |
image_checksum='sha256:abc123def456' |
| 991 |
) |
| 992 |
operator.base_image_path = '/path/to/nonexistent/image.qcow2' |
| 993 |
|
| 994 |
# Should fail in normal mode when file doesn't exist |
| 995 |
with self.assertRaises(Exception): |
| 996 |
operator.validate_checksum() |
| 997 |
|
| 998 |
self.mock_module.fail_json.assert_called() |
| 999 |
|
| 1000 |
|
| 1001 |
class TestCore(unittest.TestCase): |
| 1002 |
"""Test core() function""" |
| 1003 |
|
| 1004 |
def setUp(self): |
| 1005 |
"""Set up test fixtures""" |
| 1006 |
self.mock_module = mock.Mock() |
| 1007 |
self.mock_module.check_mode = False |
| 1008 |
# Mock run_command to return proper tuple for LibvirtConnection initialization |
| 1009 |
self.mock_module.run_command.return_value = (0, 'linux-kernel', '') |
| 1010 |
|
| 1011 |
# Default successful params |
| 1012 |
self.mock_module.params = { |
| 1013 |
'state': 'present', |
| 1014 |
'name': 'test-vm', |
| 1015 |
'recreate': False, |
| 1016 |
'base_image': 'https://example.com/image.qcow2', |
| 1017 |
'image_cache_dir': '/tmp/cache', |
| 1018 |
'force_pull': False, |
| 1019 |
'force_disk': False, |
| 1020 |
'disks': [{'path': '/var/lib/libvirt/images/test.qcow2', 'size': 20}], |
| 1021 |
'image_checksum': None, |
| 1022 |
'url_timeout': None, |
| 1023 |
'wait_for_cloud_init_reboot': True, |
| 1024 |
'cloud_init_auto_reboot': True, |
| 1025 |
'cloud_init_reboot_timeout': 600 |
| 1026 |
} |
| 1027 |
|
| 1028 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.BaseImageOperator') |
| 1029 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.VirtInstallTool') |
| 1030 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.LibvirtWrapper') |
| 1031 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.validate_disks') |
| 1032 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.update_virtinst_params') |
| 1033 |
def test_core_vm_not_exists_create_success(self, mock_update_params, mock_validate_disks, |
| 1034 |
mock_libvirt_wrapper_class, mock_virt_install_class, |
| 1035 |
mock_base_image_operator_class): |
| 1036 |
"""Test core() when VM doesn't exist and creation succeeds""" |
| 1037 |
from ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance import core, VIRT_SUCCESS |
| 1038 |
from ansible_collections.community.libvirt.plugins.module_utils.libvirt import VMNotFound |
| 1039 |
|
| 1040 |
# Mock LibvirtWrapper - VM doesn't exist |
| 1041 |
mock_virt_conn = mock.Mock() |
| 1042 |
mock_virt_conn.find_vm.side_effect = VMNotFound("VM not found") |
| 1043 |
mock_libvirt_wrapper_class.return_value = mock_virt_conn |
| 1044 |
|
| 1045 |
# Mock VirtInstallTool |
| 1046 |
mock_virt_install = mock.Mock() |
| 1047 |
mock_virt_install.execute.return_value = ( |
| 1048 |
True, VIRT_SUCCESS, {'some': 'data'}) |
| 1049 |
mock_virt_install.get_commands.return_value = [] |
| 1050 |
mock_virt_install_class.return_value = mock_virt_install |
| 1051 |
|
| 1052 |
# Mock BaseImageOperator |
| 1053 |
mock_operator = mock.Mock() |
| 1054 |
mock_operator.validate_checksum.return_value = True |
| 1055 |
mock_operator.build_system_disk.return_value = { |
| 1056 |
'path': '/var/lib/libvirt/images/test.qcow2'} |
| 1057 |
mock_operator.get_commands.return_value = [] |
| 1058 |
mock_base_image_operator_class.return_value = mock_operator |
| 1059 |
|
| 1060 |
# Execute core function |
| 1061 |
rc, result = core(self.mock_module) |
| 1062 |
|
| 1063 |
# Verify behavior |
| 1064 |
mock_validate_disks.assert_called_once() |
| 1065 |
mock_virt_conn.find_vm.assert_called_once_with('test-vm') |
| 1066 |
mock_operator.fetch_image.assert_called_once_with(False, timeout=None) |
| 1067 |
mock_operator.validate_checksum.assert_called_once() |
| 1068 |
mock_operator.build_system_disk.assert_called_once() |
| 1069 |
mock_update_params.assert_called_once() |
| 1070 |
mock_virt_install.execute.assert_called_once_with(dryrun=False, wait_timeout=600) |
| 1071 |
|
| 1072 |
# VM should not be destroyed (doesn't exist) |
| 1073 |
mock_virt_conn.destroy.assert_not_called() |
| 1074 |
mock_virt_conn.undefine.assert_not_called() |
| 1075 |
|
| 1076 |
# Should return success |
| 1077 |
self.assertEqual(rc, VIRT_SUCCESS) |
| 1078 |
self.assertTrue(result['changed']) |
| 1079 |
self.assertEqual(result['some'], 'data') |
| 1080 |
|
| 1081 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.BaseImageOperator') |
| 1082 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.VirtInstallTool') |
| 1083 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.LibvirtWrapper') |
| 1084 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.validate_disks') |
| 1085 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.update_virtinst_params') |
| 1086 |
def test_core_vm_exists_no_recreate(self, mock_update_params, mock_validate_disks, |
| 1087 |
mock_libvirt_wrapper_class, mock_virt_install_class, |
| 1088 |
mock_base_image_operator_class): |
| 1089 |
"""Test core() when VM exists and recreate=False""" |
| 1090 |
from ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance import core, VIRT_SUCCESS |
| 1091 |
|
| 1092 |
# Mock LibvirtWrapper - VM exists |
| 1093 |
mock_virt_conn = mock.Mock() |
| 1094 |
mock_vm = mock.Mock() |
| 1095 |
mock_virt_conn.find_vm.return_value = mock_vm |
| 1096 |
mock_libvirt_wrapper_class.return_value = mock_virt_conn |
| 1097 |
|
| 1098 |
# Mock VirtInstallTool (shouldn't be called) |
| 1099 |
mock_virt_install = mock.Mock() |
| 1100 |
mock_virt_install_class.return_value = mock_virt_install |
| 1101 |
|
| 1102 |
# Execute core function |
| 1103 |
rc, result = core(self.mock_module) |
| 1104 |
|
| 1105 |
# Verify behavior |
| 1106 |
mock_validate_disks.assert_called_once() |
| 1107 |
mock_virt_conn.find_vm.assert_called_once_with('test-vm') |
| 1108 |
|
| 1109 |
# Should not proceed with VM creation |
| 1110 |
mock_base_image_operator_class.assert_not_called() |
| 1111 |
mock_virt_install.execute.assert_not_called() |
| 1112 |
mock_virt_conn.destroy.assert_not_called() |
| 1113 |
mock_virt_conn.undefine.assert_not_called() |
| 1114 |
|
| 1115 |
# Should return success with message |
| 1116 |
self.assertEqual(rc, VIRT_SUCCESS) |
| 1117 |
self.assertFalse(result['changed']) |
| 1118 |
self.assertIn("already exists", result['message']) |
| 1119 |
|
| 1120 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.BaseImageOperator') |
| 1121 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.VirtInstallTool') |
| 1122 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.LibvirtWrapper') |
| 1123 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.validate_disks') |
| 1124 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.update_virtinst_params') |
| 1125 |
def test_core_vm_exists_recreate_active(self, mock_update_params, mock_validate_disks, |
| 1126 |
mock_libvirt_wrapper_class, mock_virt_install_class, |
| 1127 |
mock_base_image_operator_class): |
| 1128 |
"""Test core() when VM exists, recreate=True, and VM is active""" |
| 1129 |
from ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance import core, VIRT_SUCCESS |
| 1130 |
|
| 1131 |
# Set recreate to True |
| 1132 |
self.mock_module.params['recreate'] = True |
| 1133 |
|
| 1134 |
# Mock LibvirtWrapper - VM exists and is active |
| 1135 |
mock_virt_conn = mock.Mock() |
| 1136 |
mock_vm = mock.Mock() |
| 1137 |
mock_vm.isActive.return_value = True |
| 1138 |
mock_virt_conn.find_vm.return_value = mock_vm |
| 1139 |
mock_libvirt_wrapper_class.return_value = mock_virt_conn |
| 1140 |
|
| 1141 |
# Mock VirtInstallTool |
| 1142 |
mock_virt_install = mock.Mock() |
| 1143 |
mock_virt_install.execute.return_value = (True, VIRT_SUCCESS, {}) |
| 1144 |
mock_virt_install.get_commands.return_value = [] |
| 1145 |
mock_virt_install_class.return_value = mock_virt_install |
| 1146 |
|
| 1147 |
# Mock BaseImageOperator |
| 1148 |
mock_operator = mock.Mock() |
| 1149 |
mock_operator.validate_checksum.return_value = True |
| 1150 |
mock_operator.build_system_disk.return_value = { |
| 1151 |
'path': '/var/lib/libvirt/images/test.qcow2'} |
| 1152 |
mock_operator.get_commands.return_value = [] |
| 1153 |
mock_base_image_operator_class.return_value = mock_operator |
| 1154 |
|
| 1155 |
# Execute core function |
| 1156 |
rc, result = core(self.mock_module) |
| 1157 |
|
| 1158 |
# Verify VM destruction and recreation |
| 1159 |
mock_virt_conn.find_vm.assert_called_once_with('test-vm') |
| 1160 |
mock_virt_conn.destroy.assert_called_once_with('test-vm') |
| 1161 |
mock_virt_conn.undefine.assert_called_once_with('test-vm') |
| 1162 |
|
| 1163 |
# Verify VM recreation |
| 1164 |
mock_operator.fetch_image.assert_called_once() |
| 1165 |
mock_operator.validate_checksum.assert_called_once() |
| 1166 |
mock_operator.build_system_disk.assert_called_once() |
| 1167 |
mock_virt_install.execute.assert_called_once_with(dryrun=False, wait_timeout=600) |
| 1168 |
|
| 1169 |
# Should return success |
| 1170 |
self.assertEqual(rc, VIRT_SUCCESS) |
| 1171 |
self.assertTrue(result['changed']) |
| 1172 |
|
| 1173 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.BaseImageOperator') |
| 1174 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.VirtInstallTool') |
| 1175 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.LibvirtWrapper') |
| 1176 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.validate_disks') |
| 1177 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.update_virtinst_params') |
| 1178 |
def test_core_vm_exists_recreate_inactive(self, mock_update_params, mock_validate_disks, |
| 1179 |
mock_libvirt_wrapper_class, mock_virt_install_class, |
| 1180 |
mock_base_image_operator_class): |
| 1181 |
"""Test core() when VM exists, recreate=True, and VM is inactive""" |
| 1182 |
from ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance import core, VIRT_SUCCESS |
| 1183 |
|
| 1184 |
# Set recreate to True |
| 1185 |
self.mock_module.params['recreate'] = True |
| 1186 |
|
| 1187 |
# Mock LibvirtWrapper - VM exists but is inactive |
| 1188 |
mock_virt_conn = mock.Mock() |
| 1189 |
mock_vm = mock.Mock() |
| 1190 |
mock_vm.isActive.return_value = False |
| 1191 |
mock_virt_conn.find_vm.return_value = mock_vm |
| 1192 |
mock_libvirt_wrapper_class.return_value = mock_virt_conn |
| 1193 |
|
| 1194 |
# Mock VirtInstallTool |
| 1195 |
mock_virt_install = mock.Mock() |
| 1196 |
mock_virt_install.execute.return_value = (True, VIRT_SUCCESS, {}) |
| 1197 |
mock_virt_install.get_commands.return_value = [] |
| 1198 |
mock_virt_install_class.return_value = mock_virt_install |
| 1199 |
|
| 1200 |
# Mock BaseImageOperator |
| 1201 |
mock_operator = mock.Mock() |
| 1202 |
mock_operator.validate_checksum.return_value = True |
| 1203 |
mock_operator.build_system_disk.return_value = { |
| 1204 |
'path': '/var/lib/libvirt/images/test.qcow2'} |
| 1205 |
mock_operator.get_commands.return_value = [] |
| 1206 |
mock_base_image_operator_class.return_value = mock_operator |
| 1207 |
|
| 1208 |
# Execute core function |
| 1209 |
rc, result = core(self.mock_module) |
| 1210 |
|
| 1211 |
# Verify behavior - should not destroy (inactive), but should undefine |
| 1212 |
mock_virt_conn.destroy.assert_not_called() # VM is inactive |
| 1213 |
mock_virt_conn.undefine.assert_called_once_with('test-vm') |
| 1214 |
|
| 1215 |
# Should return success |
| 1216 |
self.assertEqual(rc, VIRT_SUCCESS) |
| 1217 |
self.assertTrue(result['changed']) |
| 1218 |
|
| 1219 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.VirtInstallTool') |
| 1220 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.LibvirtWrapper') |
| 1221 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.validate_disks') |
| 1222 |
def test_core_missing_name(self, mock_validate_disks, mock_libvirt_wrapper_class, mock_virt_install_class): |
| 1223 |
"""Test core() fails when VM name is missing""" |
| 1224 |
from ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance import core |
| 1225 |
|
| 1226 |
# Mock the module.run_command for LibvirtConnection |
| 1227 |
self.mock_module.run_command.return_value = (0, 'kernel-version', '') |
| 1228 |
|
| 1229 |
# Remove name from params |
| 1230 |
self.mock_module.params['name'] = None |
| 1231 |
|
| 1232 |
# Execute core function |
| 1233 |
core(self.mock_module) |
| 1234 |
|
| 1235 |
# Should fail with appropriate message |
| 1236 |
self.mock_module.fail_json.assert_called_once_with( |
| 1237 |
msg="virtual machine name is missing") |
| 1238 |
|
| 1239 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.VirtInstallTool') |
| 1240 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.LibvirtWrapper') |
| 1241 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.validate_disks') |
| 1242 |
def test_core_missing_base_image(self, mock_validate_disks, mock_libvirt_wrapper_class, mock_virt_install_class): |
| 1243 |
"""Test core() fails when base_image is missing""" |
| 1244 |
from ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance import core |
| 1245 |
|
| 1246 |
# Mock the module.run_command for LibvirtConnection |
| 1247 |
self.mock_module.run_command.return_value = (0, 'kernel-version', '') |
| 1248 |
|
| 1249 |
# Remove base_image from params |
| 1250 |
self.mock_module.params['base_image'] = None |
| 1251 |
|
| 1252 |
# Execute core function |
| 1253 |
core(self.mock_module) |
| 1254 |
|
| 1255 |
# Should fail with appropriate message |
| 1256 |
self.mock_module.fail_json.assert_called_once_with( |
| 1257 |
msg="base_image parameter is required") |
| 1258 |
|
| 1259 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.BaseImageOperator') |
| 1260 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.VirtInstallTool') |
| 1261 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.LibvirtWrapper') |
| 1262 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.validate_disks') |
| 1263 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.update_virtinst_params') |
| 1264 |
def test_core_checksum_validation_fails(self, mock_update_params, mock_validate_disks, mock_libvirt_wrapper_class, |
| 1265 |
mock_virt_install_class, mock_base_image_operator_class): |
| 1266 |
"""Test core() fails when checksum validation fails""" |
| 1267 |
from ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance import core |
| 1268 |
from ansible_collections.community.libvirt.plugins.module_utils.libvirt import VMNotFound |
| 1269 |
|
| 1270 |
# Mock LibvirtWrapper - VM doesn't exist |
| 1271 |
mock_virt_conn = mock.Mock() |
| 1272 |
mock_virt_conn.find_vm.side_effect = VMNotFound("VM not found") |
| 1273 |
mock_libvirt_wrapper_class.return_value = mock_virt_conn |
| 1274 |
|
| 1275 |
# Mock VirtInstallTool (shouldn't be used due to checksum failure) |
| 1276 |
mock_virt_install = mock.Mock() |
| 1277 |
mock_virt_install.params = { |
| 1278 |
'disks': [{'path': '/tmp/placeholder.qcow2'}]} |
| 1279 |
mock_virt_install_class.return_value = mock_virt_install |
| 1280 |
|
| 1281 |
# Mock BaseImageOperator - checksum validation fails |
| 1282 |
mock_operator = mock.Mock() |
| 1283 |
mock_operator.validate_checksum.return_value = False |
| 1284 |
mock_operator.get_commands.return_value = [] |
| 1285 |
mock_base_image_operator_class.return_value = mock_operator |
| 1286 |
|
| 1287 |
# Configure fail_json to raise exception (as it should in real code) |
| 1288 |
self.mock_module.fail_json.side_effect = Exception("Module failed") |
| 1289 |
|
| 1290 |
# Execute core function - should raise exception due to checksum failure |
| 1291 |
with self.assertRaises(Exception): |
| 1292 |
core(self.mock_module) |
| 1293 |
|
| 1294 |
# Should fail with checksum error |
| 1295 |
self.mock_module.fail_json.assert_called_once_with( |
| 1296 |
msg="The checksum of the base image does not match the expected value") |
| 1297 |
|
| 1298 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.VirtInstallTool') |
| 1299 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.LibvirtWrapper') |
| 1300 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.validate_disks') |
| 1301 |
def test_core_unsupported_state(self, mock_validate_disks, mock_libvirt_wrapper_class, mock_virt_install_class): |
| 1302 |
"""Test core() fails with unsupported state""" |
| 1303 |
from ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance import core |
| 1304 |
|
| 1305 |
# Mock the module.run_command for LibvirtConnection |
| 1306 |
self.mock_module.run_command.return_value = (0, 'kernel-version', '') |
| 1307 |
|
| 1308 |
# Set unsupported state |
| 1309 |
self.mock_module.params['state'] = 'destroy' |
| 1310 |
|
| 1311 |
# Execute core function |
| 1312 |
core(self.mock_module) |
| 1313 |
|
| 1314 |
# Should fail with unsupported state message |
| 1315 |
self.mock_module.fail_json.assert_called_once_with( |
| 1316 |
msg="unsupported state 'destroy'") |
| 1317 |
|
| 1318 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.BaseImageOperator') |
| 1319 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.VirtInstallTool') |
| 1320 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.LibvirtWrapper') |
| 1321 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.validate_disks') |
| 1322 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.update_virtinst_params') |
| 1323 |
def test_core_check_mode_vm_recreation(self, mock_update_params, mock_validate_disks, |
| 1324 |
mock_libvirt_wrapper_class, mock_virt_install_class, |
| 1325 |
mock_base_image_operator_class): |
| 1326 |
"""Test core() in check_mode when recreating VM""" |
| 1327 |
from ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance import core, VIRT_SUCCESS |
| 1328 |
|
| 1329 |
# Enable check_mode and recreate |
| 1330 |
self.mock_module.check_mode = True |
| 1331 |
self.mock_module.params['recreate'] = True |
| 1332 |
|
| 1333 |
# Mock LibvirtWrapper - VM exists and is active |
| 1334 |
mock_virt_conn = mock.Mock() |
| 1335 |
mock_vm = mock.Mock() |
| 1336 |
mock_vm.isActive.return_value = True |
| 1337 |
mock_virt_conn.find_vm.return_value = mock_vm |
| 1338 |
mock_libvirt_wrapper_class.return_value = mock_virt_conn |
| 1339 |
|
| 1340 |
# Mock VirtInstallTool |
| 1341 |
mock_virt_install = mock.Mock() |
| 1342 |
mock_virt_install.execute.return_value = (True, VIRT_SUCCESS, {}) |
| 1343 |
mock_virt_install.get_commands.return_value = [] |
| 1344 |
mock_virt_install_class.return_value = mock_virt_install |
| 1345 |
|
| 1346 |
# Mock BaseImageOperator |
| 1347 |
mock_operator = mock.Mock() |
| 1348 |
mock_operator.validate_checksum.return_value = True |
| 1349 |
mock_operator.build_system_disk.return_value = { |
| 1350 |
'path': '/var/lib/libvirt/images/test.qcow2'} |
| 1351 |
mock_operator.get_commands.return_value = [] |
| 1352 |
mock_base_image_operator_class.return_value = mock_operator |
| 1353 |
|
| 1354 |
# Execute core function |
| 1355 |
rc, result = core(self.mock_module) |
| 1356 |
|
| 1357 |
# In check_mode, VM should NOT be destroyed or undefined |
| 1358 |
mock_virt_conn.destroy.assert_not_called() |
| 1359 |
mock_virt_conn.undefine.assert_not_called() |
| 1360 |
|
| 1361 |
# But virt-install should be called with dryrun=True |
| 1362 |
mock_virt_install.execute.assert_called_once_with(dryrun=True, wait_timeout=600) |
| 1363 |
|
| 1364 |
# Should still return success |
| 1365 |
self.assertEqual(rc, VIRT_SUCCESS) |
| 1366 |
self.assertTrue(result['changed']) |
| 1367 |
|
| 1368 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.BaseImageOperator') |
| 1369 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.VirtInstallTool') |
| 1370 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.LibvirtWrapper') |
| 1371 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.validate_disks') |
| 1372 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.update_virtinst_params') |
| 1373 |
def test_core_force_disk_passed_to_build_system_disk(self, mock_update_params, mock_validate_disks, |
| 1374 |
mock_libvirt_wrapper_class, mock_virt_install_class, |
| 1375 |
mock_base_image_operator_class): |
| 1376 |
"""Test that force_disk parameter is passed to build_system_disk""" |
| 1377 |
from ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance import core, VIRT_SUCCESS |
| 1378 |
from ansible_collections.community.libvirt.plugins.module_utils.libvirt import VMNotFound |
| 1379 |
|
| 1380 |
# Set force_disk to True |
| 1381 |
self.mock_module.params['force_disk'] = True |
| 1382 |
|
| 1383 |
# Mock LibvirtWrapper - VM doesn't exist |
| 1384 |
mock_virt_conn = mock.Mock() |
| 1385 |
mock_virt_conn.find_vm.side_effect = VMNotFound("VM not found") |
| 1386 |
mock_libvirt_wrapper_class.return_value = mock_virt_conn |
| 1387 |
|
| 1388 |
# Mock VirtInstallTool |
| 1389 |
mock_virt_install = mock.Mock() |
| 1390 |
mock_virt_install.execute.return_value = ( |
| 1391 |
True, VIRT_SUCCESS, {'some': 'data'}) |
| 1392 |
mock_virt_install.get_commands.return_value = [] |
| 1393 |
mock_virt_install_class.return_value = mock_virt_install |
| 1394 |
|
| 1395 |
# Mock BaseImageOperator |
| 1396 |
mock_operator = mock.Mock() |
| 1397 |
mock_operator.validate_checksum.return_value = True |
| 1398 |
mock_operator.build_system_disk.return_value = { |
| 1399 |
'path': '/var/lib/libvirt/images/test.qcow2'} |
| 1400 |
mock_operator.get_commands.return_value = [] |
| 1401 |
mock_base_image_operator_class.return_value = mock_operator |
| 1402 |
|
| 1403 |
# Execute core function |
| 1404 |
rc, result = core(self.mock_module) |
| 1405 |
|
| 1406 |
# Verify force_disk=True was passed to build_system_disk |
| 1407 |
mock_operator.build_system_disk.assert_called_once_with( |
| 1408 |
{'path': '/var/lib/libvirt/images/test.qcow2', 'size': 20}, |
| 1409 |
force_disk=True |
| 1410 |
) |
| 1411 |
|
| 1412 |
# Should return success |
| 1413 |
self.assertEqual(rc, VIRT_SUCCESS) |
| 1414 |
self.assertTrue(result['changed']) |
| 1415 |
|
| 1416 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.BaseImageOperator') |
| 1417 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.VirtInstallTool') |
| 1418 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.LibvirtWrapper') |
| 1419 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.validate_disks') |
| 1420 |
@mock.patch('ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance.update_virtinst_params') |
| 1421 |
def test_core_force_disk_false_passed_to_build_system_disk(self, mock_update_params, mock_validate_disks, |
| 1422 |
mock_libvirt_wrapper_class, mock_virt_install_class, |
| 1423 |
mock_base_image_operator_class): |
| 1424 |
"""Test that force_disk=False parameter is passed to build_system_disk""" |
| 1425 |
from ansible_collections.community.libvirt.plugins.modules.virt_cloud_instance import core, VIRT_SUCCESS |
| 1426 |
from ansible_collections.community.libvirt.plugins.module_utils.libvirt import VMNotFound |
| 1427 |
|
| 1428 |
# Ensure force_disk is False (default) |
| 1429 |
self.mock_module.params['force_disk'] = False |
| 1430 |
|
| 1431 |
# Mock LibvirtWrapper - VM doesn't exist |
| 1432 |
mock_virt_conn = mock.Mock() |
| 1433 |
mock_virt_conn.find_vm.side_effect = VMNotFound("VM not found") |
| 1434 |
mock_libvirt_wrapper_class.return_value = mock_virt_conn |
| 1435 |
|
| 1436 |
# Mock VirtInstallTool |
| 1437 |
mock_virt_install = mock.Mock() |
| 1438 |
mock_virt_install.execute.return_value = ( |
| 1439 |
True, VIRT_SUCCESS, {'some': 'data'}) |
| 1440 |
mock_virt_install.get_commands.return_value = [] |
| 1441 |
mock_virt_install_class.return_value = mock_virt_install |
| 1442 |
|
| 1443 |
# Mock BaseImageOperator |
| 1444 |
mock_operator = mock.Mock() |
| 1445 |
mock_operator.validate_checksum.return_value = True |
| 1446 |
mock_operator.build_system_disk.return_value = { |
| 1447 |
'path': '/var/lib/libvirt/images/test.qcow2'} |
| 1448 |
mock_operator.get_commands.return_value = [] |
| 1449 |
mock_base_image_operator_class.return_value = mock_operator |
| 1450 |
|
| 1451 |
# Execute core function |
| 1452 |
rc, result = core(self.mock_module) |
| 1453 |
|
| 1454 |
# Verify force_disk=False was passed to build_system_disk |
| 1455 |
mock_operator.build_system_disk.assert_called_once_with( |
| 1456 |
{'path': '/var/lib/libvirt/images/test.qcow2', 'size': 20}, |
| 1457 |
force_disk=False |
| 1458 |
) |
| 1459 |
|
| 1460 |
# Should return success |
| 1461 |
self.assertEqual(rc, VIRT_SUCCESS) |
| 1462 |
self.assertTrue(result['changed']) |
| 1463 |
|
| 1464 |
|
| 1465 |
if __name__ == '__main__': |
| 1466 |
unittest.main(verbosity=2) |