1#!/usr/bin/python
2
3import cStringIO, logging, os, sys, unittest
4
5# direct imports; autotest_lib has not been setup while testing this.
6from common_lib.test_utils import mock
7import setup_modules
8
9
10class LoggingErrorStderrTests(unittest.TestCase):
11    def setUp(self):
12        autotest_dir = os.path.abspath(os.path.join(setup_modules.dirname,
13                                                    '..'))
14        setup_modules.setup(autotest_dir, root_module_name='autotest_lib')
15        self.god = mock.mock_god()
16        self.test_stderr = cStringIO.StringIO()
17        self.god.stub_with(sys, 'stderr', self.test_stderr)
18        self.old_root_logging_level = logging.root.level
19        logging.basicConfig(level=logging.ERROR)
20        # _autotest_logging_handle_error unsets this after being called once.
21        logging.raiseExceptions = 1
22
23
24    def tearDown(self):
25        self.god.unstub_all()
26        # Undo the setUp logging.basicConfig call.
27        logging.basicConfig(level=self.old_root_logging_level)
28
29
30    def assert_autotest_logging_handle_error_called(self):
31        self.stderr_str = self.test_stderr.getvalue()
32        self.assertTrue('Exception occurred formatting' in self.stderr_str,
33                        repr(self.stderr_str))
34
35
36    def test_autotest_logging_handle_error(self):
37        record = logging.LogRecord(
38                'test', logging.DEBUG, __file__, 0, 'MESSAGE', 'ARGS', None)
39        try:
40            raise RuntimeError('Exception context needed for the test.')
41        except RuntimeError:
42            setup_modules._autotest_logging_handle_error(logging.Handler(),
43                                                         record)
44        else:
45            self.fail()
46        self.assert_autotest_logging_handle_error_called()
47        stderr_repr = repr(self.stderr_str)
48        self.assertTrue(('MESSAGE' in self.stderr_str), stderr_repr)
49        self.assertTrue(('ARGS' in self.stderr_str), stderr_repr)
50        self.assertTrue(('Exception' in self.stderr_str), stderr_repr)
51        self.assertTrue(('setup_modules_unittest.py' in self.stderr_str),
52                        stderr_repr)
53        self.assertTrue(('disabled.\n' in self.stderr_str), stderr_repr)
54        # Make sure this was turned off by our handle_error.
55        self.assertFalse(logging.raiseExceptions)
56
57
58    def test_logging_monkey_patch_wrong_number_of_args(self):
59        logging.error('logging unittest %d %s', 32)
60        self.assert_autotest_logging_handle_error_called()
61        self.assertTrue('logging unittest' in self.stderr_str,
62                        repr(self.stderr_str))
63
64
65    def test_logging_monkey_patch_wrong_type_of_arg(self):
66        logging.error('logging unittest %d', 'eighteen')
67        self.assert_autotest_logging_handle_error_called()
68        self.assertTrue('logging unittest' in self.stderr_str,
69                        repr(self.stderr_str))
70
71
72    def test_logging_no_error(self):
73        logging.error('logging unittest.  %s %s', 'meep', 'meep!')
74        self.assertEqual('', self.test_stderr.getvalue())
75
76
77if __name__ == "__main__":
78    unittest.main()
79