test_loki.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. module.initialize_monitor(hook_config, flexmock(), config_filename, flexmock(), dry_run=False)
  15. for handler in tuple(logging.getLogger().handlers):
  16. if isinstance(handler, module.Loki_log_handler):
  17. assert handler.buffer.root['streams'][0]['stream']['hostname'] == platform.node()
  18. assert handler.buffer.root['streams'][0]['stream']['config'] == 'test.yaml'
  19. assert handler.buffer.root['streams'][0]['stream']['config_full'] == config_filename
  20. return
  21. raise AssertionError()
  22. def test_initialize_monitor_adds_log_handler():
  23. '''
  24. Assert that calling initialize_monitor adds our logger to the root logger.
  25. '''
  26. hook_config = {'url': 'http://localhost:3100/loki/api/v1/push', 'labels': {'app': 'borgmatic'}}
  27. module.initialize_monitor(
  28. hook_config,
  29. flexmock(),
  30. config_filename='test.yaml',
  31. monitoring_log_level=flexmock(),
  32. dry_run=True,
  33. )
  34. for handler in tuple(logging.getLogger().handlers):
  35. if isinstance(handler, module.Loki_log_handler):
  36. return
  37. raise AssertionError()
  38. def test_ping_monitor_sends_log_message():
  39. '''
  40. Assert that calling ping_monitor sends a message to Loki via our logger.
  41. '''
  42. hook_config = {'url': 'http://localhost:3100/loki/api/v1/push', 'labels': {'app': 'borgmatic'}}
  43. config_filename = 'test.yaml'
  44. post_called = False
  45. def post(url, data, timeout, headers):
  46. nonlocal post_called
  47. post_called = True
  48. assert any(
  49. value[1] == f'{module.MONITOR_STATE_TO_LOKI[module.monitor.State.FINISH]} backup'
  50. for value in module.json.loads(data)['streams'][0]['values']
  51. )
  52. return flexmock(raise_for_status=lambda: None)
  53. flexmock(module.requests).should_receive('post').replace_with(post)
  54. module.initialize_monitor(hook_config, flexmock(), config_filename, flexmock(), dry_run=False)
  55. module.ping_monitor(
  56. hook_config,
  57. flexmock(),
  58. config_filename,
  59. module.monitor.State.FINISH,
  60. flexmock(),
  61. dry_run=False,
  62. )
  63. module.destroy_monitor(hook_config, flexmock(), flexmock(), dry_run=False)
  64. assert post_called
  65. def test_destroy_monitor_removes_log_handler():
  66. '''
  67. Assert that destroy_monitor removes the logger from the root logger.
  68. '''
  69. hook_config = {'url': 'http://localhost:3100/loki/api/v1/push', 'labels': {'app': 'borgmatic'}}
  70. config_filename = 'test.yaml'
  71. flexmock(module.requests).should_receive('post').never()
  72. module.initialize_monitor(hook_config, flexmock(), config_filename, flexmock(), dry_run=False)
  73. module.destroy_monitor(hook_config, flexmock(), flexmock(), dry_run=False)
  74. for handler in tuple(logging.getLogger().handlers):
  75. if isinstance(handler, module.Loki_log_handler):
  76. raise AssertionError()