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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
#!/usr/bin/env python3
#
# Copyright (C) 2019-2025 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
import unittest
from base_vyostest_shim import VyOSUnitTestSHIM
from vyos.utils.file import read_file
from vyos.utils.process import cmd
from vyos.utils.process import process_named_running
from vyos.xml_ref import default_value
PROCESS_NAME = 'rsyslogd'
RSYSLOG_CONF = '/run/rsyslog/rsyslog.conf'
base_path = ['system', 'syslog']
def get_config(string=''):
"""
Retrieve current "running configuration" from FRR
string: search for a specific start string in the configuration
"""
command = 'cat /run/rsyslog/rsyslog.conf'
if string:
command += f' | sed -n "/^{string}$/,/}}/p"' # }} required to escape } in f-string
return cmd(command)
class TestRSYSLOGService(VyOSUnitTestSHIM.TestCase):
@classmethod
def setUpClass(cls):
super(TestRSYSLOGService, cls).setUpClass()
# ensure we can also run this test on a live system - so lets clean
# out the current configuration :)
cls.cli_delete(cls, base_path)
def tearDown(self):
# Check for running process
self.assertTrue(process_named_running(PROCESS_NAME))
# delete testing SYSLOG config
self.cli_delete(base_path)
self.cli_commit()
# Check for running process
self.assertFalse(process_named_running(PROCESS_NAME))
def test_console(self):
level = 'warning'
self.cli_set(base_path + ['console', 'facility', 'all', 'level', level])
self.cli_commit()
rsyslog_conf = get_config()
config = [
f'if prifilt("*.{level}") then {{', # {{ required to escape { in f-string
'action(type="omfile" file="/dev/console")',
]
for tmp in config:
self.assertIn(tmp, rsyslog_conf)
def test_global(self):
hostname = 'vyos123'
domain_name = 'example.local'
default_marker_interval = default_value(base_path + ['marker', 'interval'])
facility = {
'auth': {'level': 'info'},
'kern': {'level': 'debug'},
'all': {'level': 'notice'},
}
self.cli_set(['system', 'host-name', hostname])
self.cli_set(['system', 'domain-name', domain_name])
self.cli_set(base_path + ['preserve-fqdn'])
for tmp, tmp_options in facility.items():
level = tmp_options['level']
self.cli_set(base_path + ['local', 'facility', tmp, 'level', level])
self.cli_commit()
config = get_config('')
expected = [
f'module(load="immark" interval="{default_marker_interval}")',
'global(preserveFQDN="on")',
f'global(localHostname="{hostname}.{domain_name}")',
]
for e in expected:
self.assertIn(e, config)
config = get_config('#### GLOBAL LOGGING ####')
prifilt = []
for tmp, tmp_options in facility.items():
if tmp == 'all':
tmp = '*'
level = tmp_options['level']
prifilt.append(f'{tmp}.{level}')
prifilt.sort()
prifilt = ','.join(prifilt)
self.assertIn(f'if prifilt("{prifilt}") then {{', config)
self.assertIn( ' action(', config)
self.assertIn( ' type="omfile"', config)
self.assertIn( ' file="/var/log/messages"', config)
self.assertIn( ' queue.size="262144"', config)
self.assertIn( ' rotation.sizeLimitCommand="/usr/sbin/logrotate /etc/logrotate.d/vyos-rsyslog"', config)
def test_remote(self):
rhosts = {
'169.254.0.1': {
'facility': {'auth' : {'level': 'info'}},
'protocol': 'udp',
},
'169.254.0.2': {
'port': '1514',
'protocol': 'udp',
},
'169.254.0.3': {
'facility': {'auth' : {'level': 'info'},
'kern' : {'level': 'debug'},
'all' : {'level': 'notice'},
},
'format': ['include-timezone', 'octet-counted'],
'protocol': 'tcp',
'port': '10514',
},
}
default_port = default_value(base_path + ['remote', next(iter(rhosts)), 'port'])
default_protocol = default_value(base_path + ['remote', next(iter(rhosts)), 'protocol'])
for remote, remote_options in rhosts.items():
remote_base = base_path + ['remote', remote]
if 'port' in remote_options:
self.cli_set(remote_base + ['port', remote_options['port']])
if 'facility' in remote_options:
for facility, facility_options in remote_options['facility'].items():
level = facility_options['level']
self.cli_set(remote_base + ['facility', facility, 'level', level])
if 'format' in remote_options:
for format in remote_options['format']:
self.cli_set(remote_base + ['format', format])
if 'protocol' in remote_options:
protocol = remote_options['protocol']
self.cli_set(remote_base + ['protocol', protocol])
self.cli_commit()
config = read_file(RSYSLOG_CONF)
for remote, remote_options in rhosts.items():
config = get_config(f'# Remote syslog to {remote}')
prifilt = []
if 'facility' in remote_options:
for facility, facility_options in remote_options['facility'].items():
level = facility_options['level']
if facility == 'all':
facility = '*'
prifilt.append(f'{facility}.{level}')
prifilt.sort()
prifilt = ','.join(prifilt)
if not prifilt:
# Skip test - as we do not render anything if no facility is set
continue
self.assertIn(f'if prifilt("{prifilt}") then {{', config)
self.assertIn( ' type="omfwd"', config)
self.assertIn(f' target="{remote}"', config)
port = default_port
if 'port' in remote_options:
port = remote_options['port']
self.assertIn(f'port="{port}"', config)
protocol = default_protocol
if 'protocol' in remote_options:
protocol = remote_options['protocol']
self.assertIn(f'protocol="{protocol}"', config)
if 'format' in remote_options:
if 'include-timezone' in remote_options['format']:
self.assertIn( ' template="SyslogProtocol23Format"', config)
if 'octet-counted' in remote_options['format']:
self.assertIn( ' TCP_Framing="octed-counted"', config)
else:
self.assertIn( ' TCP_Framing="traditional"', config)
if __name__ == '__main__':
unittest.main(verbosity=2)
|