summaryrefslogtreecommitdiff
path: root/tests/unittests/config/test_cc_ubuntu_autoinstall.py
blob: b130ecb62ace58a5c06770653ddd255255fa39aa (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
# This file is part of cloud-init. See LICENSE file for license information.

import logging
from unittest import mock

import pytest

from cloudinit.config import cc_ubuntu_autoinstall
from cloudinit.config.schema import (
    SchemaValidationError,
    get_schema,
    validate_cloudconfig_schema,
)
from tests.unittests.helpers import skipUnlessJsonSchema
from tests.unittests.util import get_cloud

LOG = logging.getLogger(__name__)

MODPATH = "cloudinit.config.cc_ubuntu_autoinstall."

SAMPLE_SNAP_LIST_OUTPUT = """
Name                     Version                     Rev    Tracking      ...
core20                   20220527                    1518   latest/stable ...
lxd                      git-69dc707                 23315  latest/edge   ...
"""
SAMPLE_SNAP_LIST_SUBIQUITY = (
    SAMPLE_SNAP_LIST_OUTPUT
    + """
subiquity                22.06.01                 23315  latest/stable   ...
"""
)
SAMPLE_SNAP_LIST_DESKTOP_INSTALLER = (
    SAMPLE_SNAP_LIST_OUTPUT
    + """
ubuntu-desktop-installer 22.06.01                 23315  latest/stable   ...
"""
)


class TestvalidateConfigSchema:
    @pytest.mark.parametrize(
        "src_cfg,error_msg",
        [
            pytest.param(
                {"autoinstall": 1},
                "autoinstall: Expected dict type but found: int",
                id="err_non_dict",
            ),
            pytest.param(
                {"autoinstall": {}},
                "autoinstall: Missing required 'version' key",
                id="err_require_version_key",
            ),
            pytest.param(
                {"autoinstall": {"version": "v1"}},
                "autoinstall.version: Expected int type but found: str",
                id="err_version_non_int",
            ),
        ],
    )
    def test_runtime_validation_errors(self, src_cfg, error_msg):
        """cloud-init raises errors at runtime on invalid autoinstall config"""
        with pytest.raises(SchemaValidationError, match=error_msg):
            cc_ubuntu_autoinstall.validate_config_schema(src_cfg)


@mock.patch(MODPATH + "subp")
class TestHandleAutoinstall:
    """Test cc_ubuntu_autoinstall handling of config."""

    @pytest.mark.parametrize(
        "cfg,snap_list,subp_calls,logs",
        [
            pytest.param(
                {},
                SAMPLE_SNAP_LIST_OUTPUT,
                [],
                ["Skipping module named name, no 'autoinstall' key"],
                id="skip_no_cfg",
            ),
            pytest.param(
                {"autoinstall": {"version": 1}},
                SAMPLE_SNAP_LIST_OUTPUT,
                [mock.call(["snap", "list"])],
                [
                    "Skipping autoinstall module. Expected one of the Ubuntu"
                    " installer snap packages to be present: subiquity,"
                    " ubuntu-desktop-installer"
                ],
                id="valid_autoinstall_schema_checks_snaps",
            ),
            pytest.param(
                {"autoinstall": {"version": 1}},
                SAMPLE_SNAP_LIST_SUBIQUITY,
                [mock.call(["snap", "list"])],
                [
                    "Valid autoinstall schema. Config will be processed by"
                    " subiquity"
                ],
                id="valid_autoinstall_schema_sees_subiquity",
            ),
            pytest.param(
                {"autoinstall": {"version": 1}},
                SAMPLE_SNAP_LIST_DESKTOP_INSTALLER,
                [mock.call(["snap", "list"])],
                [
                    "Valid autoinstall schema. Config will be processed by"
                    " ubuntu-desktop-installer"
                ],
                id="valid_autoinstall_schema_sees_desktop_installer",
            ),
        ],
    )
    def test_handle_autoinstall_cfg(
        self, subp, cfg, snap_list, subp_calls, logs, caplog
    ):
        subp.return_value = snap_list, ""
        cloud = get_cloud(distro="ubuntu")
        cc_ubuntu_autoinstall.handle("name", cfg, cloud, None)
        assert subp_calls == subp.call_args_list
        for log in logs:
            assert log in caplog.text


class TestAutoInstallSchema:
    @pytest.mark.parametrize(
        "config, error_msg",
        (
            (
                {"autoinstall": {}},
                "autoinstall: 'version' is a required property",
            ),
        ),
    )
    @skipUnlessJsonSchema()
    def test_schema_validation(self, config, error_msg):
        if error_msg is None:
            validate_cloudconfig_schema(config, get_schema(), strict=True)
        else:
            with pytest.raises(SchemaValidationError, match=error_msg):
                validate_cloudconfig_schema(config, get_schema(), strict=True)