Debian Codegraph @

tests/test_helper.py

raw
1 # SPDX-FileCopyrightText: 2023-2024 Greenbone AG
2 #
3 # SPDX-License-Identifier: GPL-3.0-or-later
4 #
5
6 # pylint: disable=protected-access
7
8 import errno
9 import unittest
10 from io import StringIO
11 from unittest.mock import MagicMock, call, patch
12
13 from pontos.testing import temp_directory
14 from rich.console import Console
15
16 from greenbone.feed.sync.errors import FileLockingError, GreenboneFeedSyncError
17 from greenbone.feed.sync.helper import (
18 Spinner,
19 change_user_and_group,
20 flock_wait,
21 is_root,
22 )
23
24
25 class FlockTestCase(unittest.IsolatedAsyncioTestCase):
26 async def test_locking(self):
27 with temp_directory() as temp_dir:
28 lock_file = temp_dir / "file.lock"
29 self.assertFalse(lock_file.exists())
30
31 async with flock_wait(lock_file):
32 self.assertTrue(lock_file.exists())
33
34 with self.assertRaises(FileLockingError):
35 async with flock_wait(lock_file, wait_interval=None):
36 pass
37
38 async def test_can_not_create_parent_dirs(self):
39 with temp_directory() as temp_dir:
40 lock_file = temp_dir / "foo" / "file.lock"
41 temp_dir.chmod(0)
42
43 with self.assertRaisesRegex(
44 FileLockingError, "Could not create parent directories for "
45 ):
46 async with flock_wait(lock_file):
47 pass
48
49 async def test_console_output(self):
50 with temp_directory() as temp_dir:
51 lock_file = temp_dir / "file.lock"
52 console = MagicMock(spec=Console)
53
54 async with flock_wait(lock_file, console=console):
55 pass
56
57 console.print.assert_has_calls(
58 [
59 call(f"Trying to acquire lock on {lock_file.absolute()}"),
60 call(f"Acquired lock on {lock_file.absolute()}"),
61 call(f"Releasing lock on {lock_file.absolute()}"),
62 ]
63 )
64
65 @patch("greenbone.feed.sync.helper.fcntl.flock", autospec=True)
66 async def test_retry(self, flock_mock: MagicMock):
67 e = OSError()
68 e.errno = errno.EACCES
69
70 flock_mock.side_effect = [e, None, None]
71
72 with temp_directory() as temp_dir:
73 lock_file = temp_dir / "file.lock"
74 console = MagicMock(spec=Console)
75
76 async with flock_wait(
77 lock_file,
78 console=console,
79 wait_interval=0.5,
80 ):
81 pass
82
83 console.print.assert_has_calls(
84 [
85 call(f"Trying to acquire lock on {lock_file.absolute()}"),
86 call(
87 f"{lock_file.absolute()} is locked by another process."
88 " Waiting 0.5 seconds before next try."
89 ),
90 call(f"Trying to acquire lock on {lock_file.absolute()}"),
91 call(f"Acquired lock on {lock_file.absolute()}"),
92 call(f"Releasing lock on {lock_file.absolute()}"),
93 ]
94 )
95
96 @patch("greenbone.feed.sync.helper.fcntl.flock", autospec=True)
97 async def test_lock_failure(self, flock_mock: MagicMock):
98 e = OSError()
99 e.errno = errno.EACCES
100
101 flock_mock.side_effect = e
102
103 with temp_directory() as temp_dir:
104 lock_file = temp_dir / "file.lock"
105
106 with self.assertRaisesRegex(
107 FileLockingError,
108 f"{lock_file.absolute()} is locked. Another process related "
109 "to the feed update may already running.",
110 ):
111 async with flock_wait(
112 lock_file,
113 wait_interval=None,
114 ):
115 pass
116
117 @patch("greenbone.feed.sync.helper.fcntl.flock", autospec=True)
118 async def test_lock_other_failure(self, flock_mock: MagicMock):
119 e = OSError("Other OSError")
120 flock_mock.side_effect = e
121
122 with temp_directory() as temp_dir:
123 lock_file = temp_dir / "file.lock"
124
125 with self.assertRaisesRegex(OSError, "Other OSError"):
126 async with flock_wait(
127 lock_file,
128 wait_interval=None,
129 ):
130 pass
131
132 @patch("greenbone.feed.sync.helper.fcntl.flock", autospec=True)
133 async def test_unlock_oserror(self, flock_mock: MagicMock):
134 flock_mock.side_effect = [None, OSError]
135
136 with temp_directory() as temp_dir:
137 lock_file = temp_dir / "file.lock"
138 async with flock_wait(
139 lock_file,
140 ):
141 pass
142
143 async def test_permission_error(self):
144 with temp_directory() as temp_dir:
145 lock_file = temp_dir / "file.lock"
146 lock_file.touch()
147 lock_file.chmod(0)
148 with self.assertRaisesRegex(
149 FileLockingError,
150 f"^Permission error while trying to open the lock file {lock_file.absolute()}$",
151 ):
152 async with flock_wait(
153 lock_file,
154 ):
155 pass
156
157
158 class SpinnerTestCase(unittest.TestCase):
159 @patch("greenbone.feed.sync.helper.Live", autospec=True)
160 def test_context_manager(self, live_mock: MagicMock):
161 console = MagicMock(spec=Console)
162 with Spinner(console, "Some Text"):
163 pass
164
165 live_mock_instance = live_mock.return_value
166 live_mock_instance.start.assert_called_once_with()
167 live_mock_instance.stop.assert_called_once_with()
168
169 def test_render(self):
170 out = StringIO()
171 console = Console(file=out)
172 with Spinner(console, "Some Text"):
173 pass
174
175 self.assertEqual("⠋ Some Text", out.getvalue())
176
177
178 class IsRootTestCase(unittest.TestCase):
179 @patch("greenbone.feed.sync.helper.os.geteuid", autospec=True)
180 def test_not_root(self, geteuid_mock: MagicMock):
181 geteuid_mock.return_value = 123
182
183 self.assertFalse(is_root())
184
185 @patch("greenbone.feed.sync.helper.os.geteuid", autospec=True)
186 def test_root(self, geteuid_mock: MagicMock):
187 geteuid_mock.return_value = 0
188
189 self.assertTrue(is_root())
190
191
192 class ChangeUserAndGroupTestCase(unittest.TestCase):
193 @patch("greenbone.feed.sync.helper.os", autospec=True)
194 def test_change(self, os_mock: MagicMock):
195 # root user should exist on all systems
196 change_user_and_group("root", "root")
197
198 os_mock.seteuid.assert_called_once_with(0)
199 os_mock.setegid.assert_called_once_with(0)
200
201 @patch("greenbone.feed.sync.helper.shutil", autospec=True)
202 @patch("greenbone.feed.sync.helper.os", autospec=True)
203 def test_change_with_unknown_user(
204 self, os_mock: MagicMock, shutil_mock: MagicMock
205 ):
206 shutil_mock._get_uid.return_value = None
207
208 with self.assertRaisesRegex(
209 GreenboneFeedSyncError, "Can't run as user 'foo'.*"
210 ):
211 change_user_and_group("foo", "bar")
212
213 os_mock.seteuid.assert_not_called()
214 os_mock.setegid.assert_not_called()
215
216 @patch("greenbone.feed.sync.helper.shutil", autospec=True)
217 @patch("greenbone.feed.sync.helper.os", autospec=True)
218 def test_change_with_unknown_group(
219 self, os_mock: MagicMock, shutil_mock: MagicMock
220 ):
221 shutil_mock._get_uid.return_value = 123
222 shutil_mock._get_gid.return_value = None
223
224 with self.assertRaisesRegex(
225 GreenboneFeedSyncError, "Can't run as group 'bar'.*"
226 ):
227 change_user_and_group("foo", "bar")
228
229 os_mock.seteuid.assert_not_called()
230 os_mock.setegid.assert_not_called()