Terarea  2
The automation project
Loading...
Searching...
No Matches
test_action_logger.py
Go to the documentation of this file.
1"""_summary_
2 File in charge of testing the logger class for the actions
3"""
4
5import os
6import sys
7from typing import List, Dict, Any, Union
8
9sys.path.append(os.path.join("..", os.getcwd()))
10sys.path.append(os.getcwd())
11
12try:
13 import constants as TCONST
14except ImportError as e:
15 raise ImportError("Failed to import the unit test constants module") from e
16
17try:
18 from src.lib.actions import constants as ACONST
19 from src.lib.actions.logger import ActionLogger
20 from src.lib.sql.sql_manager import SQL
21 from src.lib.components import constants as CONST
22 from src.lib.components.runtime_data import RuntimeData
23 from src.lib.boilerplates.non_web import BoilerplateNonHTTP
24 from src.lib.boilerplates.responses import BoilerplateResponses
25 from src.lib.components.password_handling import PasswordHandling
26except ImportError as e:
27 raise ImportError("Failed to import the src module") from e
28
29ERROR = TCONST.ERROR
30SUCCESS = TCONST.SUCCESS
31DEBUG = TCONST.DEBUG
32
33
34RDI = RuntimeData(TCONST.SERVER_HOST, TCONST.PORT, "Area", ERROR, SUCCESS)
36 runtime_data=RDI,
37 debug=DEBUG
38)
39RDI.boilerplate_responses_initialised = BRI
40RDI.boilerplate_non_http_initialised = BoilerplateNonHTTP(
41 runtime_data_initialised=RDI,
42 success=SUCCESS,
43 error=ERROR,
44 debug=DEBUG
45)
46
47SQLI = SQL(
48 url=CONST.DB_HOST,
49 port=CONST.DB_PORT,
50 username=CONST.DB_USER,
51 password=CONST.DB_PASSWORD,
52 db_name=CONST.DB_DATABASE,
53 debug=DEBUG
54)
55RDI.database_link = SQLI
56
57RDI.boilerplate_responses_initialised = BRI
58
60 runtime_data=RDI,
61 success=SUCCESS,
62 error=ERROR,
63 debug=DEBUG
64)
65
67 error=ERROR,
68 success=SUCCESS,
69 debug=DEBUG
70)
71
72TEST_INFO = {
73 "user_id": 0,
74 "action_id": 0,
75 "cache_busting": TCONST.CACHE_BUSTER_ADMIN
76}
77
78
79def _add_dummy_user() -> None:
80 """_summary_
81 Function in charge of adding a dummy user to the database.
82 """
83 columns: List[str] = SQLI.get_table_column_names(CONST.TAB_ACCOUNTS)
84 if columns == ERROR:
85 msg = f"Failed get data from table: {CONST.TAB_ACCOUNTS}"
86 raise RuntimeError(msg)
87 columns.pop(0)
88 username = f"test_user_{TEST_INFO['cache_busting']}"
89 email = f"test_email_{TEST_INFO['cache_busting']}@combobox.ttk"
90 password = PHI.hash_password(f"test_password_{TEST_INFO['cache_busting']}")
91 method = "local"
92 favicon = "NULL"
93 admin = "1"
94 data = [
95 username,
96 email,
97 password,
98 method,
99 favicon,
100 admin
101 ]
102 status = SQLI.insert_data_into_table(
103 table=CONST.TAB_ACCOUNTS,
104 data=data,
105 column=columns
106 )
107 if status == ERROR:
108 msg = f"Failed to add user {username}"
109 msg += f"to the database {CONST.TAB_ACCOUNTS}"
110 raise RuntimeError(msg)
111 usr_id = SQLI.get_data_from_table(
112 table=CONST.TAB_ACCOUNTS,
113 column="id",
114 where=f"email='{email}'",
115 beautify=False
116 )
117 if usr_id == ERROR:
118 msg = f"Failed to get user id for {username}"
119 raise RuntimeError(msg)
120 TEST_INFO["user_id"] = usr_id[0][0]
121
122
123def _add_dummy_action() -> None:
124 """_summary_
125 Function in charge of adding a dummy action to the database.
126 """
127 title = "_add_dummy_action"
129 columns: List[str] = SQLI.get_table_column_names(CONST.TAB_ACTIONS)
130 if columns == ERROR:
131 msg = f"Failed get data from table: {CONST.TAB_ACTIONS}"
132 raise RuntimeError(msg)
133 columns.pop(0)
134 name = f"test_action_name_{TEST_INFO['cache_busting']}"
135 trigger = f"test_trigger_{TEST_INFO['cache_busting']}"
136 consequences = f"test_consequences_{TEST_INFO['cache_busting']}"
137 user_id = str(TEST_INFO["user_id"])
138 tags = "test_actions,will_not_run"
139 running = f"{int(False)}"
140 description = "This is an action that was generated during unit tests, it is thus not an action that can run."
141 colour = "#00D9FFFF"
142 favicon = "NULL"
143 frequency = "0"
144 data = [
145 name,
146 trigger,
147 consequences,
148 user_id,
149 tags,
150 running,
151 description,
152 colour,
153 favicon,
154 frequency
155 ]
156 status = SQLI.insert_data_into_table(
157 table=CONST.TAB_ACTIONS,
158 data=data,
159 column=columns
160 )
161 if status != SUCCESS:
162 msg = f"Failed to add action {name}"
163 msg += f"to the database {CONST.TAB_ACTIONS}"
164 raise RuntimeError(msg)
165 action_id = SQLI.get_data_from_table(
166 table=CONST.TAB_ACTIONS,
167 column="id",
168 where=f"name='{name}'",
169 beautify=False
170 )
171 if action_id == ERROR:
172 msg = f"Failed to get action id for {name}"
173 raise RuntimeError(msg)
174 TCONST.IDISP.log_debug(f"Gathered action: {action_id}", title)
175 TEST_INFO["action_id"] = action_id[0][0]
176
177
178def _remove_dummy_user() -> None:
179 """_summary_
180 Function in charge of removing the dummy user from the database.
181 """
183 SQLI.remove_data_from_table(
184 CONST.TAB_ACCOUNTS,
185 where=f"id={TEST_INFO['user_id']}"
186 )
187
188
190 """_summary_
191 Function in charge of removing the dummy action from the database.
192 """
193 SQLI.remove_data_from_table(
194 CONST.TAB_ACTIONS,
195 where=f"id={TEST_INFO['action_id']}"
196 )
197
198
199def _remove_log_line(log_id: Union[int, Dict[str, int]]) -> None:
200 """_summary_
201 Function in charge of removing the log line from the database.
202
203 Args:
204 log_id (int): _description_
205 """
206 if isinstance(log_id, Dict):
207 for i in log_id:
208 SQLI.remove_data_from_table(
209 CONST.TAB_ACTION_LOGGING,
210 where=f"id={log_id[i]}"
211 )
212 else:
213 SQLI.remove_data_from_table(
214 CONST.TAB_ACTION_LOGGING,
215 where=f"id={log_id}"
216 )
217
218
219def _get_log_lines(action_id: str = "") -> Dict[str, Any]:
220 """_summary_
221 Function in charge of getting the log line from the database.
222 """
223 node = action_id
224 log_line = SQLI.get_data_from_table(
225 table=CONST.TAB_ACTION_LOGGING,
226 column="*",
227 where=f"action_id='{node}'"
228 )
229 if log_line == ERROR:
230 msg = f"Failed to get log line for {TEST_INFO['log_id']}"
231 raise RuntimeError(msg)
232 return log_line
233
234
236 """_summary_
237 Function in charge of testing the log_event function.
238 """
239 title = "test_log_event_info"
241 action_id = TEST_INFO["action_id"]
242 action_type = ACONST.TYPE_ACTION
243 action_code = ACONST.CODE_INFO
244 action_test_message = "This is a test message"
245 action_resolved = False
246 action_expected_level = ACONST.LEVEL_INFO
247 status = ACLI.log_event(
248 log_type=action_type,
249 action_id=action_id,
250 code=action_code,
251 message=action_test_message,
252 resolved=action_resolved,
253 )
254 TCONST.IDISP.log_debug(f"Log id = {status}", title)
255 if status == ERROR:
256 msg = "Failed to log the event."
257 TCONST.IDISP.log_error(msg, title)
259 assert status == SUCCESS
260 data = _get_log_lines(action_id)
261 TCONST.IDISP.log_debug(f"Gathered data: {data}", title)
262 _remove_log_line(data[0]["id"])
264 data = data[0]
265 assert data["type"] == action_type
266 assert str(data["action_id"]) == str(action_id)
267 assert data["message"] == action_test_message
268 assert str(data["code"]) == str(action_code)
269 assert data["level"] == action_expected_level
270 assert str(data["resolved"]) == str(int(action_resolved))
271
272
274 """_summary_
275 Function in charge of testing the log_event function.
276 """
277 title = "test_log_event_unknown"
279 action_id = TEST_INFO["action_id"]
280 action_type = ACONST.TYPE_ACTION
281 action_code = ACONST.CODE_UNKNOWN
282 action_test_message = "This is a test message"
283 action_resolved = False
284 action_expected_level = ACONST.LEVEL_UNKNOWN
285 status = ACLI.log_event(
286 log_type=action_type,
287 action_id=action_id,
288 code=action_code,
289 message=action_test_message,
290 resolved=action_resolved,
291 )
292 TCONST.IDISP.log_debug(f"Log id = {status}", title)
293 if status == ERROR:
294 msg = "Failed to log the event."
295 TCONST.IDISP.log_error(msg, title)
297 assert status == SUCCESS
298 data = _get_log_lines(action_id)
299 TCONST.IDISP.log_debug(f"Gathered data: {data}", title)
300 _remove_log_line(data[0]["id"])
302 data = data[0]
303 assert data["type"] == action_type
304 assert str(data["action_id"]) == str(action_id)
305 assert data["message"] == action_test_message
306 assert str(data["code"]) == str(action_code)
307 assert data["level"] == action_expected_level
308 assert str(data["resolved"]) == str(int(action_resolved))
309
310
312 """_summary_
313 Function in charge of testing the log_event function.
314 """
315 title = "test_log_event_success"
317 action_id = TEST_INFO["action_id"]
318 action_type = ACONST.TYPE_ACTION
319 action_code = ACONST.CODE_SUCCESS
320 action_test_message = "This is a test message"
321 action_resolved = False
322 action_expected_level = ACONST.LEVEL_SUCCESS
323 status = ACLI.log_event(
324 log_type=action_type,
325 action_id=action_id,
326 code=action_code,
327 message=action_test_message,
328 resolved=action_resolved,
329 )
330 TCONST.IDISP.log_debug(f"Log id = {status}", title)
331 if status == ERROR:
332 msg = "Failed to log the event."
333 TCONST.IDISP.log_error(msg, title)
335 assert status == SUCCESS
336 data = _get_log_lines(action_id)
337 TCONST.IDISP.log_debug(f"Gathered data: {data}", title)
338 _remove_log_line(data[0]["id"])
340 data = data[0]
341 assert data["type"] == action_type
342 assert str(data["action_id"]) == str(action_id)
343 assert data["message"] == action_test_message
344 assert str(data["code"]) == str(action_code)
345 assert data["level"] == action_expected_level
346 assert str(data["resolved"]) == str(int(action_resolved))
347
348
350 """_summary_
351 Function in charge of testing the log_event function.
352 """
353 title = "test_log_event_debug"
355 action_id = TEST_INFO["action_id"]
356 action_type = ACONST.TYPE_ACTION
357 action_code = ACONST.CODE_DEBUG
358 action_test_message = "This is a test message"
359 action_resolved = False
360 action_expected_level = ACONST.LEVEL_DEBUG
361 status = ACLI.log_event(
362 log_type=action_type,
363 action_id=action_id,
364 code=action_code,
365 message=action_test_message,
366 resolved=action_resolved,
367 )
368 TCONST.IDISP.log_debug(f"Log id = {status}", title)
369 if status == ERROR:
370 msg = "Failed to log the event."
371 TCONST.IDISP.log_error(msg, title)
373 assert status == SUCCESS
374 data = _get_log_lines(action_id)
375 TCONST.IDISP.log_debug(f"Gathered data: {data}", title)
376 _remove_log_line(data[0]["id"])
378 data = data[0]
379 assert data["type"] == action_type
380 assert str(data["action_id"]) == str(action_id)
381 assert data["message"] == action_test_message
382 assert str(data["code"]) == str(action_code)
383 assert data["level"] == action_expected_level
384 assert str(data["resolved"]) == str(int(action_resolved))
385
386
388 """_summary_
389 Function in charge of testing the log_event function.
390 """
391 title = "test_log_event_warning"
393 action_id = TEST_INFO["action_id"]
394 action_type = ACONST.TYPE_ACTION
395 action_code = ACONST.CODE_WARNING
396 action_test_message = "This is a test message"
397 action_resolved = False
398 action_expected_level = ACONST.LEVEL_WARNING
399 status = ACLI.log_event(
400 log_type=action_type,
401 action_id=action_id,
402 code=action_code,
403 message=action_test_message,
404 resolved=action_resolved,
405 )
406 TCONST.IDISP.log_debug(f"Log id = {status}", title)
407 if status == ERROR:
408 msg = "Failed to log the event."
409 TCONST.IDISP.log_error(msg, title)
411 assert status == SUCCESS
412 data = _get_log_lines(action_id)
413 TCONST.IDISP.log_debug(f"Gathered data: {data}", title)
414 _remove_log_line(data[0]["id"])
416 data = data[0]
417 assert data["type"] == action_type
418 assert str(data["action_id"]) == str(action_id)
419 assert data["message"] == action_test_message
420 assert str(data["code"]) == str(action_code)
421 assert data["level"] == action_expected_level
422 assert str(data["resolved"]) == str(int(action_resolved))
423
424
426 """_summary_
427 Function in charge of testing the log_event function.
428 """
429 title = "test_log_event_error"
431 action_id = TEST_INFO["action_id"]
432 action_type = ACONST.TYPE_ACTION
433 action_code = ACONST.CODE_ERROR
434 action_test_message = "This is a test message"
435 action_resolved = False
436 action_expected_level = ACONST.LEVEL_ERROR
437 status = ACLI.log_event(
438 log_type=action_type,
439 action_id=action_id,
440 code=action_code,
441 message=action_test_message,
442 resolved=action_resolved,
443 )
444 TCONST.IDISP.log_debug(f"Log id = {status}", title)
445 if status == ERROR:
446 msg = "Failed to log the event."
447 TCONST.IDISP.log_error(msg, title)
449 assert status == SUCCESS
450 data = _get_log_lines(action_id)
451 TCONST.IDISP.log_debug(f"Gathered data: {data}", title)
452 _remove_log_line(data[0]["id"])
454 data = data[0]
455 assert data["type"] == action_type
456 assert str(data["action_id"]) == str(action_id)
457 assert data["message"] == action_test_message
458 assert str(data["code"]) == str(action_code)
459 assert data["level"] == action_expected_level
460 assert str(data["resolved"]) == str(int(action_resolved))
461
462
464 """_summary_
465 Function in charge of testing the log_event function.
466 """
467 title = "test_log_event_critical"
469 action_id = TEST_INFO["action_id"]
470 action_type = ACONST.TYPE_ACTION
471 action_code = ACONST.CODE_CRITICAL
472 action_test_message = "This is a test message"
473 action_resolved = False
474 action_expected_level = ACONST.LEVEL_CRITICAL
475 status = ACLI.log_event(
476 log_type=action_type,
477 action_id=action_id,
478 code=action_code,
479 message=action_test_message,
480 resolved=action_resolved,
481 )
482 TCONST.IDISP.log_debug(f"Log id = {status}", title)
483 if status == ERROR:
484 msg = "Failed to log the event."
485 TCONST.IDISP.log_error(msg, title)
487 assert status == SUCCESS
488 data = _get_log_lines(action_id)
489 TCONST.IDISP.log_debug(f"Gathered data: {data}", title)
490 _remove_log_line(data[0]["id"])
492 data = data[0]
493 assert data["type"] == action_type
494 assert str(data["action_id"]) == str(action_id)
495 assert data["message"] == action_test_message
496 assert str(data["code"]) == str(action_code)
497 assert data["level"] == action_expected_level
498 assert str(data["resolved"]) == str(int(action_resolved))
499
500
502 """_summary_
503 Function in charge of testing the log_event function.
504 """
505 title = "test_log_event_fatal"
507 action_id = TEST_INFO["action_id"]
508 action_type = ACONST.TYPE_ACTION
509 action_code = ACONST.CODE_FATAL
510 action_test_message = "This is a test message"
511 action_resolved = False
512 action_expected_level = ACONST.LEVEL_FATAL
513 status = ACLI.log_event(
514 log_type=action_type,
515 action_id=action_id,
516 code=action_code,
517 message=action_test_message,
518 resolved=action_resolved,
519 )
520 TCONST.IDISP.log_debug(f"Log id = {status}", title)
521 if status == ERROR:
522 msg = "Failed to log the event."
523 TCONST.IDISP.log_error(msg, title)
525 assert status == SUCCESS
526 data = _get_log_lines(action_id)
527 TCONST.IDISP.log_debug(f"Gathered data: {data}", title)
528 _remove_log_line(data[0]["id"])
530 data = data[0]
531 assert data["type"] == action_type
532 assert str(data["action_id"]) == str(action_id)
533 assert data["message"] == action_test_message
534 assert str(data["code"]) == str(action_code)
535 assert data["level"] == action_expected_level
536 assert str(data["resolved"]) == str(int(action_resolved))
537
538
539def test_log_success() -> None:
540 """_summary_
541 Function in charge of testing the log_event function.
542 """
543 title = "test_log_success"
545 action_id = TEST_INFO["action_id"]
546 action_type = ACONST.TYPE_ACTION
547 action_code = ACONST.CODE_SUCCESS
548 action_test_message = "This is a test message"
549 action_resolved = True
550 action_expected_level = ACONST.LEVEL_SUCCESS
551 status = ACLI.log_success(
552 log_type=action_type,
553 action_id=action_id,
554 message=action_test_message,
555 resolved=action_resolved,
556 )
557 TCONST.IDISP.log_debug(f"Log id = {status}", title)
558 if status == ERROR:
559 msg = "Failed to log the event."
560 TCONST.IDISP.log_error(msg, title)
562 assert status == SUCCESS
563 data = _get_log_lines(action_id)
564 TCONST.IDISP.log_debug(f"Gathered data: {data}", title)
565 _remove_log_line(data[0]["id"])
567 data = data[0]
568 assert data["type"] == action_type
569 assert str(data["action_id"]) == str(action_id)
570 assert data["message"] == action_test_message
571 assert str(data["code"]) == str(action_code)
572 assert data["level"] == action_expected_level
573 assert str(data["resolved"]) == str(int(action_resolved))
574
575
576def test_log_debug() -> None:
577 """_summary_
578 Function in charge of testing the log_event function.
579 """
580 title = "test_log_debug"
582 action_id = TEST_INFO["action_id"]
583 action_type = ACONST.TYPE_ACTION
584 action_code = ACONST.CODE_DEBUG
585 action_test_message = "This is a test message"
586 action_resolved = True
587 action_expected_level = ACONST.LEVEL_DEBUG
588 status = ACLI.log_debug(
589 log_type=action_type,
590 action_id=action_id,
591 message=action_test_message,
592 resolved=action_resolved,
593 )
594 TCONST.IDISP.log_debug(f"Log id = {status}", title)
595 if status == ERROR:
596 msg = "Failed to log the event."
597 TCONST.IDISP.log_error(msg, title)
599 assert status == SUCCESS
600 data = _get_log_lines(action_id)
601 TCONST.IDISP.log_debug(f"Gathered data: {data}", title)
602 _remove_log_line(data[0]["id"])
604 data = data[0]
605 assert data["type"] == action_type
606 assert str(data["action_id"]) == str(action_id)
607 assert data["message"] == action_test_message
608 assert str(data["code"]) == str(action_code)
609 assert data["level"] == action_expected_level
610 assert str(data["resolved"]) == str(int(action_resolved))
611
612
613def test_log_warning() -> None:
614 """_summary_
615 Function in charge of testing the log_event function.
616 """
617 title = "test_log_warning"
619 action_id = TEST_INFO["action_id"]
620 action_type = ACONST.TYPE_ACTION
621 action_code = ACONST.CODE_WARNING
622 action_test_message = "This is a test message"
623 action_resolved = True
624 action_expected_level = ACONST.LEVEL_WARNING
625 status = ACLI.log_warning(
626 log_type=action_type,
627 action_id=action_id,
628 message=action_test_message,
629 resolved=action_resolved,
630 )
631 TCONST.IDISP.log_debug(f"Log id = {status}", title)
632 if status == ERROR:
633 msg = "Failed to log the event."
634 TCONST.IDISP.log_error(msg, title)
636 assert status == SUCCESS
637 data = _get_log_lines(action_id)
638 TCONST.IDISP.log_debug(f"Gathered data: {data}", title)
639 _remove_log_line(data[0]["id"])
641 data = data[0]
642 assert data["type"] == action_type
643 assert str(data["action_id"]) == str(action_id)
644 assert data["message"] == action_test_message
645 assert str(data["code"]) == str(action_code)
646 assert data["level"] == action_expected_level
647 assert str(data["resolved"]) == str(int(action_resolved))
648
649
650def test_log_error() -> None:
651 """_summary_
652 Function in charge of testing the log_event function.
653 """
654 title = "test_log_error"
656 action_id = TEST_INFO["action_id"]
657 action_type = ACONST.TYPE_ACTION
658 action_code = ACONST.CODE_ERROR
659 action_test_message = "This is a test message"
660 action_resolved = True
661 action_expected_level = ACONST.LEVEL_ERROR
662 status = ACLI.log_error(
663 log_type=action_type,
664 action_id=action_id,
665 message=action_test_message,
666 resolved=action_resolved,
667 )
668 TCONST.IDISP.log_debug(f"Log id = {status}", title)
669 if status == ERROR:
670 msg = "Failed to log the event."
671 TCONST.IDISP.log_error(msg, title)
673 assert status == SUCCESS
674 data = _get_log_lines(action_id)
675 TCONST.IDISP.log_debug(f"Gathered data: {data}", title)
676 _remove_log_line(data[0]["id"])
678 data = data[0]
679 assert data["type"] == action_type
680 assert str(data["action_id"]) == str(action_id)
681 assert data["message"] == action_test_message
682 assert str(data["code"]) == str(action_code)
683 assert data["level"] == action_expected_level
684 assert str(data["resolved"]) == str(int(action_resolved))
685
686
687def test_log_critical() -> None:
688 """_summary_
689 Function in charge of testing the log_event function.
690 """
691 title = "test_log_critical"
693 action_id = TEST_INFO["action_id"]
694 action_type = ACONST.TYPE_ACTION
695 action_code = ACONST.CODE_CRITICAL
696 action_test_message = "This is a test message"
697 action_resolved = True
698 action_expected_level = ACONST.LEVEL_CRITICAL
699 status = ACLI.log_critical(
700 log_type=action_type,
701 action_id=action_id,
702 message=action_test_message,
703 resolved=action_resolved,
704 )
705 TCONST.IDISP.log_debug(f"Log id = {status}", title)
706 if status == ERROR:
707 msg = "Failed to log the event."
708 TCONST.IDISP.log_error(msg, title)
710 assert status == SUCCESS
711 data = _get_log_lines(action_id)
712 TCONST.IDISP.log_debug(f"Gathered data: {data}", title)
713 _remove_log_line(data[0]["id"])
715 data = data[0]
716 assert data["type"] == action_type
717 assert str(data["action_id"]) == str(action_id)
718 assert data["message"] == action_test_message
719 assert str(data["code"]) == str(action_code)
720 assert data["level"] == action_expected_level
721 assert str(data["resolved"]) == str(int(action_resolved))
722
723
724def test_log_fatal() -> None:
725 """_summary_
726 Function in charge of testing the log_event function.
727 """
728 title = "test_log_fatal"
730 action_id = TEST_INFO["action_id"]
731 action_type = ACONST.TYPE_ACTION
732 action_code = ACONST.CODE_FATAL
733 action_test_message = "This is a test message"
734 action_resolved = True
735 action_expected_level = ACONST.LEVEL_FATAL
736 status = ACLI.log_fatal(
737 log_type=action_type,
738 action_id=action_id,
739 message=action_test_message,
740 resolved=action_resolved,
741 )
742 TCONST.IDISP.log_debug(f"Log id = {status}", title)
743 if status == ERROR:
744 msg = "Failed to log the event."
745 TCONST.IDISP.log_error(msg, title)
747 assert status == SUCCESS
748 data = _get_log_lines(action_id)
749 TCONST.IDISP.log_debug(f"Gathered data: {data}", title)
750 _remove_log_line(data[0]["id"])
752 data = data[0]
753 assert data["type"] == action_type
754 assert str(data["action_id"]) == str(action_id)
755 assert data["message"] == action_test_message
756 assert str(data["code"]) == str(action_code)
757 assert data["level"] == action_expected_level
758 assert str(data["resolved"]) == str(int(action_resolved))
759
760
761def test_log_unknown() -> None:
762 """_summary_
763 Function in charge of testing the log_event function.
764 """
765 title = "test_log_unknown"
767 action_id = TEST_INFO["action_id"]
768 action_type = ACONST.TYPE_ACTION
769 action_code = ACONST.CODE_UNKNOWN
770 action_test_message = "This is a test message"
771 action_resolved = True
772 action_expected_level = ACONST.LEVEL_UNKNOWN
773 status = ACLI.log_unknown(
774 log_type=action_type,
775 action_id=action_id,
776 message=action_test_message,
777 resolved=action_resolved,
778 )
779 TCONST.IDISP.log_debug(f"Log id = {status}", title)
780 if status == ERROR:
781 msg = "Failed to log the event."
782 TCONST.IDISP.log_error(msg, title)
784 assert status == SUCCESS
785 data = _get_log_lines(action_id)
786 TCONST.IDISP.log_debug(f"Gathered data: {data}", title)
787 _remove_log_line(data[0]["id"])
789 data = data[0]
790 assert data["type"] == action_type
791 assert str(data["action_id"]) == str(action_id)
792 assert data["message"] == action_test_message
793 assert str(data["code"]) == str(action_code)
794 assert data["level"] == action_expected_level
795 assert str(data["resolved"]) == str(int(action_resolved))
796
797
798def test_get_logs() -> None:
799 """_summary_
800 Function in charge of testing the log_event function.
801 """
802 title = "test_get_logs"
804 action_id = TEST_INFO["action_id"]
805 action_type = ACONST.TYPE_ACTION
806 action_test_message = "This is a test message"
807 action_resolved = True
808 log_ids = {}
809 for code in ACONST.LIST_CODE:
810 status = ACLI.log_event(
811 log_type=action_type,
812 action_id=action_id,
813 code=code,
814 message=action_test_message,
815 resolved=action_resolved,
816 )
817 if status != ERROR:
818 node = _get_log_lines(action_id)
819 if len(node) > 0:
820 node = node[-1]
821 log_ids[code] = node['id']
822 if status == ERROR:
823 msg = "Failed to log the event."
824 TCONST.IDISP.log_error(msg, title)
825 _remove_log_line(log_ids)
827 assert status != ERROR
828 TCONST.IDISP.log_debug(f"Log ids = {log_ids}", title)
829 data = _get_log_lines(action_id)
830 TCONST.IDISP.log_debug(f"Gathered data: {data}", title)
831 user_logs = ACLI.get_logs(
832 action_id=action_id, code=None, beautify=True
833 )
834 TCONST.IDISP.log_debug(f"Gathered user logs = {user_logs}", title)
835 _remove_log_line(log_ids)
837 index = 0
838 for log in user_logs:
839 action_code = ACONST.LIST_CODE[index]
840 action_level = ACONST.LIST_LEVEL_INFO[index]
841 index += 1
842 assert log["type"] == action_type
843 assert str(log["action_id"]) == str(action_id)
844 assert log["message"] == action_test_message
845 assert str(log["code"]) == str(action_code)
846 assert log["level"] == action_level
847 assert str(log["resolved"]) == str(int(action_resolved))
848
849
851 """_summary_
852 Function in charge of testing the log_event function.
853 """
854 title = "test_get_logs_unknown"
856 action_id = TEST_INFO["action_id"]
857 action_type = ACONST.TYPE_ACTION
858 action_test_message = "This is a test message"
859 action_resolved = True
860 log_ids = {}
861 for code in ACONST.LIST_CODE:
862 status = ACLI.log_event(
863 log_type=action_type,
864 action_id=action_id,
865 code=code,
866 message=action_test_message,
867 resolved=action_resolved,
868 )
869 if status != ERROR:
870 node = _get_log_lines(action_id)
871 if len(node) > 0:
872 node = node[-1]
873 log_ids[code] = node['id']
874 if status == ERROR:
875 msg = "Failed to log the event."
876 TCONST.IDISP.log_error(msg, title)
877 _remove_log_line(log_ids)
879 assert status != ERROR
880 TCONST.IDISP.log_debug(f"Log ids = {log_ids}", title)
881 user_logs = ACLI.get_logs_unknown(
882 action_id=action_id, beautify=True
883 )
884 print(f"Gathered user logs = {user_logs}", title)
885 _remove_log_line(log_ids)
887 TCONST.IDISP.log_debug(f"logs = {user_logs}", title)
888 for log in user_logs:
889 action_code = ACONST.CODE_UNKNOWN
890 action_level = ACONST.LEVEL_UNKNOWN
891 assert log["type"] == action_type
892 assert str(log["action_id"]) == str(action_id)
893 assert log["message"] == action_test_message
894 assert str(log["code"]) == str(action_code)
895 assert log["level"] == action_level
896 assert str(log["resolved"]) == str(int(action_resolved))
897
898
899def test_get_logs_info() -> None:
900 """_summary_
901 Function in charge of testing the log_event function.
902 """
903 title = "test_get_logs_info"
905 action_id = TEST_INFO["action_id"]
906 action_type = ACONST.TYPE_ACTION
907 action_test_message = "This is a test message"
908 action_resolved = True
909 log_ids = {}
910 for code in ACONST.LIST_CODE:
911 status = ACLI.log_event(
912 log_type=action_type,
913 action_id=action_id,
914 code=code,
915 message=action_test_message,
916 resolved=action_resolved,
917 )
918 if status != ERROR:
919 node = _get_log_lines(action_id)
920 if len(node) > 0:
921 node = node[-1]
922 log_ids[code] = node['id']
923 if status == ERROR:
924 msg = "Failed to log the event."
925 TCONST.IDISP.log_error(msg, title)
926 _remove_log_line(log_ids)
928 assert status != ERROR
929 TCONST.IDISP.log_debug(f"Log ids = {log_ids}", title)
930 user_logs = ACLI.get_logs_info(
931 action_id=action_id, beautify=True
932 )
933 print(f"Gathered user logs = {user_logs}", title)
934 _remove_log_line(log_ids)
936 TCONST.IDISP.log_debug(f"logs = {user_logs}", title)
937 for log in user_logs:
938 action_code = ACONST.CODE_INFO
939 action_level = ACONST.LEVEL_INFO
940 assert log["type"] == action_type
941 assert str(log["action_id"]) == str(action_id)
942 assert log["message"] == action_test_message
943 assert str(log["code"]) == str(action_code)
944 assert log["level"] == action_level
945 assert str(log["resolved"]) == str(int(action_resolved))
946
947
949 """_summary_
950 Function in charge of testing the log_event function.
951 """
952 title = "test_get_logs_success"
954 action_id = TEST_INFO["action_id"]
955 action_type = ACONST.TYPE_ACTION
956 action_test_message = "This is a test message"
957 action_resolved = True
958 log_ids = {}
959 for code in ACONST.LIST_CODE:
960 status = ACLI.log_event(
961 log_type=action_type,
962 action_id=action_id,
963 code=code,
964 message=action_test_message,
965 resolved=action_resolved,
966 )
967 if status != ERROR:
968 node = _get_log_lines(action_id)
969 if len(node) > 0:
970 node = node[-1]
971 log_ids[code] = node['id']
972 if status == ERROR:
973 msg = "Failed to log the event."
974 TCONST.IDISP.log_error(msg, title)
975 _remove_log_line(log_ids)
977 assert status != ERROR
978 TCONST.IDISP.log_debug(f"Log ids = {log_ids}", title)
979 user_logs = ACLI.get_logs_success(
980 action_id=action_id, beautify=True
981 )
982 print(f"Gathered user logs = {user_logs}", title)
983 _remove_log_line(log_ids)
985 TCONST.IDISP.log_debug(f"logs = {user_logs}", title)
986 for log in user_logs:
987 action_code = ACONST.CODE_SUCCESS
988 action_level = ACONST.LEVEL_SUCCESS
989 assert log["type"] == action_type
990 assert str(log["action_id"]) == str(action_id)
991 assert log["message"] == action_test_message
992 assert str(log["code"]) == str(action_code)
993 assert log["level"] == action_level
994 assert str(log["resolved"]) == str(int(action_resolved))
995
996
998 """_summary_
999 Function in charge of testing the log_event function.
1000 """
1001 title = "test_get_logs_debug"
1003 action_id = TEST_INFO["action_id"]
1004 action_type = ACONST.TYPE_ACTION
1005 action_test_message = "This is a test message"
1006 action_resolved = True
1007 log_ids = {}
1008 for code in ACONST.LIST_CODE:
1009 status = ACLI.log_event(
1010 log_type=action_type,
1011 action_id=action_id,
1012 code=code,
1013 message=action_test_message,
1014 resolved=action_resolved,
1015 )
1016 if status != ERROR:
1017 node = _get_log_lines(action_id)
1018 if len(node) > 0:
1019 node = node[-1]
1020 log_ids[code] = node['id']
1021 if status == ERROR:
1022 msg = "Failed to log the event."
1023 TCONST.IDISP.log_error(msg, title)
1024 _remove_log_line(log_ids)
1026 assert status != ERROR
1027 TCONST.IDISP.log_debug(f"Log ids = {log_ids}", title)
1028 user_logs = ACLI.get_logs_debug(
1029 action_id=action_id, beautify=True
1030 )
1031 print(f"Gathered user logs = {user_logs}", title)
1032 _remove_log_line(log_ids)
1034 TCONST.IDISP.log_debug(f"logs = {user_logs}", title)
1035 for log in user_logs:
1036 action_code = ACONST.CODE_DEBUG
1037 action_level = ACONST.LEVEL_DEBUG
1038 assert log["type"] == action_type
1039 assert str(log["action_id"]) == str(action_id)
1040 assert log["message"] == action_test_message
1041 assert str(log["code"]) == str(action_code)
1042 assert log["level"] == action_level
1043 assert str(log["resolved"]) == str(int(action_resolved))
1044
1045
1047 """_summary_
1048 Function in charge of testing the log_event function.
1049 """
1050 title = "test_get_logs_warning"
1052 action_id = TEST_INFO["action_id"]
1053 action_type = ACONST.TYPE_ACTION
1054 action_test_message = "This is a test message"
1055 action_resolved = True
1056 log_ids = {}
1057 for code in ACONST.LIST_CODE:
1058 status = ACLI.log_event(
1059 log_type=action_type,
1060 action_id=action_id,
1061 code=code,
1062 message=action_test_message,
1063 resolved=action_resolved,
1064 )
1065 if status != ERROR:
1066 node = _get_log_lines(action_id)
1067 if len(node) > 0:
1068 node = node[-1]
1069 log_ids[code] = node['id']
1070 if status == ERROR:
1071 msg = "Failed to log the event."
1072 TCONST.IDISP.log_error(msg, title)
1073 _remove_log_line(log_ids)
1075 assert status != ERROR
1076 TCONST.IDISP.log_debug(f"Log ids = {log_ids}", title)
1077 user_logs = ACLI.get_logs_warning(
1078 action_id=action_id, beautify=True
1079 )
1080 print(f"Gathered user logs = {user_logs}", title)
1081 _remove_log_line(log_ids)
1083 TCONST.IDISP.log_debug(f"logs = {user_logs}", title)
1084 for log in user_logs:
1085 action_code = ACONST.CODE_WARNING
1086 action_level = ACONST.LEVEL_WARNING
1087 assert log["type"] == action_type
1088 assert str(log["action_id"]) == str(action_id)
1089 assert log["message"] == action_test_message
1090 assert str(log["code"]) == str(action_code)
1091 assert log["level"] == action_level
1092 assert str(log["resolved"]) == str(int(action_resolved))
1093
1094
1096 """_summary_
1097 Function in charge of testing the log_event function.
1098 """
1099 title = "test_get_logs_error"
1101 action_id = TEST_INFO["action_id"]
1102 action_type = ACONST.TYPE_ACTION
1103 action_test_message = "This is a test message"
1104 action_resolved = True
1105 log_ids = {}
1106 for code in ACONST.LIST_CODE:
1107 status = ACLI.log_event(
1108 log_type=action_type,
1109 action_id=action_id,
1110 code=code,
1111 message=action_test_message,
1112 resolved=action_resolved,
1113 )
1114 if status != ERROR:
1115 node = _get_log_lines(action_id)
1116 if len(node) > 0:
1117 node = node[-1]
1118 log_ids[code] = node['id']
1119 if status == ERROR:
1120 msg = "Failed to log the event."
1121 TCONST.IDISP.log_error(msg, title)
1122 _remove_log_line(log_ids)
1124 assert status != ERROR
1125 TCONST.IDISP.log_debug(f"Log ids = {log_ids}", title)
1126 user_logs = ACLI.get_logs_error(
1127 action_id=action_id, beautify=True
1128 )
1129 print(f"Gathered user logs = {user_logs}", title)
1130 _remove_log_line(log_ids)
1132 TCONST.IDISP.log_debug(f"logs = {user_logs}", title)
1133 for log in user_logs:
1134 action_code = ACONST.CODE_ERROR
1135 action_level = ACONST.LEVEL_ERROR
1136 assert log["type"] == action_type
1137 assert str(log["action_id"]) == str(action_id)
1138 assert log["message"] == action_test_message
1139 assert str(log["code"]) == str(action_code)
1140 assert log["level"] == action_level
1141 assert str(log["resolved"]) == str(int(action_resolved))
1142
1143
1145 """_summary_
1146 Function in charge of testing the log_event function.
1147 """
1148 title = "test_get_logs_critical"
1150 action_id = TEST_INFO["action_id"]
1151 action_type = ACONST.TYPE_ACTION
1152 action_test_message = "This is a test message"
1153 action_resolved = True
1154 log_ids = {}
1155 for code in ACONST.LIST_CODE:
1156 status = ACLI.log_event(
1157 log_type=action_type,
1158 action_id=action_id,
1159 code=code,
1160 message=action_test_message,
1161 resolved=action_resolved,
1162 )
1163 if status != ERROR:
1164 node = _get_log_lines(action_id)
1165 if len(node) > 0:
1166 node = node[-1]
1167 log_ids[code] = node['id']
1168 if status == ERROR:
1169 msg = "Failed to log the event."
1170 TCONST.IDISP.log_error(msg, title)
1171 _remove_log_line(log_ids)
1173 assert status != ERROR
1174 TCONST.IDISP.log_debug(f"Log ids = {log_ids}", title)
1175 user_logs = ACLI.get_logs_critical(
1176 action_id=action_id, beautify=True
1177 )
1178 print(f"Gathered user logs = {user_logs}", title)
1179 _remove_log_line(log_ids)
1181 TCONST.IDISP.log_debug(f"logs = {user_logs}", title)
1182 for log in user_logs:
1183 action_code = ACONST.CODE_CRITICAL
1184 action_level = ACONST.LEVEL_CRITICAL
1185 assert log["type"] == action_type
1186 assert str(log["action_id"]) == str(action_id)
1187 assert log["message"] == action_test_message
1188 assert str(log["code"]) == str(action_code)
1189 assert log["level"] == action_level
1190 assert str(log["resolved"]) == str(int(action_resolved))
1191
1192
1194 """_summary_
1195 Function in charge of testing the log_event function.
1196 """
1197 title = "test_get_logs_fatal"
1199 action_id = TEST_INFO["action_id"]
1200 action_type = ACONST.TYPE_ACTION
1201 action_test_message = "This is a test message"
1202 action_resolved = True
1203 log_ids = {}
1204 for code in ACONST.LIST_CODE:
1205 status = ACLI.log_event(
1206 log_type=action_type,
1207 action_id=action_id,
1208 code=code,
1209 message=action_test_message,
1210 resolved=action_resolved,
1211 )
1212 if status != ERROR:
1213 node = _get_log_lines(action_id)
1214 if len(node) > 0:
1215 node = node[-1]
1216 log_ids[code] = node['id']
1217 if status == ERROR:
1218 msg = "Failed to log the event."
1219 TCONST.IDISP.log_error(msg, title)
1220 _remove_log_line(log_ids)
1222 assert status != ERROR
1223 TCONST.IDISP.log_debug(f"Log ids = {log_ids}", title)
1224 user_logs = ACLI.get_logs_fatal(
1225 action_id=action_id, beautify=True
1226 )
1227 print(f"Gathered user logs = {user_logs}", title)
1228 _remove_log_line(log_ids)
1230 TCONST.IDISP.log_debug(f"logs = {user_logs}", title)
1231 for log in user_logs:
1232 action_code = ACONST.CODE_FATAL
1233 action_level = ACONST.LEVEL_FATAL
1234 assert log["type"] == action_type
1235 assert str(log["action_id"]) == str(action_id)
1236 assert log["message"] == action_test_message
1237 assert str(log["code"]) == str(action_code)
1238 assert log["level"] == action_level
1239 assert str(log["resolved"]) == str(int(action_resolved))
None _remove_log_line(Union[int, Dict[str, int]] log_id)
Dict[str, Any] _get_log_lines(str action_id="")