| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/hostops" |
| 9 | "github.com/carlosframework/platform/internal/release" |
| 10 | ) |
| 11 | |
| 12 | // claimTestBoundary is the enabled data boundary with its mutation runner |
| 13 | // bound, as ConfigureClaimRuntime publishes it. |
| 14 | func claimTestBoundary(t *testing.T, raw release.Store, w *hostops.ControlWriter) release.Store { |
| 15 | t.Helper() |
| 16 | check, err := hostops.NewProductionMutationCheck(raw, w) |
| 17 | if err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | data, err := hostops.NewMutationBoundary(true, raw, check) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | if err = hostops.BindMutationRunner(data, w, claimTestOwner, func(ctx context.Context, account, app string) (string, error) { |
| 25 | a, err := hostops.ReadCanonicalAdmission(ctx, raw, account, app) |
| 26 | return a.Value.AppID, err |
| 27 | }); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | return data |
| 31 | } |
| 32 | |
| 33 | // Review P1-2: a claimAppCall callback that refuses before any write (a |
| 34 | // stale etag at the boundary, a validation error) answers its error and |
| 35 | // leaves the app open; one that refuses after a write keeps the operation |
| 36 | // for reconciliation, and later calls are refused without running. |
| 37 | func TestClaimAppCallRefusalDoesNotWedge(t *testing.T) { |
| 38 | ctx := t.Context() |
| 39 | raw := release.NewDirStore(t.TempDir()) |
| 40 | w := claimTestWriter(t, raw) |
| 41 | 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{}}) |
| 42 | s := &Server{cfg: Config{HostClaim: true, Releases: claimTestBoundary(t, raw, w)}} |
| 43 | acct := &Account{Sqid: "bab"} |
| 44 | key := "apps/bab/demo/claim-call.json" |
| 45 | open := func(when string) { |
| 46 | t.Helper() |
| 47 | a, err := hostops.ReadAdmission(ctx, raw, "bab", "demo", "app") |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | if a.Value.State != "open" || a.Value.Op != nil { |
| 52 | t.Fatalf("%s wedged the app: %+v", when, a.Value) |
| 53 | } |
| 54 | for id, e := range a.Value.Effects { |
| 55 | if e.State != "complete" { |
| 56 | t.Fatalf("%s left effect %s %s", when, id, e.State) |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | _, err := claimAppCall(s, ctx, acct, "demo", "store-declare", func(ctx context.Context) (string, error) { |
| 62 | return s.cfg.Releases.Put(ctx, key, []byte(`{}`), "stale") |
| 63 | }) |
| 64 | if !errors.Is(err, release.ErrCASConflict) { |
| 65 | t.Fatalf("stale etag: %v", err) |
| 66 | } |
| 67 | open("stale-etag refusal") |
| 68 | |
| 69 | invalid := errors.New("invalid request") |
| 70 | if _, err = claimAppCall(s, ctx, acct, "demo", "store-destroy", func(context.Context) (string, error) { return "", invalid }); !errors.Is(err, invalid) { |
| 71 | t.Fatalf("validation: %v", err) |
| 72 | } |
| 73 | open("validation refusal") |
| 74 | |
| 75 | got, err := claimAppCall(s, ctx, acct, "demo", "store-declare", func(ctx context.Context) (string, error) { |
| 76 | return s.cfg.Releases.Put(ctx, key, []byte(`{}`), "") |
| 77 | }) |
| 78 | if err != nil || got == "" { |
| 79 | t.Fatalf("joined call: %q %v", got, err) |
| 80 | } |
| 81 | open("joined call") |
| 82 | |
| 83 | _, tag, err := raw.Get(ctx, key) |
| 84 | if err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | second := errors.New("second write refused") |
| 88 | if _, err = claimAppCall(s, ctx, acct, "demo", "store-rotate", func(ctx context.Context) (string, error) { |
| 89 | if _, err := s.cfg.Releases.Put(ctx, key, []byte(`{"v":2}`), tag); err != nil { |
| 90 | return "", err |
| 91 | } |
| 92 | return "", second |
| 93 | }); !errors.Is(err, second) { |
| 94 | t.Fatalf("issued failure: %v", err) |
| 95 | } |
| 96 | a, err := hostops.ReadAdmission(ctx, raw, "bab", "demo", "app") |
| 97 | if err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | if a.Value.State != "closed" || a.Value.Op == nil { |
| 101 | t.Fatalf("an issued write released its operation: %+v", a.Value) |
| 102 | } |
| 103 | called := false |
| 104 | if _, err = claimAppCall(s, ctx, acct, "demo", "store-grant", func(context.Context) (string, error) { called = true; return "", nil }); called || !errors.Is(err, hostops.ErrPending) { |
| 105 | t.Fatalf("held app admitted a mutation: called=%v err=%v", called, err) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // claimAppCall fails closed when host claims are on but the data store has |
| 110 | // no mutation runner: it never calls through without an operation. |
| 111 | func TestClaimAppCallRequiresMutationRunner(t *testing.T) { |
| 112 | s := &Server{cfg: Config{HostClaim: true, Releases: release.NewDirStore(t.TempDir())}} |
| 113 | called := false |
| 114 | _, err := claimAppCall(s, t.Context(), &Account{Sqid: "bab"}, "demo", "protection", func(context.Context) (struct{}, error) { called = true; return struct{}{}, nil }) |
| 115 | if called || !errors.Is(err, hostops.ErrContract) { |
| 116 | t.Fatalf("ran without an operation: called=%v err=%v", called, err) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func TestClaimEnrollSQLFailureDoesNotWedge(t *testing.T) { |
| 121 | cx := newClaimCase(t) |
| 122 | acct, err := cx.st.AccountBySqid("bab") |
| 123 | if err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | a, err := hostops.ReadCanonicalAdmission(t.Context(), cx.srv.cfg.Releases, "bab", "titogo") |
| 127 | if err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | if err = cx.st.Close(); err != nil { |
| 131 | t.Fatal(err) |
| 132 | } |
| 133 | if err = cx.srv.bindClaimAppIdentity(t.Context(), acct, "titogo", a.Value.AppID); err == nil { |
| 134 | t.Fatal("closed SQL DB unexpectedly bound") |
| 135 | } |
| 136 | after, err := hostops.ReadCanonicalAdmission(t.Context(), cx.srv.cfg.Releases, "bab", "titogo") |
| 137 | if err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | if after.Value.Op != nil || after.Value.State != "open" { |
| 141 | t.Fatalf("local SQL failure wedged app: %+v", after.Value) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func TestClaimLocalSQLFailureDoesNotPoisonExclusiveOperation(t *testing.T) { |
| 146 | for _, kind := range []string{hostops.OpPlacing, hostops.OpPurging} { |
| 147 | t.Run(kind, func(t *testing.T) { |
| 148 | cx := newClaimCase(t) |
| 149 | acct, err := cx.st.AccountBySqid("bab") |
| 150 | if err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | a, err := hostops.ReadCanonicalAdmission(t.Context(), cx.srv.cfg.Releases, "bab", "titogo") |
| 154 | if err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | w := cx.srv.cfg.ClaimAuthority.Control |
| 158 | var h *hostops.OperationHandle |
| 159 | if kind == hostops.OpPurging { |
| 160 | cx.purgeWorld(t) |
| 161 | id, requestErr := cx.srv.cfg.ClaimPurge.Request(t.Context(), "bab", "titogo", "sql-failure", "owner", claimFxOwner) |
| 162 | if id == "" || requestErr != nil && !errors.Is(requestErr, hostops.ErrPending) { |
| 163 | t.Fatalf("reserve purge: %s %v", id, requestErr) |
| 164 | } |
| 165 | h, err = w.ResumeExclusive(t.Context(), "bab", "titogo", a.Value.AppID, id, kind, cx.srv.claimOwner) |
| 166 | } else { |
| 167 | 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) |
| 168 | } |
| 169 | if err != nil { |
| 170 | t.Fatal(err) |
| 171 | } |
| 172 | fail := errors.New("local SQL unavailable") |
| 173 | if kind == hostops.OpPlacing { |
| 174 | err = cx.srv.cfg.ClaimPlacement.sqlEffect(t.Context(), h, acct.ID, "projection", func(*Store) error { return fail }) |
| 175 | } else { |
| 176 | err = cx.srv.cfg.ClaimPurge.localSQLStage(t.Context(), h, "bind-operation", func(context.Context) error { return fail }) |
| 177 | } |
| 178 | if !errors.Is(err, fail) { |
| 179 | t.Fatalf("lost local error: %v", err) |
| 180 | } |
| 181 | after, err := hostops.ReadCanonicalAdmission(t.Context(), cx.srv.cfg.Releases, "bab", "titogo") |
| 182 | if err != nil { |
| 183 | t.Fatal(err) |
| 184 | } |
| 185 | if after.Value.Op == nil || after.Value.Op.ID != h.ID() { |
| 186 | t.Fatal("released owning exclusive operation") |
| 187 | } |
| 188 | for _, e := range after.Value.Effects { |
| 189 | if e.State != "complete" { |
| 190 | t.Fatalf("local SQL left uncertain effect: %+v", e) |
| 191 | } |
| 192 | } |
| 193 | }) |
| 194 | } |
| 195 | } |
| 196 | |