package console import ( "context" "errors" "testing" "github.com/carlosframework/platform/internal/hostops" "github.com/carlosframework/platform/internal/release" ) // claimTestBoundary is the enabled data boundary with its mutation runner // bound, as ConfigureClaimRuntime publishes it. func claimTestBoundary(t *testing.T, raw release.Store, w *hostops.ControlWriter) release.Store { t.Helper() check, err := hostops.NewProductionMutationCheck(raw, w) if err != nil { t.Fatal(err) } data, err := hostops.NewMutationBoundary(true, raw, check) if err != nil { t.Fatal(err) } if err = hostops.BindMutationRunner(data, w, claimTestOwner, func(ctx context.Context, account, app string) (string, error) { a, err := hostops.ReadCanonicalAdmission(ctx, raw, account, app) return a.Value.AppID, err }); err != nil { t.Fatal(err) } return data } // Review P1-2: a claimAppCall callback that refuses before any write (a // stale etag at the boundary, a validation error) answers its error and // leaves the app open; one that refuses after a write keeps the operation // for reconciliation, and later calls are refused without running. func TestClaimAppCallRefusalDoesNotWedge(t *testing.T) { ctx := t.Context() raw := release.NewDirStore(t.TempDir()) w := claimTestWriter(t, raw) seedClaimAdmission(t, raw, "bab", "demo", hostops.AppAdmission{V: 1, AppID: "app", Revision: 1, Epoch: 1, State: "open", Registrations: map[string]hostops.Registration{}, Effects: map[string]hostops.Effect{}}) s := &Server{cfg: Config{HostClaim: true, Releases: claimTestBoundary(t, raw, w)}} acct := &Account{Sqid: "bab"} key := "apps/bab/demo/claim-call.json" open := func(when string) { t.Helper() a, err := hostops.ReadAdmission(ctx, raw, "bab", "demo", "app") if err != nil { t.Fatal(err) } if a.Value.State != "open" || a.Value.Op != nil { t.Fatalf("%s wedged the app: %+v", when, a.Value) } for id, e := range a.Value.Effects { if e.State != "complete" { t.Fatalf("%s left effect %s %s", when, id, e.State) } } } _, err := claimAppCall(s, ctx, acct, "demo", "store-declare", func(ctx context.Context) (string, error) { return s.cfg.Releases.Put(ctx, key, []byte(`{}`), "stale") }) if !errors.Is(err, release.ErrCASConflict) { t.Fatalf("stale etag: %v", err) } open("stale-etag refusal") invalid := errors.New("invalid request") if _, err = claimAppCall(s, ctx, acct, "demo", "store-destroy", func(context.Context) (string, error) { return "", invalid }); !errors.Is(err, invalid) { t.Fatalf("validation: %v", err) } open("validation refusal") got, err := claimAppCall(s, ctx, acct, "demo", "store-declare", func(ctx context.Context) (string, error) { return s.cfg.Releases.Put(ctx, key, []byte(`{}`), "") }) if err != nil || got == "" { t.Fatalf("joined call: %q %v", got, err) } open("joined call") _, tag, err := raw.Get(ctx, key) if err != nil { t.Fatal(err) } second := errors.New("second write refused") if _, err = claimAppCall(s, ctx, acct, "demo", "store-rotate", func(ctx context.Context) (string, error) { if _, err := s.cfg.Releases.Put(ctx, key, []byte(`{"v":2}`), tag); err != nil { return "", err } return "", second }); !errors.Is(err, second) { t.Fatalf("issued failure: %v", err) } a, err := hostops.ReadAdmission(ctx, raw, "bab", "demo", "app") if err != nil { t.Fatal(err) } if a.Value.State != "closed" || a.Value.Op == nil { t.Fatalf("an issued write released its operation: %+v", a.Value) } called := false if _, err = claimAppCall(s, ctx, acct, "demo", "store-grant", func(context.Context) (string, error) { called = true; return "", nil }); called || !errors.Is(err, hostops.ErrPending) { t.Fatalf("held app admitted a mutation: called=%v err=%v", called, err) } } // claimAppCall fails closed when host claims are on but the data store has // no mutation runner: it never calls through without an operation. func TestClaimAppCallRequiresMutationRunner(t *testing.T) { s := &Server{cfg: Config{HostClaim: true, Releases: release.NewDirStore(t.TempDir())}} called := false _, err := claimAppCall(s, t.Context(), &Account{Sqid: "bab"}, "demo", "protection", func(context.Context) (struct{}, error) { called = true; return struct{}{}, nil }) if called || !errors.Is(err, hostops.ErrContract) { t.Fatalf("ran without an operation: called=%v err=%v", called, err) } } func TestClaimEnrollSQLFailureDoesNotWedge(t *testing.T) { cx := newClaimCase(t) acct, err := cx.st.AccountBySqid("bab") if err != nil { t.Fatal(err) } a, err := hostops.ReadCanonicalAdmission(t.Context(), cx.srv.cfg.Releases, "bab", "titogo") if err != nil { t.Fatal(err) } if err = cx.st.Close(); err != nil { t.Fatal(err) } if err = cx.srv.bindClaimAppIdentity(t.Context(), acct, "titogo", a.Value.AppID); err == nil { t.Fatal("closed SQL DB unexpectedly bound") } after, err := hostops.ReadCanonicalAdmission(t.Context(), cx.srv.cfg.Releases, "bab", "titogo") if err != nil { t.Fatal(err) } if after.Value.Op != nil || after.Value.State != "open" { t.Fatalf("local SQL failure wedged app: %+v", after.Value) } } func TestClaimLocalSQLFailureDoesNotPoisonExclusiveOperation(t *testing.T) { for _, kind := range []string{hostops.OpPlacing, hostops.OpPurging} { t.Run(kind, func(t *testing.T) { cx := newClaimCase(t) acct, err := cx.st.AccountBySqid("bab") if err != nil { t.Fatal(err) } a, err := hostops.ReadCanonicalAdmission(t.Context(), cx.srv.cfg.Releases, "bab", "titogo") if err != nil { t.Fatal(err) } w := cx.srv.cfg.ClaimAuthority.Control var h *hostops.OperationHandle if kind == hostops.OpPurging { cx.purgeWorld(t) id, requestErr := cx.srv.cfg.ClaimPurge.Request(t.Context(), "bab", "titogo", "sql-failure", "owner", claimFxOwner) if id == "" || requestErr != nil && !errors.Is(requestErr, hostops.ErrPending) { t.Fatalf("reserve purge: %s %v", id, requestErr) } h, err = w.ResumeExclusive(t.Context(), "bab", "titogo", a.Value.AppID, id, kind, cx.srv.claimOwner) } else { h, err = w.BeginExclusive(t.Context(), "bab", "titogo", a.Value.AppID, hostops.AppOperation{ID: "sql-failure", Kind: kind, AppID: a.Value.AppID}, cx.srv.claimOwner) } if err != nil { t.Fatal(err) } fail := errors.New("local SQL unavailable") if kind == hostops.OpPlacing { err = cx.srv.cfg.ClaimPlacement.sqlEffect(t.Context(), h, acct.ID, "projection", func(*Store) error { return fail }) } else { err = cx.srv.cfg.ClaimPurge.localSQLStage(t.Context(), h, "bind-operation", func(context.Context) error { return fail }) } if !errors.Is(err, fail) { t.Fatalf("lost local error: %v", err) } after, err := hostops.ReadCanonicalAdmission(t.Context(), cx.srv.cfg.Releases, "bab", "titogo") if err != nil { t.Fatal(err) } if after.Value.Op == nil || after.Value.Op.ID != h.ID() { t.Fatal("released owning exclusive operation") } for _, e := range after.Value.Effects { if e.State != "complete" { t.Fatalf("local SQL left uncertain effect: %+v", e) } } }) } }