test_logger.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. import logging
  2. import sys
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic import logger as module
  6. @pytest.mark.parametrize('bool_val', (True, 'yes', 'on', '1', 'true', 'True', 1))
  7. def test_to_bool_parses_true_values(bool_val):
  8. assert module.to_bool(bool_val)
  9. @pytest.mark.parametrize('bool_val', (False, 'no', 'off', '0', 'false', 'False', 0))
  10. def test_to_bool_parses_false_values(bool_val):
  11. assert not module.to_bool(bool_val)
  12. def test_to_bool_passes_none_through():
  13. assert module.to_bool(None) is None
  14. def test_interactive_console_false_when_not_isatty(capsys):
  15. with capsys.disabled():
  16. flexmock(module.sys.stderr).should_receive('isatty').and_return(False)
  17. assert module.interactive_console() is False
  18. def test_interactive_console_false_when_TERM_is_dumb(capsys):
  19. with capsys.disabled():
  20. flexmock(module.sys.stderr).should_receive('isatty').and_return(True)
  21. flexmock(module.os.environ).should_receive('get').with_args('TERM').and_return('dumb')
  22. assert module.interactive_console() is False
  23. def test_interactive_console_true_when_isatty_and_TERM_is_not_dumb(capsys):
  24. with capsys.disabled():
  25. flexmock(module.sys.stderr).should_receive('isatty').and_return(True)
  26. flexmock(module.os.environ).should_receive('get').with_args('TERM').and_return('smart')
  27. assert module.interactive_console() is True
  28. def test_should_do_markup_respects_no_color_value():
  29. assert module.should_do_markup(no_color=True, configs={}) is False
  30. def test_should_do_markup_respects_config_value():
  31. assert (
  32. module.should_do_markup(no_color=False, configs={'foo.yaml': {'output': {'color': False}}})
  33. is False
  34. )
  35. def test_should_do_markup_prefers_any_false_config_value():
  36. assert (
  37. module.should_do_markup(
  38. no_color=False,
  39. configs={
  40. 'foo.yaml': {'output': {'color': True}},
  41. 'bar.yaml': {'output': {'color': False}},
  42. },
  43. )
  44. is False
  45. )
  46. def test_should_do_markup_respects_PY_COLORS_environment_variable():
  47. flexmock(module.os.environ).should_receive('get').and_return('True')
  48. flexmock(module).should_receive('to_bool').and_return(True)
  49. assert module.should_do_markup(no_color=False, configs={}) is True
  50. def test_should_do_markup_prefers_no_color_value_to_config_value():
  51. assert (
  52. module.should_do_markup(no_color=True, configs={'foo.yaml': {'output': {'color': True}}})
  53. is False
  54. )
  55. def test_should_do_markup_prefers_config_value_to_PY_COLORS():
  56. flexmock(module.os.environ).should_receive('get').and_return('True')
  57. flexmock(module).should_receive('to_bool').and_return(True)
  58. assert (
  59. module.should_do_markup(no_color=False, configs={'foo.yaml': {'output': {'color': False}}})
  60. is False
  61. )
  62. def test_should_do_markup_prefers_no_color_value_to_PY_COLORS():
  63. flexmock(module.os.environ).should_receive('get').and_return('True')
  64. flexmock(module).should_receive('to_bool').and_return(True)
  65. assert module.should_do_markup(no_color=True, configs={}) is False
  66. def test_should_do_markup_respects_interactive_console_value():
  67. flexmock(module.os.environ).should_receive('get').and_return(None)
  68. flexmock(module).should_receive('interactive_console').and_return(True)
  69. assert module.should_do_markup(no_color=False, configs={}) is True
  70. def test_should_do_markup_prefers_PY_COLORS_to_interactive_console_value():
  71. flexmock(module.os.environ).should_receive('get').and_return('True')
  72. flexmock(module).should_receive('to_bool').and_return(True)
  73. flexmock(module).should_receive('interactive_console').and_return(False)
  74. assert module.should_do_markup(no_color=False, configs={}) is True
  75. def test_multi_stream_handler_logs_to_handler_for_log_level():
  76. error_handler = flexmock()
  77. error_handler.should_receive('emit').once()
  78. info_handler = flexmock()
  79. multi_handler = module.Multi_stream_handler(
  80. {module.logging.ERROR: error_handler, module.logging.INFO: info_handler}
  81. )
  82. multi_handler.emit(flexmock(levelno=module.logging.ERROR))
  83. def test_console_color_formatter_format_includes_log_message():
  84. flexmock(module).should_receive('add_custom_log_levels')
  85. flexmock(module.logging).ANSWER = module.ANSWER
  86. plain_message = 'uh oh'
  87. record = flexmock(levelno=logging.CRITICAL, msg=plain_message)
  88. colored_message = module.Console_color_formatter().format(record)
  89. assert colored_message != plain_message
  90. assert plain_message in colored_message
  91. def test_color_text_does_not_raise():
  92. module.color_text(module.colorama.Fore.RED, 'hi')
  93. def test_color_text_without_color_does_not_raise():
  94. module.color_text(None, 'hi')
  95. def test_add_logging_level_adds_level_name_and_sets_global_attributes_and_methods():
  96. logger = flexmock()
  97. flexmock(module.logging).should_receive('getLoggerClass').and_return(logger)
  98. flexmock(module.logging).should_receive('addLevelName').with_args(99, 'PLAID')
  99. builtins = flexmock(sys.modules['builtins'])
  100. builtins.should_call('setattr')
  101. builtins.should_receive('setattr').with_args(module.logging, 'PLAID', 99).once()
  102. builtins.should_receive('setattr').with_args(logger, 'plaid', object).once()
  103. builtins.should_receive('setattr').with_args(logging, 'plaid', object).once()
  104. module.add_logging_level('PLAID', 99)
  105. def test_add_logging_level_skips_global_setting_if_already_set():
  106. logger = flexmock()
  107. flexmock(module.logging).should_receive('getLoggerClass').and_return(logger)
  108. flexmock(module.logging).PLAID = 99
  109. flexmock(logger).plaid = flexmock()
  110. flexmock(logging).plaid = flexmock()
  111. flexmock(module.logging).should_receive('addLevelName').never()
  112. builtins = flexmock(sys.modules['builtins'])
  113. builtins.should_call('setattr')
  114. builtins.should_receive('setattr').with_args(module.logging, 'PLAID', 99).never()
  115. builtins.should_receive('setattr').with_args(logger, 'plaid', object).never()
  116. builtins.should_receive('setattr').with_args(logging, 'plaid', object).never()
  117. module.add_logging_level('PLAID', 99)
  118. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_linux():
  119. flexmock(module).should_receive('add_custom_log_levels')
  120. flexmock(module.logging).ANSWER = module.ANSWER
  121. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  122. multi_stream_handler.should_receive('setFormatter').once()
  123. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  124. flexmock(module).should_receive('Console_color_formatter')
  125. flexmock(module).should_receive('interactive_console').and_return(False)
  126. flexmock(module.logging).should_receive('basicConfig').with_args(
  127. level=logging.DEBUG, handlers=list
  128. )
  129. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  130. syslog_handler = logging.handlers.SysLogHandler()
  131. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  132. address='/dev/log'
  133. ).and_return(syslog_handler).once()
  134. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  135. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_macos():
  136. flexmock(module).should_receive('add_custom_log_levels')
  137. flexmock(module.logging).ANSWER = module.ANSWER
  138. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  139. multi_stream_handler.should_receive('setFormatter').once()
  140. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  141. flexmock(module).should_receive('Console_color_formatter')
  142. flexmock(module).should_receive('interactive_console').and_return(False)
  143. flexmock(module.logging).should_receive('basicConfig').with_args(
  144. level=logging.DEBUG, handlers=list
  145. )
  146. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
  147. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(True)
  148. syslog_handler = logging.handlers.SysLogHandler()
  149. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  150. address='/var/run/syslog'
  151. ).and_return(syslog_handler).once()
  152. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  153. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_freebsd():
  154. flexmock(module).should_receive('add_custom_log_levels')
  155. flexmock(module.logging).ANSWER = module.ANSWER
  156. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  157. multi_stream_handler.should_receive('setFormatter').once()
  158. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  159. flexmock(module).should_receive('Console_color_formatter')
  160. flexmock(module).should_receive('interactive_console').and_return(False)
  161. flexmock(module.logging).should_receive('basicConfig').with_args(
  162. level=logging.DEBUG, handlers=list
  163. )
  164. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
  165. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(False)
  166. flexmock(module.os.path).should_receive('exists').with_args('/var/run/log').and_return(True)
  167. syslog_handler = logging.handlers.SysLogHandler()
  168. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  169. address='/var/run/log'
  170. ).and_return(syslog_handler).once()
  171. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  172. def test_configure_logging_without_syslog_log_level_skips_syslog():
  173. flexmock(module).should_receive('add_custom_log_levels')
  174. flexmock(module.logging).ANSWER = module.ANSWER
  175. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  176. multi_stream_handler.should_receive('setFormatter').once()
  177. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  178. flexmock(module).should_receive('Console_color_formatter')
  179. flexmock(module.logging).should_receive('basicConfig').with_args(
  180. level=logging.INFO, handlers=list
  181. )
  182. flexmock(module.os.path).should_receive('exists').never()
  183. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  184. module.configure_logging(console_log_level=logging.INFO)
  185. def test_configure_logging_skips_syslog_if_not_found():
  186. flexmock(module).should_receive('add_custom_log_levels')
  187. flexmock(module.logging).ANSWER = module.ANSWER
  188. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  189. multi_stream_handler.should_receive('setFormatter').once()
  190. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  191. flexmock(module).should_receive('Console_color_formatter')
  192. flexmock(module.logging).should_receive('basicConfig').with_args(
  193. level=logging.INFO, handlers=list
  194. )
  195. flexmock(module.os.path).should_receive('exists').and_return(False)
  196. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  197. module.configure_logging(console_log_level=logging.INFO, syslog_log_level=logging.DEBUG)
  198. def test_configure_logging_skips_log_file_if_log_file_logging_is_disabled():
  199. flexmock(module).should_receive('add_custom_log_levels')
  200. flexmock(module.logging).DISABLED = module.DISABLED
  201. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  202. multi_stream_handler.should_receive('setFormatter').once()
  203. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  204. flexmock(module.logging).should_receive('basicConfig').with_args(
  205. level=logging.INFO, handlers=list
  206. )
  207. flexmock(module.os.path).should_receive('exists').never()
  208. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  209. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  210. module.configure_logging(
  211. console_log_level=logging.INFO, log_file_log_level=logging.DISABLED, log_file='/tmp/logfile'
  212. )
  213. def test_configure_logging_to_log_file_instead_of_syslog():
  214. flexmock(module).should_receive('add_custom_log_levels')
  215. flexmock(module.logging).ANSWER = module.ANSWER
  216. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  217. multi_stream_handler.should_receive('setFormatter').once()
  218. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  219. flexmock(module.logging).should_receive('basicConfig').with_args(
  220. level=logging.DEBUG, handlers=list
  221. )
  222. flexmock(module.os.path).should_receive('exists').never()
  223. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  224. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  225. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  226. '/tmp/logfile'
  227. ).and_return(file_handler).once()
  228. module.configure_logging(
  229. console_log_level=logging.INFO,
  230. syslog_log_level=logging.DISABLED,
  231. log_file_log_level=logging.DEBUG,
  232. log_file='/tmp/logfile',
  233. )
  234. def test_configure_logging_to_both_log_file_and_syslog():
  235. flexmock(module).should_receive('add_custom_log_levels')
  236. flexmock(module.logging).ANSWER = module.ANSWER
  237. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  238. multi_stream_handler.should_receive('setFormatter').once()
  239. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  240. flexmock(module.logging).should_receive('basicConfig').with_args(
  241. level=logging.DEBUG, handlers=list
  242. )
  243. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  244. syslog_handler = logging.handlers.SysLogHandler()
  245. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  246. address='/dev/log'
  247. ).and_return(syslog_handler).once()
  248. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  249. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  250. '/tmp/logfile'
  251. ).and_return(file_handler).once()
  252. module.configure_logging(
  253. console_log_level=logging.INFO,
  254. syslog_log_level=logging.DEBUG,
  255. log_file_log_level=logging.DEBUG,
  256. log_file='/tmp/logfile',
  257. )
  258. def test_configure_logging_to_log_file_formats_with_custom_log_format():
  259. flexmock(module).should_receive('add_custom_log_levels')
  260. flexmock(module.logging).ANSWER = module.ANSWER
  261. flexmock(module.logging).should_receive('Formatter').with_args(
  262. '{message}', style='{' # noqa: FS003
  263. ).once()
  264. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  265. multi_stream_handler.should_receive('setFormatter').once()
  266. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  267. flexmock(module).should_receive('interactive_console').and_return(False)
  268. flexmock(module.logging).should_receive('basicConfig').with_args(
  269. level=logging.DEBUG, handlers=list
  270. )
  271. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  272. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  273. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  274. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  275. '/tmp/logfile'
  276. ).and_return(file_handler).once()
  277. module.configure_logging(
  278. console_log_level=logging.INFO,
  279. log_file_log_level=logging.DEBUG,
  280. log_file='/tmp/logfile',
  281. log_file_format='{message}', # noqa: FS003
  282. )
  283. def test_configure_logging_skips_log_file_if_argument_is_none():
  284. flexmock(module).should_receive('add_custom_log_levels')
  285. flexmock(module.logging).ANSWER = module.ANSWER
  286. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  287. multi_stream_handler.should_receive('setFormatter').once()
  288. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  289. flexmock(module.logging).should_receive('basicConfig').with_args(
  290. level=logging.INFO, handlers=list
  291. )
  292. flexmock(module.os.path).should_receive('exists').and_return(False)
  293. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  294. module.configure_logging(console_log_level=logging.INFO, log_file=None)
  295. def test_configure_logging_skips_console_color_formatter_if_color_disabled():
  296. flexmock(module).should_receive('add_custom_log_levels')
  297. flexmock(module.logging).ANSWER = module.ANSWER
  298. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  299. multi_stream_handler.should_receive('setFormatter').never()
  300. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  301. flexmock(module.logging).should_receive('basicConfig').with_args(
  302. level=logging.INFO, handlers=list
  303. )
  304. flexmock(module.os.path).should_receive('exists').and_return(False)
  305. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  306. module.configure_logging(console_log_level=logging.INFO, log_file=None, color_enabled=False)