Files
Reims-Go/internal/guesttools/control_loop_test.go
T

230 lines
7.8 KiB
Go

package guesttools
import "testing"
func TestGuestAgentFileSyncControlLoop(t *testing.T) {
agent := string(GuestAgentScript())
state := scriptSection(t, agent, "\nFILE_SYNC_PID=", "\n\n")
cleanup := scriptSection(t, agent, "\ncleanup() {", "\n\n")
worker := scriptSection(t, agent, "\nreap_file_sync() {", "\ntext_input_loop &")
config := scriptSection(t, agent, "\napply_host_config() {", "\nreport_status() {")
loop := scriptSection(t, agent, "\nwhile :; do\n", "\ndone") + "done\n"
fixture := fileSyncControlSetup + state + cleanup + worker + fileSyncControlStubs
t.Run("control_loop", func(t *testing.T) {
runShareShell(t, fixture+config+fileSyncControlExercise+loop, nil)
})
t.Run("cleanup_running_worker", func(t *testing.T) {
runShareShell(t, fixture+`
sync_files() {
: > "$TMP_DIR/sync-download.part"
sleep 30 >/dev/null 2>&1 &
child=$!
printf '%s\n' "$child" > child.pid
: > ready-0
command wait "$child"
}
run_file_sync_async
await_file ready-0
worker_pid=$FILE_SYNC_PID
start_sec=$(date +%s)
cleanup
elapsed=$(( $(date +%s) - start_sec ))
trap - EXIT
child_pid=$(cat child.pid)
command kill "$child_pid" 2>/dev/null || true
[ "$elapsed" -lt 5 ] || fail "cleanup blocked for ${elapsed}s waiting on foreground child"
check "$(cat reaped)" "$worker_pid" "cleanup reaped the worker"
check "$(cat parent-cleanup)" stopped "parent cleanup stopped the window reporter once"
[ ! -f "$FILE_SYNC_DONE" ] && [ ! -f "$TMP_DIR/sync-download.part" ] ||
fail "cleanup left files from the terminated worker"
[ ! -f "$INJECT_LOCK" ] || fail "cleanup left the injection lock"
if command kill -0 "$worker_pid" 2>/dev/null; then fail "worker survived cleanup"; fi
`, nil)
})
}
func TestGuestAgentDropMountHeartbeat(t *testing.T) {
agent := string(GuestAgentScript())
report := scriptSection(t, agent, "\nreport_status() {", "\nread_local_clipboard() {")
dropProbe := scriptSection(t, guestDropAgent, "\ndrop_helper() {", "\nplace_drop_unique() (")
loop := scriptSection(t, agent, "\nwhile :; do\n reap_file_sync\n", "\ndone") + "done\n"
runShareShell(t, dropProbe+report+`
set -eu
TMP_DIR=.
REIMS_HOST_URL=http://stub
REIMS_TOKEN=token
REIMS_VM=guest
DROP_MOUNT=./drop_mount
mount_mode=webdav
sleep_active=false
timezone_helper_ready=false
timezone_error=
mounted=1
mount_access=2
tick=0
load_conf() { :; }
apply_host_config() { :; }
ensure_webdav_mount() { :; }
ensure_drop_webdav_mount() { :; }
reap_file_sync() { :; }
current_timezone() { printf 'UTC\n'; }
urlencode() { printf '%s' "$1"; }
is_reims_dav_mounted() {
[ "$1" = "$DROP_MOUNT" ] && [ "$2" = /dav-drop/guest/ ] && [ "$mounted" = 1 ]
}
drop_mount_readable() {
printf '%s\n' "$tick" >> checks
return "$mount_access"
}
osascript() { printf '%s\n' "$*" >> prompts; return 1; }
curl() {
while [ "$1" != -d ]; do shift; done
[ "$3" = "$REIMS_HOST_URL/v1/status" ] || exit 1
case "$2" in
*'"drop_mount_denied":false'*) printf '%s false\n' "$tick" >> status ;;
*'"drop_mount_denied":true'*) printf '%s true\n' "$tick" >> status ;;
*) exit 1 ;;
esac
}
sleep() {
[ ! -e prompts ] || { cat prompts >&2; exit 1; }
case "$tick" in
1)
[ "$(cat status)" = '0 false' ] && [ ! -e checks ] || exit 1
: > "$TMP_DIR/drop_mount_denied"
;;
2)
[ "$(cat status)" = "$(printf '0 false\n1 true')" ] || exit 1
[ "$(cat checks)" = 1 ] && [ -f "$TMP_DIR/drop_mount_denied" ] || exit 1
mounted=0
;;
6)
[ "$(cat status)" = "$(printf '0 false\n1 true\n5 true')" ] || exit 1
[ "$(cat checks)" = 1 ] && [ -f "$TMP_DIR/drop_mount_denied" ] || exit 1
mounted=1
mount_access=0
;;
11)
cat status checks
[ "$(cat status)" = "$(printf '0 false\n1 true\n5 true\n10 false')" ] || exit 1
[ "$(cat checks)" = "$(printf '1\n10')" ] && [ ! -e "$TMP_DIR/drop_mount_denied" ] || exit 1
exit 0
;;
esac
}
`+loop, nil)
}
const fileSyncControlSetup = `set -eu
TMP_DIR=.
INJECT_LOCK=./injecting
CAFFEINATE_PID=""
TEXT_INPUT_PID=""
DROP_EVENTS_PID=""
CLIPBOARD_PID=""
tick=0
: > "$INJECT_LOCK"
: > parent-cleanup
`
const fileSyncControlStubs = `
fail() { printf '%s\n' "$*" >&2; exit 1; }
check() { [ "$1" = "$2" ] || fail "$3: expected '$2', got '$1'"; }
await_file() {
tries=0
until [ -f "$1" ]; do
tries=$((tries + 1))
[ "$tries" -le 200 ] || fail "timed out waiting for $1"
sleep 0.05
done
}
stop_window_reporter() { printf 'stopped\n' >> parent-cleanup; }
sync_window_reporter() { :; }
# Model a completed child that still answers kill -0 until wait.
kill() {
[ "$1" != -0 ] || return 0
command kill "$@"
}
wait() {
printf '%s\n' "$1" >> reaped
command wait "$@"
}
sync_files() {
printf '%s\n' "$tick" >> started
: > "$TMP_DIR/sync-download.part"
: > "ready-$tick"
until [ -f "release-$tick" ]; do
command kill -0 "$$" 2>/dev/null || return 1
sleep 0.05
done
}
`
const fileSyncControlExercise = `
REIMS_HOST_URL=http://stub
REIMS_TOKEN=token
mount_mode=sync
: > "$FILE_SYNC_DONE"
load_conf() { :; }
ensure_webdav_mount() {
if [ "$tick" -eq 15 ]; then mount_mode=webdav; fi
}
ensure_drop_webdav_mount() { :; }
curl() { printf '%s\n' '{"shared_folders_count":2}'; }
sync_drive_mounts() { printf '%s drives\n' "$tick" >> config; }
sync_folder_mounts() { printf '%s folders=%s\n' "$tick" "$1" >> config; }
sync_timezone() { :; }
report_status() { printf '%s\n' "$tick" >> status; }
# The loop sleeps at the end of each iteration, after advancing tick.
sleep() {
if [ "$1" != 2 ]; then
command sleep "$@"
return
fi
case "$tick" in
1)
first_pid=$FILE_SYNC_PID
[ -n "$first_pid" ] || fail "first fallback tick did not start a worker"
await_file ready-0
[ ! -f "$FILE_SYNC_DONE" ] || fail "worker started with a stale completion marker"
;;
11)
check "$FILE_SYNC_PID" "$first_pid" "only one worker ran while the first was blocked"
check "$(cat started)" 0 "fallback runs before the gate opens"
check "$(cat status)" "$(printf '0\n5\n10')" "status ticks while the worker is blocked"
check "$(cat config)" "$(printf '%s\n' '0 drives' '0 folders=2' \
'5 drives' '5 folders=2' '10 drives' '10 folders=2')" "host config order"
: > release-0
await_file "$FILE_SYNC_DONE"
[ ! -s parent-cleanup ] && [ -f "$INJECT_LOCK" ] || fail "worker ran parent cleanup"
[ ! -f "$TMP_DIR/sync-download.part" ] || fail "worker left a partial download"
;;
12)
check "$FILE_SYNC_PID" "" "first worker PID after reaping"
check "$(cat reaped)" "$first_pid" "first worker was waited on"
;;
13)
second_pid=$FILE_SYNC_PID
[ -n "$second_pid" ] || fail "no second fallback worker started"
await_file ready-12
check "$(cat started)" "$(printf '0\n12')" "fallback restarted after reaping"
;;
16)
check "$mount_mode" webdav "the mount recovered during the second sync"
check "$FILE_SYNC_PID" "$second_pid" "second worker was still running at the mode switch"
: > release-12
await_file "$FILE_SYNC_DONE"
;;
17)
check "$FILE_SYNC_PID" "" "completed worker was cleared in WebDAV mode"
[ ! -f "$FILE_SYNC_DONE" ] || fail "completion marker remained after reaping in WebDAV mode"
check "$(cat reaped)" "$(printf '%s\n' "$first_pid" "$second_pid")" "both workers were waited on"
check "$(cat started)" "$(printf '0\n12')" "WebDAV did not start a fallback worker"
exit 0
;;
esac
}
`