guest_test.sh 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/bin/bash
  2. # guest_test.sh - run inside a VM's desktop session by release_check.sh (or by hand): starts each test wallpaper with the
  3. # engine under test, takes a screenshot, checks the engine survived and stopped cleanly. One "PASS|FAIL <check>" line per
  4. # check, screenshots in $OUT. Needs the session environment (sessenv) sourced first.
  5. # LWE_DIR folder with lwe-hints/ (the engine), assets/ and wp-<workshop id>/ folders
  6. # LWE_ARGS how the engine is told where to draw, e.g. "--screen-root Virtual-1" or "--window 0x0x1920x1080"
  7. # SHOT_CMD screenshot command with $SHOT_FILE as the file, e.g. 'gnome-screenshot -f $SHOT_FILE'; the value
  8. # host asks the host instead (COSMIC's portal asks for permission on every call): the script creates
  9. # $SHOT_FILE.want and waits for $SHOT_FILE, host_shots.sh takes the picture through the VM's display
  10. # WALLPAPERS ids to run, default: scene with parallax, scene with particles, two web items
  11. # SETTLE seconds to wait for the first frames (default 25)
  12. # resolved: the engine's command line shows the real path (/var/home on Fedora Atomic systems)
  13. LWE_DIR=$(realpath "${LWE_DIR:-$HOME/lwe}")
  14. ENGINE=$LWE_DIR/lwe-hints/linux-wallpaperengine
  15. OUT=${OUT:-/tmp/lwe-test}
  16. WALLPAPERS=${WALLPAPERS-"1111948150 1120596121 1078208425 834237690"}
  17. SETTLE=${SETTLE:-25}
  18. mkdir -p "$OUT"
  19. rm -f "$OUT/done"
  20. status=0
  21. pass() { echo "PASS $1"; }
  22. fail() { echo "FAIL $1"; status=1; }
  23. engine_pids() { pgrep -f "^$LWE_DIR/lwe-hints/linux-wallpaperengine.[b]in"; }
  24. take_shot() {
  25. if [ "$SHOT_CMD" = host ]; then
  26. touch "$1.want"
  27. for _ in $(seq 120); do [ -s "$1" ] && break; sleep 1; done
  28. else
  29. eval "$SHOT_CMD" > "$2" 2>&1
  30. fi
  31. }
  32. stop_engine() {
  33. for p in $(engine_pids); do kill "$p"; done
  34. for _ in $(seq 20); do engine_pids >/dev/null || return 0; sleep 0.5; done
  35. for p in $(engine_pids); do kill -9 "$p"; done
  36. return 1
  37. }
  38. stop_engine
  39. # the desktop without a wallpaper, the reference check_frame.py compares the others to
  40. sleep 2
  41. SHOT_FILE=$OUT/baseline.png
  42. rm -f "$SHOT_FILE"
  43. take_shot "$SHOT_FILE" "$OUT/baseline.log"
  44. if [ -s "$SHOT_FILE" ]; then pass "baseline screenshot taken"; else fail "no baseline screenshot"; fi
  45. for id in $WALLPAPERS; do
  46. dir=$LWE_DIR/wp-$id
  47. [ -d "$dir" ] || { fail "$id: wallpaper folder missing"; continue; }
  48. log=$OUT/engine-$id.log
  49. # shellcheck disable=SC2086
  50. (setsid nohup "$ENGINE" --assets-dir "$LWE_DIR/assets" $LWE_ARGS "$dir" > "$log" 2>&1 < /dev/null &)
  51. sleep "$SETTLE"
  52. if engine_pids >/dev/null; then pass "$id: engine running after ${SETTLE}s"; else fail "$id: engine exited"; fi
  53. 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
  54. SHOT_FILE=$OUT/shot-$id.png
  55. rm -f "$SHOT_FILE"
  56. take_shot "$SHOT_FILE" "$OUT/shot-$id.log"
  57. if [ -s "$SHOT_FILE" ]; then pass "$id: screenshot taken"; else fail "$id: no screenshot"; fi
  58. if stop_engine; then pass "$id: engine stopped on SIGTERM"; else fail "$id: engine needed SIGKILL"; fi
  59. 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
  60. done
  61. # the test window's own command line is the pattern: toolkits start helpers and detach, the shell's pid is not enough
  62. stop_fullscreen_window() {
  63. [ -n "$STOP_CMD" ] && pkill -f -- "^${STOP_CMD%% *}.*${STOP_CMD#* }" 2>/dev/null
  64. pkill -f "lwe-fullscreen-test|fullscreen_window.py|maximize_kde.sh|konsole -e sleep" 2>/dev/null
  65. return 0
  66. }
  67. # wait_for_log <file> <text> <seconds> <lines of it seen before>: true once more lines than that contain the text
  68. wait_for_log() {
  69. for _ in $(seq "$3"); do
  70. [ "$(grep -c "$2" "$1")" -gt "$4" ] && return 0
  71. sleep 1
  72. done
  73. return 1
  74. }
  75. # pause_check <label> <command>: a window opened by the command must pause the engine (a "Paused: windows" log line) and
  76. # closing it must resume it. An empty command is skipped.
  77. pause_check() {
  78. local label=$1 cmd=$2 log=$OUT/engine-behavior.log
  79. if [ -z "$cmd" ]; then echo "SKIP $label pause: no command for this desktop"; return; fi
  80. STOP_CMD=$cmd
  81. stop_fullscreen_window
  82. local paused_before resumed_before fs_pid
  83. paused_before=$(grep -c "Paused: windows" "$log"); resumed_before=$(grep -c "Resumed" "$log")
  84. eval "$cmd" > "$OUT/$label-window.log" 2>&1 &
  85. fs_pid=$!
  86. sleep 2
  87. if [ "$SHOT_CMD" = host ]; then
  88. touch "$OUT/focus.click"
  89. for _ in $(seq 10); do [ -e "$OUT/focus.click" ] || break; sleep 1; done
  90. fi
  91. if ! kill -0 "$fs_pid" 2>/dev/null; then
  92. fail "$label pause: the test window did not start ($(head -c 150 "$OUT/$label-window.log" | tr '\n' ' '))"
  93. elif grep -q "Fullscreen detection not supported" "$log"; then
  94. echo "SKIP $label pause: the compositor has no window state detection"
  95. elif wait_for_log "$log" "Paused: windows" 45 "$paused_before"; then
  96. pass "$label pause: engine paused"
  97. kill "$fs_pid" 2>/dev/null; stop_fullscreen_window
  98. if wait_for_log "$log" "Resumed" 45 "$resumed_before"; then pass "$label pause: engine resumed"; else fail "$label pause: no resume after the window closed"; fi
  99. else
  100. fail "$label pause: no pause within 45s of a $label window"
  101. {
  102. echo "--- state at the failed $label check, $(date +%T)"
  103. echo "window processes:"; pgrep -fa "konsole|fullscreen_window|maximize_kde" | cut -c1-120
  104. echo "engine bus names:"; busctl --user list 2>/dev/null | grep -i linuxwallpaper
  105. echo "kwin scripts left in the runtime dir:"; ls "$XDG_RUNTIME_DIR" 2>/dev/null | grep -E "^lwe-"
  106. echo "engine log tail:"; grep -v '^ALSA' "$log" | tail -5 | cut -c1-160
  107. if command -v gdbus >/dev/null && gdbus introspect --session --dest org.kde.KWin --object-path /Scripting >/dev/null 2>&1; then
  108. echo "KWin's window list:"
  109. diag=$(mktemp /tmp/lwe-diag-XXXXXX.js)
  110. echo 'for (const w of workspace.windowList()) print("LWEDIAG", w.resourceClass, w.caption, "fs=" + w.fullScreen, "max=" + (w.maximizeMode), "min=" + w.minimized, "out=" + (w.output ? w.output.name : "none"), "desktops=" + w.desktops.length, "normal=" + w.normalWindow);' > "$diag"
  111. id=$(gdbus call --session --dest org.kde.KWin --object-path /Scripting --method org.kde.kwin.Scripting.loadScript "$diag" lwe-diag | tr -dc 0-9)
  112. gdbus call --session --dest org.kde.KWin --object-path "/Scripting/Script$id" --method org.kde.kwin.Script.run >/dev/null 2>&1
  113. sleep 1
  114. journalctl --user -b --no-pager 2>/dev/null | grep LWEDIAG | tail -12 | cut -c1-220
  115. gdbus call --session --dest org.kde.KWin --object-path /Scripting --method org.kde.kwin.Scripting.unloadScript lwe-diag >/dev/null 2>&1
  116. rm -f "$diag"
  117. fi
  118. } >> "$OUT/pause-diagnostics.txt" 2>&1
  119. fi
  120. kill "$fs_pid" 2>/dev/null; stop_fullscreen_window
  121. sleep 3
  122. }
  123. # behaviour checks, BEHAVIOR=1: hotswap, pause on fullscreen, SIGKILL leaves no helper
  124. if [ "${BEHAVIOR:-0}" = 1 ]; then
  125. A=${HOTSWAP_FROM:-1120596121}
  126. B=${HOTSWAP_TO:-1111948150}
  127. ctl=$OUT/control
  128. : > "$ctl"
  129. # shellcheck disable=SC2086
  130. (setsid nohup "$ENGINE" --assets-dir "$LWE_DIR/assets" $LWE_ARGS --control-file "$ctl" "$LWE_DIR/wp-$A" > "$OUT/engine-behavior.log" 2>&1 < /dev/null &)
  131. sleep "$SETTLE"
  132. pid=$(engine_pids | head -1)
  133. if [ -z "$pid" ]; then fail "behavior: engine did not start"; else
  134. SHOT_FILE=$OUT/hot-before.png; rm -f "$SHOT_FILE"; take_shot "$SHOT_FILE" "$OUT/hot-before.log"
  135. echo "path=$LWE_DIR/wp-$B" > "$ctl"
  136. kill -USR1 "$pid"
  137. sleep 15
  138. if grep -q "Hotswapping wallpaper to" "$OUT/engine-behavior.log"; then pass "hotswap: engine took the new wallpaper"; else fail "hotswap: no hotswap in the log"; fi
  139. if kill -0 "$pid" 2>/dev/null; then pass "hotswap: same process still running"; else fail "hotswap: engine exited"; fi
  140. SHOT_FILE=$OUT/hot-after.png; rm -f "$SHOT_FILE"; take_shot "$SHOT_FILE" "$OUT/hot-after.log"
  141. # back to the first wallpaper so the CPU numbers are of a scene with particles
  142. echo "path=$LWE_DIR/wp-$A" > "$ctl"; kill -USR1 "$pid"; sleep 12
  143. pause_check "fullscreen" "$FULLSCREEN_CMD"
  144. pause_check "maximized" "$MAXIMIZE_CMD"
  145. stop_engine
  146. fi
  147. # SIGKILL: the web host must not outlive the engine
  148. # shellcheck disable=SC2086
  149. (setsid nohup "$ENGINE" --assets-dir "$LWE_DIR/assets" $LWE_ARGS "$LWE_DIR/wp-1078208425" > "$OUT/engine-kill.log" 2>&1 < /dev/null &)
  150. sleep "$SETTLE"
  151. pid=$(engine_pids | head -1)
  152. helpers_before=$(pgrep -fc "[l]inux-wallpaperengine-web-helper")
  153. kill -9 "$pid" 2>/dev/null
  154. sleep 8
  155. helpers_after=$(pgrep -fc "[l]inux-wallpaperengine-web-helper")
  156. if [ "$helpers_before" -gt 0 ] && [ "$helpers_after" -eq 0 ]; then pass "SIGKILL: $helpers_before web host processes ended with the engine"
  157. elif [ "$helpers_before" -eq 0 ]; then fail "SIGKILL: no web host was running to begin with"
  158. else fail "SIGKILL: $helpers_after of $helpers_before web host processes left behind"; pkill -f "linux-wallpaperengine-web-helper"; fi
  159. fi
  160. touch "$OUT/done"
  161. exit $status