File: test_loggers.py

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (164 lines) | stat: -rw-r--r-- 7,244 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Copyright Contributors to the DNF5 project.
# Copyright Contributors to the libdnf project.
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
#
# Libdnf 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 2 of the License, or
# (at your option) any later version.
#
# Libdnf 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 libdnf.  If not, see <https://www.gnu.org/licenses/>.

import io
import unittest

import libdnf5.logger


# Tests overloading of log() method
class libdnf5LoggerCB1(libdnf5.logger.Logger):
    def __init__(self, stream):
        super(libdnf5LoggerCB1, self).__init__()
        self._stream = stream

    def log_line(self, level, message):
        self._stream.write("{}: {}\n".format(
            self.level_to_cstr(level), message))

    def write(self, time, pid, level, message):
        self._stream.write("Bad message\n")


# Tests overloading of write() method
class libdnf5LoggerCB2(libdnf5.logger.Logger):
    def __init__(self, stream):
        super(libdnf5LoggerCB2, self).__init__()
        self._stream = stream

    def write(self, time, pid, level, message):
        self._stream.write("{}: {}\n".format(
            self.level_to_cstr(level), message))


class TestLoggers(unittest.TestCase):
    def test_logger(self):
        expected_logger_content = "CRITICAL: Critical message\n" \
                                  "ERROR: Error message\n"       \
                                  "WARNING: Warning message\n"   \
                                  "NOTICE: Notice message\n"     \
                                  "INFO: Info message\n"         \
                                  "DEBUG: Debug message\n"       \
                                  "TRACE: Trace message\n"

        stream1 = io.StringIO()
        logger1 = libdnf5LoggerCB1(stream1)
        stream2 = io.StringIO()
        logger2 = libdnf5LoggerCB2(stream2)
        for logger in logger1, logger2:
            logger.critical("Critical message")
            logger.error("Error message")
            logger.warning("Warning message")
            logger.notice("Notice message")
            logger.info("Info message")
            logger.debug("Debug message")
            logger.trace("Trace message")
        content1 = stream1.getvalue()
        self.assertEqual(content1, expected_logger_content)
        content2 = stream2.getvalue()
        self.assertEqual(content2, expected_logger_content)

    def test_logrouter(self):
        expected_logger_content = "INFO: Info message\n"         \
                                  "DEBUG: Debug message\n"       \
                                  "TRACE: Trace message\n"       \
                                  "CRITICAL: Critical message\n" \
                                  "ERROR: Error message\n"       \
                                  "WARNING: Warning message\n"   \
                                  "NOTICE: Notice message\n"     \
                                  "INFO: Info message\n"         \
                                  "DEBUG: Debug message\n"       \
                                  "TRACE: Trace message\n"       \
                                  "INFO: Info additional message\n"

        # ====================
        #  1. Create a LogRouter instance with one MemoryBufferLogger instances attached.
        #  ====================
        # Create log router.
        log_router = libdnf5.logger.LogRouter()
        # Create circular memory buffer logger with capacity 10 messages (4 pre-allocated from start).
        max_items_to_keep = 10
        reserve = 4
        memory_buffer_logger = libdnf5.logger.MemoryBufferLogger(
            max_items_to_keep, reserve)
        logger_uniq_ptr = libdnf5.logger.LoggerUniquePtr(memory_buffer_logger)
        log_router.add_logger(logger_uniq_ptr)

        # Test the number of registered loggers.
        self.assertEqual(log_router.get_loggers_count(), 1)

        # ====================
        # 2. Write messages into log_router. They will be routed into memory_buffer_logger.
        # ====================
        for i in range(2):
            log_router.log(libdnf5.logger.Logger.Level_CRITICAL,
                           "Critical message")
            log_router.log(libdnf5.logger.Logger.Level_ERROR, "Error message")
            log_router.log(libdnf5.logger.Logger.Level_WARNING,
                           "Warning message")
            log_router.log(libdnf5.logger.Logger.Level_NOTICE,
                           "Notice message")
            log_router.log(libdnf5.logger.Logger.Level_INFO, "Info message")
            log_router.log(libdnf5.logger.Logger.Level_DEBUG, "Debug message")
            log_router.log(libdnf5.logger.Logger.Level_TRACE, "Trace message")

        # ====================
        # 3. Create two StreamLogger instances which replace (swap) MemoryBufferLogger instance in log_router.
        # ====================
        # Create two streams for logging. Save pointers to them. Pointers will be needed later
        # in test of streams content.
        stream1 = io.StringIO()
        stream2 = io.StringIO()

        # Create StreamLogger instance and swap it with MemoryBufferLogger instance which was added
        # into LogRouter before.
        tmp_logger = libdnf5LoggerCB2(stream1)
        tmp_logger_uniq_ptr = libdnf5.logger.LoggerUniquePtr(tmp_logger)
        # In the log_router is registered only instance of MemoryBufferLogger just now.
        # The index of the first logger is "0".
        log_router.swap_logger(tmp_logger_uniq_ptr, 0)

        # Create secondary StreamLogger instance and add it to LogRouter as another logger.
        log2_stream = libdnf5LoggerCB2(stream2)
        log2_stream_uniq_ptr = libdnf5.logger.LoggerUniquePtr(log2_stream)
        log_router.add_logger(log2_stream_uniq_ptr)

        # Test the number of registered loggers.
        self.assertEqual(log_router.get_loggers_count(), 2)

        # ====================
        # 4. Write messages from original (replaced) MemoryBufferLoger instance into LogRouter instance.
        #    They will be routed to both attached stream loggers.
        # ====================
        memory_buffer_logger.write_to_logger(log_router)

        # ====================
        # 5. Write additional message into LogRouter instance.
        # ====================
        log_router.log(libdnf5.logger.Logger.Level_INFO,
                       "Info additional message")

        # ====================
        # 6. Check content of streams of both StreamLogger instances.
        # ====================
        content = stream1.getvalue()
        self.assertEqual(content, expected_logger_content)
        content = stream2.getvalue()
        self.assertEqual(content, expected_logger_content)