| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/release" |
| 10 | "github.com/carlosframework/platform/internal/rootstate" |
| 11 | ) |
| 12 | |
| 13 | type activationOrderStore struct { |
| 14 | rootstate.Store |
| 15 | floor string |
| 16 | locks int |
| 17 | } |
| 18 | |
| 19 | func (s *activationOrderStore) Lock(ctx context.Context, name string) (func(), error) { |
| 20 | if name == "transaction.lock" { |
| 21 | if _, err := os.Stat(s.floor); !os.IsNotExist(err) { |
| 22 | return nil, os.ErrPermission |
| 23 | } |
| 24 | s.locks++ |
| 25 | } |
| 26 | return s.Store.Lock(ctx, name) |
| 27 | } |
| 28 | |
| 29 | func TestPlan14ActivationParticipationBeforeFloor(t *testing.T) { |
| 30 | l := updateParticipationFixture(t) |
| 31 | x := &release.ReleaseExclusion{Open: func() (rootstate.Store, error) { return &plan14ReleaseStore{}, nil }} |
| 32 | l.ExcludeLaunches = x.With |
| 33 | l.VerifyLaunches = func(ctx context.Context, _ uint64) error { return x.Check(ctx) } |
| 34 | a := &systemApplier{participation: l, optDir: t.TempDir(), dataDir: t.TempDir()} |
| 35 | s := &activationOrderStore{Store: l.State, floor: filepath.Join(a.dataDir, ".moves", "floor.lock")} |
| 36 | l.State = s |
| 37 | if err := x.With(t.Context(), func(ctx context.Context) error { return l.BeginUpdate(ctx, "update") }); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | s.locks = 0 |
| 41 | if err := a.activationGuard(t.Context(), "new", "old", func(ctx context.Context) error { |
| 42 | if s.locks != 1 { |
| 43 | t.Fatalf("participation must be held before floor: %d acquisitions", s.locks) |
| 44 | } |
| 45 | return a.clearUpdateBegun(ctx) |
| 46 | }); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | if s.locks != 1 { |
| 50 | t.Fatalf("nested close reacquired participation: %d", s.locks) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestPlan14SystemApplyOuterReleaseContext(t *testing.T) { |
| 55 | f := newApplyFixture(t, "stable", "old") |
| 56 | l := updateParticipationFixture(t) |
| 57 | lock := &plan14ReleaseStore{} |
| 58 | x := &release.ReleaseExclusion{Open: func() (rootstate.Store, error) { return lock, nil }} |
| 59 | l.ExcludeLaunches = x.With |
| 60 | verified := 0 |
| 61 | l.VerifyLaunches = func(ctx context.Context, _ uint64) error { |
| 62 | verified++ |
| 63 | return x.Check(ctx) |
| 64 | } |
| 65 | if err := x.With(t.Context(), func(ctx context.Context) error { return l.BeginUpdate(ctx, "update") }); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | verified = 0 |
| 69 | f.a.participation = l |
| 70 | f.a.floorHook = func(*systemApplier) error { |
| 71 | if !lock.held { |
| 72 | return os.ErrPermission |
| 73 | } |
| 74 | return nil |
| 75 | } |
| 76 | if err := f.a.runWithRelease(t.Context(), true); err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | if verified == 0 { |
| 80 | t.Fatal("outer release did not reach recovery verifier") |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestPlan14SystemApplyNoCensusReleaseUnchanged(t *testing.T) { |
| 85 | f := newApplyFixture(t, "stable", "old") |
| 86 | l := updateParticipationFixture(t) |
| 87 | if err := l.State.Remove("census.json"); err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | l.ExcludeLaunches = func(context.Context, func(context.Context) error) error { |
| 91 | t.Fatal("no-census apply took release exclusion") |
| 92 | return nil |
| 93 | } |
| 94 | l.VerifyLaunches = func(context.Context, uint64) error { t.Fatal("no-census apply read root participation"); return nil } |
| 95 | f.a.participation = l |
| 96 | if err := f.a.runWithRelease(t.Context(), true); err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | } |
| 100 | |