summaryrefslogtreecommitdiff
path: root/tests/unittests/test_handler
diff options
context:
space:
mode:
authorEmmanuel Thomé <Emmanuel.Thome@inria.fr>2020-09-15 21:51:52 +0200
committerGitHub <noreply@github.com>2020-09-15 15:51:52 -0400
commit6d332e5c8dbfb6521a530b1fa49d73da51efff96 (patch)
tree501e0dfcead71b501f1a1be1fc08df7c988975d4 /tests/unittests/test_handler
parent839016e3014d783354bc380799d914ff81ee4efa (diff)
downloadvyos-cloud-init-6d332e5c8dbfb6521a530b1fa49d73da51efff96.tar.gz
vyos-cloud-init-6d332e5c8dbfb6521a530b1fa49d73da51efff96.zip
create a shutdown_command method in distro classes (#567)
Under FreeBSD, we want to use "shutdown -p" for poweroff. Alpine Linux also has some specificities. We choose to define a method that returns the shutdown command line to use, rather than a method that actually does the shutdown. This makes it easier to have the tests in test_handler_power_state do their verifications. Two tests are added for the special behaviours that are known so far.
Diffstat (limited to 'tests/unittests/test_handler')
-rw-r--r--tests/unittests/test_handler/test_handler_power_state.py58
1 files changed, 44 insertions, 14 deletions
diff --git a/tests/unittests/test_handler/test_handler_power_state.py b/tests/unittests/test_handler/test_handler_power_state.py
index 93b24fdc..4ac49424 100644
--- a/tests/unittests/test_handler/test_handler_power_state.py
+++ b/tests/unittests/test_handler/test_handler_power_state.py
@@ -4,72 +4,102 @@ import sys
from cloudinit.config import cc_power_state_change as psc
+from cloudinit import distros
+from cloudinit import helpers
+
from cloudinit.tests import helpers as t_help
from cloudinit.tests.helpers import mock
class TestLoadPowerState(t_help.TestCase):
+ def setUp(self):
+ super(TestLoadPowerState, self).setUp()
+ cls = distros.fetch('ubuntu')
+ paths = helpers.Paths({})
+ self.dist = cls('ubuntu', {}, paths)
+
def test_no_config(self):
# completely empty config should mean do nothing
- (cmd, _timeout, _condition) = psc.load_power_state({}, 'ubuntu')
+ (cmd, _timeout, _condition) = psc.load_power_state({}, self.dist)
self.assertIsNone(cmd)
def test_irrelevant_config(self):
# no power_state field in config should return None for cmd
(cmd, _timeout, _condition) = psc.load_power_state({'foo': 'bar'},
- 'ubuntu')
+ self.dist)
self.assertIsNone(cmd)
def test_invalid_mode(self):
+
cfg = {'power_state': {'mode': 'gibberish'}}
- self.assertRaises(TypeError, psc.load_power_state, cfg, 'ubuntu')
+ self.assertRaises(TypeError, psc.load_power_state, cfg, self.dist)
cfg = {'power_state': {'mode': ''}}
- self.assertRaises(TypeError, psc.load_power_state, cfg, 'ubuntu')
+ self.assertRaises(TypeError, psc.load_power_state, cfg, self.dist)
def test_empty_mode(self):
cfg = {'power_state': {'message': 'goodbye'}}
- self.assertRaises(TypeError, psc.load_power_state, cfg, 'ubuntu')
+ self.assertRaises(TypeError, psc.load_power_state, cfg, self.dist)
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, 'ubuntu'), mode=mode)
+ check_lps_ret(psc.load_power_state(cfg, self.dist), mode=mode)
def test_invalid_delay(self):
cfg = {'power_state': {'mode': 'poweroff', 'delay': 'goodbye'}}
- self.assertRaises(TypeError, psc.load_power_state, cfg, 'ubuntu')
+ self.assertRaises(TypeError, psc.load_power_state, cfg, self.dist)
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, 'ubuntu'))
+ check_lps_ret(psc.load_power_state(cfg, self.dist))
def test_message_present(self):
cfg = {'power_state': {'mode': 'poweroff', 'message': 'GOODBYE'}}
- ret = psc.load_power_state(cfg, 'ubuntu')
- check_lps_ret(psc.load_power_state(cfg, 'ubuntu'))
+ ret = psc.load_power_state(cfg, self.dist)
+ check_lps_ret(psc.load_power_state(cfg, self.dist))
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, _condition) = psc.load_power_state(cfg, 'ubuntu')
+ (cmd, _timeout, _condition) = psc.load_power_state(cfg, self.dist)
self.assertNotIn("", cmd)
- check_lps_ret(psc.load_power_state(cfg, 'ubuntu'))
+ check_lps_ret(psc.load_power_state(cfg, self.dist))
self.assertTrue(len(cmd) == 3)
def test_condition_null_raises(self):
cfg = {'power_state': {'mode': 'poweroff', 'condition': None}}
- self.assertRaises(TypeError, psc.load_power_state, cfg, 'ubuntu')
+ self.assertRaises(TypeError, psc.load_power_state, cfg, self.dist)
def test_condition_default_is_true(self):
cfg = {'power_state': {'mode': 'poweroff'}}
- _cmd, _timeout, cond = psc.load_power_state(cfg, 'ubuntu')
+ _cmd, _timeout, cond = psc.load_power_state(cfg, self.dist)
self.assertEqual(cond, True)
+ def test_freebsd_poweroff_uses_lowercase_p(self):
+ cls = distros.fetch('freebsd')
+ paths = helpers.Paths({})
+ freebsd = cls('freebsd', {}, paths)
+ cfg = {'power_state': {'mode': 'poweroff'}}
+ ret = psc.load_power_state(cfg, freebsd)
+ self.assertIn('-p', ret[0])
+
+ def test_alpine_delay(self):
+ # alpine takes delay in seconds.
+ cls = distros.fetch('alpine')
+ paths = helpers.Paths({})
+ alpine = cls('alpine', {}, paths)
+ cfg = {'power_state': {'mode': 'poweroff', 'delay': ''}}
+ for delay, value in (('now', 0), ("+1", 60), ("+30", 1800)):
+ cfg['power_state']['delay'] = delay
+ ret = psc.load_power_state(cfg, alpine)
+ self.assertEqual('-d', ret[0][1])
+ self.assertEqual(str(value), ret[0][2])
+
class TestCheckCondition(t_help.TestCase):
def cmd_with_exit(self, rc):