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):
62 mdclogpy.set_level(mdclogpy.Level.INFO)
63 self.assertEqual(mdclogpy.get_level(), mdclogpy.Level.INFO)
64 mdclogpy.set_level(mdclogpy.Level.WARNING)
65 self.assertEqual(mdclogpy.get_level(), mdclogpy.Level.WARNING)
66 mdclogpy.set_level(mdclogpy.Level.ERROR)
67 self.assertEqual(mdclogpy.get_level(), mdclogpy.Level.ERROR)
68 mdclogpy.set_level(mdclogpy.Level.DEBUG)
69 self.assertEqual(mdclogpy.get_level(), mdclogpy.Level.DEBUG)
71 @patch('mdclogpy.Logger._output_log')
72 def test_that_root_logger_logs_with_correct_criticality(self, output_mock):
74 mdclogpy.set_level(mdclogpy.Level.DEBUG)
76 mdclogpy.log(mdclogpy.Level.DEBUG, "debug test log")
77 mdclogpy.log(mdclogpy.Level.INFO, "info test log")
78 mdclogpy.log(mdclogpy.Level.WARNING, "warning test log")
79 mdclogpy.log(mdclogpy.Level.ERROR, "error test log")
81 mdclogpy.debug("another debug test log")
82 mdclogpy.info("another info test log")
83 mdclogpy.warning("another warning test log")
84 mdclogpy.error("another error test log")
86 logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
87 self.assertEqual(8, output_mock.call_count)
88 self.assertEqual(logs[0]["crit"], "DEBUG")
89 self.assertEqual(logs[1]["crit"], "INFO")
90 self.assertEqual(logs[2]["crit"], "WARNING")
91 self.assertEqual(logs[3]["crit"], "ERROR")
92 self.assertEqual(logs[4]["crit"], "DEBUG")
93 self.assertEqual(logs[5]["crit"], "INFO")
94 self.assertEqual(logs[6]["crit"], "WARNING")
95 self.assertEqual(logs[7]["crit"], "ERROR")
97 @patch('mdclogpy.Logger._output_log')
98 def test_that_root_logger_logs_mdc_values_correctly(self, output_mock):
100 mdclogpy.add_mdc("key1", "value1")
101 mdclogpy.add_mdc("key2", "value2")
102 mdclogpy.error("mdc test")
104 logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
105 self.assertEqual(logs[0]["mdc"]["key1"], "value1")
106 self.assertEqual(logs[0]["mdc"]["key2"], "value2")
108 @patch('mdclogpy.Logger._output_log')
109 def test_that_non_printable_characters_are_logged_correctly(self, output_mock):
111 mdclogpy.set_level(mdclogpy.Level.DEBUG)
112 mdclogpy.info("line feed\ntest")
113 mdclogpy.info("tab\ttest")
114 mdclogpy.info("carriage return\rtest")
115 logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
116 self.assertEqual(logs[0]["msg"], "line feed\ntest")
117 self.assertEqual(logs[1]["msg"], "tab\ttest")
118 self.assertEqual(logs[2]["msg"], "carriage return\rtest")
120 if __name__ == '__main__':