package host import ( "context" "os" "path/filepath" "testing" "github.com/carlosframework/platform/internal/release" "github.com/carlosframework/platform/internal/rootstate" ) type activationOrderStore struct { rootstate.Store floor string locks int } func (s *activationOrderStore) Lock(ctx context.Context, name string) (func(), error) { if name == "transaction.lock" { if _, err := os.Stat(s.floor); !os.IsNotExist(err) { return nil, os.ErrPermission } s.locks++ } return s.Store.Lock(ctx, name) } func TestPlan14ActivationParticipationBeforeFloor(t *testing.T) { l := updateParticipationFixture(t) x := &release.ReleaseExclusion{Open: func() (rootstate.Store, error) { return &plan14ReleaseStore{}, nil }} l.ExcludeLaunches = x.With l.VerifyLaunches = func(ctx context.Context, _ uint64) error { return x.Check(ctx) } a := &systemApplier{participation: l, optDir: t.TempDir(), dataDir: t.TempDir()} s := &activationOrderStore{Store: l.State, floor: filepath.Join(a.dataDir, ".moves", "floor.lock")} l.State = s if err := x.With(t.Context(), func(ctx context.Context) error { return l.BeginUpdate(ctx, "update") }); err != nil { t.Fatal(err) } s.locks = 0 if err := a.activationGuard(t.Context(), "new", "old", func(ctx context.Context) error { if s.locks != 1 { t.Fatalf("participation must be held before floor: %d acquisitions", s.locks) } return a.clearUpdateBegun(ctx) }); err != nil { t.Fatal(err) } if s.locks != 1 { t.Fatalf("nested close reacquired participation: %d", s.locks) } } func TestPlan14SystemApplyOuterReleaseContext(t *testing.T) { f := newApplyFixture(t, "stable", "old") l := updateParticipationFixture(t) lock := &plan14ReleaseStore{} x := &release.ReleaseExclusion{Open: func() (rootstate.Store, error) { return lock, nil }} l.ExcludeLaunches = x.With verified := 0 l.VerifyLaunches = func(ctx context.Context, _ uint64) error { verified++ return x.Check(ctx) } if err := x.With(t.Context(), func(ctx context.Context) error { return l.BeginUpdate(ctx, "update") }); err != nil { t.Fatal(err) } verified = 0 f.a.participation = l f.a.floorHook = func(*systemApplier) error { if !lock.held { return os.ErrPermission } return nil } if err := f.a.runWithRelease(t.Context(), true); err != nil { t.Fatal(err) } if verified == 0 { t.Fatal("outer release did not reach recovery verifier") } } func TestPlan14SystemApplyNoCensusReleaseUnchanged(t *testing.T) { f := newApplyFixture(t, "stable", "old") l := updateParticipationFixture(t) if err := l.State.Remove("census.json"); err != nil { t.Fatal(err) } l.ExcludeLaunches = func(context.Context, func(context.Context) error) error { t.Fatal("no-census apply took release exclusion") return nil } l.VerifyLaunches = func(context.Context, uint64) error { t.Fatal("no-census apply read root participation"); return nil } f.a.participation = l if err := f.a.runWithRelease(t.Context(), true); err != nil { t.Fatal(err) } }