test_loki.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import logging
  2. import platform
  3. from flexmock import flexmock
  4. from borgmatic.hooks.monitoring import loki as module
  5. def test_initialize_monitor_replaces_labels():
  6. '''
  7. Assert that label placeholders get replaced.
  8. '''
  9. hook_config = {
  10. 'url': 'http://localhost:3100/loki/api/v1/push',
  11. 'labels': {'hostname': '__hostname', 'config': '__config', 'config_full': '__config_path'},
  12. }
  13. config_filename = '/mock/path/test.yaml'
  14. dry_run = True
  15. module.initialize_monitor(hook_config, flexmock(), config_filename, flexmock(), dry_run)
  16. for handler in tuple(logging.getLogger().handlers):
  17. if isinstance(handler, module.Loki_log_handler):
  18. assert handler.buffer.root['streams'][0]['stream']['hostname'] == platform.node()
  19. assert handler.buffer.root['streams'][0]['stream']['config'] == 'test.yaml'
  20. assert handler.buffer.root['streams'][0]['stream']['config_full'] == config_filename
  21. return
  22. raise AssertionError()
  23. def test_initialize_monitor_adds_log_handler():
  24. '''
  25. Assert that calling initialize_monitor adds our logger to the root logger.
  26. '''
  27. hook_config = {'url': 'http://localhost:3100/loki/api/v1/push', 'labels': {'app': 'borgmatic'}}
  28. module.initialize_monitor(
  29. hook_config,
  30. flexmock(),
  31. config_filename='test.yaml',
  32. monitoring_log_level=flexmock(),
  33. dry_run=True,
  34. )
  35. for handler in tuple(logging.getLogger().handlers):
  36. if isinstance(handler, module.Loki_log_handler):
  37. return
  38. raise AssertionError()
  39. def test_ping_monitor_adds_log_message():
  40. '''
  41. Assert that calling ping_monitor adds a message to our logger.
  42. '''
  43. hook_config = {'url': 'http://localhost:3100/loki/api/v1/push', 'labels': {'app': 'borgmatic'}}
  44. config_filename = 'test.yaml'
  45. dry_run = True
  46. module.initialize_monitor(hook_config, flexmock(), config_filename, flexmock(), dry_run)
  47. module.ping_monitor(
  48. hook_config,
  49. flexmock(),
  50. config_filename,
  51. module.monitor.State.FINISH,
  52. flexmock(),
  53. dry_run,
  54. )
  55. for handler in tuple(logging.getLogger().handlers):
  56. if isinstance(handler, module.Loki_log_handler):
  57. assert any(
  58. value[1] == f'{module.MONITOR_STATE_TO_LOKI[module.monitor.State.FINISH]} backup'
  59. for value in handler.buffer.root['streams'][0]['values']
  60. )
  61. return
  62. raise AssertionError()
  63. def test_destroy_monitor_removes_log_handler():
  64. '''
  65. Assert that destroy_monitor removes the logger from the root logger.
  66. '''
  67. hook_config = {'url': 'http://localhost:3100/loki/api/v1/push', 'labels': {'app': 'borgmatic'}}
  68. config_filename = 'test.yaml'
  69. dry_run = True
  70. module.initialize_monitor(hook_config, flexmock(), config_filename, flexmock(), dry_run)
  71. module.destroy_monitor(hook_config, flexmock(), flexmock(), dry_run)
  72. for handler in tuple(logging.getLogger().handlers):
  73. if isinstance(handler, module.Loki_log_handler):
  74. raise AssertionError()