summaryrefslogtreecommitdiff
path: root/tests/unittests/test_handler
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unittests/test_handler')
-rw-r--r--tests/unittests/test_handler/__init__.py0
-rw-r--r--tests/unittests/test_handler/test_handler_ca_certs.py68
-rw-r--r--tests/unittests/test_handler/test_handler_power_state.py88
-rw-r--r--tests/unittests/test_handler/test_handler_set_hostname.py58
-rw-r--r--tests/unittests/test_handler/test_handler_yum_add_repo.py68
5 files changed, 268 insertions, 14 deletions
diff --git a/tests/unittests/test_handler/__init__.py b/tests/unittests/test_handler/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/unittests/test_handler/__init__.py
diff --git a/tests/unittests/test_handler/test_handler_ca_certs.py b/tests/unittests/test_handler/test_handler_ca_certs.py
index d3df5c50..0558023a 100644
--- a/tests/unittests/test_handler/test_handler_ca_certs.py
+++ b/tests/unittests/test_handler/test_handler_ca_certs.py
@@ -77,7 +77,7 @@ class TestConfig(MockerTestCase):
"""Test that a single cert gets passed to add_ca_certs."""
config = {"ca-certs": {"trusted": ["CERT1"]}}
- self.mock_add(self.paths, ["CERT1"])
+ self.mock_add(["CERT1"])
self.mock_update()
self.mocker.replay()
@@ -87,7 +87,7 @@ class TestConfig(MockerTestCase):
"""Test that multiple certs get passed to add_ca_certs."""
config = {"ca-certs": {"trusted": ["CERT1", "CERT2"]}}
- self.mock_add(self.paths, ["CERT1", "CERT2"])
+ self.mock_add(["CERT1", "CERT2"])
self.mock_update()
self.mocker.replay()
@@ -97,7 +97,7 @@ class TestConfig(MockerTestCase):
"""Test remove_defaults works as expected."""
config = {"ca-certs": {"remove-defaults": True}}
- self.mock_remove(self.paths)
+ self.mock_remove()
self.mock_update()
self.mocker.replay()
@@ -116,8 +116,8 @@ class TestConfig(MockerTestCase):
"""Test remove_defaults is not called when config value is False."""
config = {"ca-certs": {"remove-defaults": True, "trusted": ["CERT1"]}}
- self.mock_remove(self.paths)
- self.mock_add(self.paths, ["CERT1"])
+ self.mock_remove()
+ self.mock_add(["CERT1"])
self.mock_update()
self.mocker.replay()
@@ -136,20 +136,52 @@ class TestAddCaCerts(MockerTestCase):
"""Test that no certificate are written if not provided."""
self.mocker.replace(util.write_file, passthrough=False)
self.mocker.replay()
- cc_ca_certs.add_ca_certs(self.paths, [])
+ cc_ca_certs.add_ca_certs([])
- def test_single_cert(self):
- """Test adding a single certificate to the trusted CAs."""
+ def test_single_cert_trailing_cr(self):
+ """Test adding a single certificate to the trusted CAs
+ when existing ca-certificates has trailing newline"""
cert = "CERT1\nLINE2\nLINE3"
+ ca_certs_content = "line1\nline2\ncloud-init-ca-certs.crt\nline3\n"
+ expected = "line1\nline2\nline3\ncloud-init-ca-certs.crt\n"
+
mock_write = self.mocker.replace(util.write_file, passthrough=False)
+ mock_load = self.mocker.replace(util.load_file, passthrough=False)
+
mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
cert, mode=0644)
+
+ mock_load("/etc/ca-certificates.conf")
+ self.mocker.result(ca_certs_content)
+
+ mock_write("/etc/ca-certificates.conf", expected, omode="wb")
+ self.mocker.replay()
+
+ cc_ca_certs.add_ca_certs([cert])
+
+ def test_single_cert_no_trailing_cr(self):
+ """Test adding a single certificate to the trusted CAs
+ when existing ca-certificates has no trailing newline"""
+ cert = "CERT1\nLINE2\nLINE3"
+
+ ca_certs_content = "line1\nline2\nline3"
+
+ mock_write = self.mocker.replace(util.write_file, passthrough=False)
+ mock_load = self.mocker.replace(util.load_file, passthrough=False)
+
+ mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
+ cert, mode=0644)
+
+ mock_load("/etc/ca-certificates.conf")
+ self.mocker.result(ca_certs_content)
+
mock_write("/etc/ca-certificates.conf",
- "\ncloud-init-ca-certs.crt", omode="ab")
+ "%s\n%s\n" % (ca_certs_content, "cloud-init-ca-certs.crt"),
+ omode="wb")
self.mocker.replay()
- cc_ca_certs.add_ca_certs(self.paths, [cert])
+ cc_ca_certs.add_ca_certs([cert])
def test_multiple_certs(self):
"""Test adding multiple certificates to the trusted CAs."""
@@ -157,13 +189,21 @@ class TestAddCaCerts(MockerTestCase):
expected_cert_file = "\n".join(certs)
mock_write = self.mocker.replace(util.write_file, passthrough=False)
+ mock_load = self.mocker.replace(util.load_file, passthrough=False)
+
mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
expected_cert_file, mode=0644)
- mock_write("/etc/ca-certificates.conf",
- "\ncloud-init-ca-certs.crt", omode="ab")
+
+ ca_certs_content = "line1\nline2\nline3"
+ mock_load("/etc/ca-certificates.conf")
+ self.mocker.result(ca_certs_content)
+
+ out = "%s\n%s\n" % (ca_certs_content, "cloud-init-ca-certs.crt")
+ mock_write("/etc/ca-certificates.conf", out, omode="wb")
+
self.mocker.replay()
- cc_ca_certs.add_ca_certs(self.paths, certs)
+ cc_ca_certs.add_ca_certs(certs)
class TestUpdateCaCerts(MockerTestCase):
@@ -198,4 +238,4 @@ class TestRemoveDefaultCaCerts(MockerTestCase):
"ca-certificates ca-certificates/trust_new_crts select no")
self.mocker.replay()
- cc_ca_certs.remove_default_ca_certs(self.paths)
+ cc_ca_certs.remove_default_ca_certs()
diff --git a/tests/unittests/test_handler/test_handler_power_state.py b/tests/unittests/test_handler/test_handler_power_state.py
new file mode 100644
index 00000000..f6e37fa5
--- /dev/null
+++ b/tests/unittests/test_handler/test_handler_power_state.py
@@ -0,0 +1,88 @@
+from cloudinit.config import cc_power_state_change as psc
+
+from tests.unittests import helpers as t_help
+
+
+class TestLoadPowerState(t_help.TestCase):
+ def setUp(self):
+ super(self.__class__, self).setUp()
+
+ def test_no_config(self):
+ # completely empty config should mean do nothing
+ (cmd, _timeout) = psc.load_power_state({})
+ self.assertEqual(cmd, None)
+
+ def test_irrelevant_config(self):
+ # no power_state field in config should return None for cmd
+ (cmd, _timeout) = psc.load_power_state({'foo': 'bar'})
+ self.assertEqual(cmd, None)
+
+ def test_invalid_mode(self):
+ cfg = {'power_state': {'mode': 'gibberish'}}
+ self.assertRaises(TypeError, psc.load_power_state, cfg)
+
+ cfg = {'power_state': {'mode': ''}}
+ self.assertRaises(TypeError, psc.load_power_state, cfg)
+
+ def test_empty_mode(self):
+ cfg = {'power_state': {'message': 'goodbye'}}
+ self.assertRaises(TypeError, psc.load_power_state, cfg)
+
+ def test_valid_modes(self):
+ cfg = {'power_state': {}}
+ for mode in ('halt', 'poweroff', 'reboot'):
+ cfg['power_state']['mode'] = mode
+ check_lps_ret(psc.load_power_state(cfg), mode=mode)
+
+ def test_invalid_delay(self):
+ cfg = {'power_state': {'mode': 'poweroff', 'delay': 'goodbye'}}
+ self.assertRaises(TypeError, psc.load_power_state, cfg)
+
+ def test_valid_delay(self):
+ cfg = {'power_state': {'mode': 'poweroff', 'delay': ''}}
+ for delay in ("now", "+1", "+30"):
+ cfg['power_state']['delay'] = delay
+ check_lps_ret(psc.load_power_state(cfg))
+
+ def test_message_present(self):
+ cfg = {'power_state': {'mode': 'poweroff', 'message': 'GOODBYE'}}
+ ret = psc.load_power_state(cfg)
+ check_lps_ret(psc.load_power_state(cfg))
+ self.assertIn(cfg['power_state']['message'], ret[0])
+
+ def test_no_message(self):
+ # if message is not present, then no argument should be passed for it
+ cfg = {'power_state': {'mode': 'poweroff'}}
+ (cmd, _timeout) = psc.load_power_state(cfg)
+ self.assertNotIn("", cmd)
+ check_lps_ret(psc.load_power_state(cfg))
+ self.assertTrue(len(cmd) == 3)
+
+
+def check_lps_ret(psc_return, mode=None):
+ if len(psc_return) != 2:
+ raise TypeError("length returned = %d" % len(psc_return))
+
+ errs = []
+ cmd = psc_return[0]
+ timeout = psc_return[1]
+
+ if not 'shutdown' in psc_return[0][0]:
+ errs.append("string 'shutdown' not in cmd")
+
+ if mode is not None:
+ opt = {'halt': '-H', 'poweroff': '-P', 'reboot': '-r'}[mode]
+ if opt not in psc_return[0]:
+ errs.append("opt '%s' not in cmd: %s" % (opt, cmd))
+
+ if len(cmd) != 3 and len(cmd) != 4:
+ errs.append("Invalid command length: %s" % len(cmd))
+
+ try:
+ float(timeout)
+ except:
+ errs.append("timeout failed convert to float")
+
+ if len(errs):
+ lines = ["Errors in result: %s" % str(psc_return)] + errs
+ raise Exception('\n'.join(lines))
diff --git a/tests/unittests/test_handler/test_handler_set_hostname.py b/tests/unittests/test_handler/test_handler_set_hostname.py
new file mode 100644
index 00000000..a1aba62f
--- /dev/null
+++ b/tests/unittests/test_handler/test_handler_set_hostname.py
@@ -0,0 +1,58 @@
+from cloudinit.config import cc_set_hostname
+
+from cloudinit import cloud
+from cloudinit import distros
+from cloudinit import helpers
+from cloudinit import util
+
+from tests.unittests import helpers as t_help
+
+import logging
+
+from StringIO import StringIO
+
+from configobj import ConfigObj
+
+LOG = logging.getLogger(__name__)
+
+
+class TestHostname(t_help.FilesystemMockingTestCase):
+ def setUp(self):
+ super(TestHostname, self).setUp()
+ self.tmp = self.makeDir(prefix="unittest_")
+
+ def _fetch_distro(self, kind):
+ cls = distros.fetch(kind)
+ paths = helpers.Paths({})
+ return cls(kind, {}, paths)
+
+ def test_write_hostname_rhel(self):
+ cfg = {
+ 'hostname': 'blah.blah.blah.yahoo.com',
+ }
+ distro = self._fetch_distro('rhel')
+ paths = helpers.Paths({})
+ ds = None
+ cc = cloud.Cloud(ds, paths, {}, distro, None)
+ self.patchUtils(self.tmp)
+ self.patchOS(self.tmp)
+ cc_set_hostname.handle('cc_set_hostname',
+ cfg, cc, LOG, [])
+ contents = util.load_file("/etc/sysconfig/network")
+ n_cfg = ConfigObj(StringIO(contents))
+ self.assertEquals({'HOSTNAME': 'blah.blah.blah.yahoo.com'},
+ dict(n_cfg))
+
+ def test_write_hostname_debian(self):
+ cfg = {
+ 'hostname': 'blah.blah.blah.yahoo.com',
+ }
+ distro = self._fetch_distro('debian')
+ paths = helpers.Paths({})
+ ds = None
+ cc = cloud.Cloud(ds, paths, {}, distro, None)
+ self.patchUtils(self.tmp)
+ cc_set_hostname.handle('cc_set_hostname',
+ cfg, cc, LOG, [])
+ contents = util.load_file("/etc/hostname")
+ self.assertEquals('blah', contents.strip())
diff --git a/tests/unittests/test_handler/test_handler_yum_add_repo.py b/tests/unittests/test_handler/test_handler_yum_add_repo.py
new file mode 100644
index 00000000..8df592f9
--- /dev/null
+++ b/tests/unittests/test_handler/test_handler_yum_add_repo.py
@@ -0,0 +1,68 @@
+from cloudinit import helpers
+from cloudinit import util
+
+from cloudinit.config import cc_yum_add_repo
+
+from tests.unittests import helpers
+
+import logging
+
+from StringIO import StringIO
+
+import configobj
+
+LOG = logging.getLogger(__name__)
+
+
+class TestConfig(helpers.FilesystemMockingTestCase):
+ def setUp(self):
+ super(TestConfig, self).setUp()
+ self.tmp = self.makeDir(prefix="unittest_")
+
+ def test_bad_config(self):
+ cfg = {
+ 'yum_repos': {
+ 'epel-testing': {
+ 'name': 'Extra Packages for Enterprise Linux 5 - Testing',
+ # Missing this should cause the repo not to be written
+ #'baseurl': 'http://blah.org/pub/epel/testing/5/$basearch',
+ 'enabled': False,
+ 'gpgcheck': True,
+ 'gpgkey': 'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL',
+ 'failovermethod': 'priority',
+ },
+ },
+ }
+ self.patchUtils(self.tmp)
+ cc_yum_add_repo.handle('yum_add_repo', cfg, None, LOG, [])
+ self.assertRaises(IOError, util.load_file,
+ "/etc/yum.repos.d/epel_testing.repo")
+
+ def test_write_config(self):
+ cfg = {
+ 'yum_repos': {
+ 'epel-testing': {
+ 'name': 'Extra Packages for Enterprise Linux 5 - Testing',
+ 'baseurl': 'http://blah.org/pub/epel/testing/5/$basearch',
+ 'enabled': False,
+ 'gpgcheck': True,
+ 'gpgkey': 'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL',
+ 'failovermethod': 'priority',
+ },
+ },
+ }
+ self.patchUtils(self.tmp)
+ cc_yum_add_repo.handle('yum_add_repo', cfg, None, LOG, [])
+ contents = util.load_file("/etc/yum.repos.d/epel_testing.repo")
+ contents = configobj.ConfigObj(StringIO(contents))
+ expected = {
+ 'epel_testing': {
+ 'name': 'Extra Packages for Enterprise Linux 5 - Testing',
+ 'failovermethod': 'priority',
+ 'gpgkey': 'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL',
+ 'enabled': '0',
+ 'baseurl': 'http://blah.org/pub/epel/testing/5/$basearch',
+ 'gpgcheck': '1',
+ }
+ }
+ self.assertEquals(expected, dict(contents))