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