mirror of
https://github.com/trailofbits/dropkit
synced 2026-06-21 14:11:54 +00:00
2bf36e2304
The cloud-init template hardcoded the Docker APT repo URL for Ubuntu, which breaks on Debian images (e.g. Debian 13 "trixie"). Replace the static apt.sources block with a runcmd script that detects the distro via /etc/os-release and configures the correct Docker repo dynamically. Also uses dpkg --print-architecture instead of hardcoding amd64. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
77 lines
2.6 KiB
Python
77 lines
2.6 KiB
Python
"""Tests for cloud-init template parsing and rendering."""
|
|
|
|
from jinja2 import Template, TemplateSyntaxError
|
|
|
|
from dropkit.config import Config
|
|
|
|
|
|
def _load_default_template() -> str:
|
|
"""Load the default cloud-init template content."""
|
|
path = Config.get_default_template_path()
|
|
return path.read_text()
|
|
|
|
|
|
def test_default_template_parses():
|
|
"""Verify the default template is valid Jinja2 syntax."""
|
|
content = _load_default_template()
|
|
try:
|
|
Template(content)
|
|
except TemplateSyntaxError as exc:
|
|
raise AssertionError(f"Template has Jinja2 syntax error: {exc}") from exc
|
|
|
|
|
|
def test_default_template_renders():
|
|
"""Verify the template renders with sample variables."""
|
|
content = _load_default_template()
|
|
template = Template(content)
|
|
rendered = template.render(
|
|
username="testuser",
|
|
full_name="Test User",
|
|
email="test@example.com",
|
|
ssh_keys=["ssh-ed25519 AAAAC3... test@host"],
|
|
tailscale_enabled=True,
|
|
)
|
|
assert "testuser" in rendered
|
|
assert "ssh-ed25519 AAAAC3... test@host" in rendered
|
|
assert "git config --global user.name 'Test User'" in rendered
|
|
assert "git config --global user.email 'test@example.com'" in rendered
|
|
assert "tailscale" in rendered
|
|
|
|
|
|
def test_default_template_renders_without_tailscale():
|
|
"""Verify the Tailscale section is absent when disabled."""
|
|
content = _load_default_template()
|
|
template = Template(content)
|
|
rendered = template.render(
|
|
username="testuser",
|
|
full_name="Test User",
|
|
email="test@example.com",
|
|
ssh_keys=["ssh-ed25519 AAAAC3... test@host"],
|
|
tailscale_enabled=False,
|
|
)
|
|
assert "testuser" in rendered
|
|
assert "tailscale.com/install.sh" not in rendered
|
|
|
|
|
|
def test_docker_install_uses_distro_detection():
|
|
"""Verify Docker setup detects distro dynamically instead of hardcoding Ubuntu."""
|
|
content = _load_default_template()
|
|
template = Template(content)
|
|
rendered = template.render(
|
|
username="testuser",
|
|
full_name="Test User",
|
|
email="test@example.com",
|
|
ssh_keys=["ssh-ed25519 AAAAC3... test@host"],
|
|
tailscale_enabled=True,
|
|
)
|
|
# Must not hardcode Ubuntu — should use /etc/os-release for distro detection
|
|
assert "download.docker.com/linux/ubuntu" not in rendered
|
|
assert "/etc/os-release" in rendered
|
|
assert "download.docker.com/linux/$ID" in rendered
|
|
|
|
# Docker packages installed via runcmd, not apt sources
|
|
assert "apt-get install -y docker-ce" in rendered
|
|
|
|
# Architecture detected dynamically, not hardcoded amd64
|
|
assert "dpkg --print-architecture" in rendered
|