summaryrefslogtreecommitdiff
path: root/tests/unittests/test_handler/test_handler_lxd.py
blob: 65794a41c24c99e83ddf1aa9504a24f0d397757c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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'])