test_loki.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from flexmock import flexmock
  2. from borgmatic.hooks import loki
  3. import json
  4. import platform
  5. import logging
  6. import requests
  7. def test_log_handler_gets_added():
  8. hook_config = {'url': 'http://localhost:3100/loki/api/v1/push', 'labels': {'app': 'borgmatic'}}
  9. config_filename = 'test.yaml'
  10. dry_run = True
  11. loki.initialize_monitor(hook_config, '', config_filename, '', dry_run)
  12. for handler in tuple(logging.getLogger().handlers):
  13. if isinstance(handler, loki.Loki_log_handler):
  14. assert True
  15. return
  16. assert False
  17. def test_ping():
  18. hook_config = {'url': 'http://localhost:3100/loki/api/v1/push', 'labels': {'app': 'borgmatic'}}
  19. config_filename = 'test.yaml'
  20. dry_run = True
  21. loki.initialize_monitor(hook_config, '', config_filename, '', dry_run)
  22. loki.ping_monitor(hook_config, '', config_filename, loki.monitor.State.FINISH, '', dry_run)
  23. for handler in tuple(logging.getLogger().handlers):
  24. if isinstance(handler, loki.Loki_log_handler):
  25. assert len(handler.buffer) <= 1
  26. return
  27. assert False
  28. def test_log_handler_gets_removed():
  29. hook_config = {'url': 'http://localhost:3100/loki/api/v1/push', 'labels': {'app': 'borgmatic'}}
  30. config_filename = 'test.yaml'
  31. dry_run = True
  32. loki.initialize_monitor(hook_config, '', config_filename, '', dry_run)
  33. loki.destroy_monitor(hook_config, '', config_filename, '', dry_run)
  34. for handler in tuple(logging.getLogger().handlers):
  35. if isinstance(handler, loki.Loki_log_handler):
  36. assert False
  37. def test_log_handler_gets_labels():
  38. buffer = loki.Loki_log_buffer('', False)
  39. buffer.add_label('test', 'label')
  40. assert buffer.root['streams'][0]['stream']['test'] == 'label'
  41. buffer.add_label('test2', 'label2')
  42. assert buffer.root['streams'][0]['stream']['test2'] == 'label2'
  43. def test_log_handler_label_replacment():
  44. hook_config = {
  45. 'url': 'http://localhost:3100/loki/api/v1/push',
  46. 'labels': {'hostname': '__hostname', 'config': '__config', 'config_full': '__config_path'},
  47. }
  48. config_filename = '/mock/path/test.yaml'
  49. dry_run = True
  50. loki.initialize_monitor(hook_config, '', config_filename, '', dry_run)
  51. for handler in tuple(logging.getLogger().handlers):
  52. if isinstance(handler, loki.Loki_log_handler):
  53. assert handler.buffer.root['streams'][0]['stream']['hostname'] == platform.node()
  54. assert handler.buffer.root['streams'][0]['stream']['config'] == 'test.yaml'
  55. assert handler.buffer.root['streams'][0]['stream']['config_full'] == config_filename
  56. return
  57. assert False
  58. def test_log_handler_gets_logs():
  59. buffer = loki.Loki_log_buffer('', False)
  60. assert len(buffer) == 0
  61. buffer.add_value('Some test log line')
  62. assert len(buffer) == 1
  63. buffer.add_value('Another test log line')
  64. assert len(buffer) == 2
  65. def test_log_handler_gets_raw():
  66. handler = loki.Loki_log_handler('', False)
  67. handler.emit(flexmock(getMessage=lambda: 'Some test log line'))
  68. assert len(handler.buffer) == 1
  69. def test_log_handler_json():
  70. buffer = loki.Loki_log_buffer('', False)
  71. assert json.loads(buffer.to_request()) == json.loads('{"streams":[{"stream":{},"values":[]}]}')
  72. def test_log_handler_json_labels():
  73. buffer = loki.Loki_log_buffer('', False)
  74. buffer.add_label('test', 'label')
  75. assert json.loads(buffer.to_request()) == json.loads(
  76. '{"streams":[{"stream":{"test": "label"},"values":[]}]}'
  77. )
  78. def test_log_handler_json_log_lines():
  79. buffer = loki.Loki_log_buffer('', False)
  80. buffer.add_value('Some test log line')
  81. assert json.loads(buffer.to_request())['streams'][0]['values'][0][1] == 'Some test log line'
  82. def test_log_handler_post():
  83. handler = loki.Loki_log_handler('', False)
  84. flexmock(loki.requests).should_receive('post').and_return(
  85. flexmock(raise_for_status=lambda: '')
  86. ).once()
  87. for x in range(150):
  88. handler.raw(x)
  89. def test_post_failiure():
  90. handler = loki.Loki_log_handler('', False)
  91. flexmock(loki.requests).should_receive('post').and_return(
  92. flexmock(raise_for_status=lambda: (_ for _ in ()).throw(requests.RequestException()))
  93. ).once()
  94. for x in range(150):
  95. handler.raw(x)
  96. def test_empty_flush():
  97. handler = loki.Loki_log_handler('', False)
  98. handler.flush()