Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse chattr version and fix test case #52655

Merged
merged 6 commits into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion salt/modules/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import shutil
import stat
import string
import subprocess
import sys
import tempfile
import time
Expand Down Expand Up @@ -61,6 +62,7 @@
import salt.utils.url
import salt.utils.user
import salt.utils.data
import salt.utils.versions
from salt.exceptions import CommandExecutionError, MinionError, SaltInvocationError, get_error_message as _get_error_message
from salt.utils.files import HASHES, HASHES_REVMAP

Expand Down Expand Up @@ -159,6 +161,35 @@ def _splitlines_preserving_trailing_newline(str):
return lines


def _get_chattr_man():
'''
Get the contents of the chattr man page
'''
return subprocess.check_output(['man', 'chattr'])


def _parse_chattr_man(man):
'''
Parse the contents of a chattr man page to find the E2fsprogs version
'''
match = re.search(
r'E2fsprogs version [0-9\.]+',
salt.utils.stringutils.to_str(man),
)
if match:
version = match.group().strip('E2fsprogs version ')
else:
version = None
return version


def _chattr_version():
'''
Return the version of chattr installed
'''
return _parse_chattr_man(_get_chattr_man())


def gid_to_group(gid):
'''
Convert the group id to the group name on this system
Expand Down Expand Up @@ -577,7 +608,14 @@ def lsattr(path):
for line in result.splitlines():
if not line.startswith('lsattr: '):
vals = line.split(None, 1)
results[vals[1]] = re.findall(r"[aAcCdDeijPsStTu]", vals[0])
needed_version = salt.utils.versions.LooseVersion('1.41.12')
chattr_version = salt.utils.versions.LooseVersion(_chattr_version())
# The version of chattr on Centos 6 does not support extended
# attributes.
if chattr_version > needed_version:
results[vals[1]] = re.findall(r"[aAcCdDeijPsStTu]", vals[0])
else:
results[vals[1]] = re.findall(r"[acdijstuADST]", vals[0])

return results

Expand Down
3 changes: 3 additions & 0 deletions tests/filename_map.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ salt/modules/dockermod.py:
- integration.states.test_docker_container
- integration.states.test_docker_network

salt/modules/file.py:
- integration.states.test_cron

salt/modules/influxdb08mod.py:
- unit.states.test_influxdb08_database
- unit.states.test_influxdb08_user
Expand Down
33 changes: 32 additions & 1 deletion tests/unit/modules/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import textwrap

# Import Salt Testing libs
from tests.support.helpers import with_tempfile
from tests.support.helpers import with_tempfile, dedent
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.paths import TMP
from tests.support.unit import TestCase, skipIf
Expand Down Expand Up @@ -2045,3 +2045,34 @@ def test_source_list_for_list_returns_local_file_proto_from_dict(self):
ret = filemod.source_list(
[{'file://' + self.myfile: ''}], 'filehash', 'base')
self.assertEqual(list(ret), ['file://' + self.myfile, 'filehash'])


class ChattrVersionTests(TestCase):
CHATTR_MAN = salt.utils.stringutils.to_bytes((
'AVAILABILITY\n'
'chattr is part of the e2fsprogs package and is available '
'from http://e2fsprogs.sourceforge.net.\n'
'SEE ALSO\n'
' lsattr(1), btrfs(5), ext4(5), xfs(5).\n\n'
'E2fsprogs version 1.43.4 '
' '
'January 2017 '
' '
' CHATTR(1)'
))

def test__parse_chattr_version(self):
'''
Validate we can parse the E2fsprogs version from the chattr man page
'''
man_out = dedent(self.CHATTR_MAN)
parsed_version = filemod._parse_chattr_man(man_out)
assert parsed_version == '1.43.4', parsed_version

def test__chattr_version(self):
'''
The _chattr_version method works
'''
with patch('subprocess.check_output', return_value=self.CHATTR_MAN):
parsed_version = filemod._chattr_version()
assert parsed_version == '1.43.4', parsed_version