summaryrefslogtreecommitdiff
path: root/tests/unittests/test_handler/test_handler_lxd.py
blob: 14366a1013593645e91690b54d86b37cf5be5502 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from cloudinit.config import cc_lxd
from cloudinit.sources import DataSourceNoCloud
from cloudinit import (distros, helpers, cloud)
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.assertEqual(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.assertEqual(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)

    def test_lxd_debconf_new_full(self):
        data = {"mode": "new",
                "name": "testbr0",
                "ipv4_address": "10.0.8.1",
                "ipv4_netmask": "24",
                "ipv4_dhcp_first": "10.0.8.2",
                "ipv4_dhcp_last": "10.0.8.254",
                "ipv4_dhcp_leases": "250",
                "ipv4_nat": "true",
                "ipv6_address": "fd98:9e0:3744::1",
                "ipv6_netmask": "64",
                "ipv6_nat": "true",
                "domain": "lxd"}
        self.assertEqual(
            cc_lxd.bridge_to_debconf(data),
            {"lxd/setup-bridge": "true",
             "lxd/bridge-name": "testbr0",
             "lxd/bridge-ipv4": "true",
             "lxd/bridge-ipv4-address": "10.0.8.1",
             "lxd/bridge-ipv4-netmask": "24",
             "lxd/bridge-ipv4-dhcp-first": "10.0.8.2",
             "lxd/bridge-ipv4-dhcp-last": "10.0.8.254",
             "lxd/bridge-ipv4-dhcp-leases": "250",
             "lxd/bridge-ipv4-nat": "true",
             "lxd/bridge-ipv6": "true",
             "lxd/bridge-ipv6-address": "fd98:9e0:3744::1",
             "lxd/bridge-ipv6-netmask": "64",
             "lxd/bridge-ipv6-nat": "true",
             "lxd/bridge-domain": "lxd"})

    def test_lxd_debconf_new_partial(self):
        data = {"mode": "new",
                "ipv6_address": "fd98:9e0:3744::1",
                "ipv6_netmask": "64",
                "ipv6_nat": "true"}
        self.assertEqual(
            cc_lxd.bridge_to_debconf(data),
            {"lxd/setup-bridge": "true",
             "lxd/bridge-ipv6": "true",
             "lxd/bridge-ipv6-address": "fd98:9e0:3744::1",
             "lxd/bridge-ipv6-netmask": "64",
             "lxd/bridge-ipv6-nat": "true"})

    def test_lxd_debconf_existing(self):
        data = {"mode": "existing",
                "name": "testbr0"}
        self.assertEqual(
            cc_lxd.bridge_to_debconf(data),
            {"lxd/setup-bridge": "false",
             "lxd/use-existing-bridge": "true",
             "lxd/bridge-name": "testbr0"})

    def test_lxd_debconf_none(self):
        data = {"mode": "none"}
        self.assertEqual(
            cc_lxd.bridge_to_debconf(data),
            {"lxd/setup-bridge": "false",
             "lxd/bridge-name": ""})

    def test_lxd_cmd_new_full(self):
        data = {"mode": "new",
                "name": "testbr0",
                "ipv4_address": "10.0.8.1",
                "ipv4_netmask": "24",
                "ipv4_dhcp_first": "10.0.8.2",
                "ipv4_dhcp_last": "10.0.8.254",
                "ipv4_dhcp_leases": "250",
                "ipv4_nat": "true",
                "ipv6_address": "fd98:9e0:3744::1",
                "ipv6_netmask": "64",
                "ipv6_nat": "true",
                "domain": "lxd"}
        self.assertEqual(
            cc_lxd.bridge_to_cmd(data),
            (["lxc", "network", "create", "testbr0",
              "ipv4.address=10.0.8.1/24", "ipv4.nat=true",
              "ipv4.dhcp.ranges=10.0.8.2-10.0.8.254",
              "ipv6.address=fd98:9e0:3744::1/64",
              "ipv6.nat=true", "dns.domain=lxd",
              "--force-local"],
             ["lxc", "network", "attach-profile",
              "testbr0", "default", "eth0", "--force-local"]))

    def test_lxd_cmd_new_partial(self):
        data = {"mode": "new",
                "ipv6_address": "fd98:9e0:3744::1",
                "ipv6_netmask": "64",
                "ipv6_nat": "true"}
        self.assertEqual(
            cc_lxd.bridge_to_cmd(data),
            (["lxc", "network", "create", "lxdbr0", "ipv4.address=none",
              "ipv6.address=fd98:9e0:3744::1/64", "ipv6.nat=true",
              "--force-local"],
             ["lxc", "network", "attach-profile",
              "lxdbr0", "default", "eth0", "--force-local"]))

    def test_lxd_cmd_existing(self):
        data = {"mode": "existing",
                "name": "testbr0"}
        self.assertEqual(
            cc_lxd.bridge_to_cmd(data),
            (None, ["lxc", "network", "attach-profile",
                    "testbr0", "default", "eth0", "--force-local"]))

    def test_lxd_cmd_none(self):
        data = {"mode": "none"}
        self.assertEqual(
            cc_lxd.bridge_to_cmd(data),
            (None, None))