summaryrefslogtreecommitdiff
path: root/tests/unittests/cmd/test_main.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/cmd/test_main.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/cmd/test_main.py')
-rw-r--r--tests/unittests/cmd/test_main.py188
1 files changed, 188 insertions, 0 deletions
diff --git a/tests/unittests/cmd/test_main.py b/tests/unittests/cmd/test_main.py
new file mode 100644
index 00000000..e1ce682b
--- /dev/null
+++ b/tests/unittests/cmd/test_main.py
@@ -0,0 +1,188 @@
+# This file is part of cloud-init. See LICENSE file for license information.
+
+from collections import namedtuple
+import copy
+import os
+from io import StringIO
+from unittest import mock
+
+import pytest
+
+from cloudinit.cmd import main
+from cloudinit import safeyaml
+from cloudinit.util import (
+ ensure_dir, load_file, write_file)
+from tests.unittests.helpers import (
+ FilesystemMockingTestCase, wrap_and_call)
+
+mypaths = namedtuple('MyPaths', 'run_dir')
+myargs = namedtuple('MyArgs', 'debug files force local reporter subcommand')
+
+
+class TestMain(FilesystemMockingTestCase):
+ with_logs = True
+ allowed_subp = False
+
+ def setUp(self):
+ super(TestMain, self).setUp()
+ self.new_root = self.tmp_dir()
+ self.cloud_dir = self.tmp_path('var/lib/cloud/', dir=self.new_root)
+ os.makedirs(self.cloud_dir)
+ self.replicateTestRoot('simple_ubuntu', self.new_root)
+ self.cfg = {
+ 'datasource_list': ['None'],
+ 'runcmd': ['ls /etc'], # test ALL_DISTROS
+ 'system_info': {'paths': {'cloud_dir': self.cloud_dir,
+ 'run_dir': self.new_root}},
+ 'write_files': [
+ {
+ 'path': '/etc/blah.ini',
+ 'content': 'blah',
+ 'permissions': 0o755,
+ },
+ ],
+ 'cloud_init_modules': ['write-files', 'runcmd'],
+ }
+ cloud_cfg = safeyaml.dumps(self.cfg)
+ ensure_dir(os.path.join(self.new_root, 'etc', 'cloud'))
+ self.cloud_cfg_file = os.path.join(
+ self.new_root, 'etc', 'cloud', 'cloud.cfg')
+ write_file(self.cloud_cfg_file, cloud_cfg)
+ self.patchOS(self.new_root)
+ self.patchUtils(self.new_root)
+ self.stderr = StringIO()
+ self.patchStdoutAndStderr(stderr=self.stderr)
+
+ def test_main_init_run_net_stops_on_file_no_net(self):
+ """When no-net file is present, main_init does not process modules."""
+ stop_file = os.path.join(self.cloud_dir, 'data', 'no-net') # stop file
+ write_file(stop_file, '')
+ cmdargs = myargs(
+ debug=False, files=None, force=False, local=False, reporter=None,
+ subcommand='init')
+ (_item1, item2) = wrap_and_call(
+ 'cloudinit.cmd.main',
+ {'util.close_stdin': True,
+ 'netinfo.debug_info': 'my net debug info',
+ 'util.fixup_output': ('outfmt', 'errfmt')},
+ main.main_init, 'init', cmdargs)
+ # We should not run write_files module
+ self.assertFalse(
+ os.path.exists(os.path.join(self.new_root, 'etc/blah.ini')),
+ 'Unexpected run of write_files module produced blah.ini')
+ self.assertEqual([], item2)
+ # Instancify is called
+ instance_id_path = 'var/lib/cloud/data/instance-id'
+ self.assertFalse(
+ os.path.exists(os.path.join(self.new_root, instance_id_path)),
+ 'Unexpected call to datasource.instancify produced instance-id')
+ expected_logs = [
+ "Exiting. stop file ['{stop_file}'] existed\n".format(
+ stop_file=stop_file),
+ 'my net debug info' # netinfo.debug_info
+ ]
+ for log in expected_logs:
+ self.assertIn(log, self.stderr.getvalue())
+
+ def test_main_init_run_net_runs_modules(self):
+ """Modules like write_files are run in 'net' mode."""
+ cmdargs = myargs(
+ debug=False, files=None, force=False, local=False, reporter=None,
+ subcommand='init')
+ (_item1, item2) = wrap_and_call(
+ 'cloudinit.cmd.main',
+ {'util.close_stdin': True,
+ 'netinfo.debug_info': 'my net debug info',
+ 'util.fixup_output': ('outfmt', 'errfmt')},
+ main.main_init, 'init', cmdargs)
+ self.assertEqual([], item2)
+ # Instancify is called
+ instance_id_path = 'var/lib/cloud/data/instance-id'
+ self.assertEqual(
+ 'iid-datasource-none\n',
+ os.path.join(load_file(
+ os.path.join(self.new_root, instance_id_path))))
+ # modules are run (including write_files)
+ self.assertEqual(
+ 'blah', load_file(os.path.join(self.new_root, 'etc/blah.ini')))
+ expected_logs = [
+ 'network config is disabled by fallback', # apply_network_config
+ 'my net debug info', # netinfo.debug_info
+ 'no previous run detected'
+ ]
+ for log in expected_logs:
+ self.assertIn(log, self.stderr.getvalue())
+
+ def test_main_init_run_net_calls_set_hostname_when_metadata_present(self):
+ """When local-hostname metadata is present, call cc_set_hostname."""
+ self.cfg['datasource'] = {
+ 'None': {'metadata': {'local-hostname': 'md-hostname'}}}
+ cloud_cfg = safeyaml.dumps(self.cfg)
+ write_file(self.cloud_cfg_file, cloud_cfg)
+ cmdargs = myargs(
+ debug=False, files=None, force=False, local=False, reporter=None,
+ subcommand='init')
+
+ def set_hostname(name, cfg, cloud, log, args):
+ self.assertEqual('set-hostname', name)
+ updated_cfg = copy.deepcopy(self.cfg)
+ updated_cfg.update(
+ {'def_log_file': '/var/log/cloud-init.log',
+ 'log_cfgs': [],
+ 'syslog_fix_perms': [
+ 'syslog:adm', 'root:adm', 'root:wheel', 'root:root'
+ ],
+ 'vendor_data': {'enabled': True, 'prefix': []},
+ 'vendor_data2': {'enabled': True, 'prefix': []}})
+ updated_cfg.pop('system_info')
+
+ self.assertEqual(updated_cfg, cfg)
+ self.assertEqual(main.LOG, log)
+ self.assertIsNone(args)
+
+ (_item1, item2) = wrap_and_call(
+ 'cloudinit.cmd.main',
+ {'util.close_stdin': True,
+ 'netinfo.debug_info': 'my net debug info',
+ 'cc_set_hostname.handle': {'side_effect': set_hostname},
+ 'util.fixup_output': ('outfmt', 'errfmt')},
+ main.main_init, 'init', cmdargs)
+ self.assertEqual([], item2)
+ # Instancify is called
+ instance_id_path = 'var/lib/cloud/data/instance-id'
+ self.assertEqual(
+ 'iid-datasource-none\n',
+ os.path.join(load_file(
+ os.path.join(self.new_root, instance_id_path))))
+ # modules are run (including write_files)
+ self.assertEqual(
+ 'blah', load_file(os.path.join(self.new_root, 'etc/blah.ini')))
+ expected_logs = [
+ 'network config is disabled by fallback', # apply_network_config
+ 'my net debug info', # netinfo.debug_info
+ 'no previous run detected'
+ ]
+ for log in expected_logs:
+ self.assertIn(log, self.stderr.getvalue())
+
+
+class TestShouldBringUpInterfaces:
+ @pytest.mark.parametrize('cfg_disable,args_local,expected', [
+ (True, True, False),
+ (True, False, False),
+ (False, True, False),
+ (False, False, True),
+ ])
+ def test_should_bring_up_interfaces(
+ self, cfg_disable, args_local, expected
+ ):
+ init = mock.Mock()
+ init.cfg = {'disable_network_activation': cfg_disable}
+
+ args = mock.Mock()
+ args.local = args_local
+
+ result = main._should_bring_up_interfaces(init, args)
+ assert result == expected
+
+# vi: ts=4 expandtab