Added more documentation to the test

Split tests to integration tests
This commit is contained in:
Tobias Hodapp 2023-08-24 13:17:42 +02:00
parent 9e2674ea5a
commit 099a712e53
2 changed files with 134 additions and 78 deletions

View file

@ -0,0 +1,82 @@
import logging
import platform
from flexmock import flexmock
from borgmatic.hooks import loki as module
def test_log_handler_label_replacment():
'''
Assert that label placeholders get replaced
'''
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
module.initialize_monitor(hook_config, flexmock(), config_filename, flexmock(), dry_run)
for handler in tuple(logging.getLogger().handlers):
if isinstance(handler, module.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_initalize_adds_log_handler():
'''
Assert that calling initialize_monitor adds our logger to the root logger
'''
hook_config = {'url': 'http://localhost:3100/loki/api/v1/push', 'labels': {'app': 'borgmatic'}}
module.initialize_monitor(
hook_config,
flexmock(),
config_filename='test.yaml',
monitoring_log_level=flexmock(),
dry_run=True,
)
for handler in tuple(logging.getLogger().handlers):
if isinstance(handler, module.Loki_log_handler):
return
assert False
def test_ping_adds_log_message():
'''
Assert that calling ping_monitor adds a message to our logger
'''
hook_config = {'url': 'http://localhost:3100/loki/api/v1/push', 'labels': {'app': 'borgmatic'}}
config_filename = 'test.yaml'
dry_run = True
module.initialize_monitor(hook_config, flexmock(), config_filename, flexmock(), dry_run)
module.ping_monitor(
hook_config, flexmock(), config_filename, module.monitor.State.FINISH, flexmock(), dry_run
)
for handler in tuple(logging.getLogger().handlers):
if isinstance(handler, module.Loki_log_handler):
assert any(
map(
lambda log: log
== f'{config_filename}: {module.MONITOR_STATE_TO_LOKI[module.monitor.State.FINISH]} backup',
map(lambda x: x[1], handler.buffer.root['streams'][0]['values']),
)
)
return
assert False
def test_log_handler_gets_removed():
'''
Assert that destroy_monitor removes the logger from the root logger
'''
hook_config = {'url': 'http://localhost:3100/loki/api/v1/push', 'labels': {'app': 'borgmatic'}}
config_filename = 'test.yaml'
dry_run = True
module.initialize_monitor(hook_config, flexmock(), config_filename, flexmock(), dry_run)
module.destroy_monitor(hook_config, flexmock(), config_filename, flexmock(), dry_run)
for handler in tuple(logging.getLogger().handlers):
if isinstance(handler, module.Loki_log_handler):
assert False

View file

@ -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
from borgmatic.hooks import loki as module
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
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(): def test_log_buffer_gets_raw():
hook_config = { '''
'url': 'http://localhost:3100/loki/api/v1/push', Assert that adding values to the log buffer increases it's length
'labels': {'hostname': '__hostname', 'config': '__config', 'config_full': '__config_path'}, '''
} buffer = module.Loki_log_buffer(flexmock(), False)
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)
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(): def test_log_buffer_gets_log_messages():
handler = loki.Loki_log_handler('', False) '''
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(): def test_log_buffer_json():
buffer = loki.Loki_log_buffer('', False) '''
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(): def test_log_buffer_json_labels():
buffer = loki.Loki_log_buffer('', False) '''
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(): def test_log_buffer_json_log_lines():
buffer = loki.Loki_log_buffer('', False) '''
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): for num in range(int(module.MAX_BUFFER_LINES * 1.5)):
handler.raw(x) handler.raw(num)
def test_post_failiure(): def test_log_handler_post_failiure():
handler = loki.Loki_log_handler('', False) '''
flexmock(loki.requests).should_receive('post').and_return( 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): for num in range(int(module.MAX_BUFFER_LINES * 1.5)):
handler.raw(x) handler.raw(num)
def test_empty_flush(): def test_log_handler_empty_flush_noop():
handler = loki.Loki_log_handler('', False) '''
Test that flushing an empty buffer does indeed nothing
'''
handler = module.Loki_log_handler(flexmock(), False)
handler.flush() handler.flush()