summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWesley Wiedenmeier <wesley.wiedenmeier@gmail.com>2016-02-04 19:11:02 -0600
committerWesley Wiedenmeier <wesley.wiedenmeier@gmail.com>2016-02-04 19:11:02 -0600
commit1f24e50c655ccc0c49afcd3225ce8f0e0bce05b9 (patch)
tree5b5bed757348c027a5edfd5bd60df290cfbbfc8f
parenta2e251c46307fed0b91e34084c361816829f251d (diff)
downloadvyos-cloud-init-1f24e50c655ccc0c49afcd3225ce8f0e0bce05b9.tar.gz
vyos-cloud-init-1f24e50c655ccc0c49afcd3225ce8f0e0bce05b9.zip
Use mock in test_handler_lxd.py and add test for lxd installation
-rw-r--r--tests/unittests/test_handler/test_handler_lxd.py76
1 files changed, 36 insertions, 40 deletions
diff --git a/tests/unittests/test_handler/test_handler_lxd.py b/tests/unittests/test_handler/test_handler_lxd.py
index 89863d52..4d858b8f 100644
--- a/tests/unittests/test_handler/test_handler_lxd.py
+++ b/tests/unittests/test_handler/test_handler_lxd.py
@@ -1,28 +1,31 @@
from cloudinit.config import cc_lxd
-from cloudinit import (util, distros, helpers, cloud)
+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()
- self.unapply = []
- apply_patches([(util, 'subp', self._mock_subp)])
- self.subp_called = []
-
- def tearDown(self):
- apply_patches([i for i in reversed(self.unapply)])
-
- def _mock_subp(self, *args, **kwargs):
- if 'args' not in kwargs:
- kwargs['args'] = args[0]
- self.subp_called.append(kwargs)
- return
def _get_cloud(self, distro):
cls = distros.fetch(distro)
@@ -32,31 +35,24 @@ class TestLxd(t_help.TestCase):
cc = cloud.Cloud(ds, paths, {}, d, None)
return cc
- def test_lxd_init(self):
- cfg = {
- 'lxd': {
- 'init': {
- 'network_address': '0.0.0.0',
- 'storage_backend': 'zfs',
- 'storage_pool': 'poolname',
- }
- }
- }
+ @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_lxd.handle('cc_lxd', cfg, cc, LOG, [])
-
- self.assertEqual(
- self.subp_called[0].get('args'),
- ['lxd', 'init', '--auto', '--network-address', '0.0.0.0',
- '--storage-backend', 'zfs', '--storage-pool', 'poolname'])
-
-
-def apply_patches(patches):
- ret = []
- for (ref, name, replace) in patches:
- if replace is None:
- continue
- orig = getattr(ref, name)
- setattr(ref, name, replace)
- ret.append((ref, name, orig))
- return ret
+ 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(install_pkg, ('lxd',))