|
@@ -1,74 +1,27 @@
|
|
-from flexmock import flexmock
|
|
|
|
-from borgmatic.hooks import loki
|
|
|
|
import json
|
|
import json
|
|
-import platform
|
|
|
|
-import logging
|
|
|
|
-import requests
|
|
|
|
|
|
|
|
|
|
+import requests
|
|
|
|
+from flexmock import flexmock
|
|
|
|
|
|
-def test_log_handler_gets_added():
|
|
|
|
- hook_config = {'url': 'http://localhost:3100/loki/api/v1/push', 'labels': {'app': 'borgmatic'}}
|
|
|
|
- config_filename = 'test.yaml'
|
|
|
|
- dry_run = True
|
|
|
|
- loki.initialize_monitor(hook_config, '', config_filename, '', dry_run)
|
|
|
|
- for handler in tuple(logging.getLogger().handlers):
|
|
|
|
- if isinstance(handler, loki.Loki_log_handler):
|
|
|
|
- assert True
|
|
|
|
- return
|
|
|
|
- assert False
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-def test_ping():
|
|
|
|
- hook_config = {'url': 'http://localhost:3100/loki/api/v1/push', 'labels': {'app': 'borgmatic'}}
|
|
|
|
- config_filename = 'test.yaml'
|
|
|
|
- dry_run = True
|
|
|
|
- loki.initialize_monitor(hook_config, '', config_filename, '', dry_run)
|
|
|
|
- loki.ping_monitor(hook_config, '', config_filename, loki.monitor.State.FINISH, '', dry_run)
|
|
|
|
- for handler in tuple(logging.getLogger().handlers):
|
|
|
|
- if isinstance(handler, loki.Loki_log_handler):
|
|
|
|
- assert len(handler.buffer) <= 1
|
|
|
|
- return
|
|
|
|
- assert False
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-def test_log_handler_gets_removed():
|
|
|
|
- hook_config = {'url': 'http://localhost:3100/loki/api/v1/push', 'labels': {'app': 'borgmatic'}}
|
|
|
|
- config_filename = 'test.yaml'
|
|
|
|
- dry_run = True
|
|
|
|
- loki.initialize_monitor(hook_config, '', config_filename, '', dry_run)
|
|
|
|
- loki.destroy_monitor(hook_config, '', config_filename, '', dry_run)
|
|
|
|
- for handler in tuple(logging.getLogger().handlers):
|
|
|
|
- if isinstance(handler, loki.Loki_log_handler):
|
|
|
|
- assert False
|
|
|
|
|
|
+from borgmatic.hooks import loki as module
|
|
|
|
|
|
|
|
|
|
def test_log_handler_gets_labels():
|
|
def test_log_handler_gets_labels():
|
|
- buffer = loki.Loki_log_buffer('', False)
|
|
|
|
|
|
+ '''
|
|
|
|
+ Assert that adding labels works
|
|
|
|
+ '''
|
|
|
|
+ buffer = module.Loki_log_buffer(flexmock(), False)
|
|
buffer.add_label('test', 'label')
|
|
buffer.add_label('test', 'label')
|
|
assert buffer.root['streams'][0]['stream']['test'] == 'label'
|
|
assert buffer.root['streams'][0]['stream']['test'] == 'label'
|
|
buffer.add_label('test2', 'label2')
|
|
buffer.add_label('test2', 'label2')
|
|
assert buffer.root['streams'][0]['stream']['test2'] == 'label2'
|
|
assert buffer.root['streams'][0]['stream']['test2'] == 'label2'
|
|
|
|
|
|
|
|
|
|
-def test_log_handler_label_replacment():
|
|
|
|
- hook_config = {
|
|
|
|
- 'url': 'http://localhost:3100/loki/api/v1/push',
|
|
|
|
- 'labels': {'hostname': '__hostname', 'config': '__config', 'config_full': '__config_path'},
|
|
|
|
- }
|
|
|
|
- config_filename = '/mock/path/test.yaml'
|
|
|
|
- dry_run = True
|
|
|
|
- loki.initialize_monitor(hook_config, '', config_filename, '', dry_run)
|
|
|
|
- for handler in tuple(logging.getLogger().handlers):
|
|
|
|
- if isinstance(handler, loki.Loki_log_handler):
|
|
|
|
- assert handler.buffer.root['streams'][0]['stream']['hostname'] == platform.node()
|
|
|
|
- assert handler.buffer.root['streams'][0]['stream']['config'] == 'test.yaml'
|
|
|
|
- assert handler.buffer.root['streams'][0]['stream']['config_full'] == config_filename
|
|
|
|
- return
|
|
|
|
- assert False
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-def test_log_handler_gets_logs():
|
|
|
|
- buffer = loki.Loki_log_buffer('', False)
|
|
|
|
|
|
+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)
|
|
assert len(buffer) == 0
|
|
assert len(buffer) == 0
|
|
buffer.add_value('Some test log line')
|
|
buffer.add_value('Some test log line')
|
|
assert len(buffer) == 1
|
|
assert len(buffer) == 1
|
|
@@ -76,49 +29,70 @@ def test_log_handler_gets_logs():
|
|
assert len(buffer) == 2
|
|
assert len(buffer) == 2
|
|
|
|
|
|
|
|
|
|
-def test_log_handler_gets_raw():
|
|
|
|
- handler = loki.Loki_log_handler('', False)
|
|
|
|
|
|
+def test_log_buffer_gets_log_messages():
|
|
|
|
+ '''
|
|
|
|
+ Assert that adding log records works
|
|
|
|
+ '''
|
|
|
|
+ handler = module.Loki_log_handler(flexmock(), False)
|
|
handler.emit(flexmock(getMessage=lambda: 'Some test log line'))
|
|
handler.emit(flexmock(getMessage=lambda: 'Some test log line'))
|
|
assert len(handler.buffer) == 1
|
|
assert len(handler.buffer) == 1
|
|
|
|
|
|
|
|
|
|
-def test_log_handler_json():
|
|
|
|
- buffer = loki.Loki_log_buffer('', False)
|
|
|
|
|
|
+def test_log_buffer_json():
|
|
|
|
+ '''
|
|
|
|
+ Assert that the buffer correctly serializes when empty
|
|
|
|
+ '''
|
|
|
|
+ buffer = module.Loki_log_buffer(flexmock(), False)
|
|
assert json.loads(buffer.to_request()) == json.loads('{"streams":[{"stream":{},"values":[]}]}')
|
|
assert json.loads(buffer.to_request()) == json.loads('{"streams":[{"stream":{},"values":[]}]}')
|
|
|
|
|
|
|
|
|
|
-def test_log_handler_json_labels():
|
|
|
|
- buffer = loki.Loki_log_buffer('', False)
|
|
|
|
|
|
+def test_log_buffer_json_labels():
|
|
|
|
+ '''
|
|
|
|
+ Assert that the buffer correctly serializes with labels
|
|
|
|
+ '''
|
|
|
|
+ buffer = module.Loki_log_buffer(flexmock(), False)
|
|
buffer.add_label('test', 'label')
|
|
buffer.add_label('test', 'label')
|
|
assert json.loads(buffer.to_request()) == json.loads(
|
|
assert json.loads(buffer.to_request()) == json.loads(
|
|
'{"streams":[{"stream":{"test": "label"},"values":[]}]}'
|
|
'{"streams":[{"stream":{"test": "label"},"values":[]}]}'
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
-def test_log_handler_json_log_lines():
|
|
|
|
- buffer = loki.Loki_log_buffer('', False)
|
|
|
|
|
|
+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)
|
|
buffer.add_value('Some test log line')
|
|
buffer.add_value('Some test log line')
|
|
assert json.loads(buffer.to_request())['streams'][0]['values'][0][1] == 'Some test log line'
|
|
assert json.loads(buffer.to_request())['streams'][0]['values'][0][1] == 'Some test log line'
|
|
|
|
|
|
|
|
|
|
def test_log_handler_post():
|
|
def test_log_handler_post():
|
|
- handler = loki.Loki_log_handler('', False)
|
|
|
|
- flexmock(loki.requests).should_receive('post').and_return(
|
|
|
|
|
|
+ '''
|
|
|
|
+ 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(
|
|
flexmock(raise_for_status=lambda: '')
|
|
flexmock(raise_for_status=lambda: '')
|
|
).once()
|
|
).once()
|
|
- for x in range(150):
|
|
|
|
- handler.raw(x)
|
|
|
|
|
|
+ for num in range(int(module.MAX_BUFFER_LINES * 1.5)):
|
|
|
|
+ handler.raw(num)
|
|
|
|
|
|
|
|
|
|
-def test_post_failiure():
|
|
|
|
- handler = loki.Loki_log_handler('', False)
|
|
|
|
- flexmock(loki.requests).should_receive('post').and_return(
|
|
|
|
|
|
+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(
|
|
flexmock(raise_for_status=lambda: (_ for _ in ()).throw(requests.RequestException()))
|
|
flexmock(raise_for_status=lambda: (_ for _ in ()).throw(requests.RequestException()))
|
|
).once()
|
|
).once()
|
|
- for x in range(150):
|
|
|
|
- handler.raw(x)
|
|
|
|
|
|
+ for num in range(int(module.MAX_BUFFER_LINES * 1.5)):
|
|
|
|
+ handler.raw(num)
|
|
|
|
|
|
|
|
|
|
-def test_empty_flush():
|
|
|
|
- handler = loki.Loki_log_handler('', False)
|
|
|
|
|
|
+def test_log_handler_empty_flush_noop():
|
|
|
|
+ '''
|
|
|
|
+ Test that flushing an empty buffer does indeed nothing
|
|
|
|
+ '''
|
|
|
|
+ handler = module.Loki_log_handler(flexmock(), False)
|
|
handler.flush()
|
|
handler.flush()
|