Jonathan Nieder | c74c720 | 2013-11-25 13:03:06 -0800 | [diff] [blame] | 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 | # |
Jonathan Nieder | c74c720 | 2013-11-25 13:03:06 -0800 | [diff] [blame] | 16 | # test_done |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 17 | |
SZEDER Gábor | 43a2afe | 2019-11-22 14:14:36 +0100 | [diff] [blame] | 18 | if ! test_bool_env GIT_TEST_GIT_DAEMON true |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 19 | then |
Jeff King | 83d842d | 2014-02-10 16:29:37 -0500 | [diff] [blame] | 20 | skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)" |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 21 | test_done |
| 22 | fi |
| 23 | |
Johannes Schindelin | a390d7e | 2016-01-27 17:19:48 +0100 | [diff] [blame] | 24 | if test_have_prereq !PIPE |
| 25 | then |
Ævar Arnfjörð Bjarmason | 3b072c5 | 2019-06-21 12:18:11 +0200 | [diff] [blame] | 26 | test_skip_or_die GIT_TEST_GIT_DAEMON "file system does not support FIFOs" |
Johannes Schindelin | a390d7e | 2016-01-27 17:19:48 +0100 | [diff] [blame] | 27 | fi |
| 28 | |
SZEDER Gábor | fa84058 | 2019-01-05 02:08:58 +0100 | [diff] [blame] | 29 | test_set_port LIB_GIT_DAEMON_PORT |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 30 | |
| 31 | GIT_DAEMON_PID= |
SZEDER Gábor | 80a539a | 2019-03-13 13:24:10 +0100 | [diff] [blame] | 32 | GIT_DAEMON_PIDFILE="$PWD"/daemon.pid |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 33 | GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo |
Jeff King | 4414a15 | 2018-01-24 19:58:19 -0500 | [diff] [blame] | 34 | GIT_DAEMON_HOST_PORT=127.0.0.1:$LIB_GIT_DAEMON_PORT |
| 35 | GIT_DAEMON_URL=git://$GIT_DAEMON_HOST_PORT |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 36 | |
Johannes Schindelin | 9f82b2a | 2019-03-13 13:24:12 +0100 | [diff] [blame] | 37 | registered_stop_git_daemon_atexit_handler= |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 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 | |
Johannes Schindelin | 9f82b2a | 2019-03-13 13:24:12 +0100 | [diff] [blame] | 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 |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 53 | |
| 54 | say >&3 "Starting git daemon ..." |
Clemens Buchacher | 561b133 | 2012-01-07 12:42:47 +0100 | [diff] [blame] | 55 | mkfifo git_daemon_output |
Jeff King | bd4d9d9 | 2017-02-25 04:37:30 -0500 | [diff] [blame] | 56 | ${LIB_GIT_DAEMON_COMMAND:-git daemon} \ |
| 57 | --listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \ |
SZEDER Gábor | 80a539a | 2019-03-13 13:24:10 +0100 | [diff] [blame] | 58 | --reuseaddr --verbose --pid-file="$GIT_DAEMON_PIDFILE" \ |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 59 | --base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \ |
| 60 | "$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \ |
Clemens Buchacher | 561b133 | 2012-01-07 12:42:47 +0100 | [diff] [blame] | 61 | >&3 2>git_daemon_output & |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 62 | GIT_DAEMON_PID=$! |
Clemens Buchacher | 561b133 | 2012-01-07 12:42:47 +0100 | [diff] [blame] | 63 | { |
Jeff King | 314a73d | 2018-01-25 14:16:41 -0500 | [diff] [blame] | 64 | read -r line <&7 |
Thomas Gummerer | 3c78e97 | 2019-01-06 17:53:10 +0000 | [diff] [blame] | 65 | printf "%s\n" "$line" >&4 |
| 66 | cat <&7 >&4 & |
| 67 | } 7<git_daemon_output && |
Clemens Buchacher | 561b133 | 2012-01-07 12:42:47 +0100 | [diff] [blame] | 68 | |
Johannes Sixt | 46e3581 | 2012-04-26 23:00:39 +0200 | [diff] [blame] | 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" |
Johannes Schindelin | 9f82b2a | 2019-03-13 13:24:12 +0100 | [diff] [blame] | 74 | unset GIT_DAEMON_PID |
Ævar Arnfjörð Bjarmason | 3b072c5 | 2019-06-21 12:18:11 +0200 | [diff] [blame] | 75 | test_skip_or_die GIT_TEST_GIT_DAEMON \ |
Jeff King | 83d842d | 2014-02-10 16:29:37 -0500 | [diff] [blame] | 76 | "git daemon failed to start" |
Johannes Sixt | 46e3581 | 2012-04-26 23:00:39 +0200 | [diff] [blame] | 77 | fi |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | stop_git_daemon() { |
| 81 | if test -z "$GIT_DAEMON_PID" |
| 82 | then |
| 83 | return |
| 84 | fi |
| 85 | |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 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=$? |
SZEDER Gábor | 4c2eb06 | 2018-11-26 21:03:37 +0100 | [diff] [blame] | 91 | if ! test_match_signal 15 $ret |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 92 | then |
| 93 | error "git daemon exited with status: $ret" |
| 94 | fi |
SZEDER Gábor | 80a539a | 2019-03-13 13:24:10 +0100 | [diff] [blame] | 95 | kill "$(cat "$GIT_DAEMON_PIDFILE")" 2>/dev/null |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 96 | GIT_DAEMON_PID= |
SZEDER Gábor | 80a539a | 2019-03-13 13:24:10 +0100 | [diff] [blame] | 97 | rm -f git_daemon_output "$GIT_DAEMON_PIDFILE" |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 98 | } |
Jeff King | 4414a15 | 2018-01-24 19:58:19 -0500 | [diff] [blame] | 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 | ' |