upload-pack: test negotiation with changing repository
authorBrandon Williams <bmwill@google.com>
Wed, 27 Jun 2018 22:30:18 +0000 (15:30 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 28 Jun 2018 16:33:29 +0000 (09:33 -0700)
Add tests to check the behavior of fetching from a repository which
changes between rounds of negotiation (for example, when different
servers in a load-balancing agreement participate in the same stateless
RPC negotiation). This forms a baseline of comparison to the ref-in-want
functionality (which will be introduced to the client in subsequent
commits), and ensures that subsequent commits do not change existing
behavior.

As part of this effort, a mechanism to substitute strings in a single
HTTP response is added.

Signed-off-by: Brandon Williams <bmwill@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/lib-httpd.sh
t/lib-httpd/apache.conf
t/lib-httpd/apply-one-time-sed.sh [new file with mode: 0644]
t/t5703-upload-pack-ref-in-want.sh
index 435a37465a702c35a5d53d8809f0ef700ae9ff83..ed41b155af9a38a471a2518fc42d7c9a1f110da1 100644 (file)
@@ -132,6 +132,7 @@ prepare_httpd() {
        cp "$TEST_PATH"/passwd "$HTTPD_ROOT_PATH"
        install_script broken-smart-http.sh
        install_script error.sh
+       install_script apply-one-time-sed.sh
 
        ln -s "$LIB_HTTPD_MODULE_PATH" "$HTTPD_ROOT_PATH/modules"
 
index 724d9ae462b78af95298786a1fe26f375023ad55..581c010d8fc4c1184ba17b63b940aaac30d0b0f4 100644 (file)
@@ -111,9 +111,14 @@ Alias /auth/dumb/ www/auth/dumb/
        SetEnv GIT_EXEC_PATH ${GIT_EXEC_PATH}
        SetEnv GIT_HTTP_EXPORT_ALL
 </LocationMatch>
+<LocationMatch /one_time_sed/>
+       SetEnv GIT_EXEC_PATH ${GIT_EXEC_PATH}
+       SetEnv GIT_HTTP_EXPORT_ALL
+</LocationMatch>
 ScriptAliasMatch /smart_*[^/]*/(.*) ${GIT_EXEC_PATH}/git-http-backend/$1
 ScriptAlias /broken_smart/ broken-smart-http.sh/
 ScriptAlias /error/ error.sh/
+ScriptAliasMatch /one_time_sed/(.*) apply-one-time-sed.sh/$1
 <Directory ${GIT_EXEC_PATH}>
        Options FollowSymlinks
 </Directory>
@@ -123,6 +128,9 @@ ScriptAlias /error/ error.sh/
 <Files error.sh>
   Options ExecCGI
 </Files>
+<Files apply-one-time-sed.sh>
+       Options ExecCGI
+</Files>
 <Files ${GIT_EXEC_PATH}/git-http-backend>
        Options ExecCGI
 </Files>
diff --git a/t/lib-httpd/apply-one-time-sed.sh b/t/lib-httpd/apply-one-time-sed.sh
new file mode 100644 (file)
index 0000000..fcef728
--- /dev/null
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+# If "one-time-sed" exists in $HTTPD_ROOT_PATH, run sed on the HTTP response,
+# using the contents of "one-time-sed" as the sed command to be run. If the
+# response was modified as a result, delete "one-time-sed" so that subsequent
+# HTTP responses are no longer modified.
+#
+# This can be used to simulate the effects of the repository changing in
+# between HTTP request-response pairs.
+if [ -e one-time-sed ]; then
+       "$GIT_EXEC_PATH/git-http-backend" >out
+       sed "$(cat one-time-sed)" <out >out_modified
+
+       if diff out out_modified >/dev/null; then
+               cat out
+       else
+               cat out_modified
+               rm one-time-sed
+       fi
+else
+       "$GIT_EXEC_PATH/git-http-backend"
+fi
index da86f7cde0e55bc5ab2c59fcc0ff509060ebab9b..32527a59c4dd52e89ca354fe71ecdeb20b9ce728 100755 (executable)
@@ -157,4 +157,72 @@ test_expect_success 'want-ref with ref we already have commit for' '
        check_output
 '
 
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+REPO="$HTTPD_DOCUMENT_ROOT_PATH/repo"
+LOCAL_PRISTINE="$(pwd)/local_pristine"
+
+test_expect_success 'setup repos for change-while-negotiating test' '
+       (
+               git init "$REPO" &&
+               cd "$REPO" &&
+               >.git/git-daemon-export-ok &&
+               test_commit m1 &&
+               git tag -d m1 &&
+
+               # Local repo with many commits (so that negotiation will take
+               # more than 1 request/response pair)
+               git clone "http://127.0.0.1:$LIB_HTTPD_PORT/smart/repo" "$LOCAL_PRISTINE" &&
+               cd "$LOCAL_PRISTINE" &&
+               git checkout -b side &&
+               for i in $(seq 1 33); do test_commit s$i; done &&
+
+               # Add novel commits to upstream
+               git checkout master &&
+               cd "$REPO" &&
+               test_commit m2 &&
+               test_commit m3 &&
+               git tag -d m2 m3
+       ) &&
+       git -C "$LOCAL_PRISTINE" remote set-url origin "http://127.0.0.1:$LIB_HTTPD_PORT/one_time_sed/repo" &&
+       git -C "$LOCAL_PRISTINE" config protocol.version 2
+'
+
+inconsistency () {
+       # Simulate that the server initially reports $2 as the ref
+       # corresponding to $1, and after that, $1 as the ref corresponding to
+       # $1. This corresponds to the real-life situation where the server's
+       # repository appears to change during negotiation, for example, when
+       # different servers in a load-balancing arrangement serve (stateless)
+       # RPCs during a single negotiation.
+       printf "s/%s/%s/" \
+              $(git -C "$REPO" rev-parse $1 | tr -d "\n") \
+              $(git -C "$REPO" rev-parse $2 | tr -d "\n") \
+              >"$HTTPD_ROOT_PATH/one-time-sed"
+}
+
+test_expect_success 'server is initially ahead - no ref in want' '
+       git -C "$REPO" config uploadpack.allowRefInWant false &&
+       rm -rf local &&
+       cp -r "$LOCAL_PRISTINE" local &&
+       inconsistency master 1234567890123456789012345678901234567890 &&
+       test_must_fail git -C local fetch 2>err &&
+       grep "ERR upload-pack: not our ref" err
+'
+
+test_expect_success 'server is initially behind - no ref in want' '
+       git -C "$REPO" config uploadpack.allowRefInWant false &&
+       rm -rf local &&
+       cp -r "$LOCAL_PRISTINE" local &&
+       inconsistency master "master^" &&
+       git -C local fetch &&
+
+       git -C "$REPO" rev-parse --verify "master^" >expected &&
+       git -C local rev-parse --verify refs/remotes/origin/master >actual &&
+       test_cmp expected actual
+'
+
+stop_httpd
+
 test_done