Files
moonmix/tools/test-container.py
T
Sprite 011dab35a2
CI / check (pull_request) Successful in 10s
CI / native (pull_request) Successful in 1m15s
Overhaul icons: bundled Lucide symbolics, soft tiles, mixer app icon, container host icons
2026-11-23 05:35:32 +00:00

285 lines
13 KiB
Python
Executable File

#!/usr/bin/env python3
"""Exercise container/desktop packaging without building or running an image."""
import os
from pathlib import Path
import shutil
import socket
import subprocess
import sys
import tempfile
import time
import unittest
ROOT = Path(__file__).resolve().parent.parent
class ContainerTests(unittest.TestCase):
def setUp(self):
temporary = tempfile.TemporaryDirectory()
self.addCleanup(temporary.cleanup)
self.directory = Path(temporary.name)
self.runtime = self.directory / "runtime"
self.runtime.mkdir(mode=0o700)
self.bind_socket(self.runtime / "wayland-test")
self.bind_socket(self.runtime / "pipewire-0")
self.arguments = self.directory / "podman-arguments"
bin_dir = self.directory / "bin"
bin_dir.mkdir()
for name, body in {
"podman": 'printf "%s\\n" "$@" > "$MOONMIX_TEST_ARGUMENTS"\n',
"id": 'printf "%s\\n" "${MOONMIX_TEST_UID:-1000}"\n',
}.items():
program = bin_dir / name
program.write_text("#!/bin/sh\n" + body)
program.chmod(0o755)
self.host = self.directory / "host"
self.host.mkdir()
self.empty_home = self.directory / "empty-home"
self.empty_home.mkdir()
self.environment = dict(os.environ)
for key in (
"PIPEWIRE_RUNTIME_DIR",
"PIPEWIRE_REMOTE",
"MOONMIX_IMAGE",
"XDG_DATA_HOME",
"MOONMIX_HOST_ROOT",
):
self.environment.pop(key, None)
self.environment.update(
PATH=f"{bin_dir}:/usr/bin:/bin",
HOME=str(self.empty_home),
XDG_RUNTIME_DIR=str(self.runtime),
WAYLAND_DISPLAY="wayland-test",
MOONMIX_HOST_ROOT=str(self.host),
MOONMIX_TEST_ARGUMENTS=str(self.arguments),
)
def bind_socket(self, path):
connection = socket.socket(socket.AF_UNIX)
self.addCleanup(connection.close)
connection.bind(str(path))
def launch(self, *arguments):
return subprocess.run(
["sh", str(ROOT / "tools/moonmix-container"), *arguments],
env=self.environment, capture_output=True, text=True,
)
def podman_arguments(self):
return self.arguments.read_text().splitlines()
def mounts(self):
arguments = self.podman_arguments()
return [arguments[i + 1] for i, value in enumerate(arguments) if value == "--mount"]
def test_rootless_read_only_local_image_and_narrow_socket_mounts(self):
result = self.launch()
self.assertEqual(result.returncode, 0, result.stderr)
arguments = self.podman_arguments()
for option in (
"--rm", "--pull=never", "--userns=keep-id", "--read-only",
"--network=none", "--cap-drop=all", "--security-opt=no-new-privileges",
):
self.assertIn(option, arguments)
self.assertEqual(arguments[arguments.index("--user") + 1], "1000:1000")
self.assertEqual(arguments[-1], "localhost/moonmix:latest")
self.assertIn("XDG_RUNTIME_DIR=/tmp/moonmix-runtime", arguments)
self.assertIn("WAYLAND_DISPLAY=/run/moonmix/wayland-0", arguments)
self.assertIn("XDG_ACTIVATION_TOKEN", arguments)
self.assertIn("DESKTOP_STARTUP_ID", arguments)
self.assertEqual(self.mounts(), [
f"type=bind,src={self.runtime}/wayland-test,dst=/run/moonmix/wayland-0,ro",
f"type=bind,src={self.runtime}/pipewire-0,dst=/run/moonmix/pipewire-0,ro",
])
self.assertIn("DBUS_SESSION_BUS_ADDRESS=unix:path=/run/moonmix/no-bus", arguments)
def test_session_bus_is_optional_and_mounted_individually(self):
self.bind_socket(self.runtime / "bus")
self.assertEqual(self.launch().returncode, 0)
self.assertIn(
f"type=bind,src={self.runtime}/bus,dst=/run/moonmix/bus,ro",
self.mounts(),
)
self.assertIn(
"DBUS_SESSION_BUS_ADDRESS=unix:path=/run/moonmix/bus",
self.podman_arguments(),
)
def test_absolute_wayland_and_pipewire_socket_paths(self):
wayland = self.directory / "display"
pipewire = self.directory / "audio"
self.bind_socket(wayland)
self.bind_socket(pipewire)
self.environment.update(WAYLAND_DISPLAY=str(wayland), PIPEWIRE_REMOTE=str(pipewire))
self.assertEqual(self.launch().returncode, 0)
self.assertEqual(self.mounts(), [
f"type=bind,src={wayland},dst=/run/moonmix/wayland-0,ro",
f"type=bind,src={pipewire},dst=/run/moonmix/pipewire-0,ro",
])
def test_pipewire_runtime_remote_and_image_overrides(self):
self.bind_socket(self.directory / "pipewire-custom")
self.environment.update(
PIPEWIRE_RUNTIME_DIR=str(self.directory),
PIPEWIRE_REMOTE="pipewire-custom",
MOONMIX_IMAGE="localhost/moonmix:test",
)
self.assertEqual(self.launch().returncode, 0)
self.assertIn(
f"type=bind,src={self.directory}/pipewire-custom,dst=/run/moonmix/pipewire-0,ro",
self.mounts(),
)
self.assertEqual(self.podman_arguments()[-1], "localhost/moonmix:test")
def test_missing_session_or_socket_fails_before_podman(self):
for key, value, message in (
("XDG_RUNTIME_DIR", "", "XDG_RUNTIME_DIR is not set"),
("WAYLAND_DISPLAY", "", "Wayland session is required"),
("WAYLAND_DISPLAY", "missing", "Wayland socket not found"),
("PIPEWIRE_REMOTE", "missing", "PipeWire socket not found"),
):
with self.subTest(key=key, value=value):
environment = self.environment.copy()
self.environment[key] = value
result = self.launch()
self.environment = environment
self.assertNotEqual(result.returncode, 0)
self.assertIn(message, result.stderr)
self.assertFalse(self.arguments.exists())
def test_root_and_unexpected_arguments_are_rejected(self):
self.environment["MOONMIX_TEST_UID"] = "0"
self.assertIn("not root", self.launch().stderr)
self.assertIn("does not take arguments", self.launch("--privileged").stderr)
self.assertFalse(self.arguments.exists())
def test_entrypoint_creates_a_private_user_owned_runtime_directory(self):
runtime = self.directory / "container-runtime"
environment = dict(self.environment, XDG_RUNTIME_DIR=str(runtime))
result = subprocess.run(
["sh", str(ROOT / "tools/container-entrypoint"), "sh", "-c", "exit 17"],
env=environment, capture_output=True, text=True,
)
self.assertEqual(result.returncode, 17, result.stderr)
self.assertEqual(runtime.stat().st_mode & 0o777, 0o700)
self.assertEqual(runtime.stat().st_uid, os.getuid())
def install(self):
# Exercise spaces, Exec field codes, and characters requiring both
# desktop-entry escaping layers. No shell expansion should take place.
home = self.directory / 'home with spaces $dollar % "quote" \\slash'
native = home / ".local/bin/moonmix"
native.parent.mkdir(parents=True)
native.write_text("existing native installation")
data_home = self.directory / "custom-data"
self.environment.update(HOME=str(home), XDG_DATA_HOME=str(data_home))
result = subprocess.run(
[sys.executable, str(ROOT / "tools/install-container.py")],
env=self.environment, capture_output=True, text=True,
)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(native.read_text(), "existing native installation")
runner = home / ".local/bin/moonmix-container"
self.assertTrue(os.access(runner, os.X_OK))
self.assertEqual(runner.read_bytes(), (ROOT / "tools/moonmix-container").read_bytes())
return data_home
def test_installer_reuses_desktop_identity_and_installs_icons(self):
data_home = self.install()
desktop = data_home / "applications/org.latha.moonmix.desktop"
self.assertIn("StartupWMClass=org.latha.moonmix\n", desktop.read_text())
self.assertIn(f'Exec=/usr/bin/env "{self.directory}/', desktop.read_text())
self.assertNotIn("Exec=moonmix\n", desktop.read_text())
self.assertTrue((data_home / "icons/hicolor/scalable/apps/org.latha.moonmix.svg").is_file())
self.assertTrue((data_home / "icons/hicolor/symbolic/apps/org.latha.moonmix-symbolic.svg").is_file())
if shutil.which("desktop-file-validate"):
subprocess.run(["desktop-file-validate", str(desktop)], check=True)
@unittest.skipUnless(shutil.which("gio"), "GIO is not installed")
def test_desktop_exec_round_trips_paths_without_shell_expansion(self):
desktop = self.install() / "applications/org.latha.moonmix.desktop"
result = subprocess.run(
["gio", "launch", str(desktop)],
env=self.environment, capture_output=True, text=True,
)
self.assertEqual(result.returncode, 0, result.stderr)
for _ in range(40):
if self.arguments.exists():
break
time.sleep(0.05)
self.assertTrue(self.arguments.exists(), result.stderr)
self.assertEqual(self.podman_arguments()[-1], "localhost/moonmix:latest")
def env_value(self, name):
arguments = self.podman_arguments()
prefix = f"{name}="
for i, arg in enumerate(arguments):
if arg == "--env" and i + 1 < len(arguments) and arguments[i + 1].startswith(prefix):
return arguments[i + 1][len(prefix):]
self.fail(f"missing --env {name}=... in {arguments}")
def test_no_host_icon_mounts_when_directories_missing(self):
self.assertEqual(self.launch().returncode, 0)
self.assertFalse(any("/run/moonmix/host" in m for m in self.mounts()))
self.assertEqual(self.env_value("XDG_DATA_DIRS"), "/usr/local/share:/usr/share")
def test_host_icon_and_desktop_directories_are_mounted_read_only(self):
home = self.directory / "home"
secret = home / ".ssh/id_rsa"
secret.parent.mkdir(parents=True)
secret.write_text("do not mount")
data_home = self.directory / "xdg-data"
expected = [
(data_home / "icons", "/run/moonmix/host/user/icons"),
(data_home / "applications", "/run/moonmix/host/user/applications"),
(data_home / "flatpak/exports/share/icons", "/run/moonmix/host/flatpak-user/icons"),
(data_home / "flatpak/exports/share/applications", "/run/moonmix/host/flatpak-user/applications"),
(self.host / "var/lib/flatpak/exports/share/icons", "/run/moonmix/host/flatpak-sys/icons"),
(self.host / "var/lib/flatpak/exports/share/applications", "/run/moonmix/host/flatpak-sys/applications"),
(self.host / "usr/share/icons", "/run/moonmix/host/usr/icons"),
(self.host / "usr/share/pixmaps", "/run/moonmix/host/usr/pixmaps"),
(self.host / "usr/share/applications", "/run/moonmix/host/usr/applications"),
]
for path, _ in expected:
path.mkdir(parents=True)
self.environment.update(HOME=str(home), XDG_DATA_HOME=str(data_home))
result = self.launch()
self.assertEqual(result.returncode, 0, result.stderr)
mounts = self.mounts()
for src, dst in expected:
self.assertIn(f"type=bind,src={src},dst={dst},ro", mounts)
self.assertFalse(any(str(home) in m for m in mounts))
self.assertEqual(
self.env_value("XDG_DATA_DIRS"),
"/run/moonmix/host/user:/run/moonmix/host/flatpak-user:/run/moonmix/host/flatpak-sys:/run/moonmix/host/usr:/usr/local/share:/usr/share",
)
def test_partial_host_icon_mounts_and_home_fallback(self):
home = self.directory / "home-fallback"
user_icons = home / ".local/share/icons"
sys_pixmaps = self.host / "usr/share/pixmaps"
sys_apps = self.host / "usr/share/applications"
for path in (user_icons, sys_pixmaps, sys_apps):
path.mkdir(parents=True)
self.environment["HOME"] = str(home)
result = self.launch()
self.assertEqual(result.returncode, 0, result.stderr)
mounts = self.mounts()
self.assertEqual(mounts, [
f"type=bind,src={self.runtime}/wayland-test,dst=/run/moonmix/wayland-0,ro",
f"type=bind,src={self.runtime}/pipewire-0,dst=/run/moonmix/pipewire-0,ro",
f"type=bind,src={user_icons},dst=/run/moonmix/host/user/icons,ro",
f"type=bind,src={sys_pixmaps},dst=/run/moonmix/host/usr/pixmaps,ro",
f"type=bind,src={sys_apps},dst=/run/moonmix/host/usr/applications,ro",
])
self.assertEqual(
self.env_value("XDG_DATA_DIRS"),
"/run/moonmix/host/user:/run/moonmix/host/usr:/usr/local/share:/usr/share",
)
if __name__ == "__main__":
unittest.main()