summaryrefslogtreecommitdiff
path: root/tests/unittests/config/test_cc_lxd.py
blob: 184b586ec99a43249b0e85f37abf81a5143cfa89 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# This file is part of cloud-init. See LICENSE file for license information.
import re
from copy import deepcopy
from unittest import mock

import pytest

from cloudinit.config import cc_lxd
from cloudinit.config.schema import (
    SchemaValidationError,
    get_schema,
    validate_cloudconfig_schema,
)
from tests.unittests import helpers as t_help
from tests.unittests.util import get_cloud

BACKEND_DEF = (
    ("zfs", "zfs", "zfsutils-linux"),
    ("btrfs", "mkfs.btrfs", "btrfs-progs"),
    ("lvm", "lvcreate", "lvm2"),
    ("dir", None, None),
)
LXD_INIT_CFG = {
    "lxd": {
        "init": {
            "network_address": "0.0.0.0",
            "storage_backend": "zfs",
            "storage_pool": "poolname",
        }
    }
}


class TestLxd(t_help.CiTestCase):

    with_logs = True

    @mock.patch("cloudinit.config.cc_lxd.util.system_info")
    @mock.patch("cloudinit.config.cc_lxd.os.path.exists", return_value=True)
    @mock.patch("cloudinit.config.cc_lxd.subp.subp", return_value=True)
    @mock.patch("cloudinit.config.cc_lxd.subp.which", return_value=False)
    @mock.patch(
        "cloudinit.config.cc_lxd.maybe_cleanup_default", return_value=None
    )
    def test_lxd_init(self, maybe_clean, which, subp, exists, system_info):
        system_info.return_value = {"uname": [0, 1, "mykernel"]}
        cc = get_cloud(mocked_distro=True)
        install = cc.distro.install_packages

        for backend, cmd, package in BACKEND_DEF:
            lxd_cfg = deepcopy(LXD_INIT_CFG)
            lxd_cfg["lxd"]["init"]["storage_backend"] = backend
            subp.call_args_list = []
            install.call_args_list = []
            exists.call_args_list = []
            cc_lxd.handle("cc_lxd", lxd_cfg, cc, self.logger, [])
            if cmd:
                which.assert_called_with(cmd)
            # no bridge config, so maybe_cleanup should not be called.
            self.assertFalse(maybe_clean.called)
            self.assertEqual(
                [
                    mock.call(list(filter(None, ["lxd", package]))),
                ],
                install.call_args_list,
            )
            self.assertEqual(
                [
                    mock.call(["lxd", "waitready", "--timeout=300"]),
                    mock.call(
                        [
                            "lxd",
                            "init",
                            "--auto",
                            "--network-address=0.0.0.0",
                            f"--storage-backend={backend}",
                            "--storage-pool=poolname",
                        ]
                    ),
                ],
                subp.call_args_list,
            )

            if backend == "lvm":
                self.assertEqual(
                    [
                        mock.call(
                            "/lib/modules/mykernel/"
                            "kernel/drivers/md/dm-thin-pool.ko"
                        )
                    ],
                    exists.call_args_list,
                )
            else:
                self.assertEqual([], exists.call_args_list)

    @mock.patch("cloudinit.config.cc_lxd.maybe_cleanup_default")
    @mock.patch("cloudinit.config.cc_lxd.subp")
    def test_lxd_install(self, mock_subp, m_maybe_clean):
        cc = get_cloud()
        cc.distro = mock.MagicMock()
        mock_subp.which.return_value = None
        cc_lxd.handle("cc_lxd", LXD_INIT_CFG, cc, self.logger, [])
        self.assertNotIn("WARN", self.logs.getvalue())
        self.assertTrue(cc.distro.install_packages.called)
        cc_lxd.handle("cc_lxd", LXD_INIT_CFG, cc, self.logger, [])
        self.assertFalse(m_maybe_clean.called)
        install_pkg = cc.distro.install_packages.call_args_list[0][0][0]
        self.assertEqual(sorted(install_pkg), ["lxd", "zfsutils-linux"])

    @mock.patch("cloudinit.config.cc_lxd.maybe_cleanup_default")
    @mock.patch("cloudinit.config.cc_lxd.subp")
    def test_no_init_does_nothing(self, mock_subp, m_maybe_clean):
        cc = get_cloud()
        cc.distro = mock.MagicMock()
        cc_lxd.handle("cc_lxd", {"lxd": {}}, cc, self.logger, [])
        self.assertFalse(cc.distro.install_packages.called)
        self.assertFalse(mock_subp.subp.called)
        self.assertFalse(m_maybe_clean.called)

    @mock.patch("cloudinit.config.cc_lxd.maybe_cleanup_default")
    @mock.patch("cloudinit.config.cc_lxd.subp")
    def test_no_lxd_does_nothing(self, mock_subp, m_maybe_clean):
        cc = get_cloud()
        cc.distro = mock.MagicMock()
        cc_lxd.handle("cc_lxd", {"package_update": True}, cc, self.logger, [])
        self.assertFalse(cc.distro.install_packages.called)
        self.assertFalse(mock_subp.subp.called)
        self.assertFalse(m_maybe_clean.called)

    @mock.patch("cloudinit.config.cc_lxd.subp")
    def test_lxd_preseed(self, mock_subp):
        cc = get_cloud()
        cc.distro = mock.MagicMock()
        cc_lxd.handle(
            "cc_lxd",
            {"lxd": {"preseed": '{"chad": True}'}},
            cc,
            self.logger,
            [],
        )
        self.assertEqual(
            [
                mock.call(["lxd", "waitready", "--timeout=300"]),
                mock.call(["lxd", "init", "--preseed"], data='{"chad": True}'),
            ],
            mock_subp.subp.call_args_list,
        )

    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",
            "mtu": 9000,
        }
        self.assertEqual(
            cc_lxd.bridge_to_cmd(data),
            (
                [
                    "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",
                    "bridge.mtu=9000",
                ],
                ["network", "attach-profile", "testbr0", "default", "eth0"],
            ),
        )

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

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

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


