1# Shell library to run git-daemon in tests. Ends the test early if
2# GIT_TEST_GIT_DAEMON is not set.
3#
4# Usage:
5#
6# . ./test-lib.sh
7# . "$TEST_DIRECTORY"/lib-git-daemon.sh
8# start_git_daemon
9#
10# test_expect_success '...' '
11# ...
12# '
13#
14# test_expect_success ...
15#
16# stop_git_daemon
17# test_done
1819
test_tristate GIT_TEST_GIT_DAEMON
20if test "$GIT_TEST_GIT_DAEMON" = false
21then
22skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)"
23test_done
24fi
2526
if test_have_prereq !PIPE
27then
28test_skip_or_die $GIT_TEST_GIT_DAEMON "file system does not support FIFOs"
29fi
3031
test_set_port LIB_GIT_DAEMON_PORT
3233
GIT_DAEMON_PID=
34GIT_DAEMON_PIDFILE="$PWD"/daemon.pid
35GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo
36GIT_DAEMON_HOST_PORT=127.0.0.1:$LIB_GIT_DAEMON_PORT
37GIT_DAEMON_URL=git://$GIT_DAEMON_HOST_PORT
3839
start_git_daemon() {
40if test -n "$GIT_DAEMON_PID"
41then
42error "start_git_daemon already called"
43fi
4445
mkdir -p "$GIT_DAEMON_DOCUMENT_ROOT_PATH"
4647
trap 'code=$?; stop_git_daemon; (exit $code); die' EXIT
4849
say >&3 "Starting git daemon ..."
50mkfifo git_daemon_output
51${LIB_GIT_DAEMON_COMMAND:-git daemon} \
52--listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \
53--reuseaddr --verbose --pid-file="$GIT_DAEMON_PIDFILE" \
54--base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
55"$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
56>&3 2>git_daemon_output &
57GIT_DAEMON_PID=$!
58{
59read -r line <&7
60printf "%s\n" "$line" >&4
61cat <&7 >&4 &
62} 7<git_daemon_output &&
6364
# Check expected output
65if test x"$(expr "$line" : "\[[0-9]*\] \(.*\)")" != x"Ready to rumble"
66then
67kill "$GIT_DAEMON_PID"
68wait "$GIT_DAEMON_PID"
69trap 'die' EXIT
70test_skip_or_die $GIT_TEST_GIT_DAEMON \
71"git daemon failed to start"
72fi
73}
7475
stop_git_daemon() {
76if test -z "$GIT_DAEMON_PID"
77then
78return
79fi
8081
trap 'die' EXIT
8283
# kill git-daemon child of git
84say >&3 "Stopping git daemon ..."
85kill "$GIT_DAEMON_PID"
86wait "$GIT_DAEMON_PID" >&3 2>&4
87ret=$?
88if ! test_match_signal 15 $ret
89then
90error "git daemon exited with status: $ret"
91fi
92kill "$(cat "$GIT_DAEMON_PIDFILE")" 2>/dev/null
93GIT_DAEMON_PID=
94rm -f git_daemon_output "$GIT_DAEMON_PIDFILE"
95}
9697
# A stripped-down version of a netcat client, that connects to a "host:port"
98# given in $1, sends its stdin followed by EOF, then dumps the response (until
99# EOF) to stdout.
100fake_nc() {
101if ! test_declared_prereq FAKENC
102then
103echo >&4 "fake_nc: need to declare FAKENC prerequisite"
104return 127
105fi
106perl -Mstrict -MIO::Socket::INET -e '
107my $s = IO::Socket::INET->new(shift)
108or die "unable to open socket: $!";
109print $s <STDIN>;
110$s->shutdown(1);
111print <$s>;
112' "$@"
113}
114115
test_lazy_prereq FAKENC '
116perl -MIO::Socket::INET -e "exit 0"
117'