Separate top-level declarations and multi-line statements with blank lines, keeping an "if err != nil" check next to the statement it belongs to. Expand semicolon one-liners, group the nativeTransport fields by purpose, and format the cgo preamble as readable C. Apart from the expanded one-liners and the C preamble, the change is whitespace only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
166 lines
3.7 KiB
Go
166 lines
3.7 KiB
Go
package migration
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func testState() *State {
|
|
return &State{Version: stateVersion, ID: "1234567890abcdef1234567890abcdef", Config: testConfig(), Phase: "syncing"}
|
|
}
|
|
|
|
func TestStateAtomicAndPrivate(t *testing.T) {
|
|
p := filepath.Join(t.TempDir(), "state.json")
|
|
s := testState()
|
|
s.Notes = []string{SnapshotIdentityNote}
|
|
if err := SaveState(p, s); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
st, err := os.Stat(p)
|
|
if err != nil || st.Mode().Perm() != 0600 {
|
|
t.Fatalf("state permissions: %v, %v", st, err)
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
for i := 0; i < 40; i++ {
|
|
if err := SaveState(p, s); err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
for i := 0; i < 100; i++ {
|
|
got, err := LoadState(p)
|
|
if err != nil {
|
|
t.Fatalf("reader observed partial state: %v", err)
|
|
}
|
|
|
|
if got.ID != s.ID {
|
|
t.Fatal("state identity changed")
|
|
}
|
|
|
|
if len(got.Notes) != 1 || got.Notes[0] != SnapshotIdentityNote {
|
|
t.Fatal("snapshot identity note was lost")
|
|
}
|
|
}
|
|
|
|
wg.Wait()
|
|
}
|
|
|
|
func TestStateRejectsCorruption(t *testing.T) {
|
|
for name, modify := range map[string]func(*State){
|
|
"version": func(s *State) { s.Version++ },
|
|
"short-id": func(s *State) { s.ID = "bad" },
|
|
"phase": func(s *State) { s.Phase = "finished" },
|
|
"ready-without-final": func(s *State) { s.Phase = "ready-for-cutover" },
|
|
"duplicate": func(s *State) { s.Snapshots = []Snapshot{{Name: "a"}, {Name: "a"}} },
|
|
"invalid-done": func(s *State) { s.Snapshots = []Snapshot{{Name: "a", Done: true}} },
|
|
"missing-live-base": func(s *State) { s.LiveSnapshot = "missing" },
|
|
"pending-live-base": func(s *State) {
|
|
s.Snapshots = []Snapshot{{Name: "pending"}}
|
|
s.LiveSnapshot = "pending"
|
|
},
|
|
} {
|
|
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 corrupt state")
|
|
}
|
|
})
|
|
}
|
|
|
|
p := filepath.Join(t.TempDir(), "state.json")
|
|
b, _ := json.Marshal(testState())
|
|
if err := os.WriteFile(p, append(b, []byte(" {}")...), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err := LoadState(p); err == nil {
|
|
t.Fatal("accepted trailing data")
|
|
}
|
|
}
|
|
|
|
func TestLockExclusionAndRelease(t *testing.T) {
|
|
p := filepath.Join(t.TempDir(), "lock")
|
|
first, err := lockFile(p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if second, err := lockFile(p); err == nil {
|
|
second.Close()
|
|
t.Fatal("second process acquired lock")
|
|
}
|
|
|
|
first.Close()
|
|
third, err := lockFile(p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
third.Close()
|
|
}
|
|
|
|
func TestTargetClaimExcludesOtherJobsAndSurvivesStateLoss(t *testing.T) {
|
|
root := t.TempDir()
|
|
c := testConfig()
|
|
first, err := claimTarget(root, "fsid", c.Target, "job-one")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if second, err := claimTarget(root, "fsid", c.Target, "job-one"); err == nil {
|
|
second.Close()
|
|
t.Fatal("concurrent use allowed")
|
|
}
|
|
|
|
first.Close()
|
|
if other, err := claimTarget(root, "fsid", c.Target, "job-two"); err == nil {
|
|
other.Close()
|
|
t.Fatal("different job stole target")
|
|
}
|
|
|
|
resumed, err := claimTarget(root, "fsid", c.Target, "job-one")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
resumed.Close()
|
|
}
|
|
|
|
func TestStateAndLockRejectSymlinks(t *testing.T) {
|
|
root := t.TempDir()
|
|
real := filepath.Join(root, "real")
|
|
if err := SaveState(real, testState()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
link := filepath.Join(root, "link")
|
|
if err := os.Symlink(real, link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err := LoadState(link); err == nil {
|
|
t.Fatal("followed state symlink")
|
|
}
|
|
|
|
if f, err := lockFile(link); err == nil {
|
|
f.Close()
|
|
t.Fatal("followed lock symlink")
|
|
}
|
|
}
|