class TestLxdMaybeCleanupDefault(t_help.CiTestCase):
    """Test the implementation of maybe_cleanup_default."""

    defnet = cc_lxd._DEFAULT_NETWORK_NAME

    @mock.patch("cloudinit.config.cc_lxd._lxc")
    def test_network_other_than_default_not_deleted(self, m_lxc):
        """deletion or removal should only occur if bridge is default."""
        cc_lxd.maybe_cleanup_default(
            net_name="lxdbr1", did_init=True, create=True, attach=True
        )
        m_lxc.assert_not_called()

    @mock.patch("cloudinit.config.cc_lxd._lxc")
    def test_did_init_false_does_not_delete(self, m_lxc):
        """deletion or removal should only occur if did_init is True."""
        cc_lxd.maybe_cleanup_default(
            net_name=self.defnet, did_init=False, create=True, attach=True
        )
        m_lxc.assert_not_called()

    @mock.patch("cloudinit.config.cc_lxd._lxc")
    def test_network_deleted_if_create_true(self, m_lxc):
        """deletion of network should occur if create is True."""
        cc_lxd.maybe_cleanup_default(
            net_name=self.defnet, did_init=True, create=True, attach=False
        )
        m_lxc.assert_called_with(["network", "delete", self.defnet])

    @mock.patch("cloudinit.config.cc_lxd._lxc")
    def test_device_removed_if_attach_true(self, m_lxc):
        """deletion of network should occur if create is True."""
        nic_name = "my_nic"
        profile = "my_profile"
        cc_lxd.maybe_cleanup_default(
            net_name=self.defnet,
            did_init=True,
            create=False,
            attach=True,
            profile=profile,
            nic_name=nic_name,
        )
        m_lxc.assert_called_once_with(
            ["profile", "device", "remove", profile, nic_name]
        )


class TestGetRequiredPackages:
    @pytest.mark.parametrize(
        "storage_type, cmd, preseed, package",
        (
            ("zfs", "zfs", "", "zfsutils-linux"),
            ("btrfs", "mkfs.btrfs", "", "btrfs-progs"),
            ("lvm", "lvcreate", "", "lvm2"),
            ("lvm", "lvcreate", "storage_pools: [{driver: lvm}]", "lvm2"),
            ("dir", None, "", None),
        ),
    )
    @mock.patch("cloudinit.config.cc_lxd.subp.which", return_value=False)
    def test_lxd_package_install(
        self, m_which, storage_type, cmd, preseed, package
    ):
        if preseed:  # preseed & lxd.init mutually exclusive
            init_cfg = {}
        else:
            lxd_cfg = deepcopy(LXD_INIT_CFG)
            lxd_cfg["lxd"]["init"]["storage_backend"] = storage_type
            init_cfg = lxd_cfg["lxd"]["init"]

        packages = cc_lxd.get_required_packages(init_cfg, preseed)
        assert "lxd" in packages
        which_calls = [mock.call("lxd")]
        if package:
            which_calls.append(mock.call(cmd))
            assert package in packages
        assert which_calls == m_which.call_args_list


