blob: 79db3b7ae513c01b07422ed1a8d95f9f5b285cb5 [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#
16# stop_git_daemon
17# test_done
Clemens Buchacher71039fb2012-01-07 12:42:45 +010018
Jeff King83d842d2014-02-10 16:29:37 -050019test_tristate GIT_TEST_GIT_DAEMON
20if test "$GIT_TEST_GIT_DAEMON" = false
Clemens Buchacher71039fb2012-01-07 12:42:45 +010021then
Jeff King83d842d2014-02-10 16:29:37 -050022 skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)"
Clemens Buchacher71039fb2012-01-07 12:42:45 +010023 test_done
24fi
25
Johannes Schindelina390d7e2016-01-27 17:19:48 +010026if test_have_prereq !PIPE
27then
28 test_skip_or_die $GIT_TEST_GIT_DAEMON "file system does not support FIFOs"
29fi
30
SZEDER Gáborfa840582019-01-05 02:08:58 +010031test_set_port LIB_GIT_DAEMON_PORT
Clemens Buchacher71039fb2012-01-07 12:42:45 +010032
33GIT_DAEMON_PID=
34GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo
Jeff King4414a152018-01-24 19:58:19 -050035GIT_DAEMON_HOST_PORT=127.0.0.1:$LIB_GIT_DAEMON_PORT
36GIT_DAEMON_URL=git://$GIT_DAEMON_HOST_PORT
Clemens Buchacher71039fb2012-01-07 12:42:45 +010037
38start_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
46 trap 'code=$?; stop_git_daemon; (exit $code); die' EXIT
47
48 say >&3 "Starting git daemon ..."
Clemens Buchacher561b1332012-01-07 12:42:47 +010049 mkfifo git_daemon_output
Jeff Kingbd4d9d92017-02-25 04:37:30 -050050 ${LIB_GIT_DAEMON_COMMAND:-git daemon} \
51 --listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \
Clemens Buchacher71039fb2012-01-07 12:42:45 +010052 --reuseaddr --verbose \
53 --base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
54 "$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
Clemens Buchacher561b1332012-01-07 12:42:47 +010055 >&3 2>git_daemon_output &
Clemens Buchacher71039fb2012-01-07 12:42:45 +010056 GIT_DAEMON_PID=$!
Clemens Buchacher561b1332012-01-07 12:42:47 +010057 {
Jeff King314a73d2018-01-25 14:16:41 -050058 read -r line <&7
Thomas Gummerer3c78e972019-01-06 17:53:10 +000059 printf "%s\n" "$line" >&4
60 cat <&7 >&4 &
61 } 7<git_daemon_output &&
Clemens Buchacher561b1332012-01-07 12:42:47 +010062
Johannes Sixt46e35812012-04-26 23:00:39 +020063 # Check expected output
64 if test x"$(expr "$line" : "\[[0-9]*\] \(.*\)")" != x"Ready to rumble"
65 then
66 kill "$GIT_DAEMON_PID"
67 wait "$GIT_DAEMON_PID"
68 trap 'die' EXIT
Jeff King83d842d2014-02-10 16:29:37 -050069 test_skip_or_die $GIT_TEST_GIT_DAEMON \
70 "git daemon failed to start"
Johannes Sixt46e35812012-04-26 23:00:39 +020071 fi
Clemens Buchacher71039fb2012-01-07 12:42:45 +010072}
73
74stop_git_daemon() {
75 if test -z "$GIT_DAEMON_PID"
76 then
77 return
78 fi
79
80 trap 'die' EXIT
81
82 # kill git-daemon child of git
83 say >&3 "Stopping git daemon ..."
84 kill "$GIT_DAEMON_PID"
85 wait "$GIT_DAEMON_PID" >&3 2>&4
86 ret=$?
SZEDER Gábor4c2eb062018-11-26 21:03:37 +010087 if ! test_match_signal 15 $ret
Clemens Buchacher71039fb2012-01-07 12:42:45 +010088 then
89 error "git daemon exited with status: $ret"
90 fi
91 GIT_DAEMON_PID=
Clemens Buchacher561b1332012-01-07 12:42:47 +010092 rm -f git_daemon_output
Clemens Buchacher71039fb2012-01-07 12:42:45 +010093}
Jeff King4414a152018-01-24 19:58:19 -050094
95# A stripped-down version of a netcat client, that connects to a "host:port"
96# given in $1, sends its stdin followed by EOF, then dumps the response (until
97# EOF) to stdout.
98fake_nc() {
99 if ! test_declared_prereq FAKENC
100 then
101 echo >&4 "fake_nc: need to declare FAKENC prerequisite"
102 return 127
103 fi
104 perl -Mstrict -MIO::Socket::INET -e '
105 my $s = IO::Socket::INET->new(shift)
106 or die "unable to open socket: $!";
107 print $s <STDIN>;
108 $s->shutdown(1);
109 print <$s>;
110 ' "$@"
111}
112
113test_lazy_prereq FAKENC '
114 perl -MIO::Socket::INET -e "exit 0"
115'