| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# |
| 4 |
# Harness script for Mosh tests, server side. Runs test script and |
| 5 |
# then captures screen with `tmux capture-pane`. Captures exitstatus |
| 6 |
# of both and returns appropriate errors. |
| 7 |
# |
| 8 |
|
| 9 |
# If MOSH_E2E_WAIT is set, then the test will wait for another tmux |
| 10 |
# client to attach to the test session before starting, and will wait |
| 11 |
# for other tmux clients to detach before exiting. |
| 12 |
wait_for_clients() |
| 13 |
{ |
| 14 |
if [ -z "$MOSH_E2E_WAIT" ]; then |
| 15 |
return |
| 16 |
fi |
| 17 |
expected=$1 |
| 18 |
while true; do |
| 19 |
n=$(tmux list-clients -F . | wc -l) |
| 20 |
if [ $expected -eq 1 ]; then |
| 21 |
if [ $n -eq 1 ]; then |
| 22 |
return |
| 23 |
fi |
| 24 |
elif [ $n -ne 1 ]; then |
| 25 |
return |
| 26 |
fi |
| 27 |
sleep 1 |
| 28 |
done |
| 29 |
} |
| 30 |
|
| 31 |
export MOSH_SERVER_PID=$PPID |
| 32 |
|
| 33 |
if [ $# -lt 2 ]; then |
| 34 |
printf "not enough args\n" >&2 |
| 35 |
exit 99 |
| 36 |
fi |
| 37 |
testname=$1 |
| 38 |
shift |
| 39 |
rm -f "$testname.capture" "$testname.exitstatus" |
| 40 |
trap ":" TERM HUP QUIT # If the session closes on us, let the test we're running drive. |
| 41 |
on_exit() { |
| 42 |
rv=$? |
| 43 |
echo $rv > "$testname.exitstatus" |
| 44 |
exit $rv |
| 45 |
} |
| 46 |
trap on_exit EXIT |
| 47 |
# check for tmux |
| 48 |
if [ -z "$TMUX_PANE" ]; then |
| 49 |
printf "not running under tmux\n" >&2 |
| 50 |
exit 99 |
| 51 |
fi |
| 52 |
wait_for_clients 2 |
| 53 |
# run harnessed command |
| 54 |
eval "$@" |
| 55 |
testret=$? |
| 56 |
# Capture mosh-server runtime if possible. |
| 57 |
runtime=$(ps -o time= $PPID 2>/dev/null) |
| 58 |
if [ $? -ne 0 ]; then # Cygwin... |
| 59 |
runtime=- |
| 60 |
fi |
| 61 |
# Wait for tmux client screen to become up to date. |
| 62 |
sleep 1 |
| 63 |
printf "@@@ server complete @@@" >&2 |
| 64 |
wait_for_clients 1 |
| 65 |
i=0 |
| 66 |
while [ $i -lt 60 ]; do |
| 67 |
if grep -q "@@@ server complete @@@" "$testname.tmux.log"; then |
| 68 |
break |
| 69 |
fi |
| 70 |
i=$((i+1)) |
| 71 |
sleep 1 |
| 72 |
done |
| 73 |
if [ $i -ge 60 ]; then |
| 74 |
printf "wait for tmux client update failed, erroring test\n" >&2 |
| 75 |
exit 99 |
| 76 |
fi |
| 77 |
# capture screen |
| 78 |
if ! tmux capture-pane -et "$TMUX_PANE"; then |
| 79 |
printf "tmux capture-pane failed, erroring test\n" >&2 |
| 80 |
exit 99 |
| 81 |
fi |
| 82 |
if ! tmux save-buffer "$testname.capture"; then |
| 83 |
printf "tmux save-buffer failed, erroring test\n" >&2 |
| 84 |
exit 99 |
| 85 |
fi |
| 86 |
# Dump runtime into tmux log. |
| 87 |
printf "@@@ runtime %s @@@\n" "$runtime" |
| 88 |
# return useful exitstatus from harnessed command |
| 89 |
if [ $testret -ne 0 ]; then |
| 90 |
exit 1 |
| 91 |
fi |
| 92 |
exit 0 |