package migration import ( "context" "errors" "os" "path/filepath" "syscall" "testing" "git.zb-server.de/Sebi/CephfsMig/internal/storage" ) // rootFS is a storage.FS rooted at a local directory, with real flock // locks, for the claim operations. type rootFS struct { storage.FS root string } func (f rootFS) path(p string) string { return filepath.Join(f.root, p) } func (f rootFS) Stat(p string) (storage.Node, error) { return diskFS{}.Stat(f.path(p)) } func (f rootFS) Mkdir(p string, mode uint32) error { return os.Mkdir(f.path(p), os.FileMode(mode)) } func (f rootFS) Unlink(p string) error { return syscall.Unlink(f.path(p)) } func (f rootFS) Rmdir(p string) error { return syscall.Rmdir(f.path(p)) } func (f rootFS) Close() error { return nil } func (f rootFS) Open(p string, flags int, mode uint32) (storage.File, error) { file, err := os.OpenFile(f.path(p), flags, os.FileMode(mode)) if err != nil { return nil, err } return lockingFile{diskFile{file}}, nil } type lockingFile struct{ diskFile } func (f lockingFile) Lock(uint64) error { return syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB) } func claimAccess(root string) nativeAccess { fs := rootFS{root: root} return nativeAccess{&nativeTransport{target: fs, openTarget: func() (storage.FS, error) { return fs, nil }}} } func TestNativeClaimExcludesOtherJobsAndSurvivesStateLoss(t *testing.T) { ctx, c := context.Background(), testConfig() a := claimAccess(t.TempDir()) first, err := a.Claim(ctx, c, "fsid", "job-one") if err != nil { t.Fatal(err) } if second, err := a.Claim(ctx, c, "fsid", "job-one"); err == nil { second.Close() t.Fatal("concurrent use allowed") } if err := first.Close(); err != nil { t.Fatal(err) } if other, err := a.Claim(ctx, c, "fsid", "job-two"); err == nil { other.Close() t.Fatal("different job stole target") } resumed, err := a.Claim(ctx, c, "fsid", "job-one") if err != nil { t.Fatal(err) } resumed.Close() } func TestNativeReleaseRemovesOnlyOwnClaim(t *testing.T) { ctx, root := context.Background(), t.TempDir() a := claimAccess(root) c := testConfig() other := c other.Target.Subvolume = "other" for _, x := range []struct { c Config id string }{{c, "job-one"}, {other, "job-two"}} { f, err := a.Claim(ctx, x.c, "fsid", x.id) if err != nil { t.Fatal(err) } f.Close() } if err := a.Release(ctx, c, "fsid", "job-two"); err == nil { t.Fatal("removed the claim of a different job") } if err := a.Release(ctx, 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 := a.Release(ctx, 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 := a.Release(ctx, c, "fsid", "job-one"); err != nil { t.Fatal(err) } }