-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_journalwatch.py
110 lines (96 loc) · 3.21 KB
/
test_journalwatch.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import re
from datetime import datetime, timezone
import pytest
import journalwatch
def test_read_patterns():
lines = [
'# This is a comment',
'_SYSTEMD_UNIT = foo',
'bar',
'',
'_SYSTEMD_UNIT = /baz/',
'fish',
]
expected = {
('_SYSTEMD_UNIT', 'foo'): [re.compile('bar')],
('_SYSTEMD_UNIT', re.compile('baz')): [re.compile('fish')],
}
assert journalwatch.read_patterns(lines) == expected
@pytest.mark.parametrize('entry, expected', [
({'_SYSTEMD_UNIT': 'foo'},
'U foo : EMPTY!'),
({
'_SYSTEMD_UNIT': 'foo',
'PRIORITY': 'Prio',
'__REALTIME_TIMESTAMP': datetime.fromtimestamp(0, tz=timezone.utc),
'_PID': 1337,
'MESSAGE': "Hello World"
}, 'U Thu Jan 1 00:00:00 1970 pPrio foo [1337]: Hello World'),
({'SYSLOG_IDENTIFIER': 'sys', 'MESSAGE': "Hello World"},
'S sys: Hello World'),
])
def test_format_entry(entry, expected):
assert journalwatch.format_entry(entry) == expected
@pytest.mark.parametrize('patterns, entry, filtered', [
# No patterns
({}, {'MESSAGE': 'foo'}, False),
# No message
(
{('_SYSLOG_IDENTIFIER', 'foo'): [re.compile('bar')]},
{'_SYSLOG_IDENTIFIER': 'foo'},
False
),
# No matching pattern
(
{('_SYSLOG_IDENTIFIER', 'bar'): [re.compile('bar')]},
{'_SYSLOG_IDENTIFIER': 'foo', 'MESSAGE': 'unmatched'},
False
),
# Matching pattern
(
{('_SYSLOG_IDENTIFIER', 'bar'): [re.compile('msg')]},
{'_SYSLOG_IDENTIFIER': 'bar', 'MESSAGE': 'msg'},
True
),
# Regex as identifier
(
{('_SYSLOG_IDENTIFIER', re.compile('bar')): [re.compile('msg')]},
{'_SYSLOG_IDENTIFIER': 'bar', 'MESSAGE': 'msg'},
True
),
# Matching priority (#7)
(
{('PRIORITY', '1'): [re.compile('msg')]},
{'PRIORITY': 1, 'MESSAGE': 'msg'},
True
),
(
{('PRIORITY', re.compile('1')): [re.compile('msg')]},
{'PRIORITY': 1, 'MESSAGE': 'msg'},
True
),
# Binary message (#5)
(
{('_SYSLOG_IDENTIFIER', 'bar'): [re.compile('msg')]},
{'_SYSLOG_IDENTIFIER': 'bar', 'MESSAGE': b'\xde\xad\xbe\xef'},
False
),
])
def test_filter_message(patterns, entry, filtered):
assert journalwatch.filter_message(patterns, entry) == filtered
def test_new_config_files(monkeypatch, tmpdir):
config_home = tmpdir / 'config'
data_home = tmpdir / 'data'
config_dir = config_home / 'journalwatch'
data_dir = data_home / 'journalwatch'
config_file = config_dir / 'config'
pattern_file = config_dir / 'patterns'
monkeypatch.setattr(journalwatch, 'parse_args', lambda: None)
monkeypatch.setattr(journalwatch, 'XDG_DATA_HOME', str(data_home))
monkeypatch.setattr(journalwatch, 'DATA_DIR', str(data_dir))
monkeypatch.setattr(journalwatch, 'XDG_CONFIG_HOME', str(config_home))
monkeypatch.setattr(journalwatch, 'CONFIG_DIR', str(config_dir))
monkeypatch.setattr(journalwatch, 'CONFIG_FILE', str(config_file))
monkeypatch.setattr(journalwatch, 'PATTERN_FILE', str(pattern_file))
journalwatch.parse_config_files()
assert pattern_file.read() == journalwatch.DEFAULT_PATTERNS