Files
URBM/internal/rsync/space_test.go
T

167 lines
5.4 KiB
Go

package rsync
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"git.casaderoll.de/michael/urbm/internal/model"
)
func TestSpaceReplacementsGrowthAndDeletes(t *testing.T) {
for _, tc := range []struct {
name string
old, new int
count int
growth int64
}{
{"same sized replacements", 8192, 8192, 3, 0},
{"growth", 4096, 8192, 2, 8192},
{"shrink", 8192, 4096, 2, 0},
{"new files", 0, 8192, 2, 16384},
} {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
e := newSpaceEstimate(dir)
for i := 0; i < tc.count; i++ {
name := fmt.Sprintf("file%d", i)
if tc.old > 0 {
if err := os.WriteFile(filepath.Join(dir, name), make([]byte, tc.old), 0600); err != nil {
t.Fatal(err)
}
}
e.consume(fmt.Sprintf("SPACE|>f.st......|%d|%s", tc.new, name))
}
e.consume("SPACE|*deleting |999999999|old")
e.consume(fmt.Sprintf("Total transferred file size: %d bytes", tc.new*tc.count))
e.consume(fmt.Sprintf("Number of regular files transferred: %d", tc.count))
required, why := e.finish(Summary{Files: int64(tc.count), Bytes: int64(tc.new * tc.count)})
if why != "" || e.growth != tc.growth {
t.Fatalf("growth=%d required=%d why=%s", e.growth, required, why)
}
want := tc.growth + int64(tc.new) + int64(tc.count)*65536 + 64*1024*1024
if required != want {
t.Fatalf("got %d want %d", required, want)
}
})
}
}
func TestSpaceEscapedNamesAndSymlinks(t *testing.T) {
dir := t.TempDir()
name := "ä | line\ntrail "
if err := os.WriteFile(filepath.Join(dir, name), make([]byte, 8192), 0600); err != nil {
t.Fatal(err)
}
e := newSpaceEstimate(dir)
e.consume("SPACE|>f.st......|8192|\\#303\\#244 | line\\#012trail ")
if e.problem != "" || e.growth != 0 {
t.Fatalf("%+v", e)
}
if decodeRsyncName(`literal\#134#012`) != "literal\\#012" {
t.Fatal("recursive escape decoding")
}
if err := os.Symlink(t.TempDir(), filepath.Join(dir, "link")); err != nil {
t.Fatal(err)
}
e.consume("SPACE|>f+++++++++|2|link/file")
if e.problem == "" {
t.Fatal("followed destination symlink")
}
}
func TestSpaceUnknownAndSparseHardlinks(t *testing.T) {
dir := t.TempDir()
e := newSpaceEstimate(dir)
if _, why := e.finish(Summary{}); why == "" {
t.Fatal("missing statistics accepted")
}
e.sawStats = true
if _, why := e.finish(Summary{Files: 1}); why == "" {
t.Fatal("missing file records accepted")
}
f, err := os.Create(filepath.Join(dir, "sparse"))
if err != nil {
t.Fatal(err)
}
if err = f.Truncate(1024 * 1024); err != nil {
t.Fatal(err)
}
f.Close()
e.consume("SPACE|>f.st......|1048576|sparse")
if e.growth == 0 {
t.Fatal("credited sparse logical size")
}
if err = os.WriteFile(filepath.Join(dir, "one"), make([]byte, 8192), 0600); err != nil {
t.Fatal(err)
}
if err = os.Link(filepath.Join(dir, "one"), filepath.Join(dir, "two")); err != nil {
t.Fatal(err)
}
e = newSpaceEstimate(dir)
e.consume("SPACE|>f.st......|8192|one")
if e.growth != 8192 {
t.Fatal("credited shared hardlink")
}
e = newSpaceEstimate(dir)
e.consume("SPACE|>f+++++++++|1|../escape")
if e.problem == "" {
t.Fatal("accepted traversal")
}
}
func TestRealRsyncSpaceEstimate(t *testing.T) {
binary := os.Getenv("URBM_TEST_RSYNC")
if binary == "" {
t.Skip("requires rsync 3.x")
}
root := t.TempDir()
source := filepath.Join(root, "source")
target := filepath.Join(root, "target")
os.Mkdir(source, 0700)
os.Mkdir(target, 0700)
os.Mkdir(filepath.Join(target, "source"), 0700)
for _, name := range []string{"normal", "ä | newline\ntrailing ", `literal\#012`} {
os.WriteFile(filepath.Join(source, name), make([]byte, 8192), 0600)
os.WriteFile(filepath.Join(target, "source", name), make([]byte, 4096), 0600)
}
os.WriteFile(filepath.Join(source, "new"), []byte("new"), 0600)
os.WriteFile(filepath.Join(target, "source", "obsolete"), make([]byte, 8192), 0600)
job := model.Job{Sources: []model.Source{{Path: source}}, Rsync: model.RsyncOptions{Target: target, Overwrite: true, Delete: true, PreserveTimes: true, PreserveLinks: true}}
runner := Runner{Binary: binary}
estimate, err := runner.Estimate(context.Background(), "space", job)
if err != nil || estimate.SpaceUnknown != "" || estimate.Files != 4 || estimate.Bytes != 24579 {
t.Fatalf("%+v %v", estimate, err)
}
// Bound does not subtract the obsolete file and credits existing allocations.
want := int64(3*4096 + 3 + 8192 + 4*65536 + 64*1024*1024)
if estimate.RequiredBytes < want || estimate.RequiredBytes > want+2*65536 {
t.Fatalf("unexpected bound %+v", estimate)
}
if _, err = os.Stat(filepath.Join(target, "source", "obsolete")); err != nil {
t.Fatal("dry run deleted file")
}
if _, err = os.Stat(filepath.Join(target, "source", "new")); !os.IsNotExist(err) {
t.Fatal("dry run wrote file")
}
if _, err = runner.Run(context.Background(), "copy", job, nil); err != nil {
t.Fatal(err)
}
estimate, err = runner.Estimate(context.Background(), "unchanged", job)
if err != nil || estimate.SpaceUnknown != "" || estimate.Bytes != 0 {
t.Fatalf("%+v %v", estimate, err)
}
// A missing target must never be created by the preflight.
job.Rsync.Target = filepath.Join(root, "missing")
estimate, err = runner.Estimate(context.Background(), "missing", job)
if err == nil && !strings.Contains(estimate.SpaceUnknown, "Zielpfad") {
t.Fatalf("%+v %v", estimate, err)
}
if _, err = os.Stat(job.Rsync.Target); !os.IsNotExist(err) {
t.Fatal("created target")
}
}