test_loki.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import json
  2. import requests
  3. from flexmock import flexmock
  4. from borgmatic.hooks import loki as module
  5. def test_log_handler_gets_labels():
  6. '''
  7. Assert that adding labels works
  8. '''
  9. buffer = module.Loki_log_buffer(flexmock(), False)
  10. buffer.add_label('test', 'label')
  11. assert buffer.root['streams'][0]['stream']['test'] == 'label'
  12. buffer.add_label('test2', 'label2')
  13. assert buffer.root['streams'][0]['stream']['test2'] == 'label2'
  14. def test_log_buffer_gets_raw():
  15. '''
  16. Assert that adding values to the log buffer increases it's length
  17. '''
  18. buffer = module.Loki_log_buffer(flexmock(), False)
  19. assert len(buffer) == 0
  20. buffer.add_value('Some test log line')
  21. assert len(buffer) == 1
  22. buffer.add_value('Another test log line')
  23. assert len(buffer) == 2
  24. def test_log_buffer_gets_log_messages():
  25. '''
  26. Assert that adding log records works
  27. '''
  28. handler = module.Loki_log_handler(flexmock(), False)
  29. handler.emit(flexmock(getMessage=lambda: 'Some test log line'))
  30. assert len(handler.buffer) == 1
  31. def test_log_buffer_json():
  32. '''
  33. Assert that the buffer correctly serializes when empty
  34. '''
  35. buffer = module.Loki_log_buffer(flexmock(), False)
  36. assert json.loads(buffer.to_request()) == json.loads('{"streams":[{"stream":{},"values":[]}]}')
  37. def test_log_buffer_json_labels():
  38. '''
  39. Assert that the buffer correctly serializes with labels
  40. '''
  41. buffer = module.Loki_log_buffer(flexmock(), False)
  42. buffer.add_label('test', 'label')
  43. assert json.loads(buffer.to_request()) == json.loads(
  44. '{"streams":[{"stream":{"test": "label"},"values":[]}]}'
  45. )
  46. def test_log_buffer_json_log_lines():
  47. '''
  48. Assert that log lines end up in the correct place in the log buffer
  49. '''
  50. buffer = module.Loki_log_buffer(flexmock(), False)
  51. buffer.add_value('Some test log line')
  52. assert json.loads(buffer.to_request())['streams'][0]['values'][0][1] == 'Some test log line'
  53. def test_log_handler_post():
  54. '''
  55. Assert that the flush function sends a post request after a certain limit
  56. '''
  57. handler = module.Loki_log_handler(flexmock(), False)
  58. flexmock(module.requests).should_receive('post').and_return(
  59. flexmock(raise_for_status=lambda: '')
  60. ).once()
  61. for num in range(int(module.MAX_BUFFER_LINES * 1.5)):
  62. handler.raw(num)
  63. def test_log_handler_post_failiure():
  64. '''
  65. Assert that the flush function catches request exceptions
  66. '''
  67. handler = module.Loki_log_handler(flexmock(), False)
  68. flexmock(module.requests).should_receive('post').and_return(
  69. flexmock(raise_for_status=lambda: (_ for _ in ()).throw(requests.RequestException()))
  70. ).once()
  71. for num in range(int(module.MAX_BUFFER_LINES * 1.5)):
  72. handler.raw(num)
  73. def test_log_handler_empty_flush_noop():
  74. '''
  75. Test that flushing an empty buffer does indeed nothing
  76. '''
  77. handler = module.Loki_log_handler(flexmock(), False)
  78. handler.flush()