1# Shell library to run git-daemon in tests. Ends the test early if2# GIT_TEST_GIT_DAEMON is not set.3#4# Usage:5#6# . ./test-lib.sh7# . "$TEST_DIRECTORY"/lib-git-daemon.sh8# start_git_daemon9#10# test_expect_success '...' '11# ...12# '13#14# test_expect_success ...15#16# test_done1718test_tristate GIT_TEST_GIT_DAEMON19if test "$GIT_TEST_GIT_DAEMON" = false20then21skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)"22test_done23fi2425if test_have_prereq !PIPE26then27test_skip_or_die $GIT_TEST_GIT_DAEMON "file system does not support FIFOs"28fi2930test_set_port LIB_GIT_DAEMON_PORT3132GIT_DAEMON_PID=33GIT_DAEMON_PIDFILE="$PWD"/daemon.pid34GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo35GIT_DAEMON_HOST_PORT=127.0.0.1:$LIB_GIT_DAEMON_PORT36GIT_DAEMON_URL=git://$GIT_DAEMON_HOST_PORT3738registered_stop_git_daemon_atexit_handler=39start_git_daemon() {40if test -n "$GIT_DAEMON_PID"41then42error "start_git_daemon already called"43fi4445mkdir -p "$GIT_DAEMON_DOCUMENT_ROOT_PATH"4647# One of the test scripts stops and then re-starts 'git daemon'.48# Don't register and then run the same atexit handlers several times.49if test -z "$registered_stop_git_daemon_atexit_handler"50then51test_atexit 'stop_git_daemon'52registered_stop_git_daemon_atexit_handler=AlreadyDone53fi5455say >&3 "Starting git daemon ..."56mkfifo git_daemon_output57${LIB_GIT_DAEMON_COMMAND:-git daemon} \58--listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \59--reuseaddr --verbose --pid-file="$GIT_DAEMON_PIDFILE" \60--base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \61"$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \62>&3 2>git_daemon_output &63GIT_DAEMON_PID=$!64{65read -r line <&766printf "%s\n" "$line" >&467cat <&7 >&4 &68} 7<git_daemon_output &&6970# Check expected output71if test x"$(expr "$line" : "\[[0-9]*\] \(.*\)")" != x"Ready to rumble"72then73kill "$GIT_DAEMON_PID"74wait "$GIT_DAEMON_PID"75unset GIT_DAEMON_PID76test_skip_or_die $GIT_TEST_GIT_DAEMON \77"git daemon failed to start"78fi79}8081stop_git_daemon() {82if test -z "$GIT_DAEMON_PID"83then84return85fi8687# kill git-daemon child of git88say >&3 "Stopping git daemon ..."89kill "$GIT_DAEMON_PID"90wait "$GIT_DAEMON_PID" >&3 2>&491ret=$?92if ! test_match_signal 15 $ret93then94error "git daemon exited with status: $ret"95fi96kill "$(cat "$GIT_DAEMON_PIDFILE")" 2>/dev/null97GIT_DAEMON_PID=98rm -f git_daemon_output "$GIT_DAEMON_PIDFILE"99}100101# A stripped-down version of a netcat client, that connects to a "host:port"102# given in $1, sends its stdin followed by EOF, then dumps the response (until103# EOF) to stdout.104fake_nc() {105if ! test_declared_prereq FAKENC106then107echo >&4 "fake_nc: need to declare FAKENC prerequisite"108return 127109fi110perl -Mstrict -MIO::Socket::INET -e '111my $s = IO::Socket::INET->new(shift)112or die "unable to open socket: $!";113print $s <STDIN>;114$s->shutdown(1);115print <$s>;116' "$@"117}118119test_lazy_prereq FAKENC '120perl -MIO::Socket::INET -e "exit 0"121'