Files
Reims-Go/internal/guesttools/share_mount_test.go
T
NwflowerandClaude Opus 5.5 085891961f fix(guesttools): avoid truncated overwrites and blocking share probes
- Record the uploaded revision when a file changes mid-upload, so the next
  round sends the finished copy instead of restoring the truncated one.
- Keep both copies when sizes differ but timestamps are equal.
- Probe the WebDAV mount in the background with a time limit, and keep the
  mount while the host is unreachable.
- Mount over a non-empty directory as before. A stray .DS_Store no longer
  forces sync mode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 08:27:36 -07:00

293 lines
7.9 KiB
Go

package guesttools
import (
"context"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)
func runShareShell(t *testing.T, script string, helpers map[string]string) string {
t.Helper()
lockShellFixture(t)
sh, err := exec.LookPath("sh")
if runtime.GOOS == "windows" {
gitSh := filepath.Join(os.Getenv("ProgramFiles"), "Git", "bin", "sh.exe")
if _, statErr := os.Stat(gitSh); statErr == nil {
sh, err = gitSh, nil
}
}
if err != nil {
t.Fatal(err)
}
dir := t.TempDir()
for name, helper := range helpers {
if err := os.WriteFile(filepath.Join(dir, name), []byte(helper), 0o755); err != nil {
t.Fatal(err)
}
}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, sh, "-s")
cmd.Dir = dir
cmd.Env = append(os.Environ(), "LC_ALL=C", "MSYS=winsymlinks:lnk")
cmd.Stdin = strings.NewReader(script)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("sh: %v\n%s", err, out)
}
return string(out)
}
func TestGuestAgentDefaultWebDAVMount(t *testing.T) {
agent := string(GuestAgentScript())
mounts := scriptSection(t, agent, "urlencode() {", "\nhash_file() {") +
scriptSection(t, agent, "update_desktop_link() {", "\nsleep_active=") +
scriptSection(t, agent, "\ndrive_mount_failures=", "\nmount_shared_folder() {")
for _, scenario := range []struct {
name, check, output string
}{
{"authenticated_and_healthy", `
fail_mount=22
ensure_webdav_mount
check_mode sync
expect "$share_exit" 22
fail_mount=0
ensure_webdav_mount
check_mode webdav
expect "$share_exit" 0
ensure_webdav_mount
check_mode webdav
expect "$share_mount_failed" 0
expect "$(cat calls)" "$(printf 'mount\nmount')"
expect "$(ls -A "$TMP_DIR")" ""
`, "ReimsShare: mount failed (exit 22)\nAuthentication failed\n"},
{"failed_retry_and_log_reset", `
fail_mount=22
ensure_webdav_mount
check_mode sync
ensure_webdav_mount
check_mode sync
fail_mount=0
ensure_webdav_mount
check_mode webdav
umount "$SHARE_MOUNT"
fail_mount=47
ensure_webdav_mount
check_mode sync
expect "$share_exit" 47
expect "$(cat calls)" "$(printf 'mount\nmount\nmount\numount\nmount')"
`, "ReimsShare: mount failed (exit 22)\nAuthentication failed\nReimsShare: mount failed (exit 47)\nAuthentication failed\n"},
{"symbolic_link", `
ln -s "$SHARE_SYNC" "$SHARE_MOUNT"
ensure_webdav_mount
check_mode sync
test -L "$SHARE_MOUNT"
expect "$(cat calls)" ""
`, ""},
{"nonempty_directory", `
mkdir "$SHARE_MOUNT"
printf 'keep' >"$SHARE_MOUNT/.DS_Store"
ensure_webdav_mount
check_mode webdav
expect "$(cat "$SHARE_MOUNT/.DS_Store")" keep
expect "$(cat calls)" mount
`, ""},
{"single_failed_probe", `
seed_mount "${REIMS_HOST_URL%/}/dav/mac%20test/" webdav
unresponsive=1
busy=normal
ensure_webdav_mount
check_mode webdav
expect "$(cat calls)" ""
`, ""},
{"three_failed_probes", `
seed_mount "${REIMS_HOST_URL%/}/dav/mac%20test/" webdav
unresponsive=1
busy=normal
ensure_webdav_mount
check_mode webdav
ensure_webdav_mount
check_mode webdav
expect "$(cat calls)" ""
ensure_webdav_mount
check_mode webdav
expect "$(cat calls)" "$(printf 'umount -f\nmount')"
`, "ReimsShare: WebDAV mount is unresponsive\n"},
{"successful_probe_resets_failures", `
seed_mount "${REIMS_HOST_URL%/}/dav/mac%20test/" webdav
unresponsive=1
busy=normal
ensure_webdav_mount
ensure_webdav_mount
unresponsive=0
ensure_webdav_mount
check_mode webdav
expect "$share_probe_failures" 0
unresponsive=1
ensure_webdav_mount
ensure_webdav_mount
check_mode webdav
expect "$(cat calls)" ""
ensure_webdav_mount
check_mode webdav
expect "$(cat calls)" "$(printf 'umount -f\nmount')"
`, "ReimsShare: WebDAV mount is unresponsive\n"},
{"busy_zombie", `
seed_mount "${REIMS_HOST_URL%/}/dav/mac%20test/" webdav
unresponsive=1
busy=all
ensure_webdav_mount
ensure_webdav_mount
check_mode webdav
expect "$(cat calls)" ""
ensure_webdav_mount
check_mode sync
ensure_webdav_mount
check_mode sync
expect "$(cat calls)" "$(printf 'umount -f\numount -f')"
`, "ReimsShare: WebDAV mount is unresponsive\n"},
{"hung_probe", `
seed_mount "${REIMS_HOST_URL%/}/dav/mac%20test/" webdav
hung=1
start=$(date +%s)
share_probe_failures=1
ensure_webdav_mount
check_mode webdav
expect "$(cat calls)" ""
test -n "$SHARE_PROBE_PID"
ensure_webdav_mount
test "$(($(date +%s) - start))" -lt 15
check_mode webdav
expect "$(cat calls)" "$(printf 'umount -f\nmount')"
expect "$(cat probes)" probe
expect "$SHARE_PROBE_PID" ""
`, "ReimsShare: WebDAV mount is unresponsive\n"},
{"host_down_keeps_mount", `
seed_mount "${REIMS_HOST_URL%/}/dav/mac%20test/" webdav
unresponsive=1
host_down=1
for attempt in 1 2 3 4; do ensure_webdav_mount; done
check_mode webdav
expect "$(cat calls)" ""
host_down=0
ensure_webdav_mount
check_mode webdav
expect "$(cat calls)" "$(printf 'umount -f\nmount')"
`, "ReimsShare: WebDAV mount is unresponsive\n"},
{"old_port", `
seed_mount "http://10.0.2.2:19846/dav/mac%20test/" webdav
ensure_webdav_mount
check_mode webdav
expect "$(cat calls)" "$(printf 'umount\nmount')"
`, ""},
{"busy_old_port", `
seed_mount "http://10.0.2.2:19846/dav/mac%20test/" webdav
busy=all
ensure_webdav_mount
check_mode sync
expect "$(cat calls)" "$(printf 'umount\numount -f')"
`, ""},
{"foreign_webdav_host", `
seed_mount "https://cloud.example.com/dav/mac%20test/" webdav
ensure_webdav_mount
check_mode sync
expect "$(cat calls)" ""
`, ""},
{"other_vm_webdav", `
seed_mount "http://10.0.2.2:19847/dav/other-vm/" webdav
ensure_webdav_mount
check_mode sync
expect "$(cat calls)" ""
`, ""},
{"other_filesystem", `
seed_mount /dev/disk2 apfs
ensure_webdav_mount
check_mode sync
expect "$(cat calls)" ""
`, ""},
} {
t.Run(scenario.name, func(t *testing.T) {
out := runShareShell(t, folderShellFixture+defaultShareMountFixture+mounts+scenario.check,
map[string]string{"mount_webdav": defaultShareMountWebDAV})
if out != scenario.output {
t.Fatalf("mount log = %q, want %q", out, scenario.output)
}
})
}
}
const defaultShareMountFixture = `
REIMS_HOST_URL=http://10.0.2.2:19847/
REIMS_VM="mac test"
SHARE_MOUNT="$HOME/Reims Share Mount"
SHARE_SYNC="$HOME/Reims Share"
DESKTOP_LINK="$HOME/Desktop/ReimsShare"
export SHARE_MOUNT
mkdir "$SHARE_SYNC"
unresponsive=0
hung=0
host_down=0
curl() { [ "$host_down" = 0 ]; }
umount() {
if [ "$1" = -f ]; then
printf 'umount -f\n' >>calls
shift
[ "$busy" != all ] || return 1
else
printf 'umount\n' >>calls
[ "$busy" = 0 ] || return 1
fi
expect "$1" "$SHARE_MOUNT"
: >mounts
unresponsive=0
}
ls() {
if [ "$hung" = 1 ] && [ "$1" = "$SHARE_MOUNT" ]; then
printf 'probe\n' >>probes
while :; do command sleep 0.2; done
fi
if [ "$unresponsive" = 1 ] && [ "$1" = "$SHARE_MOUNT" ]; then return 1; fi
command ls "$@"
}
seed_mount() {
mkdir "$SHARE_MOUNT"
printf '%s on %s (%s)\n' "$1" "$SHARE_MOUNT" "$2" >mounts
}
check_mode() {
expect "$mount_mode" "$1"
if [ "$1" = webdav ]; then
expect "$(readlink "$DESKTOP_LINK")" "$SHARE_MOUNT"
expect "$(cat mounts)" "${REIMS_HOST_URL%/}/dav/mac%20test/ on $SHARE_MOUNT (webdav)"
else
expect "$(readlink "$DESKTOP_LINK")" "$SHARE_SYNC"
fi
}
`
const defaultShareMountWebDAV = `#!/usr/bin/perl
use strict;
use warnings;
my $url = "http://10.0.2.2:19847/dav/mac%20test/";
my $target = $ENV{SHARE_MOUNT};
die "Unexpected mount options" unless @ARGV == 7 &&
"@ARGV[0..4]" eq "-a 3 -S -v ReimsShare" && $ARGV[5] eq $url && $ARGV[6] eq $target;
open(my $auth, "<&=3") or die "Missing credential descriptor: $!";
seek($auth, 0, 0) or die "Credential descriptor is not seekable: $!";
binmode($auth);
die "Invalid credential payload" unless do { local $/; <$auth> } eq pack("Na5Na5NNN", 5, "reims", 5, "token", 0, 0, 0);
open(my $calls, ">>", "calls") or die "Could not record mount call: $!";
print $calls "mount\n";
if ($ENV{fail_mount}) {
print STDERR "Authentication failed\n";
exit $ENV{fail_mount};
}
die "Mount point already in use" if -s "mounts";
open(my $mounts, ">", "mounts") or die "Could not record mount: $!";
print $mounts "$url on $target (webdav)\n";
`