Debian Codegraph @

test/units/module_utils/basic/test_filesystem.py

raw
1 # -*- coding: utf-8 -*-
2 # (c) 2012-2014, Michael DeHaan <[email protected]>
3 # (c) 2016 Toshio Kuratomi <[email protected]>
4 # (c) 2017 Ansible Project
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import annotations
8
9 import unittest
10
11 import pytest
12
13 from unittest.mock import patch, MagicMock
14
15
16 @pytest.mark.usefixtures("stdin")
17 class TestOtherFilesystem(unittest.TestCase):
18 def test_module_utils_basic_ansible_module_user_and_group(self):
19 from ansible.module_utils import basic
20
21 am = basic.AnsibleModule(
22 argument_spec=dict(),
23 )
24
25 mock_stat = MagicMock()
26 mock_stat.st_uid = 0
27 mock_stat.st_gid = 0
28
29 with patch('os.lstat', return_value=mock_stat):
30 self.assertEqual(am.user_and_group('/path/to/file'), (0, 0))
31
32 def test_module_utils_basic_ansible_module_find_mount_point(self):
33 from ansible.module_utils import basic
34
35 am = basic.AnsibleModule(
36 argument_spec=dict(),
37 )
38
39 def _mock_ismount(path):
40 if path == b'/':
41 return True
42 return False
43
44 with patch('os.path.ismount', side_effect=_mock_ismount):
45 self.assertEqual(am.find_mount_point('/root/fs/../mounted/path/to/whatever'), '/')
46
47 def _mock_ismount(path):
48 if path == b'/subdir/mount':
49 return True
50 return False
51
52 with patch('os.path.ismount', side_effect=_mock_ismount):
53 self.assertEqual(am.find_mount_point('/subdir/mount/path/to/whatever'), '/subdir/mount')
54
55 def test_module_utils_basic_ansible_module_set_owner_if_different(self):
56 from ansible.module_utils import basic
57
58 am = basic.AnsibleModule(
59 argument_spec=dict(),
60 )
61
62 assert am.set_owner_if_different('/path/to/file', None, True)
63 assert not am.set_owner_if_different('/path/to/file', None, False)
64
65 am.user_and_group = MagicMock(return_value=(500, 500))
66
67 with patch('os.lchown', return_value=None) as m:
68 assert am.set_owner_if_different('/path/to/file', 0, False)
69 m.assert_called_with(b'/path/to/file', 0, -1)
70
71 def _mock_getpwnam(*args, **kwargs):
72 mock_pw = MagicMock()
73 mock_pw.pw_uid = 0
74 return mock_pw
75
76 m.reset_mock()
77 with patch('pwd.getpwnam', side_effect=_mock_getpwnam):
78 assert am.set_owner_if_different('/path/to/file', 'root', False)
79 m.assert_called_with(b'/path/to/file', 0, -1)
80
81 with patch('pwd.getpwnam', side_effect=KeyError):
82 self.assertRaises(SystemExit, am.set_owner_if_different, '/path/to/file', 'root', False)
83
84 m.reset_mock()
85 am.check_mode = True
86 assert am.set_owner_if_different('/path/to/file', 0, False)
87 assert not m.called
88 am.check_mode = False
89
90 with patch('os.lchown', side_effect=OSError) as m:
91 self.assertRaises(SystemExit, am.set_owner_if_different, '/path/to/file', 'root', False)
92
93 def test_module_utils_basic_ansible_module_set_group_if_different(self):
94 from ansible.module_utils import basic
95
96 am = basic.AnsibleModule(
97 argument_spec=dict(),
98 )
99
100 assert am.set_group_if_different('/path/to/file', None, True)
101 assert not am.set_group_if_different('/path/to/file', None, False)
102
103 am.user_and_group = MagicMock(return_value=(500, 500))
104
105 with patch('os.lchown', return_value=None) as m:
106 assert am.set_group_if_different('/path/to/file', 0, False)
107 m.assert_called_with(b'/path/to/file', -1, 0)
108
109 def _mock_getgrnam(*args, **kwargs):
110 mock_gr = MagicMock()
111 mock_gr.gr_gid = 0
112 return mock_gr
113
114 m.reset_mock()
115 with patch('grp.getgrnam', side_effect=_mock_getgrnam):
116 assert am.set_group_if_different('/path/to/file', 'root', False)
117 m.assert_called_with(b'/path/to/file', -1, 0)
118
119 with patch('grp.getgrnam', side_effect=KeyError):
120 self.assertRaises(SystemExit, am.set_group_if_different, '/path/to/file', 'root', False)
121
122 m.reset_mock()
123 am.check_mode = True
124 assert am.set_group_if_different('/path/to/file', 0, False)
125 assert not m.called
126 am.check_mode = False
127
128 with patch('os.lchown', side_effect=OSError) as m:
129 self.assertRaises(SystemExit, am.set_group_if_different, '/path/to/file', 'root', False)
130
131 def test_module_utils_basic_ansible_module_set_directory_attributes_if_different(self):
132 from ansible.module_utils import basic
133
134 am = basic.AnsibleModule(
135 argument_spec=dict(),
136 )
137
138 am.selinux_enabled = lambda: False
139
140 file_args = {
141 'path': '/path/to/file',
142 'mode': None,
143 'owner': None,
144 'group': None,
145 'seuser': None,
146 'serole': None,
147 'setype': None,
148 'selevel': None,
149 'secontext': [None, None, None],
150 'attributes': None,
151 }
152
153 assert am.set_directory_attributes_if_different(file_args, True)
154 assert not am.set_directory_attributes_if_different(file_args, False)