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( 54 mock_ffx.call_args_list[0][0][0], 55 ['log', '--symbolize', 'off', '--no-color', 'test_log_args']) 56 self.assertEqual(mock_ffx.call_count, 1) 57 58 @mock.patch('log_manager.run_continuous_ffx_command') 59 def test_log_with_symbols(self, mock_ffx) -> None: 60 """Test symbols are used when pkg_paths are set.""" 61 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.LogManager(_LOGS_DIR) as log: 66 log_manager.start_system_log(log, False, pkg_paths=['test_pkg']) 67 self.assertEqual(mock_ffx.call_count, 1) 68 self.assertEqual(mock_ffx.call_args_list[0][0][0], 69 ['log', '--symbolize', 'off', '--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 79if __name__ == '__main__': 80 unittest.main() 81