From 185074f2509e8c726b3c71079479acb1e08a0886 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Mon, 13 Jun 2022 14:07:08 -0700 Subject: [PATCH] Beef up askpass_url support * Don't need to get the password at startup (it happens in the sync loop anyway) * Add tests for bad password and flaky URL --- askpass_git.sh | 2 +- cmd/git-sync/main.go | 8 ---- test_e2e.sh | 104 ++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 98 insertions(+), 16 deletions(-) diff --git a/askpass_git.sh b/askpass_git.sh index c6230f0..cac75eb 100755 --- a/askpass_git.sh +++ b/askpass_git.sh @@ -24,7 +24,7 @@ touch "${XDG_CONFIG_HOME}/git/config" # outside the e2e test environment. See https://git-scm.com/docs/git-credential-store touch "${XDG_CONFIG_HOME}/git/credentials" -if [ "$1" != "clone" ]; then +if [ "$1" != "clone" -a "$1" != "ls-remote" -a "$1" != "fetch" ]; then git "$@" exit $? fi diff --git a/cmd/git-sync/main.go b/cmd/git-sync/main.go index e0f438a..c9c107e 100644 --- a/cmd/git-sync/main.go +++ b/cmd/git-sync/main.go @@ -414,14 +414,6 @@ func main() { } } - if *flAskPassURL != "" { - if err := callGitAskPassURL(ctx, *flAskPassURL); err != nil { - askpassCount.WithLabelValues(metricKeyError).Inc() - handleError(false, "ERROR: failed to call ASKPASS callback URL: %v", err) - } - askpassCount.WithLabelValues(metricKeySuccess).Inc() - } - // Set additional configs we want, but users might override. if err := setupDefaultGitConfigs(ctx); err != nil { fmt.Fprintf(os.Stderr, "ERROR: can't set default git configs: %v\n", err) diff --git a/test_e2e.sh b/test_e2e.sh index 8de60fc..82ea186 100755 --- a/test_e2e.sh +++ b/test_e2e.sh @@ -915,13 +915,14 @@ function e2e::password_correct_password() { } ############################################## -# Test askpass-url +# Test askpass-url with bad password ############################################## -function e2e::askpass_url() { - echo "$FUNCNAME 1" > "$REPO"/file - git -C "$REPO" commit -qam "$FUNCNAME 1" +function e2e::askpass_url_wrong_password() { # run the askpass-url service with wrong password + HITLOG="$WORK/hitlog" + cat /dev/null > "$HITLOG" CTR=$(docker_run \ + -v "$HITLOG":/var/log/hits \ e2e/test/test-ncsvr \ 80 ' echo "HTTP/1.1 200 OK" @@ -943,10 +944,17 @@ function e2e::askpass_url() { >> "$1" 2>&1 || true # check for failure assert_file_absent "$ROOT"/link/file +} +############################################## +# Test askpass-url +############################################## +function e2e::askpass_url() { # run with askpass_url service with correct password - docker_kill "$CTR" + HITLOG="$WORK/hitlog" + cat /dev/null > "$HITLOG" CTR=$(docker_run \ + -v "$HITLOG":/var/log/hits \ e2e/test/test-ncsvr \ 80 ' echo "HTTP/1.1 200 OK" @@ -956,16 +964,98 @@ function e2e::askpass_url() { ') IP=$(docker_ip "$CTR") + # First sync + echo "$FUNCNAME 1" > "$REPO"/file + git -C "$REPO" commit -qam "$FUNCNAME 1" + GIT_SYNC \ --git="$ASKPASS_GIT" \ --askpass-url="http://$IP/git_askpass" \ - --one-time \ + --wait=0.1 \ --repo="file://$REPO" \ --branch="$MAIN_BRANCH" \ --rev=HEAD \ --root="$ROOT" \ --dest="link" \ - >> "$1" 2>&1 + >> "$1" 2>&1 & + sleep 3 + assert_link_exists "$ROOT"/link + assert_file_exists "$ROOT"/link/file + assert_file_eq "$ROOT"/link/file "$FUNCNAME 1" + + # Move HEAD forward + echo "$FUNCNAME 2" > "$REPO"/file + git -C "$REPO" commit -qam "$FUNCNAME 2" + sleep 3 + assert_link_exists "$ROOT"/link + assert_file_exists "$ROOT"/link/file + assert_file_eq "$ROOT"/link/file "$FUNCNAME 2" + + # Move HEAD backward + git -C "$REPO" reset -q --hard HEAD^ + sleep 3 + assert_link_exists "$ROOT"/link + assert_file_exists "$ROOT"/link/file + assert_file_eq "$ROOT"/link/file "$FUNCNAME 1" +} + +############################################## +# Test askpass-url where the URL is flaky +############################################## +function e2e::askpass_url_flaky() { + # run with askpass_url service which alternates good/bad replies. + HITLOG="$WORK/hitlog" + cat /dev/null > "$HITLOG" + CTR=$(docker_run \ + -v "$HITLOG":/var/log/hits \ + e2e/test/test-ncsvr \ + 80 ' + echo "HTTP/1.1 200 OK" + echo + if [ -f /tmp/flag ]; then + echo "username=my-username" + echo "password=my-password" + rm /tmp/flag + else + echo "username=my-username" + echo "password=wrong" + touch /tmp/flag + fi + ') + IP=$(docker_ip "$CTR") + + # First sync + echo "$FUNCNAME 1" > "$REPO"/file + git -C "$REPO" commit -qam "$FUNCNAME 1" + + GIT_SYNC \ + --git="$ASKPASS_GIT" \ + --askpass-url="http://$IP/git_askpass" \ + --max-sync-failures=2 \ + --wait=0.1 \ + --repo="file://$REPO" \ + --branch="$MAIN_BRANCH" \ + --rev=HEAD \ + --root="$ROOT" \ + --dest="link" \ + >> "$1" 2>&1 & + sleep 3 + assert_link_exists "$ROOT"/link + assert_file_exists "$ROOT"/link/file + assert_file_eq "$ROOT"/link/file "$FUNCNAME 1" + + # Move HEAD forward + echo "$FUNCNAME 2" > "$REPO"/file + git -C "$REPO" commit -qam "$FUNCNAME 2" + sleep 3 + assert_link_exists "$ROOT"/link + assert_file_exists "$ROOT"/link/file + assert_file_eq "$ROOT"/link/file "$FUNCNAME 2" + + # Move HEAD backward + git -C "$REPO" reset -q --hard HEAD^ + sleep 3 + assert_link_exists "$ROOT"/link assert_file_exists "$ROOT"/link/file assert_file_eq "$ROOT"/link/file "$FUNCNAME 1"