Users of a subvolume should not see traces of the migration. Each sync round now deletes the older cephmig-* round snapshots at source and target and keeps only the newest one as base for the next round. The new command `cephmig cleanup` deletes the remaining one after the cutover. Application snapshots are never touched. The journal marks a snapshot as removed before deleting it, so an interrupted run finishes the deletion instead of reporting a vanished snapshot. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
129 lines
3.7 KiB
Go
129 lines
3.7 KiB
Go
package migration
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"path/filepath"
|
|
"slices"
|
|
"testing"
|
|
)
|
|
|
|
// snapshotCeph fakes "ceph fs subvolume snapshot ls|rm" for two clusters,
|
|
// told apart by the --conf argument.
|
|
type snapshotCeph struct {
|
|
snaps map[string][]string
|
|
failRm string
|
|
removed []string
|
|
}
|
|
|
|
func (f *snapshotCeph) Exec(_ context.Context, out io.Writer, _ string, args ...string) error {
|
|
conf := args[1]
|
|
i := slices.Index(args, "snapshot")
|
|
switch args[i+1] {
|
|
case "ls":
|
|
var list []map[string]string
|
|
for _, n := range f.snaps[conf] {
|
|
list = append(list, map[string]string{"name": n})
|
|
}
|
|
|
|
return json.NewEncoder(out).Encode(list)
|
|
case "rm":
|
|
name := args[i+4]
|
|
if name == f.failRm {
|
|
return errors.New("injected failure")
|
|
}
|
|
|
|
f.snaps[conf] = slices.DeleteFunc(f.snaps[conf], func(n string) bool { return n == name })
|
|
f.removed = append(f.removed, conf+":"+name)
|
|
return nil
|
|
}
|
|
|
|
return errors.New("unexpected command")
|
|
}
|
|
|
|
func TestRemoveOwnedSnapshots(t *testing.T) {
|
|
all := []string{"app-1", "cephmig-a", "cephmig-b", "cephmig-c"}
|
|
newState := func() *State {
|
|
s := testState()
|
|
for _, n := range all {
|
|
s.Snapshots = append(s.Snapshots, Snapshot{Name: n, Owned: n != "app-1", Done: true,
|
|
CreatedAt: "2026-01-01 00:00:00", TargetCreatedAt: "2026-01-01 00:00:01"})
|
|
}
|
|
|
|
s.LiveSnapshot = "cephmig-c"
|
|
return s
|
|
}
|
|
|
|
statePath := filepath.Join(t.TempDir(), "state.json")
|
|
f := &snapshotCeph{snaps: map[string][]string{"src.conf": slices.Clone(all), "dst.conf": slices.Clone(all)}}
|
|
src := ceph{endpoint: Endpoint{Config: "src.conf", Filesystem: "fs", Subvolume: "v"}, exec: f}
|
|
dst := ceph{endpoint: Endpoint{Config: "dst.conf", Filesystem: "fs", Subvolume: "v"}, exec: f}
|
|
|
|
// An interrupted run leaves the journal marked, and the retry finishes.
|
|
s := newState()
|
|
f.failRm = "cephmig-b"
|
|
if err := removeOwnedSnapshots(context.Background(), src, dst, statePath, s, s.LiveSnapshot); err == nil {
|
|
t.Fatal("injected failure not reported")
|
|
}
|
|
|
|
loaded, err := LoadState(statePath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !loaded.Snapshots[1].Removed || !loaded.Snapshots[2].Removed || loaded.Snapshots[3].Removed || loaded.Snapshots[0].Removed {
|
|
t.Fatalf("journal marks: %+v", loaded.Snapshots)
|
|
}
|
|
|
|
f.failRm = ""
|
|
if err := removeOwnedSnapshots(context.Background(), src, dst, statePath, loaded, loaded.LiveSnapshot); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for conf, names := range f.snaps {
|
|
if !slices.Equal(names, []string{"app-1", "cephmig-c"}) {
|
|
t.Fatalf("%s keeps %v", conf, names)
|
|
}
|
|
}
|
|
|
|
// Cleanup removes the base too, but never application snapshots.
|
|
loaded.LiveSnapshot = ""
|
|
if err := removeOwnedSnapshots(context.Background(), src, dst, statePath, loaded, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for conf, names := range f.snaps {
|
|
if !slices.Equal(names, []string{"app-1"}) {
|
|
t.Fatalf("%s keeps %v", conf, names)
|
|
}
|
|
}
|
|
|
|
if _, err := LoadState(statePath); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestStateRejectsInvalidRemoval(t *testing.T) {
|
|
done := Snapshot{Name: "a", Owned: true, Done: true, Removed: true, CreatedAt: "x", TargetCreatedAt: "y"}
|
|
for name, modify := range map[string]func(*State){
|
|
"application snapshot": func(s *State) { d := done; d.Owned = false; s.Snapshots = []Snapshot{d} },
|
|
"incomplete": func(s *State) { s.Snapshots = []Snapshot{{Name: "a", Owned: true, Removed: true}} },
|
|
"removed live base": func(s *State) { s.Snapshots = []Snapshot{done}; s.LiveSnapshot = "a" },
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
p := filepath.Join(t.TempDir(), "state.json")
|
|
s := testState()
|
|
modify(s)
|
|
if err := SaveState(p, s); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err := LoadState(p); err == nil {
|
|
t.Fatal("accepted invalid removal")
|
|
}
|
|
})
|
|
}
|
|
}
|