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 mdclogpy root logger"""
21 from unittest.mock import patch
25 from .mdclogtestutils import TestMdcLogUtils
28 class TestMdcLog(unittest.TestCase):
29 """Unit tests for mdclog.py"""
32 self.prog_id = sys.argv[0]
38 @patch('mdclogpy.Logger._output_log')
39 def test_that_root_logger_logs_the_message_using_the_proc_name(self, output_mock):
41 mdclogpy.log(mdclogpy.Level.DEBUG, "This is a test log")
42 mdclogpy.error("This is an error log")
43 mdclogpy.warning("This is a warning log")
44 mdclogpy.info("This is an info log")
45 mdclogpy.debug("This is a debug log")
47 logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
48 self.assertEqual(self.prog_id, logs[0]["id"])
49 self.assertEqual(self.prog_id, logs[1]["id"])
50 self.assertEqual(self.prog_id, logs[2]["id"])
51 self.assertEqual(self.prog_id, logs[3]["id"])
52 self.assertEqual(self.prog_id, logs[4]["id"])
53 self.assertEqual("This is a test log", logs[0]["msg"])
54 self.assertEqual("This is an error log", logs[1]["msg"])
55 self.assertEqual("This is a warning log", logs[2]["msg"])
56 self.assertEqual("This is an info log", logs[3]["msg"])
57 self.assertEqual("This is a debug log", logs[4]["msg"])
59 def test_that_root_logger_get_level_returns_the_current_log_level(self):
61 # default level is DEBUG
62 self.assertEqual(mdclogpy.get_level(), mdclogpy.Level.DEBUG)
63 mdclogpy.set_level(mdclogpy.Level.INFO)
64 self.assertEqual(mdclogpy.get_level(), mdclogpy.Level.INFO)
65 mdclogpy.set_level(mdclogpy.Level.WARNING)
66 self.assertEqual(mdclogpy.get_level(), mdclogpy.Level.WARNING)
67 mdclogpy.set_level(mdclogpy.Level.ERROR)
68 self.assertEqual(mdclogpy.get_level(), mdclogpy.Level.ERROR)
69 mdclogpy.set_level(mdclogpy.Level.DEBUG)
70 self.assertEqual(mdclogpy.get_level(), mdclogpy.Level.DEBUG)
72 @patch('mdclogpy.Logger._output_log')
73 def test_that_root_logger_logs_with_correct_criticality(self, output_mock):
75 mdclogpy.set_level(mdclogpy.Level.DEBUG)
77 mdclogpy.log(mdclogpy.Level.DEBUG, "debug test log")
78 mdclogpy.log(mdclogpy.Level.INFO, "info test log")
79 mdclogpy.log(mdclogpy.Level.WARNING, "warning test log")
80 mdclogpy.log(mdclogpy.Level.ERROR, "error test log")
82 mdclogpy.debug("another debug test log")
83 mdclogpy.info("another info test log")
84 mdclogpy.warning("another warning test log")
85 mdclogpy.error("another error test log")
87 logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
88 self.assertEqual(8, output_mock.call_count)
89 self.assertEqual(logs[0]["crit"], "DEBUG")
90 self.assertEqual(logs[1]["crit"], "INFO")
91 self.assertEqual(logs[2]["crit"], "WARNING")
92 self.assertEqual(logs[3]["crit"], "ERROR")
93 self.assertEqual(logs[4]["crit"], "DEBUG")
94 self.assertEqual(logs[5]["crit"], "INFO")
95 self.assertEqual(logs[6]["crit"], "WARNING")
96 self.assertEqual(logs[7]["crit"], "ERROR")
98 @patch('mdclogpy.Logger._output_log')
99 def test_that_root_logger_logs_mdc_values_correctly(self, output_mock):
101 mdclogpy.add_mdc("key1", "value1")
102 mdclogpy.add_mdc("key2", "value2")
103 mdclogpy.error("mdc test")
105 logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
106 self.assertEqual(logs[0]["mdc"]["key1"], "value1")
107 self.assertEqual(logs[0]["mdc"]["key2"], "value2")
109 @patch('mdclogpy.Logger._output_log')
110 def test_that_non_printable_characters_are_logged_correctly(self, output_mock):
112 mdclogpy.set_level(mdclogpy.Level.DEBUG)
113 mdclogpy.info("line feed\ntest")
114 mdclogpy.info("tab\ttest")
115 mdclogpy.info("carriage return\rtest")
116 logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
117 self.assertEqual(logs[0]["msg"], "line feed\ntest")
118 self.assertEqual(logs[1]["msg"], "tab\ttest")
119 self.assertEqual(logs[2]["msg"], "carriage return\rtest")
121 if __name__ == '__main__':