Files
CephfsMig/internal/migration/config_test.go
Sebastian UnterschützandClaude Opus 5.5 7705ed3eaf chore: import CephMig prototype
Initial import of the existing CephFS subvolume migration prototype,
including the native libcephfs transport, rsync backend, local Ceph
lab environment and integration tests.

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

101 lines
3.4 KiB
Go

package migration
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
func testConfig() Config {
return Config{Source: Endpoint{Config: "/clusters/a/ceph.conf", Keyring: "/clusters/a/keyring", Filesystem: "cephfs", Group: "group", Subvolume: "source", Mount: "/mnt/a"},
Target: Endpoint{Config: "/clusters/b/ceph.conf", Keyring: "/clusters/b/keyring", Filesystem: "cephfs", Group: "group", Subvolume: "target", Mount: "/mnt/b"}}
}
func TestConfigRejectsUnsafePathsAndNames(t *testing.T) {
for name, modify := range map[string]func(*Config){
"overlap": func(c *Config) { c.Target.Mount = "/mnt/a/nested" },
"relative": func(c *Config) { c.Source.Mount = "relative" },
"unclean": func(c *Config) { c.Source.Mount = "/mnt/a/../a" },
"flag-name": func(c *Config) { c.Target.Subvolume = "--force" },
"traversal": func(c *Config) { c.Source.Group = "../outside" },
"negative-rate": func(c *Config) { c.BandwidthKiB = -1 },
"overflow-rate": func(c *Config) { c.BandwidthKiB = 1 << 62 },
"bad-backend": func(c *Config) { c.Backend = "raw" },
"bad-workers": func(c *Config) { c.Workers = 65 },
"invalid-user": func(c *Config) { c.Source.User = "osd.0" },
} {
t.Run(name, func(t *testing.T) {
c := testConfig()
modify(&c)
if c.Validate() == nil {
t.Fatal("accepted invalid config")
}
})
}
if err := testConfig().Validate(); err != nil {
t.Fatal(err)
}
}
func TestNativeConfigAndResumeSettings(t *testing.T) {
c := testConfig()
c.Backend = "native"
c.Source.Mount, c.Target.Mount = "", ""
if err := c.Validate(); err != nil {
t.Fatal(err)
}
changed := c
changed.Workers, changed.BandwidthKiB = 32, 100000
if !sameMigrationIdentity(c, changed) {
t.Fatal("runtime settings invalidated resume identity")
}
changed.Target.Subvolume = "another"
if sameMigrationIdentity(c, changed) {
t.Fatal("accepted a different target")
}
}
func TestStrictJSONConfig(t *testing.T) {
b, _ := json.Marshal(testConfig())
for name, data := range map[string]string{"unknown": strings.TrimSuffix(string(b), "}") + ", \"typo\": true}", "trailing": string(b) + " {}", "truncated": string(b[:len(b)-2])} {
t.Run(name, func(t *testing.T) {
p := filepath.Join(t.TempDir(), "config.json")
if err := os.WriteFile(p, []byte(data), 0600); err != nil {
t.Fatal(err)
}
if _, err := ReadConfig(p); err == nil {
t.Fatal("accepted invalid JSON config")
}
})
}
}
func TestDataPathRejectsEscapeAndSymlink(t *testing.T) {
root := t.TempDir()
if err := os.Mkdir(filepath.Join(root, "real"), 0700); err != nil {
t.Fatal(err)
}
if err := os.Symlink(t.TempDir(), filepath.Join(root, "link")); err != nil {
t.Fatal(err)
}
for _, path := range []string{"/../escape", "/real/../real", "relative", "/link", "/"} {
if _, err := dataPath(root, path); err == nil {
t.Fatalf("accepted %q", path)
}
}
if got, err := dataPath(root, "/real"); err != nil || got != filepath.Join(root, "real") {
t.Fatalf("got %q, %v", got, err)
}
}
func TestPrivateDirectoriesNeverFollowsSymlink(t *testing.T) {
root, outside := t.TempDir(), t.TempDir()
if err := os.Symlink(outside, filepath.Join(root, "link")); err != nil {
t.Fatal(err)
}
if err := privateDirectories(filepath.Join(root, "link", "new")); err == nil {
t.Fatal("followed symlink")
}
if _, err := os.Stat(filepath.Join(outside, "new")); !os.IsNotExist(err) {
t.Fatal("created directory through symlink")
}
}