1 # Copyright (c) 2019 AT&T Intellectual Property.
2 # Copyright (c) 2018-2019 Nokia.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 # platform project (RICP).
19 """Unit tests for Logger.py"""
21 from unittest.mock import patch
26 from mdclogpy import Logger
27 from mdclogpy import Level
29 from .mdclogtestutils import TestMdcLogUtils
32 class TestMdcLog(unittest.TestCase):
33 """Unit tests for mdclog.py"""
36 self.logger = Logger()
42 def test_that_get_level_returns_the_current_log_level(self):
44 # default level is ERROR
45 self.assertEqual(self.logger.get_level(), Level.ERROR)
46 self.logger.set_level(Level.INFO)
47 self.assertEqual(self.logger.get_level(), Level.INFO)
48 self.logger.set_level(Level.WARNING)
49 self.assertEqual(self.logger.get_level(), Level.WARNING)
50 self.logger.set_level(Level.ERROR)
51 self.assertEqual(self.logger.get_level(), Level.ERROR)
52 self.logger.set_level(Level.DEBUG)
53 self.assertEqual(self.logger.get_level(), Level.DEBUG)
55 def test_that_set_level_does_not_accept_incorrect_level(self):
57 self.logger.set_level(Level.INFO)
58 self.logger.set_level(55)
59 self.assertEqual(self.logger.get_level(), Level.INFO)
61 @patch('mdclogpy.Logger._output_log')
62 def test_that_logs_with_lower_than_current_level_(self, output_mock):
64 self.logger.set_level(Level.WARNING)
65 self.logger.log(Level.DEBUG, "DEBUG")
66 self.logger.log(Level.INFO, "INFO")
67 self.logger.log(Level.WARNING, "WARNING")
68 self.logger.log(Level.ERROR, "ERROR")
70 self.assertEqual(2, output_mock.call_count)
71 logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
72 self.assertEqual(logs[0]["msg"], "WARNING")
73 self.assertEqual(logs[1]["msg"], "ERROR")
75 @patch('mdclogpy.Logger._output_log')
76 def test_that_logs_with_lower_than_current_level_are_not_logged(self, output_mock):
78 self.logger.set_level(Level.WARNING)
79 self.logger.log(Level.DEBUG, "DEBUG")
80 self.logger.log(Level.INFO, "INFO")
81 self.logger.log(Level.WARNING, "WARNING")
82 self.logger.log(Level.ERROR, "ERROR")
84 self.assertEqual(2, output_mock.call_count)
85 logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
86 self.assertEqual(logs[0]["msg"], "WARNING")
87 self.assertEqual(logs[1]["msg"], "ERROR")
89 @patch('mdclogpy.Logger._output_log')
90 def test_that_log_contains_correct_criticality(self, output_mock):
92 self.logger.set_level(Level.DEBUG)
94 self.logger.log(Level.DEBUG, "debug test log")
95 self.logger.log(Level.INFO, "info test log")
96 self.logger.log(Level.WARNING, "warning test log")
97 self.logger.log(Level.ERROR, "error test log")
99 self.logger.debug("another debug test log")
100 self.logger.info("another info test log")
101 self.logger.warning("another warning test log")
102 self.logger.error("another error test log")
104 self.assertEqual(8, output_mock.call_count)
105 logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
106 self.assertEqual(logs[0]["crit"], "DEBUG")
107 self.assertEqual(logs[1]["crit"], "INFO")
108 self.assertEqual(logs[2]["crit"], "WARNING")
109 self.assertEqual(logs[3]["crit"], "ERROR")
110 self.assertEqual(logs[4]["crit"], "DEBUG")
111 self.assertEqual(logs[5]["crit"], "INFO")
112 self.assertEqual(logs[6]["crit"], "WARNING")
113 self.assertEqual(logs[7]["crit"], "ERROR")
116 @patch('mdclogpy.Logger._output_log')
117 def test_that_log_contains_correct_timestamp(self, output_mock, mock_time):
119 mock_time.return_value = 1554806251.4388545
120 self.logger.error("timestamp test")
122 logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
123 self.assertEqual(logs[0]["ts"], 1554806251439)
125 @patch('mdclogpy.Logger._output_log')
126 def test_that_log_contains_correct_message(self, output_mock):
128 self.logger.error("message test")
129 logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
131 self.assertEqual(logs[0]["msg"], "message test")
133 @patch('mdclogpy.Logger._output_log')
134 def test_that_log_message_is_escaped_to_valid_json_string(self, output_mock):
136 self.logger.set_level(Level.DEBUG)
138 self.logger.info('\ and "')
140 logs = TestMdcLogUtils.get_logs(output_mock.call_args_list)
141 self.assertTrue(r'\\ and \"' in logs[0])
142 logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
143 self.assertEqual(logs[0]["msg"], '\ and "')
146 @patch('mdclogpy.Logger._output_log')
147 def test_that_empty_mdc_is_logged_correctly(self, output_mock):
148 self.logger.mdclog_format_init(configmap_monitor=True)
149 self.logger.error("empty mdc test")
150 logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
151 self.assertEqual(logs[0]["mdc"], {'SYSTEM_NAME': '', 'HOST_NAME': '', 'SERVICE_NAME': '', 'CONTAINER_NAME': '', 'POD_NAME': ''})
155 @patch('mdclogpy.Logger._output_log')
156 def test_that_mdc_values_are_logged_correctly(self, output_mock):
158 self.logger.add_mdc("key1", "value1")
159 self.logger.add_mdc("key2", "value2")
160 self.logger.error("mdc test")
162 logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
163 self.assertEqual(logs[0]["mdc"]["key1"], "value1")
164 self.assertEqual(logs[0]["mdc"]["key2"], "value2")
166 @patch('mdclogpy.Logger._output_log')
167 def test_log_level_change_from_configmap_file(self,output_mock):
168 self.logger.mdclog_format_init(configmap_monitor=True)
169 self.logger.filename= '/tmp/log-level'
170 self.logger.dirname='/tmp'
171 self.logger.register_log_change_notify()
172 self.assertEqual(mdclogpy.get_level(), mdclogpy.Level.ERROR)
173 src = open('/tmp/log-level','w')
174 src.write('log-level: debug')
178 self.assertEqual(self.logger.get_level(), 10)
181 def test_that_mdc_values_can_be_added_and_removed(self):
183 self.logger.add_mdc("key1", "value1")
184 self.logger.add_mdc("key2", "value2")
185 self.assertEqual(self.logger.get_mdc("key2"), "value2")
186 self.assertEqual(self.logger.get_mdc("key1"), "value1")
187 self.assertEqual(self.logger.get_mdc("non_existent"), None)
188 self.logger.remove_mdc("key1")
189 self.assertEqual(self.logger.get_mdc("key1"), None)
190 self.logger.remove_mdc("non_existent")
191 self.logger.clean_mdc()
192 self.assertEqual(self.logger.get_mdc("key2"), None)
194 @patch('mdclogpy.Logger._output_log')
195 def test_multiple_logger_instances(self, output_mock):
197 logger1 = Logger("logger1")
198 logger2 = Logger("logger2")
199 logger1.add_mdc("logger1_key1", "logger1_value1")
200 logger1.add_mdc("logger1_key2", "logger1_value2")
201 logger2.add_mdc("logger2_key1", "logger2_value1")
202 logger2.add_mdc("logger2_key2", "logger2_value2")
203 mdclogpy.add_mdc("key", "value")
205 logger1.error("error msg")
206 logger2.error("warning msg")
207 mdclogpy.error("info msg")
209 logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
210 self.assertEqual(3, output_mock.call_count)
212 self.assertEqual(logs[0]["id"], "logger1")
213 self.assertEqual(logs[0]["crit"], "ERROR")
214 self.assertEqual(logs[0]["msg"], "error msg")
215 self.assertEqual(logs[0]["mdc"]["logger1_key1"], "logger1_value1")
216 self.assertEqual(logs[0]["mdc"]["logger1_key2"], "logger1_value2")
217 self.assertEqual(len(logs[0]["mdc"]), 2)
219 self.assertEqual(logs[1]["id"], "logger2")
220 self.assertEqual(logs[1]["crit"], "ERROR")
221 self.assertEqual(logs[1]["msg"], "warning msg")
222 self.assertEqual(logs[1]["mdc"]["logger2_key1"], "logger2_value1")
223 self.assertEqual(logs[1]["mdc"]["logger2_key2"], "logger2_value2")
224 self.assertEqual(len(logs[1]["mdc"]), 2)
226 self.assertEqual(logs[2]["id"], sys.argv[0])
227 self.assertEqual(logs[2]["crit"], "ERROR")
228 self.assertEqual(logs[2]["msg"], "info msg")
229 self.assertEqual(logs[2]["mdc"]["key"], "value")
230 self.assertEqual(len(logs[2]["mdc"]), 1)
232 if __name__ == '__main__':