1#!/usr/bin/env vpython3 2# Copyright 2022 The Chromium Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5"""File for testing log_manager.py.""" 6 7import sys 8import unittest 9import unittest.mock as mock 10 11import log_manager 12 13_LOGS_DIR = 'test_logs_dir' 14 15 16class LogManagerTest(unittest.TestCase): 17 """Unittests for log_manager.py.""" 18 19 @mock.patch('log_manager.run_continuous_ffx_command') 20 def test_no_logs(self, mock_ffx) -> None: 21 """Test |start_system_log| does nothing when logging is off.""" 22 23 log = log_manager.LogManager(None) 24 log_manager.start_system_log(log, False) 25 self.assertEqual(mock_ffx.call_count, 0) 26 27 @mock.patch('log_manager.run_continuous_ffx_command') 28 def test_log_to_stdout(self, mock_ffx) -> None: 29 """Test |start_system_log| logs to stdout when log manager is off.""" 30 31 log = log_manager.LogManager(None) 32 log_manager.start_system_log(log, True) 33 self.assertEqual(mock_ffx.call_args_list[0][1]['stdout'], sys.stdout) 34 self.assertEqual(mock_ffx.call_count, 1) 35 36 @mock.patch('log_manager.run_continuous_ffx_command') 37 @mock.patch('builtins.open') 38 def test_log_to_file(self, mock_open, mock_ffx) -> None: 39 """Test |start_system_log| logs to log file when log manager is on.""" 40 41 log = log_manager.LogManager(_LOGS_DIR) 42 log_manager.start_system_log(log, False) 43 self.assertEqual(mock_ffx.call_args_list[0][1]['stdout'], 44 mock_open.return_value) 45 self.assertEqual(mock_ffx.call_count, 1) 46 47 @mock.patch('log_manager.run_continuous_ffx_command') 48 def test_log_with_log_args(self, mock_ffx) -> None: 49 """Test log args are used when passed in to |start_system_log|.""" 50 51 log = log_manager.LogManager(None) 52 log_manager.start_system_log(log, True, log_args=['test_log_args']) 53 self.assertEqual(mock_ffx.call_args_list[0][0][0], 54 ['log', '--raw', '--no-color', 'test_log_args']) 55 self.assertEqual(mock_ffx.call_count, 1) 56 57 @mock.patch('log_manager.run_continuous_ffx_command') 58 def test_log_with_symbols(self, mock_ffx) -> None: 59 """Test symbols are used when pkg_paths are set.""" 60 61 log = log_manager.LogManager(_LOGS_DIR) 62 with mock.patch('os.path.isfile', return_value=True), \ 63 mock.patch('builtins.open'), \ 64 mock.patch('log_manager.run_symbolizer'): 65 log_manager.start_system_log(log, False, pkg_paths=['test_pkg']) 66 log.stop() 67 self.assertEqual(mock_ffx.call_count, 1) 68 self.assertEqual(mock_ffx.call_args_list[0][0][0], 69 ['log', '--raw', '--no-color']) 70 71 def test_no_logging_dir_exception(self) -> None: 72 """Tests empty LogManager throws an exception on |open_log_file|.""" 73 74 log = log_manager.LogManager(None) 75 with self.assertRaises(Exception): 76 log.open_log_file('test_log_file') 77 78 @mock.patch('log_manager.ScopedFfxConfig') 79 @mock.patch('log_manager.stop_ffx_daemon') 80 def test_log_manager(self, mock_stop_ffx_daemon, 81 mock_scoped_config) -> None: 82 """Tests LogManager as a context manager.""" 83 84 with mock.patch('log_manager.running_unattended', return_value=False): 85 context_mock = mock.Mock() 86 mock_scoped_config.return_value = context_mock 87 context_mock.__enter__ = mock.Mock(return_value=None) 88 context_mock.__exit__ = mock.Mock(return_value=None) 89 with log_manager.LogManager(_LOGS_DIR): 90 pass 91 self.assertEqual(mock_scoped_config.call_count, 1) 92 self.assertEqual(context_mock.__enter__.call_count, 1) 93 self.assertEqual(context_mock.__exit__.call_count, 1) 94 self.assertEqual(mock_stop_ffx_daemon.call_count, 2) 95 96 @mock.patch('log_manager.ScopedFfxConfig') 97 @mock.patch('log_manager.stop_ffx_daemon') 98 def test_log_manager_unattended_no_daemon_stop(self, mock_stop_ffx_daemon, 99 mock_scoped_config) -> None: 100 """Tests LogManager as a context manager in unattended mode.""" 101 102 with mock.patch('log_manager.running_unattended', return_value=True): 103 context_mock = mock.Mock() 104 mock_scoped_config.return_value = context_mock 105 context_mock.__enter__ = mock.Mock(return_value=None) 106 context_mock.__exit__ = mock.Mock(return_value=None) 107 with log_manager.LogManager(_LOGS_DIR): 108 pass 109 self.assertEqual(mock_scoped_config.call_count, 1) 110 self.assertEqual(context_mock.__enter__.call_count, 1) 111 self.assertEqual(context_mock.__exit__.call_count, 1) 112 self.assertEqual(mock_stop_ffx_daemon.call_count, 0) 113 114 def test_main_exception(self) -> None: 115 """Tests |main| function to throw exception on incompatible flags.""" 116 117 with mock.patch('sys.argv', 118 ['log_manager.py', '--packages', 'test_package']): 119 with self.assertRaises(ValueError): 120 log_manager.main() 121 122 @mock.patch('log_manager.read_package_paths') 123 @mock.patch('log_manager.start_system_log') 124 def test_main(self, mock_system_log, mock_read_paths) -> None: 125 """Tests |main| function.""" 126 127 with mock.patch('sys.argv', [ 128 'log_manager.py', '--packages', 'test_package', '--out-dir', 129 'test_out_dir' 130 ]): 131 with mock.patch('common.time.sleep', 132 side_effect=KeyboardInterrupt): 133 log_manager.main() 134 self.assertEqual(mock_system_log.call_count, 1) 135 self.assertEqual(mock_read_paths.call_count, 1) 136 137 138if __name__ == '__main__': 139 unittest.main() 140