package host import ( "context" "strings" "github.com/carlosframework/platform/internal/hostbox" "github.com/carlosframework/platform/internal/hostclaim" "github.com/carlosframework/platform/internal/park" "github.com/carlosframework/platform/internal/provision" "github.com/carlosframework/platform/internal/release" ) // The park fakes the moves tests inject (Plan 5 Task 9a). Production has no // stand-in for the claim's view or authorizers; these are the rules the // stand-ins had, kept for the tests that do not exercise the real view // (movesParkView, movessources_test.go). The rest is the claim's own, as // claim Plan 12 landed it (Q10): the claim's lease set and AggregateLeases over the tests' lease // table, park.Engine.RunHeld, and the MoveParks local authorizer's wrapper. // standInView is the tests' park view: the location's objects classified by // key, seen from region, and the other side of the move authoritative, which // ยง5.1's table says it is before this side's probe is admitted (every install // phase). role names this side (ruling J24): a landing (roleTarget) works // while the source's region, From, is authoritative; a dirty return // (roleSource) while the target's, To, is. func standInView(region, role string, m *provision.MoveState) func(context.Context, hostclaim.Observed) (park.View, error) { return func(_ context.Context, obs hostclaim.Observed) (park.View, error) { if obs.Absent || obs.Claim == nil || obs.Claim.Holder == nil || m == nil { return park.View{}, park.ErrUnsafe } var authoritative string switch role { case roleTarget: authoritative = park.PlacementRegion(m.From) case roleSource: authoritative = park.PlacementRegion(m.To) default: return park.View{}, park.ErrUnsafe } location := strings.TrimSuffix(obs.Claim.Holder.Location, "/") return park.View{ClaimETag: obs.ETag, Region: region, AuthoritativeRegion: authoritative, GenerationOf: func(r park.Ref) (string, bool) { return landingGenerationOf(location, r.Key) }}, nil } } // recordView is standInView over the record's move as the store holds it // when the engine asks. func recordView(s release.Store, region, role string) func(context.Context, hostclaim.Observed) (park.View, error) { return func(ctx context.Context, obs hostclaim.Observed) (park.View, error) { if obs.Absent || obs.Claim == nil || obs.Claim.Holder == nil { return park.View{}, park.ErrUnsafe } h := obs.Claim.Holder rec, err := provision.GetRecord(ctx, s, h.Account, h.App, obs.Host) if err != nil { return park.View{}, err } return standInView(region, role, rec.Move)(ctx, obs) } } // allowParkAuthorize is the tests' claim authorizer: it allows every chore, // so each arm's own operation check (movesArmAuthorize) decides alone. func allowParkAuthorize(context.Context, hostclaim.Observed, hostclaim.Obligation) error { return nil } // allowLocalAuthorize is the claim's MoveParks local authorizer // (claimMoveLocalAuthorizer, M9) over an identity check that passes and the // allowing owner: it allows every park-local and refuses anything else. var allowLocalAuthorize = claimMoveLocalAuthorizer(func(context.Context) error { return nil }, allowParkAuthorize) // claimParkLeases is the claim's park leases over the tests' lease table // leases: AggregateLeases with the replica park lease set (M6), as the // claim's MoveParks engine resolves them, lending a held lease to RunHeld. func claimParkLeases(leases park.Leases) park.Leases { return hostbox.AggregateLeases{Leases: leases, Resolve: hostbox.ReplicaParkLeaseSet} } // armParkFakes arms c's park hooks: the claim's leases over the tests' // lease table, the record-backed stand-in view for region and role, the // allowing claim authorizer, park.Engine.RunHeld (movesRunHeld, M5) and the // claim's local authorizer over the allowing owner. func armParkFakes(c *moveCfg, s release.Store, leases park.Leases, region, role string) { c.ParkLeases = claimParkLeases(leases) c.ParkView = recordView(s, region, role) c.ParkAuthorize = allowParkAuthorize c.ParkRunHeld = movesRunHeld c.ParkLocalAuthorize = allowLocalAuthorize }