package migration import ( "context" "encoding/json" "errors" "io" "os" "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") } }) } } func TestReleaseRemovesOnlyOwnClaim(t *testing.T) { root := t.TempDir() c := testConfig() c.Target.Mount = root other := c other.Target.Subvolume = "other" for _, x := range []struct { c Config id string }{{c, "job-one"}, {other, "job-two"}} { f, err := claimTarget(root, "fsid", x.c.Target, x.id) if err != nil { t.Fatal(err) } f.Close() } access := mountedAccess{} if err := access.Release(context.Background(), c, "fsid", "job-two"); err == nil { t.Fatal("removed the claim of a different job") } if err := access.Release(context.Background(), c, "fsid", "job-one"); err != nil { t.Fatal(err) } claims := filepath.Join(root, ".cephmig", "claims") if _, err := os.Stat(filepath.Join(claims, claimName("fsid", c.Target))); !errors.Is(err, os.ErrNotExist) { t.Fatalf("own claim still present: %v", err) } if _, err := os.Stat(filepath.Join(claims, claimName("fsid", other.Target))); err != nil { t.Fatalf("claim of another job removed: %v", err) } if err := access.Release(context.Background(), other, "fsid", "job-two"); err != nil { t.Fatal(err) } if _, err := os.Stat(filepath.Join(root, ".cephmig")); !errors.Is(err, os.ErrNotExist) { t.Fatalf("empty claim directories kept: %v", err) } // Releasing again is a no-op. if err := access.Release(context.Background(), c, "fsid", "job-one"); err != nil { t.Fatal(err) } } func TestCleanedUp(t *testing.T) { s := testState() s.Phase = "ready-for-cutover" s.Snapshots = []Snapshot{{Name: "app", Done: true}, {Name: "cephmig-x", Owned: true, Done: true, Final: true}} s.LiveSnapshot = "cephmig-x" if cleanedUp(s) { t.Fatal("ready job reported as cleaned up") } s.LiveSnapshot = "" s.Snapshots[1].Removed = true if !cleanedUp(s) { t.Fatal("cleaned job not recognized") } }