Files
moonmix/tools/test-container.py
T
nandi b1f0965e02
CI / check (push) Successful in 8s
CI / native (push) Successful in 1m17s
Add OCI image and container desktop launcher
Build a release OCI image with runtime dependencies and a rootless Podman launcher for Wayland and PipeWire. Install the runner, app-menu entry, and icons per user without replacing the native executable.

Use Ubuntu 26.04 and GTK 4.22 for linux-dmabuf v6 compatibility. Allow an explicit icon-theme override so the container uses its bundled Adwaita assets. Document installation and add runner/desktop regression tests.
2026-11-22 20:56:43 -08:00

206 lines
9.0 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.environment = dict(os.environ)
for key in ("PIPEWIRE_RUNTIME_DIR", "PIPEWIRE_REMOTE", "MOONMIX_IMAGE"):
self.environment.pop(key, None)
self.environment.update(
PATH=f"{bin_dir}:/usr/bin:/bin",
XDG_RUNTIME_DIR=str(self.runtime),
WAYLAND_DISPLAY="wayland-test",
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")
if __name__ == "__main__":
unittest.main()