1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

parted: fix regex for version match and partition size output (#1695) (#1732)

* Fix 2 regex in parted related to parted version string and to parsing partition size output.

* Added changelog fragment.

* Updated changelog as per recommendation.

* Fix the regex matching the parted version. The space character at the end of the string may or may not be always present

* provided sample version output and corrected regex to match

* add/correct changelog fragment

* split parted_version function to allow creating a test unit

* test unit for parted version info

* ansible-test sanity fixes

* review fix

* Update changelogs/fragments/1695-parted-updatedregex.yaml

Co-authored-by: Felix Fontein <felix@fontein.de>

* comment fixes

* better function name

* Update plugins/modules/system/parted.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* comment fixes

Co-authored-by: Claude Robitaille <claude@cbcr.me>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 43da5b88db)

Co-authored-by: Anatoly Pugachev <matorola@gmail.com>
This commit is contained in:
patchback[bot] 2021-02-05 07:51:43 +01:00 committed by GitHub
parent f613983cb4
commit a216f15dd9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 14 deletions

View file

@ -0,0 +1,4 @@
bugfixes:
- parted - change the regex that decodes the partition size to better support different formats that parted uses.
Change the regex that validates parted's version string
(https://github.com/ansible-collections/community.general/pull/1695).

View file

@ -241,7 +241,7 @@ def parse_unit(size_str, unit=''):
"""
Parses a string containing a size or boundary information
"""
matches = re.search(r'^(-?[\d.]+)([\w%]+)?$', size_str)
matches = re.search(r'^(-?[\d.]+) *([\w%]+)?$', size_str)
if matches is None:
# "<cylinder>,<head>,<sector>" format
matches = re.search(r'^(\d+),(\d+),(\d+)$', size_str)
@ -500,6 +500,33 @@ def check_parted_label(device):
return False
def parse_parted_version(out):
"""
Returns version tuple from the output of "parted --version" command
"""
lines = [x for x in out.split('\n') if x.strip() != '']
if len(lines) == 0:
return None, None, None
# Sample parted versions (see as well test unit):
# parted (GNU parted) 3.3
# parted (GNU parted) 3.4.5
# parted (GNU parted) 3.3.14-dfc61
matches = re.search(r'^parted.+\s(\d+)\.(\d+)(?:\.(\d+))?', lines[0].strip())
if matches is None:
return None, None, None
# Convert version to numbers
major = int(matches.group(1))
minor = int(matches.group(2))
rev = 0
if matches.group(3) is not None:
rev = int(matches.group(3))
return major, minor, rev
def parted_version():
"""
Returns the major and minor version of parted installed on the system.
@ -512,21 +539,10 @@ def parted_version():
msg="Failed to get parted version.", rc=rc, out=out, err=err
)
lines = [x for x in out.split('\n') if x.strip() != '']
if len(lines) == 0:
(major, minor, rev) = parse_parted_version(out)
if major is None:
module.fail_json(msg="Failed to get parted version.", rc=0, out=out)
matches = re.search(r'^parted.+(\d+)\.(\d+)(?:\.(\d+))?$', lines[0])
if matches is None:
module.fail_json(msg="Failed to get parted version.", rc=0, out=out)
# Convert version to numbers
major = int(matches.group(1))
minor = int(matches.group(2))
rev = 0
if matches.group(3) is not None:
rev = int(matches.group(3))
return major, minor, rev

View file

@ -6,6 +6,7 @@ __metaclass__ = type
from ansible_collections.community.general.tests.unit.compat.mock import patch, call
from ansible_collections.community.general.plugins.modules.system import parted as parted_module
from ansible_collections.community.general.plugins.modules.system.parted import parse_parted_version
from ansible_collections.community.general.plugins.modules.system.parted import parse_partition_info
from ansible_collections.community.general.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args
@ -17,6 +18,32 @@ BYT;
2:106MB:368MB:262MB:ext2::;
3:368MB:256061MB:255692MB:::;"""
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>.
This is free software: you are free to change and redistribute it.
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), """
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>.
This is free software: you are free to change and redistribute it.
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), """
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>.
This is free software: you are free to change and redistribute it.
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)}
# corresponding dictionary after parsing by parse_partition_info
parted_dict1 = {
"generic": {
@ -311,3 +338,8 @@ class TestParted(ModuleTestCase):
})
with patch('ansible_collections.community.general.plugins.modules.system.parted.get_device_info', return_value=parted_dict3):
self.execute_module(changed=True)
def test_version_info(self):
"""Test that the parse_parted_version returns the expected tuple"""
for key, value in parted_version_info.items():
self.assertEqual(parse_parted_version(key), value)