feat: release the target claim in cleanup

cleanup now deletes the claim file under .cephmig/claims as its last
step, if it holds the job's own ID, and removes the claim directories
when they are empty. A cleaned-up job is finished: cleanup and finalize
do nothing and do not claim the target again, and sync is refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-12-03 20:37:20 +01:00
1 parent 5f29328932
commit a6e7bbb17e
10 files changed
+218 -14

No files matched your search

+67
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"io"
"os"
"path/filepath"
"slices"
"testing"
@@ -126,3 +127,69 @@ func TestStateRejectsInvalidRemoval(t *testing.T) {
})
}
}
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")
}
}