summaryrefslogtreecommitdiffstats
path: root/pylog/tests/test_mdc_context.py
blob: 9e8b1bc87b02a8604ee86b87148a6994fc0336b7 (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Copyright (c) 2020 Deutsche Telekom.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

import logging
import sys
import unittest

if sys.version_info[0] < 3:
    from mock import MagicMock, patch
if sys.version_info[0] >= 3:
    from unittest.mock import MagicMock, patch

import pytest

from onaplogging.mdcContext import (
    _getmdcs, 
    MDCContext, 
    info, 
    debug,
    warning, 
    exception, 
    critical, 
    error, 
    log,
    handle
)


class TestMDCContext(unittest.TestCase):

    def setUp(self):
        super(TestMDCContext, self).setUp()

        self.TEST_KEY = "key"
        self.TEST_VALUE = "value"
    
        self.mdc_context = MDCContext()

    def test_mdc_context(self):

        self.assertTrue(self.mdc_context.isEmpty())
        self.assertIsNone(self.mdc_context.get(self.TEST_KEY))
        self.mdc_context.remove(self.TEST_KEY)
        self.mdc_context.put(self.TEST_KEY, self.TEST_VALUE)
        self.assertFalse(self.mdc_context.isEmpty())
        self.assertEqual(self.mdc_context.get(self.TEST_KEY), self.TEST_VALUE)
        self.assertDictEqual(self.mdc_context.result(), {self.TEST_KEY: self.TEST_VALUE})
        self.mdc_context.remove(self.TEST_KEY)
        self.assertTrue(self.mdc_context.isEmpty())
        self.assertDictEqual(self.mdc_context.result(), {})
        self.mdc_context.put(self.TEST_KEY, self.TEST_VALUE)
        self.assertFalse(self.mdc_context.isEmpty())
        self.assertEqual(self.mdc_context.get(self.TEST_KEY), self.TEST_VALUE)
        self.assertDictEqual(self.mdc_context.result(), {self.TEST_KEY: self.TEST_VALUE})
        self.mdc_context.clear()
        self.assertTrue(self.mdc_context.isEmpty())
        self.assertDictEqual(self.mdc_context.result(), {})

    def test_getmdcs(self):
        with patch("onaplogging.mdcContext.MDC", self.mdc_context):
            self.assertIsNone(_getmdcs(None))
            self.mdc_context.put(self.TEST_KEY, self.TEST_VALUE)
            self.assertDictEqual(_getmdcs(None), {"mdc": {self.TEST_KEY: self.TEST_VALUE}})
            self.assertDictEqual(_getmdcs({"test": "value"}), {"mdc": {self.TEST_KEY: self.TEST_VALUE}, "test": "value"})
            with pytest.raises(KeyError):
                _getmdcs({self.TEST_KEY: self.TEST_VALUE})
            with pytest.raises(KeyError):
                _getmdcs({"mdc": "exception"})

    def test_fetchkeys_info(self):
        with patch("onaplogging.mdcContext.MDC", self.mdc_context):
            test_self = MagicMock()
            test_self.isEnabledFor.return_value = False
            info(test_self, "msg")
            test_self._log.assert_not_called()
            test_self.isEnabledFor.return_value = True
            info(test_self, "msg")
            test_self._log.assert_called_once_with(logging.INFO, "msg", (), extra=None)
            test_self._log.reset_mock()
            self.mdc_context.put(self.TEST_KEY, self.TEST_VALUE)
            info(test_self, "msg")
            test_self._log.assert_called_once_with(logging.INFO, "msg", (), extra={"mdc": {self.TEST_KEY: self.TEST_VALUE}})

    def test_fetchkeys_debug(self):
        with patch("onaplogging.mdcContext.MDC", self.mdc_context):
            test_self = MagicMock()
            test_self.isEnabledFor.return_value = False
            debug(test_self, "msg")
            test_self._log.assert_not_called()
            test_self.isEnabledFor.return_value = True
            debug(test_self, "msg")
            test_self._log.assert_called_once_with(logging.DEBUG, "msg", (), extra=None)
            test_self._log.reset_mock()
            self.mdc_context.put(self.TEST_KEY, self.TEST_VALUE)
            debug(test_self, "msg")
            test_self._log.assert_called_once_with(logging.DEBUG, "msg", (), extra={"mdc": {self.TEST_KEY: self.TEST_VALUE}})

    def test_fetchkeys_warning(self):
        with patch("onaplogging.mdcContext.MDC", self.mdc_context):
            test_self = MagicMock()
            test_self.isEnabledFor.return_value = False
            warning(test_self, "msg")
            test_self._log.assert_not_called()
            test_self.isEnabledFor.return_value = True
            warning(test_self, "msg")
            test_self._log.assert_called_once_with(logging.WARNING, "msg", (), extra=None)
            test_self._log.reset_mock()
            self.mdc_context.put(self.TEST_KEY, self.TEST_VALUE)
            warning(test_self, "msg")
            test_self._log.assert_called_once_with(logging.WARNING, "msg", (), extra={"mdc": {self.TEST_KEY: self.TEST_VALUE}})

    def test_fetchkeys_exception(self):
        with patch("onaplogging.mdcContext.MDC", self.mdc_context):
            test_self = MagicMock()
            test_self.isEnabledFor.return_value = False
            exception(test_self, "msg")
            test_self.error.assert_called_once_with("msg", exc_info=1, extra=None)

    def test_fetchkeys_critical(self):
        with patch("onaplogging.mdcContext.MDC", self.mdc_context):
            test_self = MagicMock()
            test_self.isEnabledFor.return_value = False
            critical(test_self, "msg")
            test_self._log.assert_not_called()
            test_self.isEnabledFor.return_value = True
            critical(test_self, "msg")
            test_self._log.assert_called_once_with(logging.CRITICAL, "msg", (), extra=None)
            test_self._log.reset_mock()
            self.mdc_context.put(self.TEST_KEY, self.TEST_VALUE)
            critical(test_self, "msg")
            test_self._log.assert_called_once_with(logging.CRITICAL, "msg", (), extra={"mdc": {self.TEST_KEY: self.TEST_VALUE}})

    def test_fetchkeys_error(self):
        with patch("onaplogging.mdcContext.MDC", self.mdc_context):
            test_self = MagicMock()
            test_self.isEnabledFor.return_value = False
            error(test_self, "msg")
            test_self._log.assert_not_called()
            test_self.isEnabledFor.return_value = True
            error(test_self, "msg")
            test_self._log.assert_called_once_with(logging.ERROR, "msg", (), extra=None)
            test_self._log.reset_mock()
            self.mdc_context.put(self.TEST_KEY, self.TEST_VALUE)
            error(test_self, "msg")
            test_self._log.assert_called_once_with(logging.ERROR, "msg", (), extra={"mdc": {self.TEST_KEY: self.TEST_VALUE}})

    def test_fetchkeys_log(self):
        with patch("onaplogging.mdcContext.MDC", self.mdc_context):
            test_self = MagicMock()
            test_self.isEnabledFor.return_value = False
            logging.raiseExceptions = False
            log(test_self, "invalid_level", "msg")
            logging.raiseExceptions = True
            with pytest.raises(TypeError):
                log(test_self, "invalid_level", "msg")
            log(test_self, logging.DEBUG, "msg")
            test_self._log.assert_not_called()
            test_self.isEnabledFor.return_value = True
            log(test_self, logging.DEBUG, "msg")
            test_self._log.assert_called_once()

    def test_handle(self):
        with patch("onaplogging.mdcContext.MDC", self.mdc_context):
            test_self = MagicMock()
            record = MagicMock()
            test_self.disabled = True
            test_self.filter.return_value = False
            handle(test_self, record)
            test_self.callHandlers.assert_not_called()

            test_self.disabled = False
            test_self.filter.return_value = False
            handle(test_self, record)
            test_self.callHandlers.assert_not_called()
            test_self.filter.assert_called_once_with(record)

            test_self.filter.reset_mock()
            test_self.disabled = False
            test_self.filter.return_value = True
            handle(test_self, record)
            test_self.callHandlers.assert_called_once()
            test_self.filter.assert_called_once_with(record)