log_manager_unittests.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env vpython3
  2. # Copyright 2022 The Chromium Authors. All rights reserved.
  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. import sys
  7. import unittest
  8. import unittest.mock as mock
  9. import log_manager
  10. _LOGS_DIR = 'test_logs_dir'
  11. class LogManagerTest(unittest.TestCase):
  12. """Unittests for log_manager.py."""
  13. @mock.patch('log_manager.run_continuous_ffx_command')
  14. def test_no_logs(self, mock_ffx) -> None:
  15. """Test |start_system_log| does nothing when logging is off."""
  16. log = log_manager.LogManager(None)
  17. log_manager.start_system_log(log, False)
  18. self.assertEqual(mock_ffx.call_count, 0)
  19. @mock.patch('log_manager.run_continuous_ffx_command')
  20. def test_log_to_stdout(self, mock_ffx) -> None:
  21. """Test |start_system_log| logs to stdout when log manager is off."""
  22. log = log_manager.LogManager(None)
  23. log_manager.start_system_log(log, True)
  24. self.assertEqual(mock_ffx.call_args_list[0][1]['stdout'], sys.stdout)
  25. self.assertEqual(mock_ffx.call_count, 1)
  26. @mock.patch('log_manager.run_continuous_ffx_command')
  27. @mock.patch('builtins.open')
  28. def test_log_to_file(self, mock_open, mock_ffx) -> None:
  29. """Test |start_system_log| logs to log file when log manager is on."""
  30. log = log_manager.LogManager(_LOGS_DIR)
  31. log_manager.start_system_log(log, False)
  32. self.assertEqual(mock_ffx.call_args_list[0][1]['stdout'],
  33. mock_open.return_value)
  34. self.assertEqual(mock_ffx.call_count, 1)
  35. @mock.patch('log_manager.run_continuous_ffx_command')
  36. def test_log_with_log_args(self, mock_ffx) -> None:
  37. """Test log args are used when passed in to |start_system_log|."""
  38. log = log_manager.LogManager(None)
  39. log_manager.start_system_log(log, True, log_args=['test_log_args'])
  40. self.assertEqual(mock_ffx.call_args_list[0][0][0],
  41. ['log', '--no-symbols', 'test_log_args'])
  42. self.assertEqual(mock_ffx.call_count, 1)
  43. @mock.patch('log_manager.run_continuous_ffx_command')
  44. def test_log_with_symbols(self, mock_ffx) -> None:
  45. """Test symbols are used when pkg_paths are set."""
  46. log = log_manager.LogManager(_LOGS_DIR)
  47. with mock.patch('os.path.isfile', return_value=True):
  48. with mock.patch('builtins.open'):
  49. log_manager.start_system_log(log,
  50. False,
  51. pkg_paths=['test_pkg'])
  52. log.stop()
  53. self.assertEqual(mock_ffx.call_args_list[0][0][0],
  54. ['log', '--no-symbols'])
  55. self.assertEqual(mock_ffx.call_args_list[1][0][0], [
  56. 'debug', 'symbolize', '--', '--omit-module-lines', '--ids-txt',
  57. 'ids.txt'
  58. ])
  59. self.assertEqual(mock_ffx.call_count, 2)
  60. def test_no_logging_dir_exception(self) -> None:
  61. """Tests empty LogManager throws an exception on |open_log_file|."""
  62. log = log_manager.LogManager(None)
  63. with self.assertRaises(Exception):
  64. log.open_log_file('test_log_file')
  65. @mock.patch('log_manager.ScopedFfxConfig')
  66. @mock.patch('log_manager.run_ffx_command')
  67. def test_log_manager(self, mock_ffx, mock_scoped_config) -> None:
  68. """Tests LogManager as a context manager."""
  69. context_mock = mock.Mock()
  70. mock_scoped_config.return_value = context_mock
  71. context_mock.__enter__ = mock.Mock(return_value=None)
  72. context_mock.__exit__ = mock.Mock(return_value=None)
  73. with log_manager.LogManager(_LOGS_DIR):
  74. pass
  75. self.assertEqual(mock_ffx.call_count, 2)
  76. def test_main_exception(self) -> None:
  77. """Tests |main| function to throw exception on incompatible flags."""
  78. with mock.patch('sys.argv',
  79. ['log_manager.py', '--packages', 'test_package']):
  80. with self.assertRaises(ValueError):
  81. log_manager.main()
  82. @mock.patch('log_manager.read_package_paths')
  83. @mock.patch('log_manager.start_system_log')
  84. def test_main(self, mock_system_log, mock_read_paths) -> None:
  85. """Tests |main| function."""
  86. with mock.patch('sys.argv', [
  87. 'log_manager.py', '--packages', 'test_package', '--out-dir',
  88. 'test_out_dir'
  89. ]):
  90. with mock.patch('log_manager.time.sleep',
  91. side_effect=KeyboardInterrupt):
  92. log_manager.main()
  93. self.assertEqual(mock_system_log.call_count, 1)
  94. self.assertEqual(mock_read_paths.call_count, 1)
  95. if __name__ == '__main__':
  96. unittest.main()