2023-08-23 17:17:23 +02:00
|
|
|
import json
|
|
|
|
|
2023-08-24 13:17:42 +02:00
|
|
|
import requests
|
|
|
|
from flexmock import flexmock
|
2023-08-23 17:17:23 +02:00
|
|
|
|
2023-08-24 13:17:42 +02:00
|
|
|
from borgmatic.hooks import loki as module
|
2023-08-23 17:17:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_log_handler_gets_labels():
|
2023-08-24 13:17:42 +02:00
|
|
|
'''
|
|
|
|
Assert that adding labels works
|
|
|
|
'''
|
|
|
|
buffer = module.Loki_log_buffer(flexmock(), False)
|
2023-08-23 17:17:23 +02:00
|
|
|
buffer.add_label('test', 'label')
|
|
|
|
assert buffer.root['streams'][0]['stream']['test'] == 'label'
|
|
|
|
buffer.add_label('test2', 'label2')
|
|
|
|
assert buffer.root['streams'][0]['stream']['test2'] == 'label2'
|
|
|
|
|
|
|
|
|
2023-08-24 13:17:42 +02:00
|
|
|
def test_log_buffer_gets_raw():
|
|
|
|
'''
|
|
|
|
Assert that adding values to the log buffer increases it's length
|
|
|
|
'''
|
|
|
|
buffer = module.Loki_log_buffer(flexmock(), False)
|
2023-08-23 17:17:23 +02:00
|
|
|
assert len(buffer) == 0
|
|
|
|
buffer.add_value('Some test log line')
|
|
|
|
assert len(buffer) == 1
|
|
|
|
buffer.add_value('Another test log line')
|
|
|
|
assert len(buffer) == 2
|
|
|
|
|
|
|
|
|
2023-08-24 13:17:42 +02:00
|
|
|
def test_log_buffer_gets_log_messages():
|
|
|
|
'''
|
|
|
|
Assert that adding log records works
|
|
|
|
'''
|
|
|
|
handler = module.Loki_log_handler(flexmock(), False)
|
2023-08-23 17:17:23 +02:00
|
|
|
handler.emit(flexmock(getMessage=lambda: 'Some test log line'))
|
|
|
|
assert len(handler.buffer) == 1
|
|
|
|
|
|
|
|
|
2023-08-24 13:17:42 +02:00
|
|
|
def test_log_buffer_json():
|
|
|
|
'''
|
|
|
|
Assert that the buffer correctly serializes when empty
|
|
|
|
'''
|
|
|
|
buffer = module.Loki_log_buffer(flexmock(), False)
|
2023-08-23 17:17:23 +02:00
|
|
|
assert json.loads(buffer.to_request()) == json.loads('{"streams":[{"stream":{},"values":[]}]}')
|
|
|
|
|
|
|
|
|
2023-08-24 13:17:42 +02:00
|
|
|
def test_log_buffer_json_labels():
|
|
|
|
'''
|
|
|
|
Assert that the buffer correctly serializes with labels
|
|
|
|
'''
|
|
|
|
buffer = module.Loki_log_buffer(flexmock(), False)
|
2023-08-23 17:17:23 +02:00
|
|
|
buffer.add_label('test', 'label')
|
|
|
|
assert json.loads(buffer.to_request()) == json.loads(
|
|
|
|
'{"streams":[{"stream":{"test": "label"},"values":[]}]}'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-24 13:17:42 +02:00
|
|
|
def test_log_buffer_json_log_lines():
|
|
|
|
'''
|
|
|
|
Assert that log lines end up in the correct place in the log buffer
|
|
|
|
'''
|
|
|
|
buffer = module.Loki_log_buffer(flexmock(), False)
|
2023-08-23 17:17:23 +02:00
|
|
|
buffer.add_value('Some test log line')
|
|
|
|
assert json.loads(buffer.to_request())['streams'][0]['values'][0][1] == 'Some test log line'
|
|
|
|
|
|
|
|
|
|
|
|
def test_log_handler_post():
|
2023-08-24 13:17:42 +02:00
|
|
|
'''
|
|
|
|
Assert that the flush function sends a post request after a certain limit
|
|
|
|
'''
|
|
|
|
handler = module.Loki_log_handler(flexmock(), False)
|
|
|
|
flexmock(module.requests).should_receive('post').and_return(
|
2023-08-23 17:17:23 +02:00
|
|
|
flexmock(raise_for_status=lambda: '')
|
|
|
|
).once()
|
2023-08-24 13:17:42 +02:00
|
|
|
for num in range(int(module.MAX_BUFFER_LINES * 1.5)):
|
|
|
|
handler.raw(num)
|
2023-08-23 17:17:23 +02:00
|
|
|
|
|
|
|
|
2023-08-24 13:17:42 +02:00
|
|
|
def test_log_handler_post_failiure():
|
|
|
|
'''
|
|
|
|
Assert that the flush function catches request exceptions
|
|
|
|
'''
|
|
|
|
handler = module.Loki_log_handler(flexmock(), False)
|
|
|
|
flexmock(module.requests).should_receive('post').and_return(
|
2023-08-23 17:17:23 +02:00
|
|
|
flexmock(raise_for_status=lambda: (_ for _ in ()).throw(requests.RequestException()))
|
|
|
|
).once()
|
2023-08-24 13:17:42 +02:00
|
|
|
for num in range(int(module.MAX_BUFFER_LINES * 1.5)):
|
|
|
|
handler.raw(num)
|
2023-08-23 17:17:23 +02:00
|
|
|
|
|
|
|
|
2023-08-24 13:17:42 +02:00
|
|
|
def test_log_handler_empty_flush_noop():
|
|
|
|
'''
|
|
|
|
Test that flushing an empty buffer does indeed nothing
|
|
|
|
'''
|
|
|
|
handler = module.Loki_log_handler(flexmock(), False)
|
2023-08-23 17:17:23 +02:00
|
|
|
handler.flush()
|