test_logger.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_should_do_markup_respects_no_color_value():
  14. assert module.should_do_markup(no_color=True) is False
  15. def test_should_do_markup_respects_PY_COLORS_environment_variable():
  16. flexmock(module.os.environ).should_receive('get').and_return('True')
  17. flexmock(module).should_receive('to_bool').and_return(True)
  18. assert module.should_do_markup(no_color=False) is True
  19. def test_should_do_markup_prefers_no_color_value_to_PY_COLORS():
  20. flexmock(module.os.environ).should_receive('get').and_return('True')
  21. flexmock(module).should_receive('to_bool').and_return(True)
  22. assert module.should_do_markup(no_color=True) is False
  23. def test_should_do_markup_respects_stdout_tty_value():
  24. flexmock(module.os.environ).should_receive('get').and_return(None)
  25. assert module.should_do_markup(no_color=False) is False
  26. def test_should_do_markup_prefers_PY_COLORS_to_stdout_tty_value():
  27. flexmock(module.os.environ).should_receive('get').and_return('True')
  28. flexmock(module).should_receive('to_bool').and_return(True)
  29. assert module.should_do_markup(no_color=False) is True
  30. @pytest.mark.parametrize('method_name', ('critical', 'error', 'warn', 'info', 'debug'))
  31. def test_borgmatic_logger_log_method_does_not_raise(method_name):
  32. flexmock(module).should_receive('color_text')
  33. flexmock(module.logging.Logger).should_receive(method_name)
  34. getattr(module.Borgmatic_logger('test'), method_name)(msg='hi')
  35. def test_borgmatic_logger_handle_does_not_raise():
  36. flexmock(module).should_receive('color_text')
  37. flexmock(module.logging.Logger).should_receive('handle')
  38. module.Borgmatic_logger('test').handle(
  39. module.logging.makeLogRecord(dict(levelno=module.logging.CRITICAL, msg='hi'))
  40. )
  41. def test_color_text_does_not_raise():
  42. module.color_text(module.colorama.Fore.RED, 'hi')
  43. def test_color_text_without_color_does_not_raise():
  44. module.color_text(None, 'hi')
  45. def test_configure_logging_probes_for_log_socket_on_linux():
  46. flexmock(module.logging).should_receive('basicConfig').with_args(
  47. level=logging.INFO, handlers=tuple
  48. )
  49. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  50. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(False)
  51. syslog_handler = logging.handlers.SysLogHandler()
  52. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  53. address='/dev/log'
  54. ).and_return(syslog_handler).once()
  55. module.configure_logging(logging.INFO)
  56. def test_configure_logging_probes_for_log_socket_on_macos():
  57. flexmock(module.logging).should_receive('basicConfig').with_args(
  58. level=logging.INFO, handlers=tuple
  59. )
  60. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
  61. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(True)
  62. syslog_handler = logging.handlers.SysLogHandler()
  63. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  64. address='/var/run/syslog'
  65. ).and_return(syslog_handler).once()
  66. module.configure_logging(logging.INFO)
  67. def test_configure_logging_sets_global_logger_to_most_verbose_log_level():
  68. flexmock(module.logging).should_receive('basicConfig').with_args(
  69. level=logging.DEBUG, handlers=tuple
  70. ).once()
  71. flexmock(module.os.path).should_receive('exists').and_return(False)
  72. module.configure_logging(console_log_level=logging.INFO, syslog_log_level=logging.DEBUG)
  73. def test_configure_logging_skips_syslog_if_not_found():
  74. flexmock(module.logging).should_receive('basicConfig').with_args(
  75. level=logging.INFO, handlers=tuple
  76. )
  77. flexmock(module.os.path).should_receive('exists').and_return(False)
  78. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  79. module.configure_logging(console_log_level=logging.INFO)