]>
Commit | Line | Data |
---|---|---|
c74c7203 JN |
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 | # | |
c74c7203 | 16 | # test_done |
71039fb9 | 17 | |
43a2afee | 18 | if ! test_bool_env GIT_TEST_GIT_DAEMON true |
71039fb9 | 19 | then |
83d842dc | 20 | skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)" |
71039fb9 CB |
21 | test_done |
22 | fi | |
23 | ||
a390d7e8 JS |
24 | if test_have_prereq !PIPE |
25 | then | |
3b072c57 | 26 | test_skip_or_die GIT_TEST_GIT_DAEMON "file system does not support FIFOs" |
a390d7e8 JS |
27 | fi |
28 | ||
fa840581 | 29 | test_set_port LIB_GIT_DAEMON_PORT |
71039fb9 CB |
30 | |
31 | GIT_DAEMON_PID= | |
80a539ac | 32 | GIT_DAEMON_PIDFILE="$PWD"/daemon.pid |
71039fb9 | 33 | GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo |
4414a150 JK |
34 | GIT_DAEMON_HOST_PORT=127.0.0.1:$LIB_GIT_DAEMON_PORT |
35 | GIT_DAEMON_URL=git://$GIT_DAEMON_HOST_PORT | |
71039fb9 | 36 | |
9f82b2a6 | 37 | registered_stop_git_daemon_atexit_handler= |
71039fb9 CB |
38 | start_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 | ||
9f82b2a6 JS |
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 | |
52 | fi | |
71039fb9 CB |
53 | |
54 | say >&3 "Starting git daemon ..." | |
561b133c | 55 | mkfifo git_daemon_output |
bd4d9d99 JK |
56 | ${LIB_GIT_DAEMON_COMMAND:-git daemon} \ |
57 | --listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \ | |
80a539ac | 58 | --reuseaddr --verbose --pid-file="$GIT_DAEMON_PIDFILE" \ |
71039fb9 CB |
59 | --base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \ |
60 | "$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \ | |
561b133c | 61 | >&3 2>git_daemon_output & |
71039fb9 | 62 | GIT_DAEMON_PID=$! |
561b133c | 63 | { |
314a73d6 | 64 | read -r line <&7 |
3c78e97d TG |
65 | printf "%s\n" "$line" >&4 |
66 | cat <&7 >&4 & | |
67 | } 7<git_daemon_output && | |
561b133c | 68 | |
46e35815 JS |
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" | |
9f82b2a6 | 74 | unset GIT_DAEMON_PID |
3b072c57 | 75 | test_skip_or_die GIT_TEST_GIT_DAEMON \ |
83d842dc | 76 | "git daemon failed to start" |
46e35815 | 77 | fi |
71039fb9 CB |
78 | } |
79 | ||
80 | stop_git_daemon() { | |
81 | if test -z "$GIT_DAEMON_PID" | |
82 | then | |
83 | return | |
84 | fi | |
85 | ||
71039fb9 CB |
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=$? | |
4c2eb064 | 91 | if ! test_match_signal 15 $ret |
71039fb9 CB |
92 | then |
93 | error "git daemon exited with status: $ret" | |
94 | fi | |
80a539ac | 95 | kill "$(cat "$GIT_DAEMON_PIDFILE")" 2>/dev/null |
71039fb9 | 96 | GIT_DAEMON_PID= |
80a539ac | 97 | rm -f git_daemon_output "$GIT_DAEMON_PIDFILE" |
71039fb9 | 98 | } |
4414a150 JK |
99 | |
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 | |
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 | ||
118 | test_lazy_prereq FAKENC ' | |
119 | perl -MIO::Socket::INET -e "exit 0" | |
120 | ' |