5
from cloudinit.config.cc_rsyslog import (
6
apply_rsyslog_changes, DEF_DIR, DEF_FILENAME, DEF_RELOAD, load_config,
7
parse_remotes_line, remotes_to_rsyslog_cfg)
8
from cloudinit import util
10
from .. import helpers as t_help
13
class TestLoadConfig(t_help.TestCase):
15
super(TestLoadConfig, self).setUp()
17
'config_filename': DEF_FILENAME,
18
'config_dir': DEF_DIR,
19
'service_reload_command': DEF_RELOAD,
24
def test_legacy_full(self):
26
'rsyslog': ['*.* @192.168.1.1'],
27
'rsyslog_dir': "mydir",
28
'rsyslog_filename': "myfilename"})
30
'configs': ['*.* @192.168.1.1'],
31
'config_dir': "mydir",
32
'config_filename': 'myfilename',
33
'service_reload_command': 'auto'}
36
self.assertEqual(found, self.basecfg)
38
def test_legacy_defaults(self):
40
'rsyslog': ['*.* @192.168.1.1']})
42
'configs': ['*.* @192.168.1.1']})
43
self.assertEqual(found, self.basecfg)
45
def test_new_defaults(self):
46
self.assertEqual(load_config({}), self.basecfg)
48
def test_new_configs(self):
49
cfgs = ['*.* myhost', '*.* my2host']
50
self.basecfg.update({'configs': cfgs})
52
load_config({'rsyslog': {'configs': cfgs}}),
56
class TestApplyChanges(t_help.TestCase):
58
self.tmp = tempfile.mkdtemp()
59
self.addCleanup(shutil.rmtree, self.tmp)
61
def test_simple(self):
62
cfgline = "*.* foohost"
63
changed = apply_rsyslog_changes(
64
configs=[cfgline], def_fname="foo.cfg", cfg_dir=self.tmp)
66
fname = os.path.join(self.tmp, "foo.cfg")
67
self.assertEqual([fname], changed)
69
util.load_file(fname), cfgline + "\n")
71
def test_multiple_files(self):
74
{'content': 'abc', 'filename': 'my.cfg'},
75
{'content': 'filefoo-content',
76
'filename': os.path.join(self.tmp, 'mydir/mycfg')},
79
changed = apply_rsyslog_changes(
80
configs=configs, def_fname="default.cfg", cfg_dir=self.tmp)
83
(os.path.join(self.tmp, "default.cfg"),
85
(os.path.join(self.tmp, "my.cfg"), "abc\n"),
86
(os.path.join(self.tmp, "mydir/mycfg"), "filefoo-content\n"),
88
self.assertEqual([f[0] for f in expected], changed)
90
for fname, _content in expected:
92
actual.append((fname, util.load_file(fname),))
93
self.assertEqual(expected, actual)
95
def test_repeat_def(self):
96
configs = ['*.* foohost', "*.warn otherhost"]
98
changed = apply_rsyslog_changes(
99
configs=configs, def_fname="default.cfg", cfg_dir=self.tmp)
101
fname = os.path.join(self.tmp, "default.cfg")
102
self.assertEqual([fname], changed)
104
expected_content = '\n'.join([c for c in configs]) + '\n'
105
found_content = util.load_file(fname)
106
self.assertEqual(expected_content, found_content)
108
def test_multiline_content(self):
109
configs = ['line1', 'line2\nline3\n']
111
apply_rsyslog_changes(
112
configs=configs, def_fname="default.cfg", cfg_dir=self.tmp)
114
fname = os.path.join(self.tmp, "default.cfg")
115
expected_content = '\n'.join([c for c in configs])
116
found_content = util.load_file(fname)
117
self.assertEqual(expected_content, found_content)
120
class TestParseRemotesLine(t_help.TestCase):
121
def test_valid_port(self):
122
r = parse_remotes_line("foo:9")
123
self.assertEqual(9, r.port)
125
def test_invalid_port(self):
126
with self.assertRaises(ValueError):
127
parse_remotes_line("*.* foo:abc")
129
def test_valid_ipv6(self):
130
r = parse_remotes_line("*.* [::1]")
131
self.assertEqual("*.* @[::1]", str(r))
133
def test_valid_ipv6_with_port(self):
134
r = parse_remotes_line("*.* [::1]:100")
135
self.assertEqual(r.port, 100)
136
self.assertEqual(r.addr, "::1")
137
self.assertEqual("*.* @[::1]:100", str(r))
139
def test_invalid_multiple_colon(self):
140
with self.assertRaises(ValueError):
141
parse_remotes_line("*.* ::1:100")
143
def test_name_in_string(self):
144
r = parse_remotes_line("syslog.host", name="foobar")
145
self.assertEqual("*.* @syslog.host # foobar", str(r))
148
class TestRemotesToSyslog(t_help.TestCase):
149
def test_simple(self):
150
# str rendered line must appear in remotes_to_ryslog_cfg return
152
myline = str(parse_remotes_line(mycfg, name="myname"))
153
r = remotes_to_rsyslog_cfg({'myname': mycfg})
154
lines = r.splitlines()
155
self.assertEqual(1, len(lines))
156
self.assertTrue(myline in r.splitlines())
158
def test_header_footer(self):
161
r = remotes_to_rsyslog_cfg(
162
{'myname': "*.* myhost"}, header=header, footer=footer)
163
lines = r.splitlines()
164
self.assertTrue(header, lines[0])
165
self.assertTrue(footer, lines[-1])
167
def test_with_empty_or_null(self):
169
myline = str(parse_remotes_line(mycfg, name="myname"))
170
r = remotes_to_rsyslog_cfg(
171
{'myname': mycfg, 'removed': None, 'removed2': ""})
172
lines = r.splitlines()
173
self.assertEqual(1, len(lines))
174
self.assertTrue(myline in r.splitlines())