| 1 |
# (c) 2017 Red Hat Inc. |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
from unittest.mock import call, patch |
| 8 |
|
| 9 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 10 |
AnsibleExitJson, |
| 11 |
AnsibleFailJson, |
| 12 |
ModuleTestCase, |
| 13 |
set_module_args, |
| 14 |
) |
| 15 |
|
| 16 |
from ansible_collections.community.general.plugins.modules import parted as parted_module |
| 17 |
from ansible_collections.community.general.plugins.modules.parted import parse_parted_version, parse_partition_info |
| 18 |
|
| 19 |
# Example of output : parted -s -m /dev/sdb -- unit 'MB' print |
| 20 |
parted_output1 = """ |
| 21 |
BYT; |
| 22 |
/dev/sdb:286061MB:scsi:512:512:msdos:ATA TOSHIBA THNSFJ25:; |
| 23 |
1:1.05MB:106MB:105MB:fat32::esp; |
| 24 |
2:106MB:368MB:262MB:ext2::; |
| 25 |
3:368MB:256061MB:255692MB:::;""" |
| 26 |
|
| 27 |
parted_version_info = { |
| 28 |
""" |
| 29 |
parted (GNU parted) 3.3 |
| 30 |
Copyright (C) 2019 Free Software Foundation, Inc. |
| 31 |
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. |
| 32 |
This is free software: you are free to change and redistribute it. |
| 33 |
There is NO WARRANTY, to the extent permitted by law. |
| 34 |
|
| 35 |
Written by <http://git.debian.org/?p=parted/parted.git;a=blob_plain;f=AUTHORS>. |
| 36 |
""": (3, 3, 0), |
| 37 |
""" |
| 38 |
parted (GNU parted) 3.4.5 |
| 39 |
Copyright (C) 2019 Free Software Foundation, Inc. |
| 40 |
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. |
| 41 |
This is free software: you are free to change and redistribute it. |
| 42 |
There is NO WARRANTY, to the extent permitted by law. |
| 43 |
|
| 44 |
Written by <http://git.debian.org/?p=parted/parted.git;a=blob_plain;f=AUTHORS>. |
| 45 |
""": (3, 4, 5), |
| 46 |
""" |
| 47 |
parted (GNU parted) 3.3.14-dfc61 |
| 48 |
Copyright (C) 2019 Free Software Foundation, Inc. |
| 49 |
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. |
| 50 |
This is free software: you are free to change and redistribute it. |
| 51 |
There is NO WARRANTY, to the extent permitted by law. |
| 52 |
|
| 53 |
Written by <http://git.debian.org/?p=parted/parted.git;a=blob_plain;f=AUTHORS>. |
| 54 |
""": (3, 3, 14), |
| 55 |
} |
| 56 |
|
| 57 |
# corresponding dictionary after parsing by parse_partition_info |
| 58 |
parted_dict1 = { |
| 59 |
"generic": { |
| 60 |
"dev": "/dev/sdb", |
| 61 |
"size": 286061.0, |
| 62 |
"unit": "mb", |
| 63 |
"table": "msdos", |
| 64 |
"model": "ATA TOSHIBA THNSFJ25", |
| 65 |
"logical_block": 512, |
| 66 |
"physical_block": 512, |
| 67 |
}, |
| 68 |
"partitions": [ |
| 69 |
{ |
| 70 |
"num": 1, |
| 71 |
"begin": 1.05, |
| 72 |
"end": 106.0, |
| 73 |
"size": 105.0, |
| 74 |
"fstype": "fat32", |
| 75 |
"name": "", |
| 76 |
"flags": ["esp"], |
| 77 |
"unit": "mb", |
| 78 |
}, |
| 79 |
{ |
| 80 |
"num": 2, |
| 81 |
"begin": 106.0, |
| 82 |
"end": 368.0, |
| 83 |
"size": 262.0, |
| 84 |
"fstype": "ext2", |
| 85 |
"name": "", |
| 86 |
"flags": [], |
| 87 |
"unit": "mb", |
| 88 |
}, |
| 89 |
{ |
| 90 |
"num": 3, |
| 91 |
"begin": 368.0, |
| 92 |
"end": 256061.0, |
| 93 |
"size": 255692.0, |
| 94 |
"fstype": "", |
| 95 |
"name": "", |
| 96 |
"flags": [], |
| 97 |
"unit": "mb", |
| 98 |
}, |
| 99 |
], |
| 100 |
} |
| 101 |
|
| 102 |
parted_output2 = """ |
| 103 |
BYT; |
| 104 |
/dev/sdb:286061MB:scsi:512:512:msdos:ATA TOSHIBA THNSFJ25:;""" |
| 105 |
|
| 106 |
# corresponding dictionary after parsing by parse_partition_info |
| 107 |
parted_dict2 = { |
| 108 |
"generic": { |
| 109 |
"dev": "/dev/sdb", |
| 110 |
"size": 286061.0, |
| 111 |
"unit": "mb", |
| 112 |
"table": "msdos", |
| 113 |
"model": "ATA TOSHIBA THNSFJ25", |
| 114 |
"logical_block": 512, |
| 115 |
"physical_block": 512, |
| 116 |
}, |
| 117 |
"partitions": [], |
| 118 |
} |
| 119 |
|
| 120 |
# fake some_flag exists |
| 121 |
parted_dict3 = { |
| 122 |
"generic": { |
| 123 |
"dev": "/dev/sdb", |
| 124 |
"size": 286061.0, |
| 125 |
"unit": "mb", |
| 126 |
"table": "msdos", |
| 127 |
"model": "ATA TOSHIBA THNSFJ25", |
| 128 |
"logical_block": 512, |
| 129 |
"physical_block": 512, |
| 130 |
}, |
| 131 |
"partitions": [ |
| 132 |
{ |
| 133 |
"num": 1, |
| 134 |
"begin": 1.05, |
| 135 |
"end": 106.0, |
| 136 |
"size": 105.0, |
| 137 |
"fstype": "fat32", |
| 138 |
"name": "", |
| 139 |
"flags": ["some_flag"], |
| 140 |
"unit": "mb", |
| 141 |
} |
| 142 |
], |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
class TestParted(ModuleTestCase): |
| 147 |
def setUp(self): |
| 148 |
super().setUp() |
| 149 |
|
| 150 |
self.module = parted_module |
| 151 |
self.mock_check_parted_label = patch( |
| 152 |
"ansible_collections.community.general.plugins.modules.parted.check_parted_label", return_value=False |
| 153 |
) |
| 154 |
self.check_parted_label = self.mock_check_parted_label.start() |
| 155 |
|
| 156 |
self.mock_parted = patch("ansible_collections.community.general.plugins.modules.parted.parted") |
| 157 |
self.parted = self.mock_parted.start() |
| 158 |
|
| 159 |
self.mock_run_command = patch("ansible.module_utils.basic.AnsibleModule.run_command") |
| 160 |
self.run_command = self.mock_run_command.start() |
| 161 |
|
| 162 |
self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path") |
| 163 |
self.get_bin_path = self.mock_get_bin_path.start() |
| 164 |
|
| 165 |
def tearDown(self): |
| 166 |
super().tearDown() |
| 167 |
self.mock_run_command.stop() |
| 168 |
self.mock_get_bin_path.stop() |
| 169 |
self.mock_parted.stop() |
| 170 |
self.mock_check_parted_label.stop() |
| 171 |
|
| 172 |
def execute_module(self, failed=False, changed=False, script=None): |
| 173 |
if failed: |
| 174 |
result = self.failed() |
| 175 |
self.assertTrue(result["failed"], result) |
| 176 |
else: |
| 177 |
result = self.changed(changed) |
| 178 |
self.assertEqual(result["changed"], changed, result) |
| 179 |
|
| 180 |
if script: |
| 181 |
self.assertEqual(script, result["script"], result["script"]) |
| 182 |
|
| 183 |
return result |
| 184 |
|
| 185 |
def failed(self): |
| 186 |
with self.assertRaises(AnsibleFailJson) as exc: |
| 187 |
self.module.main() |
| 188 |
|
| 189 |
result = exc.exception.args[0] |
| 190 |
self.assertTrue(result["failed"], result) |
| 191 |
return result |
| 192 |
|
| 193 |
def changed(self, changed=False): |
| 194 |
with self.assertRaises(AnsibleExitJson) as exc: |
| 195 |
self.module.main() |
| 196 |
|
| 197 |
result = exc.exception.args[0] |
| 198 |
self.assertEqual(result["changed"], changed, result) |
| 199 |
return result |
| 200 |
|
| 201 |
def test_parse_partition_info(self): |
| 202 |
"""Test that the parse_partition_info returns the expected dictionary""" |
| 203 |
self.assertEqual(parse_partition_info(parted_output1, "MB", False), parted_dict1) |
| 204 |
self.assertEqual(parse_partition_info(parted_output2, "MB", False), parted_dict2) |
| 205 |
|
| 206 |
def test_partition_already_exists(self): |
| 207 |
with set_module_args( |
| 208 |
{ |
| 209 |
"device": "/dev/sdb", |
| 210 |
"number": 1, |
| 211 |
"state": "present", |
| 212 |
} |
| 213 |
): |
| 214 |
with patch( |
| 215 |
"ansible_collections.community.general.plugins.modules.parted.get_device_info", |
| 216 |
return_value=parted_dict1, |
| 217 |
): |
| 218 |
self.execute_module(changed=False) |
| 219 |
|
| 220 |
def test_create_new_partition(self): |
| 221 |
with set_module_args( |
| 222 |
{ |
| 223 |
"device": "/dev/sdb", |
| 224 |
"number": 4, |
| 225 |
"state": "present", |
| 226 |
} |
| 227 |
): |
| 228 |
with patch( |
| 229 |
"ansible_collections.community.general.plugins.modules.parted.get_device_info", |
| 230 |
return_value=parted_dict1, |
| 231 |
): |
| 232 |
self.execute_module(changed=True, script=["unit", "KiB", "mkpart", "primary", "0%", "100%"]) |
| 233 |
|
| 234 |
def test_create_new_partition_1G(self): |
| 235 |
with set_module_args( |
| 236 |
{ |
| 237 |
"device": "/dev/sdb", |
| 238 |
"number": 4, |
| 239 |
"state": "present", |
| 240 |
"part_end": "1GiB", |
| 241 |
} |
| 242 |
): |
| 243 |
with patch( |
| 244 |
"ansible_collections.community.general.plugins.modules.parted.get_device_info", |
| 245 |
return_value=parted_dict1, |
| 246 |
): |
| 247 |
self.execute_module(changed=True, script=["unit", "KiB", "mkpart", "primary", "0%", "1GiB"]) |
| 248 |
|
| 249 |
def test_create_new_partition_minus_1G(self): |
| 250 |
with set_module_args( |
| 251 |
{ |
| 252 |
"device": "/dev/sdb", |
| 253 |
"number": 4, |
| 254 |
"state": "present", |
| 255 |
"fs_type": "ext2", |
| 256 |
"part_start": "-1GiB", |
| 257 |
} |
| 258 |
): |
| 259 |
with patch( |
| 260 |
"ansible_collections.community.general.plugins.modules.parted.get_device_info", |
| 261 |
return_value=parted_dict1, |
| 262 |
): |
| 263 |
self.execute_module(changed=True, script=["unit", "KiB", "mkpart", "primary", "ext2", "-1GiB", "100%"]) |
| 264 |
|
| 265 |
def test_remove_partition_number_1(self): |
| 266 |
with set_module_args( |
| 267 |
{ |
| 268 |
"device": "/dev/sdb", |
| 269 |
"number": 1, |
| 270 |
"state": "absent", |
| 271 |
} |
| 272 |
): |
| 273 |
with patch( |
| 274 |
"ansible_collections.community.general.plugins.modules.parted.get_device_info", |
| 275 |
return_value=parted_dict1, |
| 276 |
): |
| 277 |
self.execute_module(changed=True, script=["rm", "1"]) |
| 278 |
|
| 279 |
def test_resize_partition(self): |
| 280 |
with set_module_args( |
| 281 |
{"device": "/dev/sdb", "number": 3, "state": "present", "part_end": "100%", "resize": True} |
| 282 |
): |
| 283 |
with patch( |
| 284 |
"ansible_collections.community.general.plugins.modules.parted.get_device_info", |
| 285 |
return_value=parted_dict1, |
| 286 |
): |
| 287 |
self.execute_module(changed=True, script=["resizepart", "3", "100%"]) |
| 288 |
|
| 289 |
def test_change_flag(self): |
| 290 |
# Flags are set in a second run of parted(). |
| 291 |
# Between the two runs, the partition dict is updated. |
| 292 |
# use checkmode here allow us to continue even if the dictionary is |
| 293 |
# not updated. |
| 294 |
with set_module_args( |
| 295 |
{ |
| 296 |
"device": "/dev/sdb", |
| 297 |
"number": 3, |
| 298 |
"state": "present", |
| 299 |
"flags": ["lvm", "boot"], |
| 300 |
"_ansible_check_mode": True, |
| 301 |
} |
| 302 |
): |
| 303 |
with patch( |
| 304 |
"ansible_collections.community.general.plugins.modules.parted.get_device_info", |
| 305 |
return_value=parted_dict1, |
| 306 |
): |
| 307 |
self.parted.reset_mock() |
| 308 |
self.execute_module(changed=True) |
| 309 |
# When using multiple flags: |
| 310 |
# order of execution is non deterministic, because set() operations are used in |
| 311 |
# the current implementation. |
| 312 |
expected_calls_order1 = [ |
| 313 |
call(["unit", "KiB", "set", "3", "lvm", "on", "set", "3", "boot", "on"], "/dev/sdb", "optimal") |
| 314 |
] |
| 315 |
expected_calls_order2 = [ |
| 316 |
call(["unit", "KiB", "set", "3", "boot", "on", "set", "3", "lvm", "on"], "/dev/sdb", "optimal") |
| 317 |
] |
| 318 |
self.assertTrue( |
| 319 |
self.parted.mock_calls == expected_calls_order1 or self.parted.mock_calls == expected_calls_order2 |
| 320 |
) |
| 321 |
|
| 322 |
def test_create_new_primary_lvm_partition(self): |
| 323 |
# use check_mode, see previous test comment |
| 324 |
with set_module_args( |
| 325 |
{ |
| 326 |
"device": "/dev/sdb", |
| 327 |
"number": 4, |
| 328 |
"flags": ["boot"], |
| 329 |
"state": "present", |
| 330 |
"part_start": "257GiB", |
| 331 |
"fs_type": "ext3", |
| 332 |
"_ansible_check_mode": True, |
| 333 |
} |
| 334 |
): |
| 335 |
with patch( |
| 336 |
"ansible_collections.community.general.plugins.modules.parted.get_device_info", |
| 337 |
return_value=parted_dict1, |
| 338 |
): |
| 339 |
self.execute_module( |
| 340 |
changed=True, |
| 341 |
script=[ |
| 342 |
"unit", |
| 343 |
"KiB", |
| 344 |
"mkpart", |
| 345 |
"primary", |
| 346 |
"ext3", |
| 347 |
"257GiB", |
| 348 |
"100%", |
| 349 |
"unit", |
| 350 |
"KiB", |
| 351 |
"set", |
| 352 |
"4", |
| 353 |
"boot", |
| 354 |
"on", |
| 355 |
], |
| 356 |
) |
| 357 |
|
| 358 |
def test_create_label_gpt(self): |
| 359 |
# Like previous test, current implementation use parted to create the partition and |
| 360 |
# then retrieve and update the dictionary. Use check_mode to force to continue even if |
| 361 |
# dictionary is not updated. |
| 362 |
with set_module_args( |
| 363 |
{ |
| 364 |
"device": "/dev/sdb", |
| 365 |
"number": 1, |
| 366 |
"flags": ["lvm"], |
| 367 |
"label": "gpt", |
| 368 |
"name": "lvmpartition", |
| 369 |
"state": "present", |
| 370 |
"_ansible_check_mode": True, |
| 371 |
} |
| 372 |
): |
| 373 |
with patch( |
| 374 |
"ansible_collections.community.general.plugins.modules.parted.get_device_info", |
| 375 |
return_value=parted_dict2, |
| 376 |
): |
| 377 |
self.execute_module( |
| 378 |
changed=True, |
| 379 |
script=[ |
| 380 |
"unit", |
| 381 |
"KiB", |
| 382 |
"mklabel", |
| 383 |
"gpt", |
| 384 |
"mkpart", |
| 385 |
"primary", |
| 386 |
"0%", |
| 387 |
"100%", |
| 388 |
"unit", |
| 389 |
"KiB", |
| 390 |
"name", |
| 391 |
"1", |
| 392 |
'"lvmpartition"', |
| 393 |
"set", |
| 394 |
"1", |
| 395 |
"lvm", |
| 396 |
"on", |
| 397 |
], |
| 398 |
) |
| 399 |
|
| 400 |
def test_change_label_gpt(self): |
| 401 |
# When partitions already exists and label is changed, mkpart should be called even when partition already exists, |
| 402 |
# because new empty label will be created anyway |
| 403 |
with set_module_args( |
| 404 |
{ |
| 405 |
"device": "/dev/sdb", |
| 406 |
"number": 1, |
| 407 |
"state": "present", |
| 408 |
"label": "gpt", |
| 409 |
"_ansible_check_mode": True, |
| 410 |
} |
| 411 |
): |
| 412 |
with patch( |
| 413 |
"ansible_collections.community.general.plugins.modules.parted.get_device_info", |
| 414 |
return_value=parted_dict1, |
| 415 |
): |
| 416 |
self.execute_module( |
| 417 |
changed=True, script=["unit", "KiB", "mklabel", "gpt", "mkpart", "primary", "0%", "100%"] |
| 418 |
) |
| 419 |
|
| 420 |
def test_check_mode_unchanged(self): |
| 421 |
# Test that get_device_info result is checked in check mode too |
| 422 |
# No change on partition 1 |
| 423 |
with set_module_args( |
| 424 |
{ |
| 425 |
"device": "/dev/sdb", |
| 426 |
"number": 1, |
| 427 |
"state": "present", |
| 428 |
"flags": ["some_flag"], |
| 429 |
"_ansible_check_mode": True, |
| 430 |
} |
| 431 |
): |
| 432 |
with patch( |
| 433 |
"ansible_collections.community.general.plugins.modules.parted.get_device_info", |
| 434 |
return_value=parted_dict3, |
| 435 |
): |
| 436 |
self.execute_module(changed=False) |
| 437 |
|
| 438 |
def test_check_mode_changed(self): |
| 439 |
# Test that get_device_info result is checked in check mode too |
| 440 |
# Flag change on partition 1 |
| 441 |
with set_module_args( |
| 442 |
{ |
| 443 |
"device": "/dev/sdb", |
| 444 |
"number": 1, |
| 445 |
"state": "present", |
| 446 |
"flags": ["other_flag"], |
| 447 |
"_ansible_check_mode": True, |
| 448 |
} |
| 449 |
): |
| 450 |
with patch( |
| 451 |
"ansible_collections.community.general.plugins.modules.parted.get_device_info", |
| 452 |
return_value=parted_dict3, |
| 453 |
): |
| 454 |
self.execute_module(changed=True) |
| 455 |
|
| 456 |
def test_version_info(self): |
| 457 |
"""Test that the parse_parted_version returns the expected tuple""" |
| 458 |
for key, value in parted_version_info.items(): |
| 459 |
self.assertEqual(parse_parted_version(key), value) |