blob: e62569222b55aa3d3036eb9e61562af8d13159ba [file] [log] [blame]
Jonathan Niederc74c7202013-11-25 13:03:06 -08001# 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#
Jonathan Niederc74c7202013-11-25 13:03:06 -080016# test_done
Clemens Buchacher71039fb2012-01-07 12:42:45 +010017
SZEDER Gábor43a2afe2019-11-22 14:14:36 +010018if ! test_bool_env GIT_TEST_GIT_DAEMON true
Clemens Buchacher71039fb2012-01-07 12:42:45 +010019then
Jeff King83d842d2014-02-10 16:29:37 -050020 skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)"
Clemens Buchacher71039fb2012-01-07 12:42:45 +010021 test_done
22fi
23
Johannes Schindelina390d7e2016-01-27 17:19:48 +010024if test_have_prereq !PIPE
25then
Ævar Arnfjörð Bjarmason3b072c52019-06-21 12:18:11 +020026 test_skip_or_die GIT_TEST_GIT_DAEMON "file system does not support FIFOs"
Johannes Schindelina390d7e2016-01-27 17:19:48 +010027fi
28
SZEDER Gáborfa840582019-01-05 02:08:58 +010029test_set_port LIB_GIT_DAEMON_PORT
Clemens Buchacher71039fb2012-01-07 12:42:45 +010030
31GIT_DAEMON_PID=
SZEDER Gábor80a539a2019-03-13 13:24:10 +010032GIT_DAEMON_PIDFILE="$PWD"/daemon.pid
Clemens Buchacher71039fb2012-01-07 12:42:45 +010033GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo
Jeff King4414a152018-01-24 19:58:19 -050034GIT_DAEMON_HOST_PORT=127.0.0.1:$LIB_GIT_DAEMON_PORT
35GIT_DAEMON_URL=git://$GIT_DAEMON_HOST_PORT
Clemens Buchacher71039fb2012-01-07 12:42:45 +010036
Johannes Schindelin9f82b2a2019-03-13 13:24:12 +010037registered_stop_git_daemon_atexit_handler=
Clemens Buchacher71039fb2012-01-07 12:42:45 +010038start_git_daemon() {
39 if test -n "$GIT_DAEMON_PID"
40 then
41 error "start_git_daemon already called"
42 fi
43
44 mkdir -p "$GIT_DAEMON_DOCUMENT_ROOT_PATH"
45
Johannes Schindelin9f82b2a2019-03-13 13:24:12 +010046 # 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
52 fi
Clemens Buchacher71039fb2012-01-07 12:42:45 +010053
54 say >&3 "Starting git daemon ..."
Clemens Buchacher561b1332012-01-07 12:42:47 +010055 mkfifo git_daemon_output
Jeff Kingbd4d9d92017-02-25 04:37:30 -050056 ${LIB_GIT_DAEMON_COMMAND:-git daemon} \
57 --listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \
SZEDER Gábor80a539a2019-03-13 13:24:10 +010058 --reuseaddr --verbose --pid-file="$GIT_DAEMON_PIDFILE" \
Clemens Buchacher71039fb2012-01-07 12:42:45 +010059 --base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
60 "$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
Clemens Buchacher561b1332012-01-07 12:42:47 +010061 >&3 2>git_daemon_output &
Clemens Buchacher71039fb2012-01-07 12:42:45 +010062 GIT_DAEMON_PID=$!
Clemens Buchacher561b1332012-01-07 12:42:47 +010063 {
Jeff King314a73d2018-01-25 14:16:41 -050064 read -r line <&7
Thomas Gummerer3c78e972019-01-06 17:53:10 +000065 printf "%s\n" "$line" >&4
66 cat <&7 >&4 &
67 } 7<git_daemon_output &&
Clemens Buchacher561b1332012-01-07 12:42:47 +010068
Johannes Sixt46e35812012-04-26 23:00:39 +020069 # 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"
Johannes Schindelin9f82b2a2019-03-13 13:24:12 +010074 unset GIT_DAEMON_PID
Ævar Arnfjörð Bjarmason3b072c52019-06-21 12:18:11 +020075 test_skip_or_die GIT_TEST_GIT_DAEMON \
Jeff King83d842d2014-02-10 16:29:37 -050076 "git daemon failed to start"
Johannes Sixt46e35812012-04-26 23:00:39 +020077 fi
Clemens Buchacher71039fb2012-01-07 12:42:45 +010078}
79
80stop_git_daemon() {
81 if test -z "$GIT_DAEMON_PID"
82 then
83 return
84 fi
85
Clemens Buchacher71039fb2012-01-07 12:42:45 +010086 # 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=$?
SZEDER Gábor4c2eb062018-11-26 21:03:37 +010091 if ! test_match_signal 15 $ret
Clemens Buchacher71039fb2012-01-07 12:42:45 +010092 then
93 error "git daemon exited with status: $ret"
94 fi
SZEDER Gábor80a539a2019-03-13 13:24:10 +010095 kill "$(cat "$GIT_DAEMON_PIDFILE")" 2>/dev/null
Clemens Buchacher71039fb2012-01-07 12:42:45 +010096 GIT_DAEMON_PID=
SZEDER Gábor80a539a2019-03-13 13:24:10 +010097 rm -f git_daemon_output "$GIT_DAEMON_PIDFILE"
Clemens Buchacher71039fb2012-01-07 12:42:45 +010098}
Jeff King4414a152018-01-24 19:58:19 -050099
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.
103fake_nc() {
104 if ! test_declared_prereq FAKENC
105 then
106 echo >&4 "fake_nc: need to declare FAKENC prerequisite"
107 return 127
108 fi
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 ' "$@"
116}
117
118test_lazy_prereq FAKENC '
119 perl -MIO::Socket::INET -e "exit 0"
120'