summaryrefslogtreecommitdiff
path: root/tests/unittests/test_handler/test_handler_rsyslog.py
diff options
context:
space:
mode:
authorBrett Holman <bholman.devel@gmail.com>2021-12-03 13:11:46 -0700
committerGitHub <noreply@github.com>2021-12-03 13:11:46 -0700
commit039c40f9b3d88ee8158604bb18ca4bf2fb5d5e51 (patch)
tree5f1b09486ccaf98ee8159de58d9a2a1ef0af5dc1 /tests/unittests/test_handler/test_handler_rsyslog.py
parentffa6fc88249aa080aa31811a45569a45e567418a (diff)
downloadvyos-cloud-init-039c40f9b3d88ee8158604bb18ca4bf2fb5d5e51.tar.gz
vyos-cloud-init-039c40f9b3d88ee8158604bb18ca4bf2fb5d5e51.zip
Reorganize unit test locations under tests/unittests (#1126)
This attempts to standardize unit test file location under test/unittests/ such that any source file located at cloudinit/path/to/file.py may have a corresponding unit test file at test/unittests/path/to/test_file.py. Noteworthy Comments: ==================== Four different duplicate test files existed: test_{gpg,util,cc_mounts,cc_resolv_conf}.py Each of these duplicate file pairs has been merged together. This is a break in git history for these files. The test suite appears to have a dependency on test order. Changing test order causes some tests to fail. This should be rectified, but for now some tests have been modified in tests/unittests/config/test_set_passwords.py. A helper class name starts with "Test" which causes pytest to try executing it as a test case, which then throws warnings "due to Class having __init__()". Silence by changing the name of the class. # helpers.py is imported in many test files, import paths change cloudinit/tests/helpers.py -> tests/unittests/helpers.py # Move directories: cloudinit/distros/tests -> tests/unittests/distros cloudinit/cmd/devel/tests -> tests/unittests/cmd/devel cloudinit/cmd/tests -> tests/unittests/cmd/ cloudinit/sources/helpers/tests -> tests/unittests/sources/helpers cloudinit/sources/tests -> tests/unittests/sources cloudinit/net/tests -> tests/unittests/net cloudinit/config/tests -> tests/unittests/config cloudinit/analyze/tests/ -> tests/unittests/analyze/ # Standardize tests already in tests/unittests/ test_datasource -> sources test_distros -> distros test_vmware -> sources/vmware test_handler -> config # this contains cloudconfig module tests test_runs -> runs
Diffstat (limited to 'tests/unittests/test_handler/test_handler_rsyslog.py')
-rw-r--r--tests/unittests/test_handler/test_handler_rsyslog.py178
1 files changed, 0 insertions, 178 deletions
diff --git a/tests/unittests/test_handler/test_handler_rsyslog.py b/tests/unittests/test_handler/test_handler_rsyslog.py
deleted file mode 100644
index 8c8e2838..00000000
--- a/tests/unittests/test_handler/test_handler_rsyslog.py
+++ /dev/null
@@ -1,178 +0,0 @@
-# This file is part of cloud-init. See LICENSE file for license information.
-
-import os
-import shutil
-import tempfile
-
-from cloudinit.config.cc_rsyslog import (
- apply_rsyslog_changes, DEF_DIR, DEF_FILENAME, DEF_RELOAD, load_config,
- parse_remotes_line, remotes_to_rsyslog_cfg)
-from cloudinit import util
-
-from cloudinit.tests 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': [],
- 'remotes': {},
- }
-
- def test_legacy_full(self):
- found = load_config({
- 'rsyslog': ['*.* @192.168.1.1'],
- 'rsyslog_dir': "mydir",
- 'rsyslog_filename': "myfilename"})
- self.basecfg.update({
- 'configs': ['*.* @192.168.1.1'],
- 'config_dir': "mydir",
- 'config_filename': 'myfilename',
- 'service_reload_command': 'auto'}
- )
-
- self.assertEqual(found, self.basecfg)
-
- 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']
-
- 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])
- found_content = util.load_file(fname)
- self.assertEqual(expected_content, found_content)
-
-
-class TestParseRemotesLine(t_help.TestCase):
- def test_valid_port(self):
- r = parse_remotes_line("foo:9")
- self.assertEqual(9, r.port)
-
- def test_invalid_port(self):
- with self.assertRaises(ValueError):
- parse_remotes_line("*.* foo:abc")
-
- def test_valid_ipv6(self):
- r = parse_remotes_line("*.* [::1]")
- self.assertEqual("*.* @[::1]", str(r))
-
- def test_valid_ipv6_with_port(self):
- r = parse_remotes_line("*.* [::1]:100")
- self.assertEqual(r.port, 100)
- self.assertEqual(r.addr, "::1")
- self.assertEqual("*.* @[::1]:100", str(r))
-
- def test_invalid_multiple_colon(self):
- with self.assertRaises(ValueError):
- parse_remotes_line("*.* ::1:100")
-
- def test_name_in_string(self):
- r = parse_remotes_line("syslog.host", name="foobar")
- self.assertEqual("*.* @syslog.host # foobar", str(r))
-
-
-class TestRemotesToSyslog(t_help.TestCase):
- def test_simple(self):
- # str rendered line must appear in remotes_to_ryslog_cfg return
- mycfg = "*.* myhost"
- myline = str(parse_remotes_line(mycfg, name="myname"))
- r = remotes_to_rsyslog_cfg({'myname': mycfg})
- lines = r.splitlines()
- self.assertEqual(1, len(lines))
- self.assertTrue(myline in r.splitlines())
-
- def test_header_footer(self):
- header = "#foo head"
- footer = "#foo foot"
- r = remotes_to_rsyslog_cfg(
- {'myname': "*.* myhost"}, header=header, footer=footer)
- lines = r.splitlines()
- self.assertTrue(header, lines[0])
- self.assertTrue(footer, lines[-1])
-
- def test_with_empty_or_null(self):
- mycfg = "*.* myhost"
- myline = str(parse_remotes_line(mycfg, name="myname"))
- r = remotes_to_rsyslog_cfg(
- {'myname': mycfg, 'removed': None, 'removed2': ""})
- lines = r.splitlines()
- self.assertEqual(1, len(lines))
- self.assertTrue(myline in r.splitlines())
-
-# vi: ts=4 expandtab