phoebus_os/installer/tests/vm/postinstall-build-execute.sh
2026-10-09 05:24:39 -07:00

187 lines
8.6 KiB
Bash
Executable file

#!/bin/bash
# Real compilation/installation acceptance, ONLY inside the disposable guest.
# The host driver provisions the marker after checking its qcow2 boundary.
set -euo pipefail
choice=${1:?choose firefox/libreoffice/thunderbird/calibre/gimp/balanced/tuned}
phase=${2:-execute}
case "$phase" in prepare|verify|execute) ;; *) echo "unsupported phase" >&2; exit 2 ;; esac
case "$choice" in
firefox) atom=www-client/firefox; old=www-client/firefox-bin; program=firefox ;;
libreoffice) atom=app-office/libreoffice; old=app-office/libreoffice-bin; program=libreoffice ;;
thunderbird) atom=mail-client/thunderbird; old=mail-client/thunderbird-bin; program=thunderbird ;;
calibre) atom=app-text/calibre; old=com.calibre_ebook.calibre; program=calibre ;;
gimp) atom=media-gfx/gimp; old=org.gimp.GIMP; program=gimp ;;
balanced|tuned) atom=sys-kernel/gentoo-kernel; old=''; program='' ;;
*) echo 'unsupported test choice' >&2; exit 2 ;;
esac
if [[ $EUID != 0 || ! -f /var/lib/phoebus/postinstall-test-vm ]]; then
echo 'This acceptance test requires the marked disposable VM and root.' >&2
exit 2
fi
manufacturer=$(cat /sys/class/dmi/id/sys_vendor)
case "$manufacturer" in QEMU|KVM) ;; *) echo 'refusing a non-QEMU guest' >&2; exit 2 ;; esac
trap 'echo "CASE source-build-$choice FAIL"' ERR
if [[ $phase != verify ]]; then
started=$(date +%s)
echo "$started" > /tmp/phoebus-build-started
if [[ -n $program ]]; then
runuser -u phoebus -- python3 - "$choice" <<'PY'
import hashlib
import json
import sys
from pathlib import Path
directories = {'firefox': ['.mozilla', '.config/mozilla'],
'thunderbird': ['.thunderbird', '.config/thunderbird'],
'libreoffice': ['.config/libreoffice'],
'calibre': ['.var/app/com.calibre_ebook.calibre', 'Calibre Library'],
'gimp': ['.var/app/org.gimp.GIMP']}
preserved = {}
for relative in directories[sys.argv[1]]:
directory = Path.home() / relative
directory.mkdir(parents=True, exist_ok=True)
sentinel = directory / '.phoebus-preservation-test'
sentinel.write_text('Phoebus post-install data preservation acceptance\n')
files = [sentinel]
for name in ['profiles.ini', 'compatibility.ini', 'prefs.js', 'gui.json',
'gimprc', 'registrymodifications.xcu', 'metadata.db', '*.epub']:
files.extend(directory.rglob(name))
for file in files:
preserved[str(file)] = hashlib.sha256(file.read_bytes()).hexdigest()
Path('/tmp/phoebus-build-user-data.json').write_text(json.dumps(preserved))
PY
fi
if [[ -n $program ]]; then
runuser -u phoebus -- python3 - "$choice" <<'PY'
import json
import subprocess
import sys
from pathlib import Path
types = {'firefox': ['text/html', 'x-scheme-handler/https'],
'thunderbird': ['x-scheme-handler/mailto'],
'libreoffice': ['application/vnd.oasis.opendocument.text'],
'calibre': ['application/epub+zip'], 'gimp': ['image/png']}
defaults = {mime: subprocess.check_output(['xdg-mime', 'query', 'default', mime],
text=True).strip()
for mime in types[sys.argv[1]]}
if sys.argv[1] == 'libreoffice':
writers = list(Path('/usr/share/applications').glob('libreoffice*-writer.desktop'))
assert len(writers) == 1, 'no unique original binary Writer desktop entry'
defaults[types['libreoffice'][0]] = writers[0].name
assert all(defaults.values()), 'initial desktop default is missing'
# Pin these as explicit fixture preferences. An inferred fallback can change
# when a new MIME provider appears even though no preference file was touched.
for mime, desktop_id in defaults.items():
subprocess.run(['xdg-mime', 'default', desktop_id, mime], check=True)
Path('/tmp/phoebus-build-desktop-defaults.json').write_text(json.dumps(defaults))
PY
fi
portageq match / sys-kernel/gentoo-kernel-bin > /tmp/phoebus-fallback-before
[[ -s /tmp/phoebus-fallback-before ]]
# Keep a digest of the existing generic kernel and initramfs images, so a
# successful source build cannot hide a overwritten fallback.
python3 - <<'PY'
import hashlib
import json
from pathlib import Path
images = {}
for path in Path('/efi/loader/entries').glob('*.conf'):
text = path.read_text()
if 'recovery' in text.lower() or '-phoebus-' in text:
continue
for line in text.splitlines():
words = line.split()
if len(words) == 2 and words[0] in ['linux', 'initrd']:
image = Path('/efi') / words[1].lstrip('/')
images[str(image)] = hashlib.sha256(image.read_bytes()).hexdigest()
assert len(images) >= 2, 'no complete generic kernel entry'
Path('/tmp/phoebus-fallback-images.json').write_text(json.dumps(images))
PY
fi
if [[ $phase == prepare ]]; then
echo "CASE source-build-$choice prepared"
exit 0
fi
started=$(cat /tmp/phoebus-build-started)
if [[ $phase == execute ]]; then
if [[ $choice == balanced || $choice == tuned ]]; then
action="{\"action\":\"build_${choice}_kernel\",\"make_default\":false}"
else
action="{\"action\":\"convert_to_source\",\"app\":\"$choice\"}"
fi
/usr/local/bin/sys --legacy --yes --action "$action"
fi
portageq match / "$atom" > /tmp/phoebus-source-after
[[ -s /tmp/phoebus-source-after ]]
portageq match / sys-kernel/gentoo-kernel-bin > /tmp/phoebus-fallback-after
cmp /tmp/phoebus-fallback-before /tmp/phoebus-fallback-after
python3 - <<'PY'
import hashlib
import json
from pathlib import Path
for image, expected in json.loads(Path('/tmp/phoebus-fallback-images.json').read_text()).items():
assert hashlib.sha256(Path(image).read_bytes()).hexdigest() == expected, image
print('CASE generic-kernel-images-preserved PASS')
PY
if [[ -n $program ]]; then
python3 - <<'PY'
import hashlib
import json
from pathlib import Path
for file, expected in json.loads(Path('/tmp/phoebus-build-user-data.json').read_text()).items():
assert hashlib.sha256(Path(file).read_bytes()).hexdigest() == expected, file
print('CASE converted-user-data-preserved PASS')
PY
if [[ $old == */* ]]; then
[[ -z $(portageq match / "$old") ]]
else
if flatpak info --system "$old" >/dev/null 2>&1; then
echo 'replaced Flatpak is still installed' >&2
exit 1
fi
fi
# Use the real installation user. Browsers retain their normal sandbox.
timeout 60 runuser -u phoebus -- env QT_QPA_PLATFORM=offscreen "$program" --version
runuser -u phoebus -- python3 - "$choice" <<'PY'
import json
import subprocess
import sys
from pathlib import Path
directories = [Path.home() / '.local/share/applications',
Path('/usr/local/share/applications'), Path('/usr/share/applications'),
Path('/var/lib/flatpak/exports/share/applications')]
for mime, previous in json.loads(Path('/tmp/phoebus-build-desktop-defaults.json').read_text()).items():
desktop_id = subprocess.check_output(['xdg-mime', 'query', 'default', mime], text=True).strip()
assert desktop_id == previous, f'{mime}: desktop default changed from {previous} to {desktop_id}'
entry = next((directory / desktop_id for directory in directories
if (directory / desktop_id).is_file()), None)
assert desktop_id and entry, f'{mime}: default application no longer resolves'
text = entry.read_text()
removed_flatpaks = {'calibre': 'com.calibre_ebook.calibre.desktop', 'gimp': 'org.gimp.GIMP.desktop'}
if desktop_id == removed_flatpaks.get(sys.argv[1]):
assert 'flatpak run' not in text, f'{mime}: default still launches removed Flatpak'
assert 'Exec=firefox-bin' not in text and 'Exec=thunderbird-bin' not in text, f'{mime}: default still launches removed binary'
print('CASE converted-desktop-defaults-preserved PASS')
PY
else
test -s "/efi/loader/entries/phoebus-${choice}.conf"
grep -q -- "-phoebus-${choice}/linux" "/efi/loader/entries/phoebus-${choice}.conf"
if grep -q '^default \*$' /efi/loader/loader.conf; then
echo 'wildcard default could select the new source kernel' >&2
exit 1
fi
python3 - <<'PY'
from pathlib import Path
for entry in Path('/efi/loader/entries').glob('*.conf'):
for line in entry.read_text().splitlines():
words = line.split()
if len(words) == 2 and words[0] in ['linux', 'initrd']:
image = Path('/efi') / words[1].lstrip('/')
assert image.is_file(), f'{entry.name}: missing {image}'
if words[0] == 'linux':
modules = Path('/usr/lib/modules') / image.parent.name / 'kernel'
assert any(modules.rglob('*.ko*')), f'{entry.name}: missing kernel modules'
print('CASE all-boot-entry-images-present PASS')
PY
fi
echo "BUILD_SECONDS=$(( $(date +%s) - started ))"
echo "CASE source-build-$choice PASS"