Procházet zdrojové kódy

added VM tools for automatical checking

UwU před 1 měsícem
rodič
revize
a47c98c676

+ 53 - 0
tools/linuxvm/check_frame.py

@@ -0,0 +1,53 @@
+"""check_frame.py [--baseline base.png] shot.png [shot.png ...] - did the wallpaper reach the screen?
+
+Without --baseline a screenshot only fails when it is pure black. With the baseline (the desktop's own screenshot taken
+before the engine started) it must also differ from it along the right edge and the top, where windows, panels and
+desktop icons rarely are, so a desktop that still shows its default background fails. Prints one "PASS|FAIL <file>: <why>"
+line per file, exit code 1 when any failed. Reference frames per wallpaper (upside down, wrong crop) would be the next
+step, see RELEASE_TESTING.md.
+"""
+import sys
+from PIL import Image, ImageChops, ImageStat
+
+
+def strips(image):
+    w, h = image.size
+    return [
+        image.crop((w * 88 // 100, h * 15 // 100, w * 99 // 100, h * 80 // 100)),
+        image.crop((w * 30 // 100, h * 2 // 100, w * 85 // 100, h * 10 // 100)),
+    ]
+
+
+def check(path, baseline):
+    image = Image.open(path).convert("RGB")
+    parts = strips(image)
+    stats = [ImageStat.Stat(part.convert("L")) for part in parts]
+    mean = sum(stat.mean[0] for stat in stats) / len(stats)
+    stddev = max(stat.stddev[0] for stat in stats)
+    if mean < 1 and stddev < 1:
+        return False, "black"
+    if baseline is not None:
+        base_parts = strips(baseline.resize(image.size))
+        diffs = [ImageStat.Stat(ImageChops.difference(a, b).convert("L")).mean[0] for a, b in zip(parts, base_parts)]
+        diff = sum(diffs) / len(diffs)
+        if diff < 6:
+            return False, f"same as the desktop without a wallpaper (difference {diff:.1f})"
+        return True, f"mean {mean:.0f}, difference from the baseline {diff:.0f}"
+    return True, f"mean {mean:.0f}, stddev {stddev:.0f}"
+
+
+def main():
+    args = sys.argv[1:]
+    baseline = None
+    if args[:1] == ["--baseline"]:
+        baseline = Image.open(args[1]).convert("RGB")
+        args = args[2:]
+    failed = False
+    for path in args:
+        ok, why = check(path, baseline)
+        failed |= not ok
+        print(f"{'PASS' if ok else 'FAIL'} {path}: {why}")
+    sys.exit(1 if failed else 0)
+
+
+main()

+ 70 - 0
tools/linuxvm/guest_test.sh

@@ -0,0 +1,70 @@
+#!/bin/bash
+# guest_test.sh - run inside a VM's desktop session by release_check.sh (or by hand): starts each test wallpaper with the
+# engine under test, takes a screenshot, checks the engine survived and stopped cleanly. One "PASS|FAIL <check>" line per
+# check, screenshots in $OUT. Needs the session environment (sessenv) sourced first.
+#   LWE_DIR       folder with lwe-hints/ (the engine), assets/ and wp-<workshop id>/ folders
+#   LWE_ARGS      how the engine is told where to draw, e.g. "--screen-root Virtual-1" or "--window 0x0x1920x1080"
+#   SHOT_CMD      screenshot command with $SHOT_FILE as the file, e.g. 'gnome-screenshot -f $SHOT_FILE'; the value
+#                 host asks the host instead (COSMIC's portal asks for permission on every call): the script creates
+#                 $SHOT_FILE.want and waits for $SHOT_FILE, host_shots.sh takes the picture through the VM's display
+#   WALLPAPERS    ids to run, default: scene with parallax, scene with particles, two web items
+#   SETTLE        seconds to wait for the first frames (default 25)
+# resolved: the engine's command line shows the real path (/var/home on Fedora Atomic systems)
+LWE_DIR=$(realpath "${LWE_DIR:-$HOME/lwe}")
+ENGINE=$LWE_DIR/lwe-hints/linux-wallpaperengine
+OUT=${OUT:-/tmp/lwe-test}
+WALLPAPERS=${WALLPAPERS:-"1111948150 1120596121 1078208425 834237690"}
+SETTLE=${SETTLE:-25}
+mkdir -p "$OUT"
+rm -f "$OUT/done"
+status=0
+
+pass() { echo "PASS $1"; }
+fail() { echo "FAIL $1"; status=1; }
+engine_pids() { pgrep -f "^$LWE_DIR/lwe-hints/linux-wallpaperengine.[b]in"; }
+
+take_shot() {
+    if [ "$SHOT_CMD" = host ]; then
+        touch "$1.want"
+        for _ in $(seq 60); do [ -s "$1" ] && break; sleep 1; done
+    else
+        eval "$SHOT_CMD" > "$2" 2>&1
+    fi
+}
+
+stop_engine() {
+    for p in $(engine_pids); do kill "$p"; done
+    for _ in $(seq 20); do engine_pids >/dev/null || return 0; sleep 0.5; done
+    for p in $(engine_pids); do kill -9 "$p"; done
+    return 1
+}
+
+stop_engine
+# the desktop without a wallpaper, the reference check_frame.py compares the others to
+sleep 2
+SHOT_FILE=$OUT/baseline.png
+rm -f "$SHOT_FILE"
+take_shot "$SHOT_FILE" "$OUT/baseline.log"
+if [ -s "$SHOT_FILE" ]; then pass "baseline screenshot taken"; else fail "no baseline screenshot"; fi
+
+for id in $WALLPAPERS; do
+    dir=$LWE_DIR/wp-$id
+    [ -d "$dir" ] || { fail "$id: wallpaper folder missing"; continue; }
+    log=$OUT/engine-$id.log
+    # shellcheck disable=SC2086
+    (setsid nohup "$ENGINE" --assets-dir "$LWE_DIR/assets" $LWE_ARGS "$dir" > "$log" 2>&1 < /dev/null &)
+    sleep "$SETTLE"
+
+    if engine_pids >/dev/null; then pass "$id: engine running after ${SETTLE}s"; else fail "$id: engine exited"; fi
+    if grep -qiE "segmentation|SIGSEGV|terminate called|Aborted|core dumped" "$log"; then fail "$id: crash text in the log"; else pass "$id: no crash text in the log"; fi
+
+    SHOT_FILE=$OUT/shot-$id.png
+    rm -f "$SHOT_FILE"
+    take_shot "$SHOT_FILE" "$OUT/shot-$id.log"
+    if [ -s "$SHOT_FILE" ]; then pass "$id: screenshot taken"; else fail "$id: no screenshot"; fi
+
+    if stop_engine; then pass "$id: engine stopped on SIGTERM"; else fail "$id: engine needed SIGKILL"; fi
+    if pgrep -f "[l]inux-wallpaperengine-web-helper" >/dev/null; then fail "$id: web helper left behind"; else pass "$id: no web helper left behind"; fi
+done
+touch "$OUT/done"
+exit $status

+ 20 - 0
tools/linuxvm/host_shots.sh

@@ -0,0 +1,20 @@
+#!/bin/bash
+# host_shots.sh <vm> <guest out dir> [seconds] - serves the "SHOT_CMD=host" requests of guest_test.sh: whenever the guest
+# creates <file>.png.want, takes a screenshot of the VM's display (the same VNC socket `linuxvm.sh shot` uses) and copies
+# it into the guest as <file>.png. Runs until the seconds (default 900) are over or the guest's test run has finished
+# (the guest creates <out dir>/done).
+HERE=$(cd "$(dirname "$0")" && pwd)
+vm=$1 out=$2 limit=${3:-900}
+tmp=$(mktemp -d)
+end=$((SECONDS + limit))
+while [ $SECONDS -lt $end ]; do
+    wanted=$("$HERE/linuxvm.sh" ssh "$vm" "ls $out/*.want 2>/dev/null; [ -e $out/done ] && echo DONE" </dev/null 2>/dev/null)
+    echo "$wanted" | grep -q '^DONE$' && break
+    for want in $(echo "$wanted" | grep '\.want$'); do
+        name=$(basename "${want%.want}")
+        "$HERE/linuxvm.sh" shot "$vm" "$tmp/$name" && "$HERE/linuxvm.sh" scp "$vm" "$tmp/$name" "${want%.want}" \
+            && "$HERE/linuxvm.sh" ssh "$vm" "rm -f $want" </dev/null
+    done
+    sleep 1
+done
+rm -rf "$tmp"

+ 18 - 0
tools/linuxvm/sessenv.sh

@@ -0,0 +1,18 @@
+#!/bin/bash
+# sessenv.sh - run inside a VM over ssh: writes ~/sessenv with the variables of the logged in desktop session (taken from
+# the environment of a client of the session), so commands run over ssh behave like they were started from the desktop.
+# Usage: ./sessenv.sh; . ~/sessenv
+vars="XDG_RUNTIME_DIR DBUS_SESSION_BUS_ADDRESS WAYLAND_DISPLAY DISPLAY XAUTHORITY XDG_SESSION_TYPE XDG_CURRENT_DESKTOP \
+XDG_SESSION_DESKTOP XDG_SESSION_ID HYPRLAND_INSTANCE_SIGNATURE KDE_SESSION_VERSION KDE_FULL_SESSION DESKTOP_SESSION"
+for name in plasmashell gsd-xsettings cosmic-panel Hyprland cinnamon xfce4-session xdg-desktop-portal; do
+    pid=$(pgrep -u "$(id -u)" -x "$name" | head -1)
+    [ -n "$pid" ] && break
+done
+[ -n "$pid" ] || { echo "no desktop session process found" >&2; exit 1; }
+: > ~/sessenv
+for v in $vars; do
+    value=$(tr '\0' '\n' < "/proc/$pid/environ" | sed -n "s/^$v=//p" | head -1)
+    [ -n "$value" ] && echo "export $v=\"$value\"" >> ~/sessenv
+done
+echo "session of $name (pid $pid):"
+cat ~/sessenv