Skip to content

Commit

Permalink
Normalize the root path
Browse files Browse the repository at this point in the history
This makes sure there's never any confusion about whether it is an
absolute path or has symlinks or whatever.  Add e2e cases to cover.
  • Loading branch information
thockin committed Nov 9, 2020
1 parent 93e74c2 commit 24c06a5
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 5 deletions.
26 changes: 24 additions & 2 deletions cmd/git-sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,16 @@ func main() {
}
}

if err := os.MkdirAll(*flRoot, 0700); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: can't make root dir: %v", err)
os.Exit(1)
}
absRoot, err := normalizePath(*flRoot)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: can't normalize root path: %v", err)
os.Exit(1)
}

if *flAddUser {
if err := addUser(); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: can't write to /etc/passwd: %v\n", err)
Expand Down Expand Up @@ -496,7 +506,7 @@ func main() {
for {
start := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), *flSyncTimeout)
if changed, hash, err := syncRepo(ctx, *flRepo, *flBranch, *flRev, *flDepth, *flRoot, *flLink, *flAskPassURL, *flSubmodules); err != nil {
if changed, hash, err := syncRepo(ctx, *flRepo, *flBranch, *flRev, *flDepth, absRoot, *flLink, *flAskPassURL, *flSubmodules); err != nil {
updateSyncMetrics(metricKeyError, start)
if *flMaxSyncFailures != -1 && failCount >= *flMaxSyncFailures {
// Exit after too many retries, maybe the error is not recoverable.
Expand All @@ -523,7 +533,7 @@ func main() {
if *flOneTime {
os.Exit(0)
}
if isHash, err := revIsHash(ctx, *flRev, *flRoot); err != nil {
if isHash, err := revIsHash(ctx, *flRev, absRoot); err != nil {
log.Error(err, "can't tell if rev is a git hash, exiting", "rev", *flRev)
os.Exit(1)
} else if isHash {
Expand All @@ -540,6 +550,18 @@ func main() {
}
}

func normalizePath(path string) (string, error) {
delinked, err := filepath.EvalSymlinks(path)
if err != nil {
return "", err
}
abs, err := filepath.Abs(delinked)
if err != nil {
return "", err
}
return abs, nil
}

func updateSyncMetrics(key string, start time.Time) {
syncDuration.WithLabelValues(key).Observe(time.Since(start).Seconds())
syncCount.WithLabelValues(key).Inc()
Expand Down
67 changes: 64 additions & 3 deletions test_e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ function remove_containers() {
}

##############################################
# Test HEAD one-time
# Test HEAD one-time when root doesn't exist
##############################################
testcase "head-once"
# First sync
testcase "head-once-root-doesnt-exist"
echo "$TESTCASE" > "$REPO"/file
git -C "$REPO" commit -qam "$TESTCASE"
rm -rf "$ROOT" # remove the root to test
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
Expand All @@ -164,6 +164,67 @@ assert_file_eq "$ROOT"/link/file "$TESTCASE"
# Wrap up
pass

##############################################
# Test HEAD one-time when root exists
##############################################
testcase "head-once-root-exists"
echo "$TESTCASE" > "$REPO"/file
git -C "$REPO" commit -qam "$TESTCASE"
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--branch=master \
--rev=HEAD \
--root="$ROOT" \
--link="link" \
> "$DIR"/log."$TESTCASE" 2>&1
assert_link_exists "$ROOT"/link
assert_file_exists "$ROOT"/link/file
assert_file_eq "$ROOT"/link/file "$TESTCASE"
# Wrap up
pass

##############################################
# Test HEAD one-time with a weird --root flag
##############################################
testcase "head-once-root-flag-is-weird"
echo "$TESTCASE" > "$REPO"/file
git -C "$REPO" commit -qam "$TESTCASE"
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--branch=master \
--rev=HEAD \
--root="../../../../../$ROOT/../../../../../../$ROOT" \
--link="link" \
> "$DIR"/log."$TESTCASE" 2>&1
assert_link_exists "$ROOT"/link
assert_file_exists "$ROOT"/link/file
assert_file_eq "$ROOT"/link/file "$TESTCASE"
# Wrap up
pass

##############################################
# Test HEAD one-time with a symlink in --root
##############################################
testcase "head-once-root-flag-has-symlink"
echo "$TESTCASE" > "$REPO"/file
git -C "$REPO" commit -qam "$TESTCASE"
ln -s "$ROOT" "$DIR/rootlink" # symlink to test
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--branch=master \
--rev=HEAD \
--root="$DIR/rootlink" \
--link="link" \
> "$DIR"/log."$TESTCASE" 2>&1
assert_link_exists "$ROOT"/link
assert_file_exists "$ROOT"/link/file
assert_file_eq "$ROOT"/link/file "$TESTCASE"
# Wrap up
pass

##############################################
# Test default syncing
##############################################
Expand Down

0 comments on commit 24c06a5

Please sign in to comment.