summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cloudinit/config/cc_rsyslog.py27
-rw-r--r--tests/unittests/test_handler/test_handler_rsyslog.py113
2 files changed, 122 insertions, 18 deletions
diff --git a/cloudinit/config/cc_rsyslog.py b/cloudinit/config/cc_rsyslog.py
index 7d5657bc..a07200c3 100644
--- a/cloudinit/config/cc_rsyslog.py
+++ b/cloudinit/config/cc_rsyslog.py
@@ -113,17 +113,18 @@ def load_config(cfg):
# support converting the old top level format into new format
mycfg = cfg.get('rsyslog', {})
- if isinstance(mycfg, list):
- mycfg[KEYNAME_CONFIGS] = mycfg
+ if isinstance(cfg.get('rsyslog'), list):
+ mycfg = {KEYNAME_CONFIGS: cfg.get('rsyslog')}
if KEYNAME_LEGACY_FILENAME in cfg:
mycfg[KEYNAME_FILENAME] = cfg[KEYNAME_LEGACY_FILENAME]
if KEYNAME_LEGACY_DIR in cfg:
- mycfg[KEYNAME_DIR] = cfg[KEYNAME_DIR]
+ mycfg[KEYNAME_DIR] = cfg[KEYNAME_LEGACY_DIR]
fillup = (
- (KEYNAME_DIR, DEF_DIR, six.text_type),
- (KEYNAME_FILENAME, DEF_FILENAME, six.text_type)
- (KEYNAME_RELOAD, DEF_RELOAD, (six.text_type, list)))
+ (KEYNAME_CONFIGS, [], list),
+ (KEYNAME_DIR, DEF_DIR, six.string_types),
+ (KEYNAME_FILENAME, DEF_FILENAME, six.string_types),
+ (KEYNAME_RELOAD, DEF_RELOAD, six.string_types + (list,)))
for key, default, vtypes in fillup:
if key not in mycfg or not isinstance(mycfg[key], vtypes):
@@ -156,7 +157,8 @@ def apply_rsyslog_changes(configs, def_fname, cfg_dir):
filename = os.path.join(cfg_dir, filename)
# Truncate filename first time you see it
- omode = "ab" if filename not in files:
+ omode = "ab"
+ if filename not in files:
omode = "wb"
files.append(filename)
@@ -170,17 +172,6 @@ def apply_rsyslog_changes(configs, def_fname, cfg_dir):
def handle(name, cfg, cloud, log, _args):
- # rsyslog:
- # configs:
- # - "*.* @@192.158.1.1"
- # - content: "*.* @@192.0.2.1:10514"
- # - filename: 01-examplecom.conf
- # content: |
- # *.* @@syslogd.example.com
- # config_dir: DEF_DIR
- # config_filename: DEF_FILENAME
- # service_reload: "auto"
-
if 'rsyslog' not in cfg:
log.debug(("Skipping module named %s,"
" no 'rsyslog' key in configuration"), name)
diff --git a/tests/unittests/test_handler/test_handler_rsyslog.py b/tests/unittests/test_handler/test_handler_rsyslog.py
new file mode 100644
index 00000000..3501ff95
--- /dev/null
+++ b/tests/unittests/test_handler/test_handler_rsyslog.py
@@ -0,0 +1,113 @@
+import os
+import shutil
+import tempfile
+
+from cloudinit.config.cc_rsyslog import (
+ load_config, DEF_FILENAME, DEF_DIR, DEF_RELOAD, apply_rsyslog_changes)
+from cloudinit import util
+
+from .. import helpers as t_help
+
+
+class TestLoadConfig(t_help.TestCase):
+ def setUp(self):
+ super(TestLoadConfig, self).setUp()
+ self.basecfg = {
+ 'config_filename': DEF_FILENAME,
+ 'config_dir': DEF_DIR,
+ 'service_reload_command': DEF_RELOAD,
+ 'configs': [],
+ }
+
+ def test_legacy_full(self):
+ found = load_config({
+ 'rsyslog': ['*.* @192.168.1.1'],
+ 'rsyslog_dir': "mydir",
+ 'rsyslog_filename': "myfilename"})
+ expected = {
+ 'configs': ['*.* @192.168.1.1'],
+ 'config_dir': "mydir",
+ 'config_filename': 'myfilename',
+ 'service_reload_command': 'auto'}
+ self.assertEqual(found, expected)
+
+ def test_legacy_defaults(self):
+ found = load_config({
+ 'rsyslog': ['*.* @192.168.1.1']})
+ self.basecfg.update({
+ 'configs': ['*.* @192.168.1.1']})
+ self.assertEqual(found, self.basecfg)
+
+ def test_new_defaults(self):
+ self.assertEqual(load_config({}), self.basecfg)
+
+ def test_new_configs(self):
+ cfgs = ['*.* myhost', '*.* my2host']
+ self.basecfg.update({'configs': cfgs})
+ self.assertEqual(
+ load_config({'rsyslog': {'configs': cfgs}}),
+ self.basecfg)
+
+
+class TestApplyChanges(t_help.TestCase):
+ def setUp(self):
+ self.tmp = tempfile.mkdtemp()
+ self.addCleanup(shutil.rmtree, self.tmp)
+
+ def test_simple(self):
+ cfgline = "*.* foohost"
+ changed = apply_rsyslog_changes(
+ configs=[cfgline], def_fname="foo.cfg", cfg_dir=self.tmp)
+
+ fname = os.path.join(self.tmp, "foo.cfg")
+ self.assertEqual([fname], changed)
+ self.assertEqual(
+ util.load_file(fname), cfgline + "\n")
+
+ def test_multiple_files(self):
+ configs = [
+ '*.* foohost',
+ {'content': 'abc', 'filename': 'my.cfg'},
+ {'content': 'filefoo-content',
+ 'filename': os.path.join(self.tmp, 'mydir/mycfg')},
+ ]
+
+ changed = apply_rsyslog_changes(
+ configs=configs, def_fname="default.cfg", cfg_dir=self.tmp)
+
+ expected = [
+ (os.path.join(self.tmp, "default.cfg"),
+ "*.* foohost\n"),
+ (os.path.join(self.tmp, "my.cfg"), "abc\n"),
+ (os.path.join(self.tmp, "mydir/mycfg"), "filefoo-content\n"),
+ ]
+ self.assertEqual([f[0] for f in expected], changed)
+ actual = []
+ for fname, _content in expected:
+ util.load_file(fname)
+ actual.append((fname, util.load_file(fname),))
+ self.assertEqual(expected, actual)
+
+ def test_repeat_def(self):
+ configs = ['*.* foohost', "*.warn otherhost"]
+
+ changed = apply_rsyslog_changes(
+ configs=configs, def_fname="default.cfg", cfg_dir=self.tmp)
+
+ fname = os.path.join(self.tmp, "default.cfg")
+ self.assertEqual([fname], changed)
+
+ expected_content = '\n'.join([c for c in configs]) + '\n'
+ found_content = util.load_file(fname)
+ self.assertEqual(expected_content, found_content)
+
+ def test_multiline_content(self):
+ configs = ['line1', 'line2\nline3\n']
+
+ changed = apply_rsyslog_changes(
+ configs=configs, def_fname="default.cfg", cfg_dir=self.tmp)
+
+ fname = os.path.join(self.tmp, "default.cfg")
+ expected_content = '\n'.join([c for c in configs]) + '\n'
+ found_content = util.load_file(fname)
+ self.assertEqual(expected_content, found_content)