Skip to content

Commit f21d266

Browse files
fix: Ensure 822 template renders correctly on Debian (#6381)
Fixes GH-6380
1 parent 3fc9a06 commit f21d266

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

templates/sources.list.debian.deb822.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ Signed-By: {{primary_key | default('/usr/share/keyrings/debian-archive-keyring.g
2929
## Major bug fix updates produced after the final release of the distribution.
3030
Types: deb deb-src
3131
URIs: {{security}}
32-
Suites: {{codename}}{% if codename in ('buster', 'stretch') %}/updates{% else %}-security{% endif %}
32+
Suites: {{codename}}{% if codename in ('buster', 'stretch') %}/updates{% else %}-security{% endif +%}
3333
Components: main
3434
Signed-By: {{security_key | default(primary_key, true) | default('/usr/share/keyrings/debian-archive-keyring.gpg', true)}}

tests/unittests/helpers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,16 @@ def skipUnlessJinja():
475475
return skipIf(not JINJA_AVAILABLE, "No jinja dependency present.")
476476

477477

478+
@skipUnlessJinja()
479+
def skipUnlessJinjaVersionGreaterThan(version=(0, 0, 0)):
480+
import jinja2
481+
482+
return skipIf(
483+
condition=tuple(map(int, jinja2.__version__.split("."))) < version,
484+
reason=f"jinj2 version is less than {version}",
485+
)
486+
487+
478488
def skipIfJinja():
479489
return skipIf(JINJA_AVAILABLE, "Jinja dependency present.")
480490

tests/unittests/test_render_template.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from cloudinit import subp, templater, util
88
from tests.helpers import cloud_init_project_dir
9+
from tests.unittests.helpers import skipUnlessJinjaVersionGreaterThan
910

1011
# TODO(Look to align with tools.render-template or cloudinit.distos.OSFAMILIES)
1112
DISTRO_VARIANTS = [
@@ -124,3 +125,99 @@ def test_variant_sets_network_renderer_priority_in_cloud_cfg(
124125
system_cfg = util.load_yaml(stream.read())
125126

126127
assert renderers == system_cfg["system_info"]["network"]["renderers"]
128+
129+
130+
EXPECTED_DEBIAN = """\
131+
deb testmirror testcodename main
132+
deb-src testmirror testcodename main
133+
deb testsecurity testcodename-security main
134+
deb-src testsecurity testcodename-security main
135+
deb testmirror testcodename-updates main
136+
deb-src testmirror testcodename-updates main
137+
deb testmirror testcodename-backports main
138+
deb-src testmirror testcodename-backports main
139+
"""
140+
141+
EXPECTED_DEBIAN_DEB822 = """\
142+
Types: deb deb-src
143+
URIs: testmirror
144+
Suites: testcodename testcodename-updates testcodename-backports
145+
Components: main
146+
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
147+
Types: deb deb-src
148+
URIs: testsecurity
149+
Suites: testcodename-security
150+
Components: main
151+
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
152+
"""
153+
154+
EXPECTED_UBUNTU = """\
155+
deb testmirror testcodename main restricted
156+
deb testmirror testcodename-updates main restricted
157+
deb testmirror testcodename universe
158+
deb testmirror testcodename-updates universe
159+
deb testmirror testcodename multiverse
160+
deb testmirror testcodename-updates multiverse
161+
deb testmirror testcodename-backports main restricted universe multiverse
162+
deb testsecurity testcodename-security main restricted
163+
deb testsecurity testcodename-security universe
164+
deb testsecurity testcodename-security multiverse
165+
"""
166+
167+
EXPECTED_UBUNTU_DEB822 = """\
168+
Types: deb
169+
URIs: testmirror
170+
Suites: testcodename testcodename-updates testcodename-backports
171+
Components: main universe restricted multiverse
172+
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
173+
Types: deb
174+
URIs: testsecurity
175+
Suites: testcodename-security
176+
Components: main universe restricted multiverse
177+
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
178+
"""
179+
180+
181+
class TestRenderSourcesList:
182+
@pytest.mark.parametrize(
183+
"template_path,expected",
184+
[
185+
pytest.param(
186+
"templates/sources.list.debian.tmpl",
187+
EXPECTED_DEBIAN,
188+
id="debian",
189+
),
190+
pytest.param(
191+
"templates/sources.list.debian.deb822.tmpl",
192+
EXPECTED_DEBIAN_DEB822,
193+
id="debian_822",
194+
),
195+
pytest.param(
196+
"templates/sources.list.ubuntu.tmpl",
197+
EXPECTED_UBUNTU,
198+
id="ubuntu",
199+
),
200+
pytest.param(
201+
"templates/sources.list.ubuntu.deb822.tmpl",
202+
EXPECTED_UBUNTU_DEB822,
203+
id="ubuntu_822",
204+
),
205+
],
206+
)
207+
@skipUnlessJinjaVersionGreaterThan((3, 0, 0))
208+
def test_render_sources_list_templates(
209+
self, tmpdir, template_path, expected
210+
):
211+
params = {
212+
"mirror": "testmirror",
213+
"security": "testsecurity",
214+
"codename": "testcodename",
215+
}
216+
template_path = cloud_init_project_dir(template_path)
217+
rendered = templater.render_string(open(template_path).read(), params)
218+
filtered = "\n".join(
219+
line
220+
for line in rendered.splitlines()
221+
if line.strip() and not line.strip().startswith("#")
222+
)
223+
assert filtered.strip() == expected.strip()

0 commit comments

Comments
 (0)