|
1
|
|
|
""" |
|
2
|
|
|
:Copyright: 2007-2021 Jochen Kupperschmidt |
|
3
|
|
|
:License: MIT, see LICENSE for details. |
|
4
|
|
|
""" |
|
5
|
|
|
|
|
6
|
|
|
from io import StringIO |
|
7
|
|
|
|
|
8
|
|
|
from syslog2irc.config import load_config |
|
9
|
|
|
from syslog2irc.irc import IrcChannel, IrcConfig, IrcServer |
|
10
|
|
|
from syslog2irc.network import Port, TransportProtocol |
|
11
|
|
|
from syslog2irc.routing import Route |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
TOML_CONFIG = '''\ |
|
15
|
|
|
log_level = "warning" |
|
16
|
|
|
|
|
17
|
|
|
[irc.server] |
|
18
|
|
|
host = "irc.acme.test" |
|
19
|
|
|
port = 6669 |
|
20
|
|
|
ssl = true |
|
21
|
|
|
password = "EverythingReally!" |
|
22
|
|
|
rate_limit = 0.5 |
|
23
|
|
|
|
|
24
|
|
|
[irc.bot] |
|
25
|
|
|
nickname = "syslogger" |
|
26
|
|
|
realname = "Monsieur Syslog" |
|
27
|
|
|
|
|
28
|
|
|
[irc] |
|
29
|
|
|
commands = [ |
|
30
|
|
|
"MODE syslogger +i", |
|
31
|
|
|
] |
|
32
|
|
|
channels = [ |
|
33
|
|
|
{ name = "#monitoring" }, |
|
34
|
|
|
{ name = "#network" }, |
|
35
|
|
|
{ name = "#serverfarm", password = "more-is-more" }, |
|
36
|
|
|
] |
|
37
|
|
|
|
|
38
|
|
|
[routes] |
|
39
|
|
|
"514/udp" = [ "#monitoring" ] |
|
40
|
|
|
"10514/udp" = [ "#monitoring", "#network" ] |
|
41
|
|
|
"11514/tcp" = [ "#monitoring", "#serverfarm" ] |
|
42
|
|
|
''' |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
def test_load_config(): |
|
46
|
|
|
toml = StringIO(TOML_CONFIG) |
|
47
|
|
|
|
|
48
|
|
|
config = load_config(toml) |
|
49
|
|
|
|
|
50
|
|
|
assert config.log_level == 'WARNING' |
|
51
|
|
|
|
|
52
|
|
|
assert config.irc == IrcConfig( |
|
53
|
|
|
server=IrcServer( |
|
54
|
|
|
host='irc.acme.test', |
|
55
|
|
|
port=6669, |
|
56
|
|
|
ssl=True, |
|
57
|
|
|
password='EverythingReally!', |
|
58
|
|
|
rate_limit=0.5, |
|
59
|
|
|
), |
|
60
|
|
|
nickname='syslogger', |
|
61
|
|
|
realname='Monsieur Syslog', |
|
62
|
|
|
commands=[ |
|
63
|
|
|
'MODE syslogger +i', |
|
64
|
|
|
], |
|
65
|
|
|
channels={ |
|
66
|
|
|
IrcChannel('#monitoring'), |
|
67
|
|
|
IrcChannel('#network'), |
|
68
|
|
|
IrcChannel('#serverfarm', password='more-is-more'), |
|
69
|
|
|
}, |
|
70
|
|
|
) |
|
71
|
|
|
|
|
72
|
|
|
assert config.routes == { |
|
73
|
|
|
Route(Port(514, TransportProtocol.UDP), "#monitoring"), |
|
74
|
|
|
Route(Port(10514, TransportProtocol.UDP), "#monitoring"), |
|
75
|
|
|
Route(Port(10514, TransportProtocol.UDP), "#network"), |
|
76
|
|
|
Route(Port(11514, TransportProtocol.TCP), "#monitoring"), |
|
77
|
|
|
Route(Port(11514, TransportProtocol.TCP), "#serverfarm"), |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
TOML_CONFIG_WITH_DEFAULTS = '''\ |
|
82
|
|
|
[irc.server] |
|
83
|
|
|
host = "irc.organization.test" |
|
84
|
|
|
|
|
85
|
|
|
[irc.bot] |
|
86
|
|
|
nickname = "monitor" |
|
87
|
|
|
''' |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
def test_load_config_with_defaults(): |
|
91
|
|
|
toml = StringIO(TOML_CONFIG_WITH_DEFAULTS) |
|
92
|
|
|
|
|
93
|
|
|
config = load_config(toml) |
|
94
|
|
|
|
|
95
|
|
|
assert config.log_level == 'DEBUG' |
|
96
|
|
|
|
|
97
|
|
|
assert config.irc == IrcConfig( |
|
98
|
|
|
server=IrcServer( |
|
99
|
|
|
host='irc.organization.test', |
|
100
|
|
|
port=6667, |
|
101
|
|
|
ssl=False, |
|
102
|
|
|
password=None, |
|
103
|
|
|
rate_limit=None, |
|
104
|
|
|
), |
|
105
|
|
|
nickname='monitor', |
|
106
|
|
|
realname='syslog', |
|
107
|
|
|
commands=[], |
|
108
|
|
|
channels=set(), |
|
109
|
|
|
) |
|
110
|
|
|
|
|
111
|
|
|
assert config.routes == set() |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
TOML_CONFIG_WITHOUT_IRC_SERVER_TABLE = '''\ |
|
115
|
|
|
[irc.bot] |
|
116
|
|
|
nickname = "monitor" |
|
117
|
|
|
''' |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
def test_load_config_without_irc_server_table(): |
|
121
|
|
|
toml = StringIO(TOML_CONFIG_WITHOUT_IRC_SERVER_TABLE) |
|
122
|
|
|
|
|
123
|
|
|
config = load_config(toml) |
|
124
|
|
|
|
|
125
|
|
|
assert config.irc.server is None |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
TOML_CONFIG_WITHOUT_IRC_SERVER_HOST = '''\ |
|
129
|
|
|
[irc.server] |
|
130
|
|
|
|
|
131
|
|
|
[irc.bot] |
|
132
|
|
|
nickname = "monitor" |
|
133
|
|
|
''' |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
def test_load_config_without_irc_server_host(): |
|
137
|
|
|
toml = StringIO(TOML_CONFIG_WITHOUT_IRC_SERVER_HOST) |
|
138
|
|
|
|
|
139
|
|
|
config = load_config(toml) |
|
140
|
|
|
|
|
141
|
|
|
assert config.irc.server is None |
|
142
|
|
|
|