File: test_render_template.py

package info (click to toggle)
cloud-init 25.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,412 kB
  • sloc: python: 135,894; sh: 3,883; makefile: 141; javascript: 30; xml: 22
file content (223 lines) | stat: -rw-r--r-- 7,266 bytes parent folder | download
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
"""Tests for tools/render-template"""

import sys

import pytest

from cloudinit import subp, templater, util
from tests.helpers import cloud_init_project_dir
from tests.unittests.helpers import skipUnlessJinjaVersionGreaterThan

# TODO(Look to align with tools.render-template or cloudinit.distos.OSFAMILIES)
DISTRO_VARIANTS = [
    "amazon",
    "arch",
    "azurelinux",
    "centos",
    "debian",
    "eurolinux",
    "fedora",
    "freebsd",
    "gentoo",
    "mariner",
    "netbsd",
    "openbsd",
    "photon",
    "raspberry-pi-os",
    "rhel",
    "suse",
    "ubuntu",
    "unknown",
]


@pytest.mark.allow_subp_for(sys.executable)
class TestRenderCloudCfg:
    cmd = [sys.executable, cloud_init_project_dir("tools/render-template")]
    tmpl_path = cloud_init_project_dir("config/cloud.cfg.tmpl")
    init_path = cloud_init_project_dir("sysvinit/freebsd/dsidentify.tmpl")

    def test_variant_sets_distro_in_cloud_cfg_subp(self, tmpdir):
        outfile = tmpdir.join("outcfg").strpath

        subp.subp(self.cmd + ["--variant", "ubuntu", self.tmpl_path, outfile])
        with open(outfile) as stream:
            system_cfg = util.load_yaml(stream.read())
        assert system_cfg["system_info"]["distro"] == "ubuntu"

    def test_variant_sets_prefix_in_cloud_cfg_subp(self, tmpdir):
        outfile = tmpdir.join("outcfg").strpath

        subp.subp(
            self.cmd
            + [
                "--variant",
                "freebsd",
                "--prefix",
                "/usr/local",
                self.init_path,
                outfile,
            ]
        )
        with open(outfile) as stream:
            init_cfg = stream.readlines()
        assert 'command="/usr/local/lib/cloud-init/ds-identify"\n' in init_cfg

    @pytest.mark.parametrize("variant", (DISTRO_VARIANTS))
    def test_variant_sets_distro_in_cloud_cfg(self, variant, tmpdir):
        """Testing parametrized inputs with imported function saves ~0.5s per
        call versus calling as subp
        """
        outfile = tmpdir.join("outcfg").strpath

        templater.render_template(
            variant, self.tmpl_path, outfile, is_yaml=True
        )
        with open(outfile) as stream:
            system_cfg = util.load_yaml(stream.read())
        if variant == "unknown":
            variant = "ubuntu"  # Unknown is defaulted to ubuntu
        assert system_cfg["system_info"]["distro"] == variant

    @pytest.mark.parametrize("variant", (DISTRO_VARIANTS))
    def test_variant_sets_default_user_in_cloud_cfg(self, variant, tmpdir):
        """Testing parametrized inputs with imported function saves ~0.5s per
        call versus calling as subp
        """
        outfile = tmpdir.join("outcfg").strpath
        templater.render_template(
            variant, self.tmpl_path, outfile, is_yaml=True
        )
        with open(outfile) as stream:
            system_cfg = util.load_yaml(stream.read())

        default_user_exceptions = {
            "amazon": "ec2-user",
            "rhel": "cloud-user",
            "centos": "cloud-user",
            "raspberry-pi-os": "pi",
            "unknown": "ubuntu",
        }
        default_user = system_cfg["system_info"]["default_user"]["name"]
        assert default_user == default_user_exceptions.get(variant, variant)

    @pytest.mark.parametrize(
        "variant,renderers",
        (
            ("freebsd", ["freebsd"]),
            ("netbsd", ["netbsd"]),
            ("openbsd", ["openbsd"]),
            ("ubuntu", ["netplan", "eni", "sysconfig"]),
            ("raspberry-pi-os", ["netplan", "network-manager"]),
        ),
    )
    def test_variant_sets_network_renderer_priority_in_cloud_cfg(
        self, variant, renderers, tmpdir
    ):
        """Testing parametrized inputs with imported function saves ~0.5s per
        call versus calling as subp
        """
        outfile = tmpdir.join("outcfg").strpath
        templater.render_template(
            variant, self.tmpl_path, outfile, is_yaml=True
        )
        with open(outfile) as stream:
            system_cfg = util.load_yaml(stream.read())

        assert renderers == system_cfg["system_info"]["network"]["renderers"]


EXPECTED_DEBIAN = """\
deb testmirror testcodename main
deb-src testmirror testcodename main
deb testsecurity testcodename-security main
deb-src testsecurity testcodename-security main
deb testmirror testcodename-updates main
deb-src testmirror testcodename-updates main
deb testmirror testcodename-backports main
deb-src testmirror testcodename-backports main
"""

EXPECTED_DEBIAN_DEB822 = """\
Types: deb deb-src
URIs: testmirror
Suites: testcodename testcodename-updates testcodename-backports
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
Types: deb deb-src
URIs: testsecurity
Suites: testcodename-security
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
"""

EXPECTED_UBUNTU = """\
deb testmirror testcodename main restricted
deb testmirror testcodename-updates main restricted
deb testmirror testcodename universe
deb testmirror testcodename-updates universe
deb testmirror testcodename multiverse
deb testmirror testcodename-updates multiverse
deb testmirror testcodename-backports main restricted universe multiverse
deb testsecurity testcodename-security main restricted
deb testsecurity testcodename-security universe
deb testsecurity testcodename-security multiverse
"""

EXPECTED_UBUNTU_DEB822 = """\
Types: deb
URIs: testmirror
Suites: testcodename testcodename-updates testcodename-backports
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
Types: deb
URIs: testsecurity
Suites: testcodename-security
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
"""


class TestRenderSourcesList:
    @pytest.mark.parametrize(
        "template_path,expected",
        [
            pytest.param(
                "templates/sources.list.debian.tmpl",
                EXPECTED_DEBIAN,
                id="debian",
            ),
            pytest.param(
                "templates/sources.list.debian.deb822.tmpl",
                EXPECTED_DEBIAN_DEB822,
                id="debian_822",
            ),
            pytest.param(
                "templates/sources.list.ubuntu.tmpl",
                EXPECTED_UBUNTU,
                id="ubuntu",
            ),
            pytest.param(
                "templates/sources.list.ubuntu.deb822.tmpl",
                EXPECTED_UBUNTU_DEB822,
                id="ubuntu_822",
            ),
        ],
    )
    @skipUnlessJinjaVersionGreaterThan((3, 0, 0))
    def test_render_sources_list_templates(
        self, tmpdir, template_path, expected
    ):
        params = {
            "mirror": "testmirror",
            "security": "testsecurity",
            "codename": "testcodename",
        }
        template_path = cloud_init_project_dir(template_path)
        rendered = templater.render_string(open(template_path).read(), params)
        filtered = "\n".join(
            line
            for line in rendered.splitlines()
            if line.strip() and not line.strip().startswith("#")
        )
        assert filtered.strip() == expected.strip()