Files
CephfsMig/internal/migration/rsync_test.go
T
Sebastian UnterschützandClaude Opus 5.5 a6fda8f8d3 style: add blank lines between declarations and statement blocks
Separate top-level declarations and multi-line statements with blank
lines, keeping an "if err != nil" check next to the statement it
belongs to. Expand semicolon one-liners, group the nativeTransport
fields by purpose, and format the cgo preamble as readable C.
Apart from the expanded one-liners and the C preamble, the change
is whitespace only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 18:36:19 +01:00

173 lines
4.5 KiB
Go

package migration
import (
"context"
"errors"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"testing"
"time"
)
type executorFunc func(context.Context, io.Writer, string, ...string) error
func (f executorFunc) Exec(ctx context.Context, out io.Writer, name string, args ...string) error {
return f(ctx, out, name, args...)
}
func TestRsyncRetriesOnlyObservedHardlinkEEXIST(t *testing.T) {
exitErr := exec.Command("sh", "-c", "exit 23").Run()
for name, tc := range map[string]struct {
diagnostic string
attempts int
}{
"hardlink": {"link target failed: File exists (17)", 2},
"permission": {"Permission denied (13)", 1},
"other-eexist": {"mkdir failed: File exists (17)", 1},
} {
t.Run(name, func(t *testing.T) {
calls := 0
x := executorFunc(func(context.Context, io.Writer, string, ...string) error {
calls++
if calls == 1 {
return &ProcessError{Name: "rsync", Err: exitErr, Diagnostic: tc.diagnostic}
}
return nil
})
err := (Rsync{Exec: x, Log: io.Discard}).Copy(context.Background(), "/a", "/b")
if calls != tc.attempts {
t.Fatalf("attempts: %d", calls)
}
if (err == nil) != (tc.attempts == 2) {
t.Fatalf("unexpected result: %v", err)
}
})
}
calls := 0
x := executorFunc(func(context.Context, io.Writer, string, ...string) error { calls++; return context.Canceled })
err := (Rsync{Exec: x}).Copy(context.Background(), "/a", "/b")
if !errors.Is(err, context.Canceled) || calls != 1 {
t.Fatalf("retried cancellation: %v, %d", err, calls)
}
}
func TestRsyncDetectsSameSizeSameMtimeAndPreservesLinks(t *testing.T) {
if _, err := exec.LookPath("rsync"); err != nil {
t.Skip("rsync is not installed")
}
src, dst := t.TempDir(), t.TempDir()
path := filepath.Join(src, "file with spaces\nand newline")
if err := os.WriteFile(path, []byte("AAAA"), 0640); err != nil {
t.Fatal(err)
}
if err := os.Link(path, filepath.Join(src, "hardlink")); err != nil {
t.Fatal(err)
}
if err := os.Symlink("nonexistent", filepath.Join(src, "dangling")); err != nil {
t.Fatal(err)
}
if err := syscall.Setxattr(path, "user.cephmig", []byte("test"), 0); err != nil {
t.Fatal(err)
}
r := Rsync{Exec: Process{Stderr: io.Discard}, Log: io.Discard}
ctx := context.Background()
if err := r.Copy(ctx, src, dst); err != nil {
t.Fatal(err)
}
if err := r.Verify(ctx, src, dst); err != nil {
t.Fatal(err)
}
a, _ := os.Stat(filepath.Join(dst, "hardlink"))
b, _ := os.Stat(filepath.Join(dst, filepath.Base(path)))
if !os.SameFile(a, b) {
t.Fatal("hardlinks were split")
}
st, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if err = os.WriteFile(path, []byte("BBBB"), 0640); err != nil {
t.Fatal(err)
}
if err = os.Chtimes(path, st.ModTime(), st.ModTime()); err != nil {
t.Fatal(err)
}
if err = r.Verify(ctx, src, dst); err == nil {
t.Fatal("checksum verification missed changed bytes")
}
if err = os.WriteFile(filepath.Join(dst, "obsolete"), []byte("extra"), 0600); err != nil {
t.Fatal(err)
}
if err = r.Copy(ctx, src, dst); err != nil {
t.Fatal(err)
}
if err = r.Verify(ctx, src, dst); err != nil {
t.Fatal(err)
}
if _, err = os.Stat(filepath.Join(dst, "obsolete")); !os.IsNotExist(err) {
t.Fatal("obsolete target file survived")
}
got, err := xattr(filepath.Join(dst, "hardlink"), "user.cephmig")
if err != nil || string(got) != "test" {
t.Fatalf("xattr: %q, %v", got, err)
}
}
func TestProcessCancellation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
if err := (Process{Stderr: io.Discard}).Exec(ctx, io.Discard, "sleep", "60"); err != context.Canceled {
t.Fatalf("got %v", err)
}
ctx, cancel = context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
start := time.Now()
if err := (Process{Stderr: io.Discard}).Exec(ctx, io.Discard, "sleep", "60"); err != context.DeadlineExceeded {
t.Fatalf("got %v", err)
}
if time.Since(start) > 2*time.Second {
t.Fatal("cancellation was too slow")
}
}
func TestRsyncUsesIncrementalSafeOptions(t *testing.T) {
joined := strings.Join((Rsync{BandwidthKiB: 1024}).args("/source", "/target"), " ")
for _, required := range []string{"-aHAXS", "--checksum", "--delete-during", "--no-whole-file", "--fsync", "--bwlimit=1024", "--filter=-x ceph.*"} {
if !strings.Contains(joined, required) {
t.Fatalf("missing %s", required)
}
}
for _, bad := range []string{"--inplace", "--delete-before", "--delete-after", "--delay-updates"} {
if strings.Contains(joined, bad) {
t.Fatalf("unsafe option %s", bad)
}
}
}