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 | # |
| 16 | # stop_git_daemon |
| 17 | # test_done |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 18 | |
Jeff King | 83d842d | 2014-02-10 16:29:37 -0500 | [diff] [blame] | 19 | test_tristate GIT_TEST_GIT_DAEMON |
| 20 | if test "$GIT_TEST_GIT_DAEMON" = false |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 21 | then |
Jeff King | 83d842d | 2014-02-10 16:29:37 -0500 | [diff] [blame] | 22 | 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] | 23 | test_done |
| 24 | fi |
| 25 | |
Johannes Schindelin | a390d7e | 2016-01-27 17:19:48 +0100 | [diff] [blame] | 26 | if test_have_prereq !PIPE |
| 27 | then |
| 28 | test_skip_or_die $GIT_TEST_GIT_DAEMON "file system does not support FIFOs" |
| 29 | fi |
| 30 | |
Jeff King | c44132f | 2014-02-10 14:16:38 -0500 | [diff] [blame] | 31 | LIB_GIT_DAEMON_PORT=${LIB_GIT_DAEMON_PORT-${this_test#t}} |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 32 | |
| 33 | GIT_DAEMON_PID= |
| 34 | GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo |
Jeff King | 4414a15 | 2018-01-24 19:58:19 -0500 | [diff] [blame] | 35 | GIT_DAEMON_HOST_PORT=127.0.0.1:$LIB_GIT_DAEMON_PORT |
| 36 | GIT_DAEMON_URL=git://$GIT_DAEMON_HOST_PORT |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 37 | |
| 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 | |
| 46 | trap 'code=$?; stop_git_daemon; (exit $code); die' EXIT |
| 47 | |
| 48 | say >&3 "Starting git daemon ..." |
Clemens Buchacher | 561b133 | 2012-01-07 12:42:47 +0100 | [diff] [blame] | 49 | mkfifo git_daemon_output |
Jeff King | bd4d9d9 | 2017-02-25 04:37:30 -0500 | [diff] [blame] | 50 | ${LIB_GIT_DAEMON_COMMAND:-git daemon} \ |
| 51 | --listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \ |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 52 | --reuseaddr --verbose \ |
| 53 | --base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \ |
| 54 | "$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \ |
Clemens Buchacher | 561b133 | 2012-01-07 12:42:47 +0100 | [diff] [blame] | 55 | >&3 2>git_daemon_output & |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 56 | GIT_DAEMON_PID=$! |
Jeff King | 314a73d | 2018-01-25 14:16:41 -0500 | [diff] [blame] | 57 | >daemon.log |
Clemens Buchacher | 561b133 | 2012-01-07 12:42:47 +0100 | [diff] [blame] | 58 | { |
Jeff King | 314a73d | 2018-01-25 14:16:41 -0500 | [diff] [blame] | 59 | read -r line <&7 |
| 60 | printf "%s\n" "$line" |
| 61 | printf >&4 "%s\n" "$line" |
| 62 | ( |
| 63 | while read -r line <&7 |
| 64 | do |
| 65 | printf "%s\n" "$line" |
| 66 | printf >&4 "%s\n" "$line" |
| 67 | done |
| 68 | ) & |
| 69 | } 7<git_daemon_output >>"$TRASH_DIRECTORY/daemon.log" && |
Clemens Buchacher | 561b133 | 2012-01-07 12:42:47 +0100 | [diff] [blame] | 70 | |
Johannes Sixt | 46e3581 | 2012-04-26 23:00:39 +0200 | [diff] [blame] | 71 | # Check expected output |
| 72 | if test x"$(expr "$line" : "\[[0-9]*\] \(.*\)")" != x"Ready to rumble" |
| 73 | then |
| 74 | kill "$GIT_DAEMON_PID" |
| 75 | wait "$GIT_DAEMON_PID" |
| 76 | trap 'die' EXIT |
Jeff King | 83d842d | 2014-02-10 16:29:37 -0500 | [diff] [blame] | 77 | test_skip_or_die $GIT_TEST_GIT_DAEMON \ |
| 78 | "git daemon failed to start" |
Johannes Sixt | 46e3581 | 2012-04-26 23:00:39 +0200 | [diff] [blame] | 79 | fi |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | stop_git_daemon() { |
| 83 | if test -z "$GIT_DAEMON_PID" |
| 84 | then |
| 85 | return |
| 86 | fi |
| 87 | |
| 88 | trap 'die' EXIT |
| 89 | |
| 90 | # kill git-daemon child of git |
| 91 | say >&3 "Stopping git daemon ..." |
| 92 | kill "$GIT_DAEMON_PID" |
| 93 | wait "$GIT_DAEMON_PID" >&3 2>&4 |
| 94 | ret=$? |
Jeff King | 03c39b3 | 2016-06-24 15:45:12 -0400 | [diff] [blame] | 95 | if test_match_signal 15 $? |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 96 | then |
| 97 | error "git daemon exited with status: $ret" |
| 98 | fi |
| 99 | GIT_DAEMON_PID= |
Clemens Buchacher | 561b133 | 2012-01-07 12:42:47 +0100 | [diff] [blame] | 100 | rm -f git_daemon_output |
Clemens Buchacher | 71039fb | 2012-01-07 12:42:45 +0100 | [diff] [blame] | 101 | } |
Jeff King | 4414a15 | 2018-01-24 19:58:19 -0500 | [diff] [blame] | 102 | |
| 103 | # A stripped-down version of a netcat client, that connects to a "host:port" |
| 104 | # given in $1, sends its stdin followed by EOF, then dumps the response (until |
| 105 | # EOF) to stdout. |
| 106 | fake_nc() { |
| 107 | if ! test_declared_prereq FAKENC |
| 108 | then |
| 109 | echo >&4 "fake_nc: need to declare FAKENC prerequisite" |
| 110 | return 127 |
| 111 | fi |
| 112 | perl -Mstrict -MIO::Socket::INET -e ' |
| 113 | my $s = IO::Socket::INET->new(shift) |
| 114 | or die "unable to open socket: $!"; |
| 115 | print $s <STDIN>; |
| 116 | $s->shutdown(1); |
| 117 | print <$s>; |
| 118 | ' "$@" |
| 119 | } |
| 120 | |
| 121 | test_lazy_prereq FAKENC ' |
| 122 | perl -MIO::Socket::INET -e "exit 0" |
| 123 | ' |