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
18
19test_tristate GIT_TEST_GIT_DAEMON
20if test "$GIT_TEST_GIT_DAEMON" = false
21then
22 skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)"
23 test_done
24fi
25
26if test_have_prereq !PIPE
27then
28 test_skip_or_die $GIT_TEST_GIT_DAEMON "file system does not support FIFOs"
29fi
30
31test_set_port LIB_GIT_DAEMON_PORT
32
33GIT_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
38
39start_git_daemon() {
40 if test -n "$GIT_DAEMON_PID"
41 then
42 error "start_git_daemon already called"
43 fi
44
45 mkdir -p "$GIT_DAEMON_DOCUMENT_ROOT_PATH"
46
47 trap 'code=$?; stop_git_daemon; (exit $code); die' EXIT
48
49 say >&3 "Starting git daemon ..."
50 mkfifo 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 &
57 GIT_DAEMON_PID=$!
58 {
59 read -r line <&7
60 printf "%s\n" "$line" >&4
61 cat <&7 >&4 &
62 } 7<git_daemon_output &&
63
64 # Check expected output
65 if test x"$(expr "$line" : "\[[0-9]*\] \(.*\)")" != x"Ready to rumble"
66 then
67 kill "$GIT_DAEMON_PID"
68 wait "$GIT_DAEMON_PID"
69 trap 'die' EXIT
70 test_skip_or_die $GIT_TEST_GIT_DAEMON \
71 "git daemon failed to start"
72 fi
73}
74
75stop_git_daemon() {
76 if test -z "$GIT_DAEMON_PID"
77 then
78 return
79 fi
80
81 trap 'die' EXIT
82
83 # kill git-daemon child of git
84 say >&3 "Stopping git daemon ..."
85 kill "$GIT_DAEMON_PID"
86 wait "$GIT_DAEMON_PID" >&3 2>&4
87 ret=$?
88 if ! test_match_signal 15 $ret
89 then
90 error "git daemon exited with status: $ret"
91 fi
92 kill "$(cat "$GIT_DAEMON_PIDFILE")" 2>/dev/null
93 GIT_DAEMON_PID=
94 rm -f git_daemon_output "$GIT_DAEMON_PIDFILE"
95}
96
97# 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() {
101 if ! test_declared_prereq FAKENC
102 then
103 echo >&4 "fake_nc: need to declare FAKENC prerequisite"
104 return 127
105 fi
106 perl -Mstrict -MIO::Socket::INET -e '
107 my $s = IO::Socket::INET->new(shift)
108 or die "unable to open socket: $!";
109 print $s <STDIN>;
110 $s->shutdown(1);
111 print <$s>;
112 ' "$@"
113}
114
115test_lazy_prereq FAKENC '
116 perl -MIO::Socket::INET -e "exit 0"
117'