| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/hostclaim" |
| 9 | "github.com/carlosframework/platform/internal/hostguard" |
| 10 | "github.com/carlosframework/platform/internal/instancectl" |
| 11 | "github.com/carlosframework/platform/internal/rootstate" |
| 12 | ) |
| 13 | |
| 14 | type generationExclusiveStore struct { |
| 15 | rootstate.Store |
| 16 | held bool |
| 17 | nested int |
| 18 | } |
| 19 | |
| 20 | func (s *generationExclusiveStore) Lock(ctx context.Context, name string) (func(), error) { |
| 21 | if s.held { |
| 22 | s.nested++ |
| 23 | return nil, os.ErrPermission |
| 24 | } |
| 25 | unlock, err := s.Store.Lock(ctx, name) |
| 26 | if err != nil { |
| 27 | return nil, err |
| 28 | } |
| 29 | s.held = true |
| 30 | return func() { s.held = false; unlock() }, nil |
| 31 | } |
| 32 | |
| 33 | func TestPlan14GenerationLaunchDoesNotReenterRootLock(t *testing.T) { |
| 34 | for _, validator := range []bool{false, true} { |
| 35 | t.Run(map[bool]string{false: "reservation", true: "production-validator"}[validator], func(t *testing.T) { |
| 36 | f, disk, rec := generationLaunchFixture(t) |
| 37 | rec.Schema, rec.Channel = 1, "canary/console" |
| 38 | plan14PutRecord(t, f.root.generations.store, rec) |
| 39 | store := &generationExclusiveStore{Store: &rollbackDisk{dir: t.TempDir()}} |
| 40 | f.root.Guard.Root = hostguard.NewRootAuthority(store) |
| 41 | in := hostguard.RootInstallation{Host: rec.Host, Account: rec.Account, App: rec.App, Record: "apps/" + rec.Account + "/" + rec.App + "/instances/" + rec.Host + ".json", Incarnation: f.target.Incarnation, Stamp: f.target.Stamp, Box: f.target.Box, Enrollment: f.target.Enrollment, Migration: 1, InventoryRevision: 1} |
| 42 | if err := f.root.Guard.EstablishRoot(t.Context(), in, func(*hostclaim.Claim) error { return nil }); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | if validator { |
| 46 | f.root.ValidateExecutable = claimStagedExecutable(&instancectl.Server{OptDir: f.root.generations.optDir, HostApp: rootInstallationHostApp(f.root.Guard, f.root.generations.store)}) |
| 47 | } |
| 48 | if _, err := f.root.Serve(t.Context(), f.peer, f.req); err != nil { |
| 49 | t.Fatalf("launch refused: %v; recursive locks=%d", err, store.nested) |
| 50 | } |
| 51 | if store.nested != 0 { |
| 52 | t.Fatalf("recursive root locks: %d", store.nested) |
| 53 | } |
| 54 | if r := readGenerationLaunch(t, disk); r.Process.PID == 0 { |
| 55 | t.Fatal("launch was not recorded") |
| 56 | } |
| 57 | }) |
| 58 | } |
| 59 | } |
| 60 | |