File: test_log.py

package info (click to toggle)
fangfrisch 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 804 kB
  • sloc: python: 1,326; sh: 113; makefile: 64; sql: 24
file content (72 lines) | stat: -rw-r--r-- 2,190 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Copyright © 2020-2025 Ralph Seichter

This file is part of "Fangfrisch".

Fangfrisch is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Fangfrisch is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Fangfrisch. If not, see <https://www.gnu.org/licenses/>.
"""

import logging
import unittest

import fangfrisch.log
from fangfrisch.log import LogHandlerType

# noinspection PyProtectedMember
from fangfrisch.log import _create_handler
from fangfrisch.log import init_logger
from fangfrisch.log import parse_syslog_target

FORMAT = r"%(message)s"
LEVEL = "FATAL"


class LogTests(unittest.TestCase):
    def setUp(self) -> None:
        super().setUp()
        fangfrisch.log._handler = None
        fangfrisch.log._logger = None

    def test_init_console_handler(self):
        x = _create_handler(LogHandlerType.CONSOLE, FORMAT, "")
        self.assertTrue(isinstance(x, logging.Handler))

    def test_init_syslog_handler(self):
        x = _create_handler(LogHandlerType.SYSLOG, FORMAT, "localhost")
        self.assertTrue(isinstance(x, logging.Handler))
        x.close()

    def test_init_syslog_handler_port(self):
        x = _create_handler(LogHandlerType.SYSLOG, FORMAT, "127.0.0.1:514")
        self.assertTrue(isinstance(x, logging.Handler))
        x.close()

    def test_init_logger(self):
        x = init_logger(LogHandlerType.CONSOLE, LEVEL, FORMAT)
        self.assertTrue(isinstance(x, logging.Logger))

    def test_parse_dgram(self):
        a = "/dev/log"
        self.assertEqual(a, parse_syslog_target(a))

    def test_parse_inet(self):
        h = "127.0.0.1"
        p = 101
        a, b = parse_syslog_target(f"{h}:{p}")
        self.assertEqual(h, a)
        self.assertEqual(p, b)


if __name__ == "__main__":
    unittest.main()