-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtest_common_fields.py
More file actions
73 lines (60 loc) · 2.82 KB
/
test_common_fields.py
File metadata and controls
73 lines (60 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import socket
import pytest
import mock
from pygelf import GelfTcpHandler, GelfUdpHandler, GelfHttpHandler, GelfTlsHandler, GelfHttpsHandler
from tests.helper import logger, get_unique_message, log_warning, log_exception
SYSLOG_LEVEL_ERROR = 3
SYSLOG_LEVEL_WARNING = 4
@pytest.fixture(params=[
GelfTcpHandler(host='localhost', port=12201),
GelfUdpHandler(host='localhost', port=12202),
GelfUdpHandler(host='localhost', port=12202, compress=False),
GelfHttpHandler(host='localhost', port=12203),
GelfHttpHandler(host='localhost', port=12203, compress=False),
GelfTlsHandler(host='localhost', port=12204),
GelfHttpsHandler(host='localhost', port=12205, validate=False),
GelfHttpsHandler(host='localhost', port=12205, validate=True, ca_certs='tests/config/cert.pem'),
GelfTlsHandler(host='localhost', port=12204, validate=True, ca_certs='tests/config/cert.pem'),
])
def handler(request):
return request.param
def test_simple_message(logger):
message = get_unique_message()
graylog_response = log_warning(logger, message)
assert graylog_response['message'] == message
assert graylog_response['level'] == SYSLOG_LEVEL_WARNING
assert 'full_message' not in graylog_response
assert 'file' not in graylog_response
assert 'module' not in graylog_response
assert 'func' not in graylog_response
assert 'logger_name' not in graylog_response
assert 'line' not in graylog_response
def test_formatted_message(logger):
message = get_unique_message()
template = message + '_%s_%s'
graylog_response = log_warning(logger, template, args=('hello', 'gelf'))
assert graylog_response['message'] == message + '_hello_gelf'
assert graylog_response['level'] == SYSLOG_LEVEL_WARNING
assert 'full_message' not in graylog_response
def test_full_message(logger):
message = get_unique_message()
try:
raise ValueError(message)
except ValueError as e:
graylog_response = log_exception(logger, message, e)
assert graylog_response['message'] == message
assert graylog_response['level'] == SYSLOG_LEVEL_ERROR
assert message in graylog_response['full_message']
assert 'Traceback (most recent call last)' in graylog_response['full_message']
assert 'ValueError: ' in graylog_response['full_message']
assert 'file' not in graylog_response
assert 'module' not in graylog_response
assert 'func' not in graylog_response
assert 'logger_name' not in graylog_response
assert 'line' not in graylog_response
def test_source(logger):
original_source = socket.gethostname()
with mock.patch('socket.gethostname', return_value='different_domain'):
message = get_unique_message()
graylog_response = log_warning(logger, message)
assert graylog_response['source'] == original_source