class TestLXDSchema:
    @pytest.mark.parametrize(
        "config, error_msg",
        [
            # Only allow init, bridge and preseed keys
            ({"lxd": {"bridgeo": 1}}, "Additional properties are not allowed"),
            # Only allow init.storage_backend values zfs and dir
            (
                {"lxd": {"init": {"storage_backend": "1zfs"}}},
                re.escape("not one of ['zfs', 'dir', 'lvm', 'btrfs']"),
            ),
            ({"lxd": {"init": {"storage_backend": "lvm"}}}, None),
            ({"lxd": {"init": {"storage_backend": "btrfs"}}}, None),
            ({"lxd": {"init": {"storage_backend": "zfs"}}}, None),
            # Require bridge.mode
            ({"lxd": {"bridge": {}}}, "bridge: 'mode' is a required property"),
            # Require init or bridge keys
            ({"lxd": {}}, "lxd: {} does not have enough properties"),
            # Require some non-empty preseed config of type string
            ({"lxd": {"preseed": {}}}, "not of type 'string'"),
            ({"lxd": {"preseed": ""}}, None),
            ({"lxd": {"preseed": "this is {} opaque"}}, None),
            # Require bridge.mode
            ({"lxd": {"bridge": {"mode": "new", "mtu": 9000}}}, None),
            # LXD's default value
            ({"lxd": {"bridge": {"mode": "new", "mtu": -1}}}, None),
            # No additionalProperties
            (
                {"lxd": {"init": {"invalid": None}}},
                "Additional properties are not allowed",
            ),
            (
                {"lxd": {"bridge": {"mode": None, "garbage": None}}},
                "Additional properties are not allowed",
            ),
        ],
    )
    @t_help.skipUnlessJsonSchema()
    def test_schema_validation(self, config, error_msg):
        if error_msg:
            with pytest.raises(SchemaValidationError, match=error_msg):
                validate_cloudconfig_schema(config, get_schema(), strict=True)
        else:
            validate_cloudconfig_schema(config, get_schema(), strict=True)

    @pytest.mark.parametrize(
        "init_cfg, bridge_cfg, preseed_str, error_expectation",
        (
            pytest.param(
                {}, {}, "", t_help.does_not_raise(), id="empty_cfgs_no_errors"
            ),
            pytest.param(
                {"init-cfg": 1},
                {"bridge-cfg": 2},
                "",
                t_help.does_not_raise(),
                id="cfg_init_and_bridge_allowed",
            ),
            pytest.param(
                {},
                {},
                "profiles: []",
                t_help.does_not_raise(),
                id="cfg_preseed_allowed_without_bridge_or_init",
            ),
            pytest.param(
                {"init-cfg": 1},
                {"bridge-cfg": 2},
                "profiles: []",
                pytest.raises(
                    ValueError,
                    match=re.escape(
                        "Unable to configure LXD. lxd.preseed config can not"
                        " be provided with key(s): lxd.init, lxd.bridge"
                    ),
                ),
            ),
            pytest.param(
                "nope",
                {},
                "",
                pytest.raises(
                    ValueError,
                    match=re.escape(
                        "lxd.init config must be a dictionary. found a 'str'"
                    ),
                ),
            ),
        ),
    )
    def test_supplemental_schema_validation_raises_value_error(
        self, init_cfg, bridge_cfg, preseed_str, error_expectation
    ):
        """LXD is strict on invalid user-data raising conspicuous ValueErrors
        cc_lxd.supplemental_schema_validation

        Hard errors result is faster triage/awareness of config problems than
        warnings do.
        """
        with error_expectation:
            cc_lxd.supplemental_schema_validation(
                init_cfg, bridge_cfg, preseed_str
            )


# vi: ts=4 expandtab