summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorChad Smith <chad.smith@canonical.com>2017-09-13 22:26:03 -0600
committerChad Smith <chad.smith@canonical.com>2017-09-13 22:26:03 -0600
commitf761f2b5f58c8cf13cfee63619f32046216cf66a (patch)
treee534cc7bd15d3ecf0cd0b09e0e3c50042dea1bcc /tests
parentcf10a2ff2e2f666d9370f38297a5a105e809ea3c (diff)
downloadvyos-cloud-init-f761f2b5f58c8cf13cfee63619f32046216cf66a.tar.gz
vyos-cloud-init-f761f2b5f58c8cf13cfee63619f32046216cf66a.zip
cloud-config modules: honor distros definitions in each module
Modules can optionally define a list of supported distros on which they can run by declaring a distros attribute in the cc_*py module. This branch fixes handling of cloudinit.stages.Modules.run_section. The behavior of run_section is now the following: - always run a module if the module doesn't declare a distros attribute - always run a module if the module declares distros = [ALL_DISTROS] - skip a module if the distribution on which we run isn't in module.distros - force a run of a skipped module if unverified_modules configuration contains the module name LP: #1715738 LP: #1715690
Diffstat (limited to 'tests')
-rw-r--r--tests/unittests/test_runs/test_simple_run.py125
1 files changed, 105 insertions, 20 deletions
diff --git a/tests/unittests/test_runs/test_simple_run.py b/tests/unittests/test_runs/test_simple_run.py
index 5cf666fe..b8fb4794 100644
--- a/tests/unittests/test_runs/test_simple_run.py
+++ b/tests/unittests/test_runs/test_simple_run.py
@@ -1,8 +1,6 @@
# This file is part of cloud-init. See LICENSE file for license information.
import os
-import shutil
-import tempfile
from cloudinit.tests import helpers
@@ -12,16 +10,19 @@ from cloudinit import util
class TestSimpleRun(helpers.FilesystemMockingTestCase):
- def _patchIn(self, root):
- self.patchOS(root)
- self.patchUtils(root)
-
- def test_none_ds(self):
- new_root = tempfile.mkdtemp()
- self.addCleanup(shutil.rmtree, new_root)
- self.replicateTestRoot('simple_ubuntu', new_root)
- cfg = {
+
+ with_logs = True
+
+ def setUp(self):
+ super(TestSimpleRun, self).setUp()
+ self.new_root = self.tmp_dir()
+ self.replicateTestRoot('simple_ubuntu', self.new_root)
+
+ # Seed cloud.cfg file for our tests
+ self.cfg = {
'datasource_list': ['None'],
+ 'runcmd': ['ls /etc'], # test ALL_DISTROS
+ 'spacewalk': {}, # test non-ubuntu distros module definition
'write_files': [
{
'path': '/etc/blah.ini',
@@ -29,14 +30,17 @@ class TestSimpleRun(helpers.FilesystemMockingTestCase):
'permissions': 0o755,
},
],
- 'cloud_init_modules': ['write-files'],
+ 'cloud_init_modules': ['write-files', 'spacewalk', 'runcmd'],
}
- cloud_cfg = util.yaml_dumps(cfg)
- util.ensure_dir(os.path.join(new_root, 'etc', 'cloud'))
- util.write_file(os.path.join(new_root, 'etc',
+ cloud_cfg = util.yaml_dumps(self.cfg)
+ util.ensure_dir(os.path.join(self.new_root, 'etc', 'cloud'))
+ util.write_file(os.path.join(self.new_root, 'etc',
'cloud', 'cloud.cfg'), cloud_cfg)
- self._patchIn(new_root)
+ self.patchOS(self.new_root)
+ self.patchUtils(self.new_root)
+ def test_none_ds_populates_var_lib_cloud(self):
+ """Init and run_section default behavior creates appropriate dirs."""
# Now start verifying whats created
initer = stages.Init()
initer.read_cfg()
@@ -51,10 +55,16 @@ class TestSimpleRun(helpers.FilesystemMockingTestCase):
initer.update()
self.assertTrue(os.path.islink("var/lib/cloud/instance"))
- initer.cloudify().run('consume_data',
- initer.consume_data,
- args=[PER_INSTANCE],
- freq=PER_INSTANCE)
+ def test_none_ds_runs_modules_which_do_not_define_distros(self):
+ """Any modules which do not define a distros attribute are run."""
+ initer = stages.Init()
+ initer.read_cfg()
+ initer.initialize()
+ initer.fetch()
+ initer.instancify()
+ initer.update()
+ initer.cloudify().run('consume_data', initer.consume_data,
+ args=[PER_INSTANCE], freq=PER_INSTANCE)
mods = stages.Modules(initer)
(which_ran, failures) = mods.run_section('cloud_init_modules')
@@ -63,5 +73,80 @@ class TestSimpleRun(helpers.FilesystemMockingTestCase):
self.assertIn('write-files', which_ran)
contents = util.load_file('/etc/blah.ini')
self.assertEqual(contents, 'blah')
+ self.assertNotIn(
+ "Skipping modules ['write-files'] because they are not verified on"
+ " distro 'ubuntu'",
+ self.logs.getvalue())
+
+ def test_none_ds_skips_modules_which_define_unmatched_distros(self):
+ """Skip modules which define distros which don't match the current."""
+ initer = stages.Init()
+ initer.read_cfg()
+ initer.initialize()
+ initer.fetch()
+ initer.instancify()
+ initer.update()
+ initer.cloudify().run('consume_data', initer.consume_data,
+ args=[PER_INSTANCE], freq=PER_INSTANCE)
+
+ mods = stages.Modules(initer)
+ (which_ran, failures) = mods.run_section('cloud_init_modules')
+ self.assertTrue(len(failures) == 0)
+ self.assertIn(
+ "Skipping modules 'spacewalk' because they are not verified on"
+ " distro 'ubuntu'",
+ self.logs.getvalue())
+ self.assertNotIn('spacewalk', which_ran)
+
+ def test_none_ds_runs_modules_which_distros_all(self):
+ """Skip modules which define distros attribute as supporting 'all'.
+
+ This is done in the module with the declaration:
+ distros = [ALL_DISTROS]. runcmd is an example.
+ """
+ initer = stages.Init()
+ initer.read_cfg()
+ initer.initialize()
+ initer.fetch()
+ initer.instancify()
+ initer.update()
+ initer.cloudify().run('consume_data', initer.consume_data,
+ args=[PER_INSTANCE], freq=PER_INSTANCE)
+
+ mods = stages.Modules(initer)
+ (which_ran, failures) = mods.run_section('cloud_init_modules')
+ self.assertTrue(len(failures) == 0)
+ self.assertIn('runcmd', which_ran)
+ self.assertNotIn(
+ "Skipping modules 'runcmd' because they are not verified on"
+ " distro 'ubuntu'",
+ self.logs.getvalue())
+
+ def test_none_ds_forces_run_via_unverified_modules(self):
+ """run_section forced skipped modules by using unverified_modules."""
+
+ # re-write cloud.cfg with unverified_modules override
+ self.cfg['unverified_modules'] = ['spacewalk'] # Would have skipped
+ cloud_cfg = util.yaml_dumps(self.cfg)
+ util.ensure_dir(os.path.join(self.new_root, 'etc', 'cloud'))
+ util.write_file(os.path.join(self.new_root, 'etc',
+ 'cloud', 'cloud.cfg'), cloud_cfg)
+
+ initer = stages.Init()
+ initer.read_cfg()
+ initer.initialize()
+ initer.fetch()
+ initer.instancify()
+ initer.update()
+ initer.cloudify().run('consume_data', initer.consume_data,
+ args=[PER_INSTANCE], freq=PER_INSTANCE)
+
+ mods = stages.Modules(initer)
+ (which_ran, failures) = mods.run_section('cloud_init_modules')
+ self.assertTrue(len(failures) == 0)
+ self.assertIn('spacewalk', which_ran)
+ self.assertIn(
+ "running unverified_modules: 'spacewalk'",
+ self.logs.getvalue())
# vi: ts=4 expandtab