]>
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 | |
83d842dc JK |
18 | test_tristate GIT_TEST_GIT_DAEMON |
19 | if test "$GIT_TEST_GIT_DAEMON" = false | |
71039fb9 | 20 | then |
83d842dc | 21 | skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)" |
71039fb9 CB |
22 | test_done |
23 | fi | |
24 | ||
a390d7e8 JS |
25 | if test_have_prereq !PIPE |
26 | then | |
27 | test_skip_or_die $GIT_TEST_GIT_DAEMON "file system does not support FIFOs" | |
28 | fi | |
29 | ||
fa840581 | 30 | test_set_port LIB_GIT_DAEMON_PORT |
71039fb9 CB |
31 | |
32 | GIT_DAEMON_PID= | |
80a539ac | 33 | GIT_DAEMON_PIDFILE="$PWD"/daemon.pid |
71039fb9 | 34 | GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo |
4414a150 JK |
35 | GIT_DAEMON_HOST_PORT=127.0.0.1:$LIB_GIT_DAEMON_PORT |
36 | GIT_DAEMON_URL=git://$GIT_DAEMON_HOST_PORT | |
71039fb9 | 37 | |
9f82b2a6 | 38 | registered_stop_git_daemon_atexit_handler= |
71039fb9 CB |
39 | start_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 | ||
9f82b2a6 JS |
47 | # One of the test scripts stops and then re-starts 'git daemon'. |
48 | # Don't register and then run the same atexit handlers several times. | |
49 | if test -z "$registered_stop_git_daemon_atexit_handler" | |
50 | then | |
51 | test_atexit 'stop_git_daemon' | |
52 | registered_stop_git_daemon_atexit_handler=AlreadyDone | |
53 | fi | |
71039fb9 CB |
54 | |
55 | say >&3 "Starting git daemon ..." | |
561b133c | 56 | mkfifo git_daemon_output |
bd4d9d99 JK |
57 | ${LIB_GIT_DAEMON_COMMAND:-git daemon} \ |
58 | --listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \ | |
80a539ac | 59 | --reuseaddr --verbose --pid-file="$GIT_DAEMON_PIDFILE" \ |
71039fb9 CB |
60 | --base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \ |
61 | "$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \ | |
561b133c | 62 | >&3 2>git_daemon_output & |
71039fb9 | 63 | GIT_DAEMON_PID=$! |
561b133c | 64 | { |
314a73d6 | 65 | read -r line <&7 |
3c78e97d TG |
66 | printf "%s\n" "$line" >&4 |
67 | cat <&7 >&4 & | |
68 | } 7<git_daemon_output && | |
561b133c | 69 | |
46e35815 JS |
70 | # Check expected output |
71 | if test x"$(expr "$line" : "\[[0-9]*\] \(.*\)")" != x"Ready to rumble" | |
72 | then | |
73 | kill "$GIT_DAEMON_PID" | |
74 | wait "$GIT_DAEMON_PID" | |
9f82b2a6 | 75 | unset GIT_DAEMON_PID |
83d842dc JK |
76 | test_skip_or_die $GIT_TEST_GIT_DAEMON \ |
77 | "git daemon failed to start" | |
46e35815 | 78 | fi |
71039fb9 CB |
79 | } |
80 | ||
81 | stop_git_daemon() { | |
82 | if test -z "$GIT_DAEMON_PID" | |
83 | then | |
84 | return | |
85 | fi | |
86 | ||
71039fb9 CB |
87 | # kill git-daemon child of git |
88 | say >&3 "Stopping git daemon ..." | |
89 | kill "$GIT_DAEMON_PID" | |
90 | wait "$GIT_DAEMON_PID" >&3 2>&4 | |
91 | ret=$? | |
4c2eb064 | 92 | if ! test_match_signal 15 $ret |
71039fb9 CB |
93 | then |
94 | error "git daemon exited with status: $ret" | |
95 | fi | |
80a539ac | 96 | kill "$(cat "$GIT_DAEMON_PIDFILE")" 2>/dev/null |
71039fb9 | 97 | GIT_DAEMON_PID= |
80a539ac | 98 | rm -f git_daemon_output "$GIT_DAEMON_PIDFILE" |
71039fb9 | 99 | } |
4414a150 JK |
100 | |
101 | # A stripped-down version of a netcat client, that connects to a "host:port" | |
102 | # given in $1, sends its stdin followed by EOF, then dumps the response (until | |
103 | # EOF) to stdout. | |
104 | fake_nc() { | |
105 | if ! test_declared_prereq FAKENC | |
106 | then | |
107 | echo >&4 "fake_nc: need to declare FAKENC prerequisite" | |
108 | return 127 | |
109 | fi | |
110 | perl -Mstrict -MIO::Socket::INET -e ' | |
111 | my $s = IO::Socket::INET->new(shift) | |
112 | or die "unable to open socket: $!"; | |
113 | print $s <STDIN>; | |
114 | $s->shutdown(1); | |
115 | print <$s>; | |
116 | ' "$@" | |
117 | } | |
118 | ||
119 | test_lazy_prereq FAKENC ' | |
120 | perl -MIO::Socket::INET -e "exit 0" | |
121 | ' |