Debian Codegraph @

tests/test_main.py

raw
1 # SPDX-FileCopyrightText: 2023-2024 Greenbone AG
2 #
3 # SPDX-License-Identifier: GPL-3.0-or-later
4 #
5
6 import sys
7 import unittest
8 from pathlib import Path
9 from unittest.mock import MagicMock, call, patch
10
11 from pontos.testing import temp_directory
12
13 from greenbone.feed.sync.config import DEFAULT_FEED_RELEASE
14 from greenbone.feed.sync.errors import GreenboneFeedSyncError, RsyncError
15 from greenbone.feed.sync.main import (
16 Sync,
17 do_selftest,
18 feed_sync,
19 filter_syncs,
20 main,
21 )
22
23
24 class FilterSyncsTestCase(unittest.TestCase):
25 def test_filter_syncs(self):
26 sync_a = Sync(name="a", types=["foo", "bar"], url="a", destination="a")
27 sync_b = Sync(name="b", types=["foo", "baz"], url="b", destination="b")
28 sync_c = Sync(name="c", types=["bar", "baz"], url="c", destination="c")
29
30 sync_list = filter_syncs(
31 "file.lock",
32 "foo",
33 sync_a,
34 sync_b,
35 sync_c,
36 )
37
38 self.assertEqual(len(sync_list.syncs), 2)
39 self.assertEqual(sync_list.lock_file, "file.lock")
40
41 self.assertEqual(sync_list.syncs[0], sync_a)
42 self.assertEqual(sync_list.syncs[1], sync_b)
43
44
45 class DoSelftestTestCase(unittest.TestCase):
46 @patch("greenbone.feed.sync.main.subprocess.run")
47 def test_do_selftest_success(self, mock_subprocess_run: MagicMock):
48 mock_subprocess_run.side_effect = [""]
49 do_selftest()
50
51 @patch("greenbone.feed.sync.main.subprocess.run")
52 def test_do_selftest_rsync_fail(self, mock_subprocess_run: MagicMock):
53 mock_subprocess_run.side_effect = [PermissionError]
54 with self.assertRaisesRegex(
55 GreenboneFeedSyncError, "The rsync binary could not be found."
56 ):
57 do_selftest()
58
59
60 class FeedSyncTestCase(unittest.IsolatedAsyncioTestCase):
61 @patch("greenbone.feed.sync.main.Rsync", autospec=True)
62 @patch("greenbone.feed.sync.main.change_user_and_group", autospec=True)
63 @patch("greenbone.feed.sync.main.is_root", autospec=True)
64 async def test_do_not_run_as_root(
65 self,
66 is_root_mock: MagicMock,
67 change_user_mock: MagicMock,
68 rsync_mock: MagicMock,
69 ):
70 is_root_mock.return_value = True
71 console = MagicMock()
72 rsync_mock_instance = rsync_mock.return_value
73
74 with (
75 temp_directory() as temp_dir,
76 patch.dict(
77 "os.environ",
78 {"GREENBONE_FEED_SYNC_DESTINATION_PREFIX": str(temp_dir)},
79 ),
80 patch.object(
81 sys,
82 "argv",
83 [
84 "greenbone-feed-sync",
85 "--type",
86 "nvt",
87 ],
88 ),
89 ):
90 ret = await feed_sync(console=console, error_console=console)
91 self.assertEqual(ret, 0)
92
93 change_user_mock.assert_called_once_with("gvm", "gvm")
94 rsync_mock.assert_called_once_with(
95 private_subdir=None,
96 verbose=False,
97 compression_level=9,
98 ssh_key=Path("/etc/gvm/greenbone-enterprise-feed-key"),
99 )
100 console.print.assert_has_calls(
101 [
102 call(
103 "Trying to acquire lock on "
104 f"{temp_dir}/openvas/feed-update.lock"
105 ),
106 call(f"Acquired lock on {temp_dir}/openvas/feed-update.lock"),
107 call(f"Releasing lock on {temp_dir}/openvas/feed-update.lock"),
108 call(),
109 ]
110 )
111
112 rsync_mock_instance.sync.assert_has_awaits(
113 [
114 call(
115 url="rsync://feed.community.greenbone.net/community/"
116 f"vulnerability-feed/{DEFAULT_FEED_RELEASE}/vt-data/notus/",
117 destination=temp_dir / "notus",
118 ),
119 call(
120 url="rsync://feed.community.greenbone.net/community/"
121 f"vulnerability-feed/{DEFAULT_FEED_RELEASE}/vt-data/nasl/",
122 destination=temp_dir / "openvas/plugins",
123 ),
124 ]
125 )
126
127 @patch("greenbone.feed.sync.main.Rsync", autospec=True)
128 async def test_sync_nvts(self, rsync_mock: MagicMock):
129 console = MagicMock()
130 rsync_mock_instance = rsync_mock.return_value
131
132 with (
133 temp_directory() as temp_dir,
134 patch.dict(
135 "os.environ",
136 {"GREENBONE_FEED_SYNC_DESTINATION_PREFIX": str(temp_dir)},
137 ),
138 patch.object(
139 sys,
140 "argv",
141 [
142 "greenbone-feed-sync",
143 "--type",
144 "nvt",
145 ],
146 ),
147 ):
148 ret = await feed_sync(console=console, error_console=console)
149 self.assertEqual(ret, 0)
150
151 rsync_mock.assert_called_once_with(
152 private_subdir=None,
153 verbose=False,
154 compression_level=9,
155 ssh_key=Path("/etc/gvm/greenbone-enterprise-feed-key"),
156 )
157 console.print.assert_has_calls(
158 [
159 call(
160 "Trying to acquire lock on "
161 f"{temp_dir}/openvas/feed-update.lock"
162 ),
163 call(
164 f"Acquired lock on {temp_dir}/openvas/feed-update.lock"
165 ),
166 call(
167 f"Releasing lock on {temp_dir}/openvas/feed-update.lock"
168 ),
169 call(),
170 ]
171 )
172
173 rsync_mock_instance.sync.assert_has_awaits(
174 [
175 call(
176 url="rsync://feed.community.greenbone.net/community/"
177 f"vulnerability-feed/{DEFAULT_FEED_RELEASE}/vt-data/notus/",
178 destination=temp_dir / "notus",
179 ),
180 call(
181 url="rsync://feed.community.greenbone.net/community/"
182 f"vulnerability-feed/{DEFAULT_FEED_RELEASE}/vt-data/nasl/",
183 destination=temp_dir / "openvas/plugins",
184 ),
185 ]
186 )
187
188 @patch("greenbone.feed.sync.main.Rsync", autospec=True)
189 async def test_sync_nvts_verbose(self, rsync_mock: MagicMock):
190 console = MagicMock()
191 rsync_mock_instance = rsync_mock.return_value
192
193 with (
194 temp_directory() as temp_dir,
195 patch.dict(
196 "os.environ",
197 {"GREENBONE_FEED_SYNC_DESTINATION_PREFIX": str(temp_dir)},
198 ),
199 patch.object(
200 sys,
201 "argv",
202 ["greenbone-feed-sync", "--type", "nvt", "-vvv"],
203 ),
204 ):
205 ret = await feed_sync(console=console, error_console=console)
206 self.assertEqual(ret, 0)
207
208 rsync_mock.assert_called_once_with(
209 private_subdir=None,
210 verbose=True,
211 compression_level=9,
212 ssh_key=Path("/etc/gvm/greenbone-enterprise-feed-key"),
213 )
214 console.print.assert_has_calls(
215 [
216 call(
217 "Trying to acquire lock on "
218 f"{temp_dir}/openvas/feed-update.lock"
219 ),
220 call(
221 f"Acquired lock on {temp_dir}/openvas/feed-update.lock"
222 ),
223 call(
224 "Downloading Notus files from "
225 "rsync://feed.community.greenbone.net/community/"
226 f"vulnerability-feed/{DEFAULT_FEED_RELEASE}/vt-data/notus/ "
227 f"to {temp_dir}/notus"
228 ),
229 call(),
230 call(
231 "Downloading NASL files from "
232 "rsync://feed.community.greenbone.net/community/"
233 f"vulnerability-feed/{DEFAULT_FEED_RELEASE}/vt-data/nasl/ "
234 f"to {temp_dir}/openvas/plugins"
235 ),
236 call(),
237 call(
238 f"Releasing lock on {temp_dir}/openvas/feed-update.lock"
239 ),
240 call(),
241 ]
242 )
243
244 rsync_mock_instance.sync.assert_has_awaits(
245 [
246 call(
247 url="rsync://feed.community.greenbone.net/community/"
248 f"vulnerability-feed/{DEFAULT_FEED_RELEASE}/vt-data/notus/",
249 destination=temp_dir / "notus",
250 ),
251 call(
252 url="rsync://feed.community.greenbone.net/community/"
253 f"vulnerability-feed/{DEFAULT_FEED_RELEASE}/vt-data/nasl/",
254 destination=temp_dir / "openvas/plugins",
255 ),
256 ]
257 )
258
259 @patch("greenbone.feed.sync.main.Rsync", autospec=True)
260 async def test_sync_nvts_quiet(self, rsync_mock: MagicMock):
261 console = MagicMock()
262 rsync_mock_instance = rsync_mock.return_value
263
264 with (
265 temp_directory() as temp_dir,
266 patch.dict(
267 "os.environ",
268 {"GREENBONE_FEED_SYNC_DESTINATION_PREFIX": str(temp_dir)},
269 ),
270 patch.object(
271 sys,
272 "argv",
273 ["greenbone-feed-sync", "--type", "nvt", "--quiet"],
274 ),
275 ):
276 ret = await feed_sync(console=console, error_console=console)
277 self.assertEqual(ret, 0)
278
279 rsync_mock.assert_called_once_with(
280 private_subdir=None,
281 verbose=False,
282 compression_level=9,
283 ssh_key=Path("/etc/gvm/greenbone-enterprise-feed-key"),
284 )
285 console.print.assert_not_called()
286
287 rsync_mock_instance.sync.assert_has_awaits(
288 [
289 call(
290 url="rsync://feed.community.greenbone.net/community/"
291 f"vulnerability-feed/{DEFAULT_FEED_RELEASE}/vt-data/notus/",
292 destination=temp_dir / "notus",
293 ),
294 call(
295 url="rsync://feed.community.greenbone.net/community/"
296 f"vulnerability-feed/{DEFAULT_FEED_RELEASE}/vt-data/nasl/",
297 destination=temp_dir / "openvas/plugins",
298 ),
299 ]
300 )
301
302 @patch("greenbone.feed.sync.main.Rsync", autospec=True)
303 async def test_sync_nvts_rsync_error(self, rsync_mock: MagicMock):
304 console = MagicMock()
305 rsync_mock_instance = rsync_mock.return_value
306 rsync_mock_instance.sync.side_effect = RsyncError(
307 2, [], b"An rsync error"
308 )
309
310 with (
311 temp_directory() as temp_dir,
312 patch.dict(
313 "os.environ",
314 {"GREENBONE_FEED_SYNC_DESTINATION_PREFIX": str(temp_dir)},
315 ),
316 patch.object(
317 sys,
318 "argv",
319 ["greenbone-feed-sync", "--type", "nvt", "--fail-fast"],
320 ),
321 ):
322 ret = await feed_sync(console=console, error_console=console)
323 self.assertEqual(ret, 1)
324
325 rsync_mock.assert_called_once_with(
326 private_subdir=None,
327 verbose=False,
328 compression_level=9,
329 ssh_key=Path("/etc/gvm/greenbone-enterprise-feed-key"),
330 )
331 console.print.assert_has_calls(
332 [
333 call(
334 "Trying to acquire lock on "
335 f"{temp_dir}/openvas/feed-update.lock"
336 ),
337 call(
338 f"Acquired lock on {temp_dir}/openvas/feed-update.lock"
339 ),
340 call("An rsync error"),
341 call(
342 f"Releasing lock on {temp_dir}/openvas/feed-update.lock"
343 ),
344 ]
345 )
346
347 rsync_mock_instance.sync.assert_has_awaits(
348 [
349 call(
350 url="rsync://feed.community.greenbone.net/community/"
351 f"vulnerability-feed/{DEFAULT_FEED_RELEASE}/vt-data/notus/",
352 destination=temp_dir / "notus",
353 ),
354 ]
355 )
356
357
358 class MainFunctionTestCase(unittest.TestCase):
359 @patch("greenbone.feed.sync.main.Console")
360 @patch("greenbone.feed.sync.main.Rsync", autospec=True)
361 def test_sync_nvts(self, rsync_mock: MagicMock, console_mock: MagicMock):
362 rsync_mock_instance = rsync_mock.return_value
363 console_mock_instance = console_mock.return_value
364
365 with (
366 temp_directory() as temp_dir,
367 patch.dict(
368 "os.environ",
369 {"GREENBONE_FEED_SYNC_DESTINATION_PREFIX": str(temp_dir)},
370 ),
371 patch.object(
372 sys,
373 "argv",
374 [
375 "greenbone-feed-sync",
376 "--type",
377 "nvt",
378 ],
379 ),
380 ):
381 with self.assertRaises(SystemExit) as cm:
382 main()
383
384 self.assertEqual(cm.exception.code, 0)
385
386 rsync_mock.assert_called_once_with(
387 private_subdir=None,
388 verbose=False,
389 compression_level=9,
390 ssh_key=Path("/etc/gvm/greenbone-enterprise-feed-key"),
391 )
392 console_mock_instance.print.assert_has_calls(
393 [
394 call(
395 "Trying to acquire lock on "
396 f"{temp_dir}/openvas/feed-update.lock"
397 ),
398 call(
399 f"Acquired lock on {temp_dir}/openvas/feed-update.lock"
400 ),
401 call(
402 f"Releasing lock on {temp_dir}/openvas/feed-update.lock"
403 ),
404 call(),
405 ]
406 )
407
408 rsync_mock_instance.sync.assert_has_awaits(
409 [
410 call(
411 url="rsync://feed.community.greenbone.net/community/"
412 f"vulnerability-feed/{DEFAULT_FEED_RELEASE}/vt-data/notus/",
413 destination=temp_dir / "notus",
414 ),
415 call(
416 url="rsync://feed.community.greenbone.net/community/"
417 f"vulnerability-feed/{DEFAULT_FEED_RELEASE}/vt-data/nasl/",
418 destination=temp_dir / "openvas/plugins",
419 ),
420 ]
421 )
422
423 @patch("greenbone.feed.sync.main.Console")
424 @patch("greenbone.feed.sync.main.Rsync", autospec=True)
425 def test_sync_nvts_error(
426 self, rsync_mock: MagicMock, console_mock: MagicMock
427 ):
428 rsync_mock_instance = rsync_mock.return_value
429 console_mock_instance = console_mock.return_value
430 rsync_mock_instance.sync.side_effect = GreenboneFeedSyncError(
431 "An error"
432 )
433
434 with (
435 temp_directory() as temp_dir,
436 patch.dict(
437 "os.environ",
438 {"GREENBONE_FEED_SYNC_DESTINATION_PREFIX": str(temp_dir)},
439 ),
440 patch.object(
441 sys,
442 "argv",
443 ["greenbone-feed-sync", "--type", "nvt", "--fail-fast"],
444 ),
445 ):
446 with self.assertRaises(SystemExit) as cm:
447 main()
448
449 self.assertEqual(cm.exception.code, 1)
450
451 rsync_mock.assert_called_once_with(
452 private_subdir=None,
453 verbose=False,
454 compression_level=9,
455 ssh_key=Path("/etc/gvm/greenbone-enterprise-feed-key"),
456 )
457 console_mock_instance.print.assert_has_calls(
458 [
459 call(
460 "Trying to acquire lock on "
461 f"{temp_dir}/openvas/feed-update.lock"
462 ),
463 call(
464 f"Acquired lock on {temp_dir}/openvas/feed-update.lock"
465 ),
466 call(
467 f"Releasing lock on {temp_dir}/openvas/feed-update.lock"
468 ),
469 call("[red]❌[/red]Error: An error"),
470 ]
471 )
472
473 rsync_mock_instance.sync.assert_has_awaits(
474 [
475 call(
476 url="rsync://feed.community.greenbone.net/community/"
477 f"vulnerability-feed/{DEFAULT_FEED_RELEASE}/vt-data/notus/",
478 destination=temp_dir / "notus",
479 ),
480 ]
481 )