Let get_file_attributes() work without lsattr -v (#71845)

* Let get_file_attributes() work without `lsattr -v`

Change:
- module_utils's get_file_attributes() expects `lsattr -v` to work, but
  in some cases, it may not.
- The function now takes an optional include_version bool parameter,
  which removes this expectation.
- Places where we call get_file_attributes() without using the 'version'
  it returns, we now call it with include_version=False.

Test Plan:
- New unit tests

Signed-off-by: Rick Elrod <rick@elrod.me>
This commit is contained in:
Rick Elrod
2020-09-22 16:15:47 -05:00
committed by GitHub
parent 5b27b307b9
commit 5cd489af06
4 changed files with 41 additions and 14 deletions

View File

@ -39,6 +39,21 @@ DATA = (
),
)
NO_VERSION_DATA = (
(
'--------------e---- /usr/lib32',
{'attr_flags': 'e', 'attributes': ['extents']}
),
(
'-----------I--e---- /usr/lib',
{'attr_flags': 'Ie', 'attributes': ['indexed', 'extents']}
),
(
'-------A------e---- /tmp/test',
{'attr_flags': 'Ae', 'attributes': ['noatime', 'extents']}
),
)
@pytest.mark.parametrize('stdin, data', product(({},), DATA), indirect=['stdin'])
def test_get_file_attributes(am, stdin, mocker, data):
@ -48,3 +63,13 @@ def test_get_file_attributes(am, stdin, mocker, data):
result = am.get_file_attributes('/path/to/file')
for key, value in data[1].items():
assert key in result and result[key] == value
@pytest.mark.parametrize('stdin, data', product(({},), NO_VERSION_DATA), indirect=['stdin'])
def test_get_file_attributes_no_version(am, stdin, mocker, data):
# Test #18731
mocker.patch.object(AnsibleModule, 'get_bin_path', return_value=(0, '/usr/bin/lsattr', ''))
mocker.patch.object(AnsibleModule, 'run_command', return_value=(0, data[0], ''))
result = am.get_file_attributes('/path/to/file', include_version=False)
for key, value in data[1].items():
assert key in result and result[key] == value