test_loki.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. assert False
  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. assert False
  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, flexmock(), config_filename, module.monitor.State.FINISH, flexmock(), dry_run
  49. )
  50. for handler in tuple(logging.getLogger().handlers):
  51. if isinstance(handler, module.Loki_log_handler):
  52. assert any(
  53. map(
  54. lambda log: log
  55. == f'{config_filename}: {module.MONITOR_STATE_TO_LOKI[module.monitor.State.FINISH]} backup',
  56. map(lambda x: x[1], handler.buffer.root['streams'][0]['values']),
  57. )
  58. )
  59. return
  60. assert False
  61. def test_destroy_monitor_removes_log_handler():
  62. '''
  63. Assert that destroy_monitor removes the logger from the root logger.
  64. '''
  65. hook_config = {'url': 'http://localhost:3100/loki/api/v1/push', 'labels': {'app': 'borgmatic'}}
  66. config_filename = 'test.yaml'
  67. dry_run = True
  68. module.initialize_monitor(hook_config, flexmock(), config_filename, flexmock(), dry_run)
  69. module.destroy_monitor(hook_config, flexmock(), config_filename, flexmock(), dry_run)
  70. for handler in tuple(logging.getLogger().handlers):
  71. if isinstance(handler, module.Loki_log_handler):
  72. assert False