The twelfth batch
[git/gitster.git] / t / lib-git-daemon.sh
blobe62569222b55aa3d3036eb9e61562af8d13159ba
1 # Shell library to run git-daemon in tests. Ends the test early if
2 # GIT_TEST_GIT_DAEMON is not set.
4 # Usage:
6 # . ./test-lib.sh
7 # . "$TEST_DIRECTORY"/lib-git-daemon.sh
8 # start_git_daemon
10 # test_expect_success '...' '
11 # ...
12 # '
14 # test_expect_success ...
16 # test_done
18 if ! test_bool_env GIT_TEST_GIT_DAEMON true
19 then
20 skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)"
21 test_done
24 if test_have_prereq !PIPE
25 then
26 test_skip_or_die GIT_TEST_GIT_DAEMON "file system does not support FIFOs"
29 test_set_port LIB_GIT_DAEMON_PORT
31 GIT_DAEMON_PID=
32 GIT_DAEMON_PIDFILE="$PWD"/daemon.pid
33 GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo
34 GIT_DAEMON_HOST_PORT=127.0.0.1:$LIB_GIT_DAEMON_PORT
35 GIT_DAEMON_URL=git://$GIT_DAEMON_HOST_PORT
37 registered_stop_git_daemon_atexit_handler=
38 start_git_daemon() {
39 if test -n "$GIT_DAEMON_PID"
40 then
41 error "start_git_daemon already called"
44 mkdir -p "$GIT_DAEMON_DOCUMENT_ROOT_PATH"
46 # One of the test scripts stops and then re-starts 'git daemon'.
47 # Don't register and then run the same atexit handlers several times.
48 if test -z "$registered_stop_git_daemon_atexit_handler"
49 then
50 test_atexit 'stop_git_daemon'
51 registered_stop_git_daemon_atexit_handler=AlreadyDone
54 say >&3 "Starting git daemon ..."
55 mkfifo git_daemon_output
56 ${LIB_GIT_DAEMON_COMMAND:-git daemon} \
57 --listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \
58 --reuseaddr --verbose --pid-file="$GIT_DAEMON_PIDFILE" \
59 --base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
60 "$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
61 >&3 2>git_daemon_output &
62 GIT_DAEMON_PID=$!
64 read -r line <&7
65 printf "%s\n" "$line" >&4
66 cat <&7 >&4 &
67 } 7<git_daemon_output &&
69 # Check expected output
70 if test x"$(expr "$line" : "\[[0-9]*\] \(.*\)")" != x"Ready to rumble"
71 then
72 kill "$GIT_DAEMON_PID"
73 wait "$GIT_DAEMON_PID"
74 unset GIT_DAEMON_PID
75 test_skip_or_die GIT_TEST_GIT_DAEMON \
76 "git daemon failed to start"
80 stop_git_daemon() {
81 if test -z "$GIT_DAEMON_PID"
82 then
83 return
86 # kill git-daemon child of git
87 say >&3 "Stopping git daemon ..."
88 kill "$GIT_DAEMON_PID"
89 wait "$GIT_DAEMON_PID" >&3 2>&4
90 ret=$?
91 if ! test_match_signal 15 $ret
92 then
93 error "git daemon exited with status: $ret"
95 kill "$(cat "$GIT_DAEMON_PIDFILE")" 2>/dev/null
96 GIT_DAEMON_PID=
97 rm -f git_daemon_output "$GIT_DAEMON_PIDFILE"
100 # A stripped-down version of a netcat client, that connects to a "host:port"
101 # given in $1, sends its stdin followed by EOF, then dumps the response (until
102 # EOF) to stdout.
103 fake_nc() {
104 if ! test_declared_prereq FAKENC
105 then
106 echo >&4 "fake_nc: need to declare FAKENC prerequisite"
107 return 127
109 perl -Mstrict -MIO::Socket::INET -e '
110 my $s = IO::Socket::INET->new(shift)
111 or die "unable to open socket: $!";
112 print $s <STDIN>;
113 $s->shutdown(1);
114 print <$s>;
115 ' "$@"
118 test_lazy_prereq FAKENC '
119 perl -MIO::Socket::INET -e "exit 0"