2
0

test_logger.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. flexmock(module).should_receive('interactive_console').never()
  30. assert module.should_do_markup(no_color=True, configs={}) is False
  31. def test_should_do_markup_respects_config_value():
  32. flexmock(module).should_receive('interactive_console').never()
  33. assert module.should_do_markup(no_color=False, configs={'foo.yaml': {'color': False}}) is False
  34. flexmock(module).should_receive('interactive_console').and_return(True).once()
  35. assert module.should_do_markup(no_color=False, configs={'foo.yaml': {'color': True}}) is True
  36. def test_should_do_markup_prefers_any_false_config_value():
  37. flexmock(module).should_receive('interactive_console').never()
  38. assert (
  39. module.should_do_markup(
  40. no_color=False,
  41. configs={
  42. 'foo.yaml': {'color': True},
  43. 'bar.yaml': {'color': False},
  44. },
  45. )
  46. is False
  47. )
  48. def test_should_do_markup_respects_PY_COLORS_environment_variable():
  49. flexmock(module.os.environ).should_receive('get').with_args('PY_COLORS', None).and_return(
  50. 'True'
  51. )
  52. flexmock(module.os.environ).should_receive('get').with_args('NO_COLOR', None).and_return(None)
  53. flexmock(module).should_receive('to_bool').and_return(True)
  54. assert module.should_do_markup(no_color=False, configs={}) is True
  55. def test_should_do_markup_prefers_no_color_value_to_config_value():
  56. flexmock(module).should_receive('interactive_console').never()
  57. assert module.should_do_markup(no_color=True, configs={'foo.yaml': {'color': True}}) is False
  58. def test_should_do_markup_prefers_config_value_to_environment_variables():
  59. flexmock(module.os.environ).should_receive('get').and_return('True')
  60. flexmock(module).should_receive('to_bool').and_return(True)
  61. flexmock(module).should_receive('interactive_console').never()
  62. assert module.should_do_markup(no_color=False, configs={'foo.yaml': {'color': False}}) is False
  63. def test_should_do_markup_prefers_no_color_value_to_environment_variables():
  64. flexmock(module.os.environ).should_receive('get').and_return('True')
  65. flexmock(module).should_receive('to_bool').and_return(True)
  66. flexmock(module).should_receive('interactive_console').never()
  67. assert module.should_do_markup(no_color=True, configs={}) is False
  68. def test_should_do_markup_respects_interactive_console_value():
  69. flexmock(module.os.environ).should_receive('get').and_return(None)
  70. flexmock(module).should_receive('interactive_console').and_return(True)
  71. assert module.should_do_markup(no_color=False, configs={}) is True
  72. def test_should_do_markup_prefers_PY_COLORS_to_interactive_console_value():
  73. flexmock(module.os.environ).should_receive('get').with_args('PY_COLORS', None).and_return(
  74. 'True'
  75. )
  76. flexmock(module.os.environ).should_receive('get').with_args('NO_COLOR', None).and_return(None)
  77. flexmock(module).should_receive('to_bool').and_return(True)
  78. flexmock(module).should_receive('interactive_console').never()
  79. assert module.should_do_markup(no_color=False, configs={}) is True
  80. def test_should_do_markup_prefers_NO_COLOR_to_interactive_console_value():
  81. flexmock(module.os.environ).should_receive('get').with_args('PY_COLORS', None).and_return(None)
  82. flexmock(module.os.environ).should_receive('get').with_args('NO_COLOR', None).and_return('True')
  83. flexmock(module).should_receive('interactive_console').never()
  84. assert module.should_do_markup(no_color=False, configs={}) is False
  85. def test_should_do_markup_respects_NO_COLOR_environment_variable():
  86. flexmock(module.os.environ).should_receive('get').with_args('NO_COLOR', None).and_return('True')
  87. flexmock(module.os.environ).should_receive('get').with_args('PY_COLORS', None).and_return(None)
  88. flexmock(module).should_receive('interactive_console').never()
  89. assert module.should_do_markup(no_color=False, configs={}) is False
  90. def test_should_do_markup_ignores_empty_NO_COLOR_environment_variable():
  91. flexmock(module.os.environ).should_receive('get').with_args('NO_COLOR', None).and_return('')
  92. flexmock(module.os.environ).should_receive('get').with_args('PY_COLORS', None).and_return(None)
  93. flexmock(module).should_receive('interactive_console').and_return(True)
  94. assert module.should_do_markup(no_color=False, configs={}) is True
  95. def test_should_do_markup_prefers_NO_COLOR_to_PY_COLORS():
  96. flexmock(module.os.environ).should_receive('get').with_args('PY_COLORS', None).and_return(
  97. 'True'
  98. )
  99. flexmock(module.os.environ).should_receive('get').with_args('NO_COLOR', None).and_return(
  100. 'SomeValue'
  101. )
  102. flexmock(module).should_receive('interactive_console').never()
  103. assert module.should_do_markup(no_color=False, configs={}) is False
  104. def test_multi_stream_handler_logs_to_handler_for_log_level():
  105. error_handler = flexmock()
  106. error_handler.should_receive('emit').once()
  107. info_handler = flexmock()
  108. multi_handler = module.Multi_stream_handler(
  109. {module.logging.ERROR: error_handler, module.logging.INFO: info_handler}
  110. )
  111. multi_handler.emit(flexmock(levelno=module.logging.ERROR))
  112. def test_console_color_formatter_format_includes_log_message():
  113. flexmock(module).should_receive('add_custom_log_levels')
  114. flexmock(module.logging).ANSWER = module.ANSWER
  115. plain_message = 'uh oh'
  116. record = flexmock(levelno=logging.CRITICAL, msg=plain_message)
  117. colored_message = module.Console_color_formatter().format(record)
  118. assert colored_message != plain_message
  119. assert plain_message in colored_message
  120. def test_color_text_does_not_raise():
  121. module.color_text(module.colorama.Fore.RED, 'hi')
  122. def test_color_text_without_color_does_not_raise():
  123. module.color_text(None, 'hi')
  124. def test_add_logging_level_adds_level_name_and_sets_global_attributes_and_methods():
  125. logger = flexmock()
  126. flexmock(module.logging).should_receive('getLoggerClass').and_return(logger)
  127. flexmock(module.logging).should_receive('addLevelName').with_args(99, 'PLAID')
  128. builtins = flexmock(sys.modules['builtins'])
  129. builtins.should_call('setattr')
  130. builtins.should_receive('setattr').with_args(module.logging, 'PLAID', 99).once()
  131. builtins.should_receive('setattr').with_args(logger, 'plaid', object).once()
  132. builtins.should_receive('setattr').with_args(logging, 'plaid', object).once()
  133. module.add_logging_level('PLAID', 99)
  134. def test_add_logging_level_skips_global_setting_if_already_set():
  135. logger = flexmock()
  136. flexmock(module.logging).should_receive('getLoggerClass').and_return(logger)
  137. flexmock(module.logging).PLAID = 99
  138. flexmock(logger).plaid = flexmock()
  139. flexmock(logging).plaid = flexmock()
  140. flexmock(module.logging).should_receive('addLevelName').never()
  141. builtins = flexmock(sys.modules['builtins'])
  142. builtins.should_call('setattr')
  143. builtins.should_receive('setattr').with_args(module.logging, 'PLAID', 99).never()
  144. builtins.should_receive('setattr').with_args(logger, 'plaid', object).never()
  145. builtins.should_receive('setattr').with_args(logging, 'plaid', object).never()
  146. module.add_logging_level('PLAID', 99)
  147. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_linux():
  148. flexmock(module).should_receive('add_custom_log_levels')
  149. flexmock(module.logging).ANSWER = module.ANSWER
  150. fake_formatter = flexmock()
  151. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  152. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  153. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  154. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  155. flexmock(module).should_receive('interactive_console').and_return(False)
  156. flexmock(module.logging).should_receive('basicConfig').with_args(
  157. level=logging.DEBUG, handlers=list
  158. )
  159. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  160. syslog_handler = logging.handlers.SysLogHandler()
  161. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  162. address='/dev/log'
  163. ).and_return(syslog_handler).once()
  164. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  165. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_macos():
  166. flexmock(module).should_receive('add_custom_log_levels')
  167. flexmock(module.logging).ANSWER = module.ANSWER
  168. fake_formatter = flexmock()
  169. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  170. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  171. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  172. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  173. flexmock(module).should_receive('interactive_console').and_return(False)
  174. flexmock(module.logging).should_receive('basicConfig').with_args(
  175. level=logging.DEBUG, handlers=list
  176. )
  177. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
  178. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(True)
  179. syslog_handler = logging.handlers.SysLogHandler()
  180. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  181. address='/var/run/syslog'
  182. ).and_return(syslog_handler).once()
  183. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  184. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_freebsd():
  185. flexmock(module).should_receive('add_custom_log_levels')
  186. flexmock(module.logging).ANSWER = module.ANSWER
  187. fake_formatter = flexmock()
  188. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  189. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  190. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  191. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  192. flexmock(module).should_receive('interactive_console').and_return(False)
  193. flexmock(module.logging).should_receive('basicConfig').with_args(
  194. level=logging.DEBUG, handlers=list
  195. )
  196. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
  197. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(False)
  198. flexmock(module.os.path).should_receive('exists').with_args('/var/run/log').and_return(True)
  199. syslog_handler = logging.handlers.SysLogHandler()
  200. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  201. address='/var/run/log'
  202. ).and_return(syslog_handler).once()
  203. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  204. def test_configure_logging_without_syslog_log_level_skips_syslog():
  205. flexmock(module).should_receive('add_custom_log_levels')
  206. flexmock(module.logging).ANSWER = module.ANSWER
  207. fake_formatter = flexmock()
  208. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  209. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  210. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  211. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  212. flexmock(module.logging).should_receive('basicConfig').with_args(
  213. level=logging.INFO, handlers=list
  214. )
  215. flexmock(module.os.path).should_receive('exists').never()
  216. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  217. module.configure_logging(console_log_level=logging.INFO)
  218. def test_configure_logging_skips_syslog_if_not_found():
  219. flexmock(module).should_receive('add_custom_log_levels')
  220. flexmock(module.logging).ANSWER = module.ANSWER
  221. fake_formatter = flexmock()
  222. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  223. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  224. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  225. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  226. flexmock(module.logging).should_receive('basicConfig').with_args(
  227. level=logging.INFO, handlers=list
  228. )
  229. flexmock(module.os.path).should_receive('exists').and_return(False)
  230. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  231. module.configure_logging(console_log_level=logging.INFO, syslog_log_level=logging.DEBUG)
  232. def test_configure_logging_skips_log_file_if_log_file_logging_is_disabled():
  233. flexmock(module).should_receive('add_custom_log_levels')
  234. flexmock(module.logging).DISABLED = module.DISABLED
  235. fake_formatter = flexmock()
  236. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  237. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  238. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).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.INFO, handlers=list
  242. )
  243. flexmock(module.os.path).should_receive('exists').never()
  244. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  245. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  246. module.configure_logging(
  247. console_log_level=logging.INFO, log_file_log_level=logging.DISABLED, log_file='/tmp/logfile'
  248. )
  249. def test_configure_logging_to_log_file_instead_of_syslog():
  250. flexmock(module).should_receive('add_custom_log_levels')
  251. flexmock(module.logging).ANSWER = module.ANSWER
  252. fake_formatter = flexmock()
  253. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  254. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  255. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  256. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  257. flexmock(module.logging).should_receive('basicConfig').with_args(
  258. level=logging.DEBUG, handlers=list
  259. )
  260. flexmock(module.os.path).should_receive('exists').never()
  261. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  262. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  263. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  264. '/tmp/logfile'
  265. ).and_return(file_handler).once()
  266. module.configure_logging(
  267. console_log_level=logging.INFO,
  268. syslog_log_level=logging.DISABLED,
  269. log_file_log_level=logging.DEBUG,
  270. log_file='/tmp/logfile',
  271. )
  272. def test_configure_logging_to_both_log_file_and_syslog():
  273. flexmock(module).should_receive('add_custom_log_levels')
  274. flexmock(module.logging).ANSWER = module.ANSWER
  275. fake_formatter = flexmock()
  276. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  277. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  278. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  279. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  280. flexmock(module.logging).should_receive('basicConfig').with_args(
  281. level=logging.DEBUG, handlers=list
  282. )
  283. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  284. syslog_handler = logging.handlers.SysLogHandler()
  285. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  286. address='/dev/log'
  287. ).and_return(syslog_handler).once()
  288. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  289. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  290. '/tmp/logfile'
  291. ).and_return(file_handler).once()
  292. module.configure_logging(
  293. console_log_level=logging.INFO,
  294. syslog_log_level=logging.DEBUG,
  295. log_file_log_level=logging.DEBUG,
  296. log_file='/tmp/logfile',
  297. )
  298. def test_configure_logging_to_log_file_formats_with_custom_log_format():
  299. flexmock(module).should_receive('add_custom_log_levels')
  300. flexmock(module.logging).ANSWER = module.ANSWER
  301. flexmock(module.logging).should_receive('Formatter').with_args(
  302. '{message}', style='{' # noqa: FS003
  303. ).once()
  304. fake_formatter = flexmock()
  305. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  306. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  307. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  308. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  309. flexmock(module).should_receive('interactive_console').and_return(False)
  310. flexmock(module.logging).should_receive('basicConfig').with_args(
  311. level=logging.DEBUG, handlers=list
  312. )
  313. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  314. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  315. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  316. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  317. '/tmp/logfile'
  318. ).and_return(file_handler).once()
  319. module.configure_logging(
  320. console_log_level=logging.INFO,
  321. log_file_log_level=logging.DEBUG,
  322. log_file='/tmp/logfile',
  323. log_file_format='{message}', # noqa: FS003
  324. )
  325. def test_configure_logging_skips_log_file_if_argument_is_none():
  326. flexmock(module).should_receive('add_custom_log_levels')
  327. flexmock(module.logging).ANSWER = module.ANSWER
  328. fake_formatter = flexmock()
  329. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  330. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  331. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  332. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  333. flexmock(module.logging).should_receive('basicConfig').with_args(
  334. level=logging.INFO, handlers=list
  335. )
  336. flexmock(module.os.path).should_receive('exists').and_return(False)
  337. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  338. module.configure_logging(console_log_level=logging.INFO, log_file=None)
  339. def test_configure_logging_uses_console_no_color_formatter_if_color_disabled():
  340. flexmock(module).should_receive('add_custom_log_levels')
  341. flexmock(module.logging).ANSWER = module.ANSWER
  342. fake_formatter = flexmock()
  343. flexmock(module).should_receive('Console_color_formatter').never()
  344. flexmock(module).should_receive('Console_no_color_formatter').and_return(fake_formatter)
  345. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  346. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  347. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  348. flexmock(module.logging).should_receive('basicConfig').with_args(
  349. level=logging.INFO, handlers=list
  350. )
  351. flexmock(module.os.path).should_receive('exists').and_return(False)
  352. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  353. module.configure_logging(console_log_level=logging.INFO, log_file=None, color_enabled=False)