| index be759a41a..9da835c01 100644 |
| --- a/internal/provision/seed_test.go |
| +++ b/internal/provision/seed_test.go |
| @@ -308,13 +308,22 @@ func TestSeedTransitionTable(t *testing.T) { |
| "swapped": {SeedSwapped, map[cell]string{{seeds.KindImport, seeds.StateReady}: seeds.StateSwapped}}, |
| "place": {SeedPlace, map[cell]string{ |
| {seeds.KindReplica, seeds.StateReady}: seeds.StatePlaced, |
| + {seeds.KindFile, seeds.StateReady}: seeds.StatePlaced, |
| {seeds.KindImport, seeds.StateSwapped}: seeds.StatePlaced}}, |
| + "publish file": {SeedPublishFile, map[cell]string{{seeds.KindFile, seeds.StateReserving}: seeds.StateReady}}, |
| + "fail": {func(id string) func(*Record) error { return SeedFail(id, "x") }, map[cell]string{ |
| + {seeds.KindFile, seeds.StateReady}: seeds.StateAborted, |
| + {seeds.KindFile, seeds.StateAborted}: seeds.StateAborted}}, |
| + "abort file upload": {func(id string) func(*Record) error { return SeedAbortFileUploadBecause(id, "no") }, |
| + map[cell]string{{seeds.KindFile, seeds.StateReserving}: seeds.StateAborted}}, |
| "commit": {SeedCommit, map[cell]string{{seeds.KindImport, seeds.StatePlaced}: seeds.StateCommitted}}, |
| "refuse": {func(id string) func(*Record) error { return SeedRefuse(id, "no") }, |
| map[cell]string{{seeds.KindImport, seeds.StateReady}: seeds.StateRefused}}, |
| "abort": {SeedAbort, map[cell]string{ |
| {seeds.KindReplica, seeds.StateReserving}: seeds.StateAborted, |
| {seeds.KindReplica, seeds.StateReady}: seeds.StateAborted, |
| + {seeds.KindFile, seeds.StateReserving}: seeds.StateAborted, |
| + {seeds.KindFile, seeds.StateReady}: seeds.StateAborted, |
| {seeds.KindImport, seeds.StateAwaitingUpload}: seeds.StateAborted, |
| {seeds.KindImport, seeds.StateUploaded}: seeds.StateAborted, |
| {seeds.KindImport, seeds.StateReady}: seeds.StateAborted, |
| @@ -325,6 +334,7 @@ func TestSeedTransitionTable(t *testing.T) { |
| map[cell]string{{seeds.KindImport, seeds.StateAwaitingUpload}: seeds.StateAborted}}, |
| "rolled back": {SeedRolledBack, map[cell]string{ |
| {seeds.KindReplica, seeds.StateAborted}: seeds.StateRolledBack, |
| + {seeds.KindFile, seeds.StateAborted}: seeds.StateRolledBack, |
| {seeds.KindImport, seeds.StateAborted}: seeds.StateRolledBack}}, |
| "rolled back before swap": {SeedRolledBackBeforeSwap, map[cell]string{ |
| {seeds.KindImport, seeds.StateAwaitingUpload}: seeds.StateRolledBack, |
| @@ -336,7 +346,7 @@ func TestSeedTransitionTable(t *testing.T) { |
| t.Fatal(err) |
| } |
| for name, tr := range table { |
| - for _, kind := range []string{seeds.KindReplica, seeds.KindImport} { |
| + for _, kind := range []string{seeds.KindReplica, seeds.KindImport, seeds.KindFile} { |
| for _, from := range all { |
| seed := testSeed(t, kind, from) |
| // Under another id, no cell moves. |
| @@ -712,3 +722,90 @@ func TestSetRecordRegionRefusesAnHARecord(t *testing.T) { |
| t.Fatalf("a write that keeps the region: %v", err) |
| } |
| } |
| + |
| +// TestSeedLifecycleFileKind pins the file kind's rows against the other two |
| +// kinds (the full grid is TestSeedTransitionTable). |
| +func TestSeedLifecycleFileKind(t *testing.T) { |
| + apply := func(kind, state string, mut func(string) func(*Record) error) error { |
| + seed := testSeed(t, kind, state) |
| + return mut(seed.ID)(&Record{Seed: seed}) |
| + } |
| + if err := apply(seeds.KindFile, seeds.StateReserving, SeedPublishFile); err != nil { |
| + t.Fatalf("publish file: %v", err) |
| + } |
| + for _, kind := range []string{seeds.KindReplica, seeds.KindImport} { |
| + if err := apply(kind, seeds.StateReserving, SeedPublishFile); !errors.Is(err, ErrSeedConflict) { |
| + t.Errorf("SeedPublishFile on %s: %v", kind, err) |
| + } |
| + } |
| + replica := func(id string) func(*Record) error { return SeedPublishReplica(id, "1", strings.Repeat("e", 64), 1) } |
| + if err := apply(seeds.KindFile, seeds.StateReserving, replica); !errors.Is(err, ErrSeedConflict) { |
| + t.Errorf("SeedPublishReplica on file: %v", err) |
| + } |
| + if err := apply(seeds.KindFile, seeds.StateReady, SeedPlace); err != nil { |
| + t.Errorf("place: %v", err) |
| + } |
| + for _, from := range []string{seeds.StateReserving, seeds.StateReady} { |
| + if err := apply(seeds.KindFile, from, SeedAbort); err != nil { |
| + t.Errorf("abort from %s: %v", from, err) |
| + } |
| + } |
| + if err := apply(seeds.KindFile, seeds.StateAborted, SeedRolledBack); err != nil { |
| + t.Errorf("rolled back: %v", err) |
| + } |
| +} |
| + |
| +func TestUnfinishedFilePlacedIsFinished(t *testing.T) { |
| + for state, want := range map[string]bool{seeds.StatePlaced: false, seeds.StateReady: true, |
| + seeds.StateAborted: true, seeds.StateCommitted: true} { |
| + if got := (&SeedSource{Kind: seeds.KindFile, State: state}).Unfinished(); got != want { |
| + t.Errorf("file %s: unfinished %v, want %v", state, got, want) |
| + } |
| + } |
| +} |
| + |
| +func TestSeedFailFileOnly(t *testing.T) { |
| + seed := testSeed(t, seeds.KindFile, seeds.StateReady) |
| + rec := Record{Seed: seed} |
| + if err := SeedFail(seed.ID, "placing-failed")(&rec); err != nil || rec.Seed.State != seeds.StateAborted || rec.Seed.Reason != "placing-failed" { |
| + t.Fatalf("ready: %v %+v", err, rec.Seed) |
| + } |
| + if err := SeedFail(seed.ID, "copy-in-storage")(&rec); err != nil || rec.Seed.State != seeds.StateAborted || rec.Seed.Reason != "copy-in-storage" { |
| + t.Fatalf("aborted: %v %+v", err, rec.Seed) |
| + } |
| + for _, c := range []struct{ kind, state string }{ |
| + {seeds.KindImport, seeds.StateReady}, {seeds.KindImport, seeds.StateAborted}, |
| + {seeds.KindFile, seeds.StateReserving}, {seeds.KindFile, seeds.StatePlaced}, |
| + } { |
| + other := testSeed(t, c.kind, c.state) |
| + if err := SeedFail(other.ID, "x")(&Record{Seed: other}); !errors.Is(err, ErrSeedConflict) { |
| + t.Errorf("%s %s: %v", c.kind, c.state, err) |
| + } |
| + } |
| + if err := SeedFail("sd_other", "x")(&Record{Seed: testSeed(t, seeds.KindFile, seeds.StateReady)}); !errors.Is(err, ErrSeedConflict) { |
| + t.Errorf("another id: %v", err) |
| + } |
| +} |
| + |
| +func TestSeedAbortFileUploadOnlyWhileReserving(t *testing.T) { |
| + seed := testSeed(t, seeds.KindFile, seeds.StateReserving) |
| + rec := Record{Seed: seed} |
| + if err := SeedAbortFileUploadBecause(seed.ID, "too big")(&rec); err != nil || rec.Seed.State != seeds.StateAborted || rec.Seed.Reason != "too big" { |
| + t.Fatalf("reserving: %v %+v", err, rec.Seed) |
| + } |
| + for _, state := range []string{seeds.StateReady, seeds.StatePlaced, seeds.StateAborted, seeds.StateRolledBack} { |
| + s := testSeed(t, seeds.KindFile, state) |
| + if err := SeedAbortFileUploadBecause(s.ID, "x")(&Record{Seed: s}); !errors.Is(err, ErrSeedConflict) || s.State != state { |
| + t.Errorf("%s: %v", state, err) |
| + } |
| + } |
| +} |
| + |
| +func TestMoveBlockFileUnplaced(t *testing.T) { |
| + for state, want := range map[string]string{seeds.StateReserving: SeedMoveUnplaced, seeds.StateReady: SeedMoveUnplaced, |
| + seeds.StateAborted: SeedMoveUnplaced, seeds.StatePlaced: ""} { |
| + if got := (&SeedSource{Kind: seeds.KindFile, State: state}).MoveBlock(); got != want { |
| + t.Errorf("file %s: %q, want %q", state, got, want) |
| + } |
| + } |
| +} |