| 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 | # test_done |
| 17 | |
| 18 | if ! test_bool_env GIT_TEST_GIT_DAEMON true |
| 19 | then |
| 20 | skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)" |
| 21 | test_done |
| 22 | fi |
| 23 | |
| 24 | if test_have_prereq !PIPE |
| 25 | then |
| 26 | test_skip_or_die GIT_TEST_GIT_DAEMON "file system does not support FIFOs" |
| 27 | fi |
| 28 | |
| 29 | test_set_port LIB_GIT_DAEMON_PORT |
| 30 | |
| 31 | GIT_DAEMON_PID= |
| 32 | GIT_DAEMON_PIDFILE="$PWD"/daemon.pid |
| 33 | GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo |
| 34 | GIT_DAEMON_HOST_PORT=127.0.0.1:$LIB_GIT_DAEMON_PORT |
| 35 | GIT_DAEMON_URL=git://$GIT_DAEMON_HOST_PORT |
| 36 | |
| 37 | registered_stop_git_daemon_atexit_handler= |
| 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 | # 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 |
| 53 | |
| 54 | say >&3 "Starting git daemon ..." |
| 55 | mkfifo git_daemon_output |
| 56 | ${LIB_GIT_DAEMON_COMMAND:-git daemon} \ |
| 57 | --listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \ |
| 58 | --reuseaddr --verbose --pid-file="$GIT_DAEMON_PIDFILE" \ |
| 59 | --base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \ |
| 60 | "$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \ |
| 61 | >&3 2>git_daemon_output & |
| 62 | GIT_DAEMON_PID=$! |
| 63 | { |
| 64 | read -r line <&7 |
| 65 | printf "%s\n" "$line" >&4 |
| 66 | cat <&7 >&4 & |
| 67 | } 7<git_daemon_output && |
| 68 | |
| 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" |
| 74 | unset GIT_DAEMON_PID |
| 75 | test_skip_or_die GIT_TEST_GIT_DAEMON \ |
| 76 | "git daemon failed to start" |
| 77 | fi |
| 78 | } |
| 79 | |
| 80 | stop_git_daemon() { |
| 81 | if test -z "$GIT_DAEMON_PID" |
| 82 | then |
| 83 | return |
| 84 | fi |
| 85 | |
| 86 | # kill git-daemon child of git |
| 87 | say >&3 "Stopping git daemon ..." |
| 88 | |
| 89 | kill "$GIT_DAEMON_PID" |
| 90 | ret=0; wait "$GIT_DAEMON_PID" >&3 2>&4 || ret=$? |
| 91 | |
| 92 | if ! test_match_signal 15 $ret |
| 93 | then |
| 94 | error "git daemon exited with status: $ret" |
| 95 | fi |
| 96 | |
| 97 | kill "$(cat "$GIT_DAEMON_PIDFILE")" 2>/dev/null || : |
| 98 | GIT_DAEMON_PID= |
| 99 | rm -f git_daemon_output "$GIT_DAEMON_PIDFILE" |
| 100 | } |
| 101 | |
| 102 | # A stripped-down version of a netcat client, that connects to a "host:port" |
| 103 | # given in $1, sends its stdin followed by EOF, then dumps the response (until |
| 104 | # EOF) to stdout. |
| 105 | fake_nc() { |
| 106 | if ! test_declared_prereq FAKENC |
| 107 | then |
| 108 | echo >&4 "fake_nc: need to declare FAKENC prerequisite" |
| 109 | return 127 |
| 110 | fi |
| 111 | perl -Mstrict -MIO::Socket::INET -e ' |
| 112 | my $s = IO::Socket::INET->new(shift) |
| 113 | or die "unable to open socket: $!"; |
| 114 | print $s <STDIN>; |
| 115 | $s->shutdown(1); |
| 116 | print <$s>; |
| 117 | ' "$@" |
| 118 | } |
| 119 | |
| 120 | test_lazy_prereq FAKENC ' |
| 121 | perl -MIO::Socket::INET -e "exit 0" |
| 122 | ' |