t / lib-git-daemon.shon commit Merge branch 'dl/compat-cleanup' (00bb744)
   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
  18if ! git env--helper --type=bool --default=true --exit-code GIT_TEST_GIT_DAEMON
  19then
  20        skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)"
  21        test_done
  22fi
  23
  24if test_have_prereq !PIPE
  25then
  26        test_skip_or_die GIT_TEST_GIT_DAEMON "file system does not support FIFOs"
  27fi
  28
  29test_set_port LIB_GIT_DAEMON_PORT
  30
  31GIT_DAEMON_PID=
  32GIT_DAEMON_PIDFILE="$PWD"/daemon.pid
  33GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo
  34GIT_DAEMON_HOST_PORT=127.0.0.1:$LIB_GIT_DAEMON_PORT
  35GIT_DAEMON_URL=git://$GIT_DAEMON_HOST_PORT
  36
  37registered_stop_git_daemon_atexit_handler=
  38start_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
  80stop_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        kill "$GIT_DAEMON_PID"
  89        wait "$GIT_DAEMON_PID" >&3 2>&4
  90        ret=$?
  91        if ! test_match_signal 15 $ret
  92        then
  93                error "git daemon exited with status: $ret"
  94        fi
  95        kill "$(cat "$GIT_DAEMON_PIDFILE")" 2>/dev/null
  96        GIT_DAEMON_PID=
  97        rm -f git_daemon_output "$GIT_DAEMON_PIDFILE"
  98}
  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.
 103fake_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
 118test_lazy_prereq FAKENC '
 119        perl -MIO::Socket::INET -e "exit 0"
 120'