test_logger.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic import logger as module
  5. @pytest.mark.parametrize('bool_val', (True, 'yes', 'on', '1', 'true', 'True', 1))
  6. def test_to_bool_parses_true_values(bool_val):
  7. assert module.to_bool(bool_val)
  8. @pytest.mark.parametrize('bool_val', (False, 'no', 'off', '0', 'false', 'False', 0))
  9. def test_to_bool_parses_false_values(bool_val):
  10. assert not module.to_bool(bool_val)
  11. def test_to_bool_passes_none_through():
  12. assert module.to_bool(None) is None
  13. def test_interactive_console_false_when_not_isatty(capsys):
  14. with capsys.disabled():
  15. flexmock(module.sys.stderr).should_receive('isatty').and_return(False)
  16. assert module.interactive_console() is False
  17. def test_interactive_console_false_when_TERM_is_dumb(capsys):
  18. with capsys.disabled():
  19. flexmock(module.sys.stderr).should_receive('isatty').and_return(True)
  20. flexmock(module.os.environ).should_receive('get').with_args('TERM').and_return('dumb')
  21. assert module.interactive_console() is False
  22. def test_interactive_console_true_when_isatty_and_TERM_is_not_dumb(capsys):
  23. with capsys.disabled():
  24. flexmock(module.sys.stderr).should_receive('isatty').and_return(True)
  25. flexmock(module.os.environ).should_receive('get').with_args('TERM').and_return('smart')
  26. assert module.interactive_console() is True
  27. def test_should_do_markup_respects_no_color_value():
  28. assert module.should_do_markup(no_color=True, configs={}) is False
  29. def test_should_do_markup_respects_config_value():
  30. assert (
  31. module.should_do_markup(no_color=False, configs={'foo.yaml': {'output': {'color': False}}})
  32. is False
  33. )
  34. def test_should_do_markup_prefers_any_false_config_value():
  35. assert (
  36. module.should_do_markup(
  37. no_color=False,
  38. configs={
  39. 'foo.yaml': {'output': {'color': True}},
  40. 'bar.yaml': {'output': {'color': False}},
  41. },
  42. )
  43. is False
  44. )
  45. def test_should_do_markup_respects_PY_COLORS_environment_variable():
  46. flexmock(module.os.environ).should_receive('get').and_return('True')
  47. flexmock(module).should_receive('to_bool').and_return(True)
  48. assert module.should_do_markup(no_color=False, configs={}) is True
  49. def test_should_do_markup_prefers_no_color_value_to_config_value():
  50. assert (
  51. module.should_do_markup(no_color=True, configs={'foo.yaml': {'output': {'color': True}}})
  52. is False
  53. )
  54. def test_should_do_markup_prefers_config_value_to_PY_COLORS():
  55. flexmock(module.os.environ).should_receive('get').and_return('True')
  56. flexmock(module).should_receive('to_bool').and_return(True)
  57. assert (
  58. module.should_do_markup(no_color=False, configs={'foo.yaml': {'output': {'color': False}}})
  59. is False
  60. )
  61. def test_should_do_markup_prefers_no_color_value_to_PY_COLORS():
  62. flexmock(module.os.environ).should_receive('get').and_return('True')
  63. flexmock(module).should_receive('to_bool').and_return(True)
  64. assert module.should_do_markup(no_color=True, configs={}) is False
  65. def test_should_do_markup_respects_interactive_console_value():
  66. flexmock(module.os.environ).should_receive('get').and_return(None)
  67. flexmock(module).should_receive('interactive_console').and_return(True)
  68. assert module.should_do_markup(no_color=False, configs={}) is True
  69. def test_should_do_markup_prefers_PY_COLORS_to_interactive_console_value():
  70. flexmock(module.os.environ).should_receive('get').and_return('True')
  71. flexmock(module).should_receive('to_bool').and_return(True)
  72. flexmock(module).should_receive('interactive_console').and_return(False)
  73. assert module.should_do_markup(no_color=False, configs={}) is True
  74. def test_multi_stream_handler_logs_to_handler_for_log_level():
  75. error_handler = flexmock()
  76. error_handler.should_receive('emit').once()
  77. info_handler = flexmock()
  78. multi_handler = module.Multi_stream_handler(
  79. {module.logging.ERROR: error_handler, module.logging.INFO: info_handler}
  80. )
  81. multi_handler.emit(flexmock(levelno=module.logging.ERROR))
  82. def test_console_color_formatter_format_includes_log_message():
  83. plain_message = 'uh oh'
  84. record = flexmock(levelno=logging.CRITICAL, msg=plain_message)
  85. colored_message = module.Console_color_formatter().format(record)
  86. assert colored_message != plain_message
  87. assert plain_message in colored_message
  88. def test_color_text_does_not_raise():
  89. module.color_text(module.colorama.Fore.RED, 'hi')
  90. def test_color_text_without_color_does_not_raise():
  91. module.color_text(None, 'hi')
  92. def test_configure_logging_probes_for_log_socket_on_linux():
  93. flexmock(module).should_receive('Multi_stream_handler').and_return(
  94. flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
  95. )
  96. flexmock(module).should_receive('Console_color_formatter')
  97. flexmock(module).should_receive('interactive_console').and_return(False)
  98. flexmock(module.logging).should_receive('basicConfig').with_args(
  99. level=logging.INFO, handlers=tuple
  100. )
  101. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  102. syslog_handler = logging.handlers.SysLogHandler()
  103. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  104. address='/dev/log'
  105. ).and_return(syslog_handler).once()
  106. module.configure_logging(logging.INFO)
  107. def test_configure_logging_probes_for_log_socket_on_macos():
  108. flexmock(module).should_receive('Multi_stream_handler').and_return(
  109. flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
  110. )
  111. flexmock(module).should_receive('Console_color_formatter')
  112. flexmock(module).should_receive('interactive_console').and_return(False)
  113. flexmock(module.logging).should_receive('basicConfig').with_args(
  114. level=logging.INFO, handlers=tuple
  115. )
  116. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
  117. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(True)
  118. syslog_handler = logging.handlers.SysLogHandler()
  119. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  120. address='/var/run/syslog'
  121. ).and_return(syslog_handler).once()
  122. module.configure_logging(logging.INFO)
  123. def test_configure_logging_sets_global_logger_to_most_verbose_log_level():
  124. flexmock(module).should_receive('Multi_stream_handler').and_return(
  125. flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
  126. )
  127. flexmock(module).should_receive('Console_color_formatter')
  128. flexmock(module.logging).should_receive('basicConfig').with_args(
  129. level=logging.DEBUG, handlers=tuple
  130. ).once()
  131. flexmock(module.os.path).should_receive('exists').and_return(False)
  132. module.configure_logging(console_log_level=logging.INFO, syslog_log_level=logging.DEBUG)
  133. def test_configure_logging_skips_syslog_if_not_found():
  134. flexmock(module).should_receive('Multi_stream_handler').and_return(
  135. flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
  136. )
  137. flexmock(module).should_receive('Console_color_formatter')
  138. flexmock(module.logging).should_receive('basicConfig').with_args(
  139. level=logging.INFO, handlers=tuple
  140. )
  141. flexmock(module.os.path).should_receive('exists').and_return(False)
  142. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  143. module.configure_logging(console_log_level=logging.INFO)
  144. def test_configure_logging_skips_syslog_if_interactive_console():
  145. flexmock(module).should_receive('Multi_stream_handler').and_return(
  146. flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
  147. )
  148. flexmock(module).should_receive('Console_color_formatter')
  149. flexmock(module).should_receive('interactive_console').and_return(True)
  150. flexmock(module.logging).should_receive('basicConfig').with_args(
  151. level=logging.INFO, handlers=tuple
  152. )
  153. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  154. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  155. module.configure_logging(console_log_level=logging.INFO)
  156. def test_configure_logging_to_logfile_instead_of_syslog():
  157. flexmock(module).should_receive('Multi_stream_handler').and_return(
  158. flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
  159. )
  160. # syslog skipped in non-interactive console if --log-file argument provided
  161. flexmock(module).should_receive('interactive_console').and_return(False)
  162. flexmock(module.logging).should_receive('basicConfig').with_args(
  163. level=logging.DEBUG, handlers=tuple
  164. )
  165. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  166. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  167. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  168. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  169. '/tmp/logfile'
  170. ).and_return(file_handler).once()
  171. module.configure_logging(
  172. console_log_level=logging.INFO, log_file_log_level=logging.DEBUG, log_file='/tmp/logfile'
  173. )
  174. def test_configure_logging_skips_logfile_if_argument_is_none():
  175. flexmock(module).should_receive('Multi_stream_handler').and_return(
  176. flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
  177. )
  178. # No WatchedFileHandler added if argument --log-file is None
  179. flexmock(module).should_receive('interactive_console').and_return(False)
  180. flexmock(module.logging).should_receive('basicConfig').with_args(
  181. level=logging.INFO, handlers=tuple
  182. )
  183. flexmock(module.os.path).should_receive('exists').and_return(False)
  184. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  185. module.configure_logging(console_log_level=logging.INFO, log_file=None)