2
0

test_logger.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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_probes_for_log_socket_on_freebsd():
  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).should_receive('interactive_console').and_return(False)
  129. flexmock(module.logging).should_receive('basicConfig').with_args(
  130. level=logging.INFO, handlers=tuple
  131. )
  132. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
  133. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(False)
  134. flexmock(module.os.path).should_receive('exists').with_args('/var/run/log').and_return(True)
  135. syslog_handler = logging.handlers.SysLogHandler()
  136. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  137. address='/var/run/log'
  138. ).and_return(syslog_handler).once()
  139. module.configure_logging(logging.INFO)
  140. def test_configure_logging_sets_global_logger_to_most_verbose_log_level():
  141. flexmock(module).should_receive('Multi_stream_handler').and_return(
  142. flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
  143. )
  144. flexmock(module).should_receive('Console_color_formatter')
  145. flexmock(module.logging).should_receive('basicConfig').with_args(
  146. level=logging.DEBUG, handlers=tuple
  147. ).once()
  148. flexmock(module.os.path).should_receive('exists').and_return(False)
  149. module.configure_logging(console_log_level=logging.INFO, syslog_log_level=logging.DEBUG)
  150. def test_configure_logging_skips_syslog_if_not_found():
  151. flexmock(module).should_receive('Multi_stream_handler').and_return(
  152. flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
  153. )
  154. flexmock(module).should_receive('Console_color_formatter')
  155. flexmock(module.logging).should_receive('basicConfig').with_args(
  156. level=logging.INFO, handlers=tuple
  157. )
  158. flexmock(module.os.path).should_receive('exists').and_return(False)
  159. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  160. module.configure_logging(console_log_level=logging.INFO)
  161. def test_configure_logging_skips_syslog_if_interactive_console():
  162. flexmock(module).should_receive('Multi_stream_handler').and_return(
  163. flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
  164. )
  165. flexmock(module).should_receive('Console_color_formatter')
  166. flexmock(module).should_receive('interactive_console').and_return(True)
  167. flexmock(module.logging).should_receive('basicConfig').with_args(
  168. level=logging.INFO, handlers=tuple
  169. )
  170. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  171. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  172. module.configure_logging(console_log_level=logging.INFO)
  173. def test_configure_logging_to_logfile_instead_of_syslog():
  174. flexmock(module).should_receive('Multi_stream_handler').and_return(
  175. flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
  176. )
  177. # syslog skipped in non-interactive console if --log-file argument provided
  178. flexmock(module).should_receive('interactive_console').and_return(False)
  179. flexmock(module.logging).should_receive('basicConfig').with_args(
  180. level=logging.DEBUG, handlers=tuple
  181. )
  182. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  183. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  184. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  185. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  186. '/tmp/logfile'
  187. ).and_return(file_handler).once()
  188. module.configure_logging(
  189. console_log_level=logging.INFO, log_file_log_level=logging.DEBUG, log_file='/tmp/logfile'
  190. )
  191. def test_configure_logging_skips_logfile_if_argument_is_none():
  192. flexmock(module).should_receive('Multi_stream_handler').and_return(
  193. flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
  194. )
  195. # No WatchedFileHandler added if argument --log-file is None
  196. flexmock(module).should_receive('interactive_console').and_return(False)
  197. flexmock(module.logging).should_receive('basicConfig').with_args(
  198. level=logging.INFO, handlers=tuple
  199. )
  200. flexmock(module.os.path).should_receive('exists').and_return(False)
  201. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  202. module.configure_logging(console_log_level=logging.INFO, log_file=None)