1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-18 18:01:31 +00:00

Reformat everything.

This commit is contained in:
Felix Fontein 2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View file

@ -9,7 +9,12 @@ from unittest.mock import patch, call
from ansible_collections.community.general.plugins.modules import parted as parted_module
from ansible_collections.community.general.plugins.modules.parted import parse_parted_version
from ansible_collections.community.general.plugins.modules.parted import parse_partition_info
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
AnsibleExitJson,
AnsibleFailJson,
ModuleTestCase,
set_module_args,
)
# Example of output : parted -s -m /dev/sdb -- unit 'MB' print
parted_output1 = """
@ -19,7 +24,8 @@ BYT;
2:106MB:368MB:262MB:ext2::;
3:368MB:256061MB:255692MB:::;"""
parted_version_info = {"""
parted_version_info = {
"""
parted (GNU parted) 3.3
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
@ -27,7 +33,8 @@ parted_version_info = {"""
There is NO WARRANTY, to the extent permitted by law.
Written by <http://git.debian.org/?p=parted/parted.git;a=blob_plain;f=AUTHORS>.
""": (3, 3, 0), """
""": (3, 3, 0),
"""
parted (GNU parted) 3.4.5
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
@ -35,7 +42,8 @@ parted_version_info = {"""
There is NO WARRANTY, to the extent permitted by law.
Written by <http://git.debian.org/?p=parted/parted.git;a=blob_plain;f=AUTHORS>.
""": (3, 4, 5), """
""": (3, 4, 5),
"""
parted (GNU parted) 3.3.14-dfc61
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
@ -43,7 +51,8 @@ parted_version_info = {"""
There is NO WARRANTY, to the extent permitted by law.
Written by <http://git.debian.org/?p=parted/parted.git;a=blob_plain;f=AUTHORS>.
""": (3, 3, 14)}
""": (3, 3, 14),
}
# corresponding dictionary after parsing by parse_partition_info
parted_dict1 = {
@ -54,36 +63,40 @@ parted_dict1 = {
"table": "msdos",
"model": "ATA TOSHIBA THNSFJ25",
"logical_block": 512,
"physical_block": 512
"physical_block": 512,
},
"partitions": [{
"num": 1,
"begin": 1.05,
"end": 106.0,
"size": 105.0,
"fstype": "fat32",
"name": '',
"flags": ["esp"],
"unit": "mb"
}, {
"num": 2,
"begin": 106.0,
"end": 368.0,
"size": 262.0,
"fstype": "ext2",
"name": '',
"flags": [],
"unit": "mb"
}, {
"num": 3,
"begin": 368.0,
"end": 256061.0,
"size": 255692.0,
"fstype": "",
"name": '',
"flags": [],
"unit": "mb"
}]
"partitions": [
{
"num": 1,
"begin": 1.05,
"end": 106.0,
"size": 105.0,
"fstype": "fat32",
"name": "",
"flags": ["esp"],
"unit": "mb",
},
{
"num": 2,
"begin": 106.0,
"end": 368.0,
"size": 262.0,
"fstype": "ext2",
"name": "",
"flags": [],
"unit": "mb",
},
{
"num": 3,
"begin": 368.0,
"end": 256061.0,
"size": 255692.0,
"fstype": "",
"name": "",
"flags": [],
"unit": "mb",
},
],
}
parted_output2 = """
@ -99,9 +112,9 @@ parted_dict2 = {
"table": "msdos",
"model": "ATA TOSHIBA THNSFJ25",
"logical_block": 512,
"physical_block": 512
"physical_block": 512,
},
"partitions": []
"partitions": [],
}
# fake some_flag exists
@ -113,18 +126,20 @@ parted_dict3 = {
"table": "msdos",
"model": "ATA TOSHIBA THNSFJ25",
"logical_block": 512,
"physical_block": 512
"physical_block": 512,
},
"partitions": [{
"num": 1,
"begin": 1.05,
"end": 106.0,
"size": 105.0,
"fstype": "fat32",
"name": '',
"flags": ["some_flag"],
"unit": "mb"
}]
"partitions": [
{
"num": 1,
"begin": 1.05,
"end": 106.0,
"size": 105.0,
"fstype": "fat32",
"name": "",
"flags": ["some_flag"],
"unit": "mb",
}
],
}
@ -133,16 +148,18 @@ class TestParted(ModuleTestCase):
super().setUp()
self.module = parted_module
self.mock_check_parted_label = (patch('ansible_collections.community.general.plugins.modules.parted.check_parted_label', return_value=False))
self.mock_check_parted_label = patch(
"ansible_collections.community.general.plugins.modules.parted.check_parted_label", return_value=False
)
self.check_parted_label = self.mock_check_parted_label.start()
self.mock_parted = (patch('ansible_collections.community.general.plugins.modules.parted.parted'))
self.mock_parted = patch("ansible_collections.community.general.plugins.modules.parted.parted")
self.parted = self.mock_parted.start()
self.mock_run_command = (patch('ansible.module_utils.basic.AnsibleModule.run_command'))
self.mock_run_command = patch("ansible.module_utils.basic.AnsibleModule.run_command")
self.run_command = self.mock_run_command.start()
self.mock_get_bin_path = (patch('ansible.module_utils.basic.AnsibleModule.get_bin_path'))
self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path")
self.get_bin_path = self.mock_get_bin_path.start()
def tearDown(self):
@ -155,13 +172,13 @@ class TestParted(ModuleTestCase):
def execute_module(self, failed=False, changed=False, script=None):
if failed:
result = self.failed()
self.assertTrue(result['failed'], result)
self.assertTrue(result["failed"], result)
else:
result = self.changed(changed)
self.assertEqual(result['changed'], changed, result)
self.assertEqual(result["changed"], changed, result)
if script:
self.assertEqual(script, result['script'], result['script'])
self.assertEqual(script, result["script"], result["script"])
return result
@ -170,7 +187,7 @@ class TestParted(ModuleTestCase):
self.module.main()
result = exc.exception.args[0]
self.assertTrue(result['failed'], result)
self.assertTrue(result["failed"], result)
return result
def changed(self, changed=False):
@ -178,170 +195,262 @@ class TestParted(ModuleTestCase):
self.module.main()
result = exc.exception.args[0]
self.assertEqual(result['changed'], changed, result)
self.assertEqual(result["changed"], changed, result)
return result
def test_parse_partition_info(self):
"""Test that the parse_partition_info returns the expected dictionary"""
self.assertEqual(parse_partition_info(parted_output1, 'MB'), parted_dict1)
self.assertEqual(parse_partition_info(parted_output2, 'MB'), parted_dict2)
self.assertEqual(parse_partition_info(parted_output1, "MB"), parted_dict1)
self.assertEqual(parse_partition_info(parted_output2, "MB"), parted_dict2)
def test_partition_already_exists(self):
with set_module_args({
'device': '/dev/sdb',
'number': 1,
'state': 'present',
}):
with patch('ansible_collections.community.general.plugins.modules.parted.get_device_info', return_value=parted_dict1):
with set_module_args(
{
"device": "/dev/sdb",
"number": 1,
"state": "present",
}
):
with patch(
"ansible_collections.community.general.plugins.modules.parted.get_device_info",
return_value=parted_dict1,
):
self.execute_module(changed=False)
def test_create_new_partition(self):
with set_module_args({
'device': '/dev/sdb',
'number': 4,
'state': 'present',
}):
with patch('ansible_collections.community.general.plugins.modules.parted.get_device_info', return_value=parted_dict1):
self.execute_module(changed=True, script=['unit', 'KiB', 'mkpart', 'primary', '0%', '100%'])
with set_module_args(
{
"device": "/dev/sdb",
"number": 4,
"state": "present",
}
):
with patch(
"ansible_collections.community.general.plugins.modules.parted.get_device_info",
return_value=parted_dict1,
):
self.execute_module(changed=True, script=["unit", "KiB", "mkpart", "primary", "0%", "100%"])
def test_create_new_partition_1G(self):
with set_module_args({
'device': '/dev/sdb',
'number': 4,
'state': 'present',
'part_end': '1GiB',
}):
with patch('ansible_collections.community.general.plugins.modules.parted.get_device_info', return_value=parted_dict1):
self.execute_module(changed=True, script=['unit', 'KiB', 'mkpart', 'primary', '0%', '1GiB'])
with set_module_args(
{
"device": "/dev/sdb",
"number": 4,
"state": "present",
"part_end": "1GiB",
}
):
with patch(
"ansible_collections.community.general.plugins.modules.parted.get_device_info",
return_value=parted_dict1,
):
self.execute_module(changed=True, script=["unit", "KiB", "mkpart", "primary", "0%", "1GiB"])
def test_create_new_partition_minus_1G(self):
with set_module_args({
'device': '/dev/sdb',
'number': 4,
'state': 'present',
'fs_type': 'ext2',
'part_start': '-1GiB',
}):
with patch('ansible_collections.community.general.plugins.modules.parted.get_device_info', return_value=parted_dict1):
self.execute_module(changed=True, script=['unit', 'KiB', 'mkpart', 'primary', 'ext2', '-1GiB', '100%'])
with set_module_args(
{
"device": "/dev/sdb",
"number": 4,
"state": "present",
"fs_type": "ext2",
"part_start": "-1GiB",
}
):
with patch(
"ansible_collections.community.general.plugins.modules.parted.get_device_info",
return_value=parted_dict1,
):
self.execute_module(changed=True, script=["unit", "KiB", "mkpart", "primary", "ext2", "-1GiB", "100%"])
def test_remove_partition_number_1(self):
with set_module_args({
'device': '/dev/sdb',
'number': 1,
'state': 'absent',
}):
with patch('ansible_collections.community.general.plugins.modules.parted.get_device_info', return_value=parted_dict1):
self.execute_module(changed=True, script=['rm', '1'])
with set_module_args(
{
"device": "/dev/sdb",
"number": 1,
"state": "absent",
}
):
with patch(
"ansible_collections.community.general.plugins.modules.parted.get_device_info",
return_value=parted_dict1,
):
self.execute_module(changed=True, script=["rm", "1"])
def test_resize_partition(self):
with set_module_args({
'device': '/dev/sdb',
'number': 3,
'state': 'present',
'part_end': '100%',
'resize': True
}):
with patch('ansible_collections.community.general.plugins.modules.parted.get_device_info', return_value=parted_dict1):
self.execute_module(changed=True, script=['resizepart', '3', '100%'])
with set_module_args(
{"device": "/dev/sdb", "number": 3, "state": "present", "part_end": "100%", "resize": True}
):
with patch(
"ansible_collections.community.general.plugins.modules.parted.get_device_info",
return_value=parted_dict1,
):
self.execute_module(changed=True, script=["resizepart", "3", "100%"])
def test_change_flag(self):
# Flags are set in a second run of parted().
# Between the two runs, the partition dict is updated.
# use checkmode here allow us to continue even if the dictionary is
# not updated.
with set_module_args({
'device': '/dev/sdb',
'number': 3,
'state': 'present',
'flags': ['lvm', 'boot'],
'_ansible_check_mode': True,
}):
with patch('ansible_collections.community.general.plugins.modules.parted.get_device_info', return_value=parted_dict1):
with set_module_args(
{
"device": "/dev/sdb",
"number": 3,
"state": "present",
"flags": ["lvm", "boot"],
"_ansible_check_mode": True,
}
):
with patch(
"ansible_collections.community.general.plugins.modules.parted.get_device_info",
return_value=parted_dict1,
):
self.parted.reset_mock()
self.execute_module(changed=True)
# When using multiple flags:
# order of execution is non deterministic, because set() operations are used in
# the current implementation.
expected_calls_order1 = [call(['unit', 'KiB', 'set', '3', 'lvm', 'on', 'set', '3', 'boot', 'on'],
'/dev/sdb', 'optimal')]
expected_calls_order2 = [call(['unit', 'KiB', 'set', '3', 'boot', 'on', 'set', '3', 'lvm', 'on'],
'/dev/sdb', 'optimal')]
self.assertTrue(self.parted.mock_calls == expected_calls_order1 or
self.parted.mock_calls == expected_calls_order2)
expected_calls_order1 = [
call(["unit", "KiB", "set", "3", "lvm", "on", "set", "3", "boot", "on"], "/dev/sdb", "optimal")
]
expected_calls_order2 = [
call(["unit", "KiB", "set", "3", "boot", "on", "set", "3", "lvm", "on"], "/dev/sdb", "optimal")
]
self.assertTrue(
self.parted.mock_calls == expected_calls_order1 or self.parted.mock_calls == expected_calls_order2
)
def test_create_new_primary_lvm_partition(self):
# use check_mode, see previous test comment
with set_module_args({
'device': '/dev/sdb',
'number': 4,
'flags': ["boot"],
'state': 'present',
'part_start': '257GiB',
'fs_type': 'ext3',
'_ansible_check_mode': True,
}):
with patch('ansible_collections.community.general.plugins.modules.parted.get_device_info', return_value=parted_dict1):
self.execute_module(changed=True,
script=['unit', 'KiB', 'mkpart', 'primary', 'ext3', '257GiB', '100%', 'unit', 'KiB', 'set', '4', 'boot', 'on'])
with set_module_args(
{
"device": "/dev/sdb",
"number": 4,
"flags": ["boot"],
"state": "present",
"part_start": "257GiB",
"fs_type": "ext3",
"_ansible_check_mode": True,
}
):
with patch(
"ansible_collections.community.general.plugins.modules.parted.get_device_info",
return_value=parted_dict1,
):
self.execute_module(
changed=True,
script=[
"unit",
"KiB",
"mkpart",
"primary",
"ext3",
"257GiB",
"100%",
"unit",
"KiB",
"set",
"4",
"boot",
"on",
],
)
def test_create_label_gpt(self):
# Like previous test, current implementation use parted to create the partition and
# then retrieve and update the dictionary. Use check_mode to force to continue even if
# dictionary is not updated.
with set_module_args({
'device': '/dev/sdb',
'number': 1,
'flags': ["lvm"],
'label': 'gpt',
'name': 'lvmpartition',
'state': 'present',
'_ansible_check_mode': True,
}):
with patch('ansible_collections.community.general.plugins.modules.parted.get_device_info', return_value=parted_dict2):
with set_module_args(
{
"device": "/dev/sdb",
"number": 1,
"flags": ["lvm"],
"label": "gpt",
"name": "lvmpartition",
"state": "present",
"_ansible_check_mode": True,
}
):
with patch(
"ansible_collections.community.general.plugins.modules.parted.get_device_info",
return_value=parted_dict2,
):
self.execute_module(
changed=True,
script=['unit', 'KiB', 'mklabel', 'gpt', 'mkpart', 'primary', '0%', '100%',
'unit', 'KiB', 'name', '1', '"lvmpartition"', 'set', '1', 'lvm', 'on'])
script=[
"unit",
"KiB",
"mklabel",
"gpt",
"mkpart",
"primary",
"0%",
"100%",
"unit",
"KiB",
"name",
"1",
'"lvmpartition"',
"set",
"1",
"lvm",
"on",
],
)
def test_change_label_gpt(self):
# When partitions already exists and label is changed, mkpart should be called even when partition already exists,
# because new empty label will be created anyway
with set_module_args({
'device': '/dev/sdb',
'number': 1,
'state': 'present',
'label': 'gpt',
'_ansible_check_mode': True,
}):
with patch('ansible_collections.community.general.plugins.modules.parted.get_device_info', return_value=parted_dict1):
self.execute_module(changed=True, script=['unit', 'KiB', 'mklabel', 'gpt', 'mkpart', 'primary', '0%', '100%'])
with set_module_args(
{
"device": "/dev/sdb",
"number": 1,
"state": "present",
"label": "gpt",
"_ansible_check_mode": True,
}
):
with patch(
"ansible_collections.community.general.plugins.modules.parted.get_device_info",
return_value=parted_dict1,
):
self.execute_module(
changed=True, script=["unit", "KiB", "mklabel", "gpt", "mkpart", "primary", "0%", "100%"]
)
def test_check_mode_unchanged(self):
# Test that get_device_info result is checked in check mode too
# No change on partition 1
with set_module_args({
'device': '/dev/sdb',
'number': 1,
'state': 'present',
'flags': ['some_flag'],
'_ansible_check_mode': True,
}):
with patch('ansible_collections.community.general.plugins.modules.parted.get_device_info', return_value=parted_dict3):
with set_module_args(
{
"device": "/dev/sdb",
"number": 1,
"state": "present",
"flags": ["some_flag"],
"_ansible_check_mode": True,
}
):
with patch(
"ansible_collections.community.general.plugins.modules.parted.get_device_info",
return_value=parted_dict3,
):
self.execute_module(changed=False)
def test_check_mode_changed(self):
# Test that get_device_info result is checked in check mode too
# Flag change on partition 1
with set_module_args({
'device': '/dev/sdb',
'number': 1,
'state': 'present',
'flags': ['other_flag'],
'_ansible_check_mode': True,
}):
with patch('ansible_collections.community.general.plugins.modules.parted.get_device_info', return_value=parted_dict3):
with set_module_args(
{
"device": "/dev/sdb",
"number": 1,
"state": "present",
"flags": ["other_flag"],
"_ansible_check_mode": True,
}
):
with patch(
"ansible_collections.community.general.plugins.modules.parted.get_device_info",
return_value=parted_dict3,
):
self.execute_module(changed=True)
def test_version_info(self):