#!/bin/bash
set -e
. tests/lib
. $troot/lib-mirror

t-dependencies rsync

t-drs

: ---- "basic test" ----

t-tstunt-parsechangelog
t-prep-newpackage example 1.0

t-mirror-setup

cd $p
revision=1
t-dgit build
t-dgit push --new

t-check-mirrored

: ---- "stunt ssh test" ----

sentinel=$tmp/stunt-ssh-sentinel

cat <<END >$tmp/stunt-ssh
#!/bin/sh
set -ex
: $sentinel
cat >&2 $sentinel
shift # eat HOST
export SSH_ORIGINAL_COMMAND="\$*"
exec \${DGIT_MIRROR_SSH_WRAP_TEST-dgit-mirror-ssh-wrap} \
	'$tmp/git-mirror/' .git --
END
chmod +x $tmp/stunt-ssh

t-mirror-set rsyncssh=$tmp/stunt-ssh
t-mirror-set remoterepos=HOST:$reposmirror

# mirror should fail due to lack of stunt-ssh-sentinel

t-commit-build-push-expect-log "stunt ssh test" \
	E:'mirror hook failed: .*exited'

ls -al $queuedir/$p.a
t-check-not-mirrored

touch $sentinel

t-mirror-hook backlog
t-check-mirrored

: ----- "stall timeout test" -----

rm -f $sentinel
mkfifo $sentinel

t-mirror-set hooktimeout=5

t-commit-build-push-expect-log "stall timeout test" \
	E:'mirror hook failed: .*timeout'

t-check-not-mirrored

exec 3<>$sentinel
exec 3>&-

attempts=100
while [ -f $queuedir/$p.lock ]; do
	if [ $attempts = 0 ]; then \
		fail "timed out waiting for lock to go away"
	fi
	attempts=$(( $attempts - 1 ))
	sleep 0.1
done

t-check-mirrored

t-ok
