Debian Codegraph @

test/units/module_utils/basic/test_get_file_attributes.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright:
3 # (c) 2017, Pierre-Louis Bonicoli <[email protected]>
4 # License: GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import annotations
7
8 from itertools import product
9
10 from ansible.module_utils.basic import AnsibleModule
11
12 import pytest
13
14
15 DATA = (
16 (
17 '3353595900 --------------e---- /usr/lib32',
18 {'attr_flags': 'e', 'version': '3353595900', 'attributes': ['extents']}
19 ),
20 # with e2fsprogs < 1.43, output isn't aligned
21 (
22 '78053594 -----------I--e---- /usr/lib',
23 {'attr_flags': 'Ie', 'version': '78053594', 'attributes': ['indexed', 'extents']}
24 ),
25 (
26 '15711607 -------A------e---- /tmp/test',
27 {'attr_flags': 'Ae', 'version': '15711607', 'attributes': ['noatime', 'extents']}
28 ),
29 # with e2fsprogs >= 1.43, output is aligned
30 (
31 '78053594 -----------I--e---- /usr/lib',
32 {'attr_flags': 'Ie', 'version': '78053594', 'attributes': ['indexed', 'extents']}
33 ),
34 (
35 '15711607 -------A------e---- /tmp/test',
36 {'attr_flags': 'Ae', 'version': '15711607', 'attributes': ['noatime', 'extents']}
37 ),
38 )
39
40 NO_VERSION_DATA = (
41 (
42 '--------------e---- /usr/lib32',
43 {'attr_flags': 'e', 'attributes': ['extents']}
44 ),
45 (
46 '-----------I--e---- /usr/lib',
47 {'attr_flags': 'Ie', 'attributes': ['indexed', 'extents']}
48 ),
49 (
50 '-------A------e---- /tmp/test',
51 {'attr_flags': 'Ae', 'attributes': ['noatime', 'extents']}
52 ),
53 )
54
55
56 @pytest.mark.parametrize('stdin, data', list(product(({},), DATA)), indirect=['stdin'])
57 def test_get_file_attributes(am, stdin, mocker, data):
58 # Test #18731
59 mocker.patch.object(AnsibleModule, 'get_bin_path', return_value=(0, '/usr/bin/lsattr', ''))
60 mocker.patch.object(AnsibleModule, 'run_command', return_value=(0, data[0], ''))
61 result = am.get_file_attributes('/path/to/file')
62 for key, value in data[1].items():
63 assert key in result and result[key] == value
64
65
66 @pytest.mark.parametrize('stdin, data', list(product(({},), NO_VERSION_DATA)), indirect=['stdin'])
67 def test_get_file_attributes_no_version(am, stdin, mocker, data):
68 # Test #18731
69 mocker.patch.object(AnsibleModule, 'get_bin_path', return_value=(0, '/usr/bin/lsattr', ''))
70 mocker.patch.object(AnsibleModule, 'run_command', return_value=(0, data[0], ''))
71 result = am.get_file_attributes('/path/to/file', include_version=False)
72 for key, value in data[1].items():
73 assert key in result and result[key] == value