diff options
Diffstat (limited to 'tests/unittests/test_handler')
4 files changed, 80 insertions, 6 deletions
| diff --git a/tests/unittests/test_handler/test_handler_lxd.py b/tests/unittests/test_handler/test_handler_lxd.py new file mode 100644 index 00000000..7ffa2a53 --- /dev/null +++ b/tests/unittests/test_handler/test_handler_lxd.py @@ -0,0 +1,75 @@ +from cloudinit.config import cc_lxd +from cloudinit import (distros, helpers, cloud) +from cloudinit.sources import DataSourceNoCloud +from .. import helpers as t_help + +import logging + +try: +    from unittest import mock +except ImportError: +    import mock + +LOG = logging.getLogger(__name__) + + +class TestLxd(t_help.TestCase): +    lxd_cfg = { +        'lxd': { +            'init': { +                'network_address': '0.0.0.0', +                'storage_backend': 'zfs', +                'storage_pool': 'poolname', +            } +        } +    } + +    def setUp(self): +        super(TestLxd, self).setUp() + +    def _get_cloud(self, distro): +        cls = distros.fetch(distro) +        paths = helpers.Paths({}) +        d = cls(distro, {}, paths) +        ds = DataSourceNoCloud.DataSourceNoCloud({}, d, paths) +        cc = cloud.Cloud(ds, paths, {}, d, None) +        return cc + +    @mock.patch("cloudinit.config.cc_lxd.util") +    def test_lxd_init(self, mock_util): +        cc = self._get_cloud('ubuntu') +        mock_util.which.return_value = True +        cc_lxd.handle('cc_lxd', self.lxd_cfg, cc, LOG, []) +        self.assertTrue(mock_util.which.called) +        init_call = mock_util.subp.call_args_list[0][0][0] +        self.assertEquals(init_call, +                          ['lxd', 'init', '--auto', +                           '--network-address=0.0.0.0', +                           '--storage-backend=zfs', +                           '--storage-pool=poolname']) + +    @mock.patch("cloudinit.config.cc_lxd.util") +    def test_lxd_install(self, mock_util): +        cc = self._get_cloud('ubuntu') +        cc.distro = mock.MagicMock() +        mock_util.which.return_value = None +        cc_lxd.handle('cc_lxd', self.lxd_cfg, cc, LOG, []) +        self.assertTrue(cc.distro.install_packages.called) +        install_pkg = cc.distro.install_packages.call_args_list[0][0][0] +        self.assertEquals(sorted(install_pkg), ['lxd', 'zfs']) + +    @mock.patch("cloudinit.config.cc_lxd.util") +    def test_no_init_does_nothing(self, mock_util): +        cc = self._get_cloud('ubuntu') +        cc.distro = mock.MagicMock() +        cc_lxd.handle('cc_lxd', {'lxd': {}}, cc, LOG, []) +        self.assertFalse(cc.distro.install_packages.called) +        self.assertFalse(mock_util.subp.called) + +    @mock.patch("cloudinit.config.cc_lxd.util") +    def test_no_lxd_does_nothing(self, mock_util): +        cc = self._get_cloud('ubuntu') +        cc.distro = mock.MagicMock() +        cc_lxd.handle('cc_lxd', {'package_update': True}, cc, LOG, []) +        self.assertFalse(cc.distro.install_packages.called) +        self.assertFalse(mock_util.subp.called) diff --git a/tests/unittests/test_handler/test_handler_power_state.py b/tests/unittests/test_handler/test_handler_power_state.py index 5687b10d..04ce5687 100644 --- a/tests/unittests/test_handler/test_handler_power_state.py +++ b/tests/unittests/test_handler/test_handler_power_state.py @@ -74,7 +74,7 @@ class TestLoadPowerState(t_help.TestCase):  class TestCheckCondition(t_help.TestCase):      def cmd_with_exit(self, rc):          return([sys.executable, '-c', 'import sys; sys.exit(%s)' % rc]) -         +      def test_true_is_true(self):          self.assertEqual(psc.check_condition(True), True) @@ -94,7 +94,6 @@ class TestCheckCondition(t_help.TestCase):          self.assertEqual(mocklog.warn.call_count, 1) -  def check_lps_ret(psc_return, mode=None):      if len(psc_return) != 3:          raise TypeError("length returned = %d" % len(psc_return)) @@ -107,7 +106,7 @@ def check_lps_ret(psc_return, mode=None):      if 'shutdown' not in psc_return[0][0]:          errs.append("string 'shutdown' not in cmd") -    if 'condition' is None: +    if condition is None:          errs.append("condition was not returned")      if mode is not None: diff --git a/tests/unittests/test_handler/test_handler_seed_random.py b/tests/unittests/test_handler/test_handler_seed_random.py index 0bcdcb31..34d11f21 100644 --- a/tests/unittests/test_handler/test_handler_seed_random.py +++ b/tests/unittests/test_handler/test_handler_seed_random.py @@ -190,7 +190,8 @@ class TestRandomSeed(t_help.TestCase):          c = self._get_cloud('ubuntu', {})          self.whichdata = {}          self.assertRaises(ValueError, cc_seed_random.handle, -            'test', {'random_seed': {'command_required': True}}, c, LOG, []) +                          'test', {'random_seed': {'command_required': True}}, +                          c, LOG, [])      def test_seed_command_and_required(self):          c = self._get_cloud('ubuntu', {}) diff --git a/tests/unittests/test_handler/test_handler_snappy.py b/tests/unittests/test_handler/test_handler_snappy.py index eceb14d9..8aeff53c 100644 --- a/tests/unittests/test_handler/test_handler_snappy.py +++ b/tests/unittests/test_handler/test_handler_snappy.py @@ -125,8 +125,7 @@ class TestInstallPackages(t_help.TestCase):               "pkg1.smoser.config": "pkg1.smoser.config-data",               "pkg1.config": "pkg1.config-data",               "pkg2.smoser_0.0_amd64.snap": "pkg2-snapdata", -             "pkg2.smoser_0.0_amd64.config": "pkg2.config", -            }) +             "pkg2.smoser_0.0_amd64.config": "pkg2.config"})          ret = get_package_ops(              packages=[], configs={}, installed=[], fspath=self.tmp) | 
