| new file mode 100644 |
| index 000000000..aee458990 |
| --- /dev/null |
| +++ b/internal/console/appregion_ha_test.go |
| @@ -0,0 +1,676 @@ |
| +package console |
| + |
| +// G6 Task 4: a born-HA app's region is fixed. The region doors (PUT |
| +// .../region and the Settings form) and the place door refuse an app whose |
| +// ROW carries ha_pool, and the swap doors refuse a concurrency above 1 for |
| +// it. Each door decides from the row, and a request admitted for a plain app |
| +// that pauses while the name is trashed, purged and recreated as HA is |
| +// refused by the checks under the door's locks and by the store's |
| +// conditional write. A legacy app (no ha_pool) answers exactly as before: |
| +// every refusal here has a control that repeats the same request on one. |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "maps" |
| + "net/http" |
| + "net/url" |
| + "path/filepath" |
| + "slices" |
| + "strings" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/appconfig" |
| + "github.com/carlosframework/platform/internal/provision" |
| +) |
| + |
| +// unchanged runs req, a request on an existing app the test expects |
| +// refused, and proves it had no side effects: its witnessed bucket |
| +// operations (puts and deletes whether or not they succeeded, and every |
| +// listing) are exactly allow.Ops, every bucket object's bytes are as they |
| +// were (but for allow.Changed), and every console database table is |
| +// unchanged (no row written, no audit line). |
| +func (w *haRegionWorld) unchanged(t *testing.T, what string, allow haAllow, req func()) { |
| + t.Helper() |
| + bucket, db := w.bucketSnap(t), w.dbSnap(t) |
| + var mu sync.Mutex |
| + var ops []string |
| + ds := w.rel.(*deletableStore) |
| + ds.setOpWitness(func(op, key string) { |
| + mu.Lock() |
| + defer mu.Unlock() |
| + ops = append(ops, op+" "+key) |
| + }) |
| + req() |
| + ds.setOpWitness(nil) |
| + mu.Lock() |
| + defer mu.Unlock() |
| + got, want := slices.Sorted(slices.Values(ops)), slices.Sorted(slices.Values(allow.Ops)) |
| + if !slices.Equal(got, want) { |
| + t.Errorf("%s: the refused request's bucket operations = %v, want exactly %v", what, got, want) |
| + } |
| + after, before := w.bucketSnap(t), maps.Clone(bucket) |
| + for _, k := range allow.Changed { |
| + delete(after, k) |
| + delete(before, k) |
| + } |
| + if !maps.Equal(after, before) { |
| + t.Errorf("%s: the bucket changed: %v", what, mapDiff(before, after)) |
| + } |
| + if got := w.dbSnap(t); !maps.Equal(got, db) { |
| + t.Errorf("%s: the console database changed: %v", what, mapDiff(db, got)) |
| + } |
| +} |
| + |
| +// haCreate creates app through the API create door as the world's person: |
| +// born HA in region when ha, else plain. It must answer 200. |
| +func (w *haRegionWorld) haCreate(t *testing.T, app, region string, ha bool) { |
| + t.Helper() |
| + body := map[string]any{"name": app, "region": region} |
| + if ha { |
| + body["ha"] = true |
| + } |
| + status, res := postJSON(t, newAppURL(w.consoleFixture), body, w.token) |
| + if status != http.StatusOK { |
| + t.Fatalf("create %s (ha %v) in %s = %d %v, want 200", app, ha, region, status, res) |
| + } |
| + if pool, _, err := w.st.AppHAPool(context.Background(), OpsAccountID, app); err != nil || (pool == "ha") != ha { |
| + t.Fatalf("create %s: AppHAPool = %q %v, want ha %v", app, pool, err, ha) |
| + } |
| +} |
| + |
| +func (w *haRegionWorld) appURL(app, door string) string { |
| + return w.ts.URL + "/api/cli/apps/bab/" + app + "/" + door |
| +} |
| + |
| +func (w *haRegionWorld) regionOf(t *testing.T, app string) string { |
| + t.Helper() |
| + r, _, err := w.st.AppRegion(OpsAccountID, app) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return r |
| +} |
| + |
| +func (w *haRegionWorld) placementOf(t *testing.T, app string) string { |
| + t.Helper() |
| + p, err := w.st.AppPlacement(OpsAccountID, app) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return p |
| +} |
| + |
| +func (w *haRegionWorld) swapOf(t *testing.T, app string) appconfig.SwapPolicy { |
| + t.Helper() |
| + sp, err := appconfig.GetSwapPolicy(context.Background(), w.rel, "bab", app) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return sp |
| +} |
| + |
| +// manifestOf reads app's instance manifest; the test fails when there is |
| +// none. |
| +func (w *haRegionWorld) manifestOf(t *testing.T, app string) *provision.Manifest { |
| + t.Helper() |
| + m, _, err := provision.GetManifestETag(context.Background(), w.rel, "bab", app) |
| + if err != nil { |
| + t.Fatalf("manifest of %s: %v", app, err) |
| + } |
| + return m |
| +} |
| + |
| +// enable PUTs {} to app's enable door; it must answer 200 binding pool ha |
| +// to regions [region]. |
| +func (w *haRegionWorld) enable(t *testing.T, app, region string) { |
| + t.Helper() |
| + status, res := putJSON(t, enableURL(w.consoleFixture, "bab", app), map[string]any{}, w.token) |
| + if status != http.StatusOK || res["pool"] != "ha" || !slices.Equal(anyStrings(res["regions"]), []string{region}) { |
| + t.Fatalf("enable %s = %d %v, want 200 pool ha regions [%s]", app, status, res, region) |
| + } |
| +} |
| + |
| +func anyStrings(v any) []string { |
| + raw, _ := v.([]any) |
| + out := make([]string, 0, len(raw)) |
| + for _, x := range raw { |
| + s, _ := x.(string) |
| + out = append(out, s) |
| + } |
| + return out |
| +} |
| + |
| +// browser signs paul in for the world's browser doors: the client and its |
| +// CSRF token. |
| +func (w *haRegionWorld) browser(t *testing.T) (*http.Client, string) { |
| + t.Helper() |
| + c := signInPaul(t, w.consoleFixture) |
| + return c, sessionToken(t, w.consoleFixture, c) |
| +} |
| + |
| +// postFlash posts form to the browser door at path, wants the Settings |
| +// redirect, and returns the flash it left. |
| +func (w *haRegionWorld) postFlash(t *testing.T, c *http.Client, path string, form url.Values) (kind, msg string) { |
| + t.Helper() |
| + claimClearFlash(c, w.ts.URL) |
| + status, loc, body := postForm(t, c, w.ts.URL+path, form) |
| + if status != http.StatusSeeOther || !strings.Contains(loc, "/settings") { |
| + t.Fatalf("POST %s = %d %q %s, want the Settings redirect", path, status, loc, body) |
| + } |
| + return claimFlashOf(c, w.ts.URL) |
| +} |
| + |
| +// haReply is one HTTP answer from a request run on another goroutine. |
| +type haReply struct { |
| + status int |
| + res map[string]any |
| + err error |
| +} |
| + |
| +// haSend runs a JSON request on its own goroutine (no t.Fatal there) and |
| +// delivers the answer on the returned channel. |
| +func haSend(method, u, token string, body any) <-chan haReply { |
| + out := make(chan haReply, 1) |
| + go func() { |
| + raw, err := json.Marshal(body) |
| + if err != nil { |
| + out <- haReply{err: err} |
| + return |
| + } |
| + req, err := http.NewRequest(method, u, strings.NewReader(string(raw))) |
| + if err != nil { |
| + out <- haReply{err: err} |
| + return |
| + } |
| + req.Header.Set("Content-Type", "application/json") |
| + req.Header.Set("Authorization", "Bearer "+token) |
| + resp, err := http.DefaultClient.Do(req) |
| + if err != nil { |
| + out <- haReply{err: err} |
| + return |
| + } |
| + defer resp.Body.Close() |
| + var res map[string]any |
| + if err := json.NewDecoder(resp.Body).Decode(&res); err != nil { |
| + out <- haReply{status: resp.StatusCode, err: err} |
| + return |
| + } |
| + out <- haReply{status: resp.StatusCode, res: res} |
| + }() |
| + return out |
| +} |
| + |
| +// haPause is a hook that stops one request at its admitted point: arrived |
| +// closes when the request reaches it, and the request waits there until |
| +// release. Hooks for other apps pass straight through. The test's cleanup |
| +// releases a request a failed test left waiting. |
| +type haPause struct { |
| + arrived, release chan struct{} |
| + arriveOnce sync.Once |
| + releaseOnce sync.Once |
| +} |
| + |
| +func newHAPause(t *testing.T) *haPause { |
| + p := &haPause{arrived: make(chan struct{}), release: make(chan struct{})} |
| + t.Cleanup(p.free) |
| + return p |
| +} |
| + |
| +func (p *haPause) hook(app string) func(sqid, app string) { |
| + want := app |
| + return func(sqid, app string) { |
| + if sqid != "bab" || app != want { |
| + return |
| + } |
| + p.arriveOnce.Do(func() { close(p.arrived) }) |
| + <-p.release |
| + } |
| +} |
| + |
| +func (p *haPause) free() { p.releaseOnce.Do(func() { close(p.release) }) } |
| + |
| +// wait blocks until the paused request has arrived, or its answer came |
| +// first (it was refused before the hook), which fails the test. |
| +func (p *haPause) wait(t *testing.T, done <-chan haReply) { |
| + t.Helper() |
| + select { |
| + case <-p.arrived: |
| + case r := <-done: |
| + t.Fatalf("the request answered before its admitted point: %d %v %v", r.status, r.res, r.err) |
| + } |
| +} |
| + |
| +// recreate trashes and purges app's row and creates it again under the |
| +// SAME created_at, as an HA app (pool ha in us) when ha, else plain, so the |
| +// created_at condition cannot be what tells the two incarnations apart. |
| +func (w *haRegionWorld) recreate(t *testing.T, app string, ha bool) { |
| + t.Helper() |
| + ctx := context.Background() |
| + inc, live, err := w.st.AppIncarnationContext(ctx, OpsAccountID, app) |
| + if err != nil || !live { |
| + t.Fatalf("incarnation of %s = %v %v", app, live, err) |
| + } |
| + if err := w.st.TrashAppRow(OpsAccountID, app, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := w.st.PurgeAppRow(OpsAccountID, app); err != nil { |
| + t.Fatal(err) |
| + } |
| + created := time.Unix(inc.createdAt, 0) |
| + if ha { |
| + _, err = w.st.CreateHAAppInRegion(OpsAccountID, app, "paul@keymail.dev", "us", "chosen", "ha", created) |
| + } else { |
| + _, err = w.st.CreateAppInRegion(OpsAccountID, app, "paul@keymail.dev", "", "", created) |
| + } |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + now, live, err := w.st.AppIncarnationContext(ctx, OpsAccountID, app) |
| + if err != nil || !live || now.createdAt != inc.createdAt || now == inc { |
| + t.Fatalf("recreated %s: %+v %v %v, want the same created_at, another incarnation", app, now, live, err) |
| + } |
| +} |
| + |
| +// haStore is a fresh console database, for the store-level tests. |
| +func haStore(t *testing.T) *Store { |
| + t.Helper() |
| + st, err := Open(filepath.Join(t.TempDir(), "console.db")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { st.Close() }) |
| + return st |
| +} |
| + |
| +// TestAppRegionSetRefusesHAApp: both region doors refuse a born-HA app, |
| +// before its enable (no manifest: the row decides, not appPolicy) and |
| +// after it, changing nothing. Controls: the same requests for plain apps |
| +// in the same world change their region. |
| +func TestAppRegionSetRefusesHAApp(t *testing.T) { |
| + w := haRegionFixture(t, haRegionOpts{Accounts: []string{"bab"}}) |
| + w.haCreate(t, "haapp", "us", true) |
| + w.haCreate(t, "plain", "us", false) |
| + w.haCreate(t, "plainweb", "us", false) |
| + put := func(app string) (int, map[string]any) { |
| + return putJSON(t, w.appURL(app, "region"), map[string]any{"region": "uk"}, w.token) |
| + } |
| + refusedPut := func(what string) { |
| + t.Helper() |
| + var status int |
| + var res map[string]any |
| + w.unchanged(t, what, haAllow{}, func() { status, res = put("haapp") }) |
| + wantHARefusal(t, what, status, res, http.StatusConflict, haCopyRegionLocked, "") |
| + if r := w.regionOf(t, "haapp"); r != "us" { |
| + t.Errorf("%s: haapp's region = %q, want us", what, r) |
| + } |
| + } |
| + |
| + refusedPut("api region, before enable") |
| + // Control: the same request for a plain app. |
| + if status, res := put("plain"); status != http.StatusOK || res["region"] != "uk" || res["changed"] != true { |
| + t.Fatalf("control: plain region change = %d %v, want 200 uk changed", status, res) |
| + } |
| + if r := w.regionOf(t, "plain"); r != "uk" { |
| + t.Fatalf("control: plain's region = %q, want uk", r) |
| + } |
| + |
| + w.enable(t, "haapp", "us") |
| + refusedPut("api region, after enable") |
| + |
| + c, csrf := w.browser(t) |
| + form := url.Values{"csrf": {csrf}, "region": {"uk"}} |
| + var kind, msg string |
| + w.unchanged(t, "browser region", haAllow{}, func() { kind, msg = w.postFlash(t, c, "/a/bab/apps/haapp/region", form) }) |
| + if kind != "err" || msg != sentenceCase(haCopyRegionLocked) { |
| + t.Errorf("browser region flash = %q %q, want err %q", kind, msg, sentenceCase(haCopyRegionLocked)) |
| + } |
| + if r := w.regionOf(t, "haapp"); r != "us" { |
| + t.Errorf("browser: haapp's region = %q, want us", r) |
| + } |
| + // Control: the same form for a plain app. |
| + if kind, msg := w.postFlash(t, c, "/a/bab/apps/plainweb/region", form); kind != "ok" { |
| + t.Fatalf("control: browser plain region change flash = %q %q, want ok", kind, msg) |
| + } |
| + if r := w.regionOf(t, "plainweb"); r != "uk" { |
| + t.Fatalf("control: plainweb's region = %q, want uk", r) |
| + } |
| +} |
| + |
| +// TestStoreSetAppRegionSkipsHARow: the store's conditional update writes |
| +// no HA row, whatever its caller checked. Control: the same call on a |
| +// plain row in us lands. |
| +func TestStoreSetAppRegionSkipsHARow(t *testing.T) { |
| + st := haStore(t) |
| + ctx, now := context.Background(), time.Now() |
| + if _, err := st.CreateHAAppInRegion(OpsAccountID, "haapp", "paul@keymail.dev", "us", "chosen", "ha", now); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := st.CreateAppInRegion(OpsAccountID, "plain", "paul@keymail.dev", "us", "chosen", now); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, c := range []struct { |
| + app string |
| + want bool |
| + to string |
| + }{{"haapp", false, "us"}, {"plain", true, "uk"}} { |
| + inc, live, err := st.AppIncarnationContext(ctx, OpsAccountID, c.app) |
| + if err != nil || !live { |
| + t.Fatal(live, err) |
| + } |
| + ok, err := st.SetAppRegion(ctx, OpsAccountID, c.app, inc, "us", "uk", now) |
| + if err != nil || ok != c.want { |
| + t.Errorf("SetAppRegion %s = %v %v, want %v", c.app, ok, err, c.want) |
| + } |
| + if r, _, err := st.AppRegion(OpsAccountID, c.app); err != nil || r != c.to { |
| + t.Errorf("%s's region = %q %v, want %q", c.app, r, err, c.to) |
| + } |
| + } |
| +} |
| + |
| +// TestRegionChangeAcrossHARecreation (round 1, P2-5): a region change |
| +// admitted for plain shop pauses (appRegionAdmitted) after every check and |
| +// before any lock; shop is trashed, purged and recreated as HA under the |
| +// same created_at, and enabled; the paused change then answers 409 Changed |
| +// and writes nothing. Every step must succeed. |
| +// |
| +// The mutation that makes it fail: delete the incarnation comparison in |
| +// setAppRegion AND in Store.SetAppRegion, AND Store.SetAppRegion's empty |
| +// ha_pool condition; step 5 then answers 200 and the row reads uk. With any one layer left it |
| +// passes (TestStoreSetAppRegionSkipsHARow pins the ha_pool layer alone). |
| +func TestRegionChangeAcrossHARecreation(t *testing.T) { |
| + w := haRegionFixture(t, haRegionOpts{Accounts: []string{"bab"}}) |
| + // 1. A plain shop in us. |
| + w.haCreate(t, "shop", "us", false) |
| + if r := w.regionOf(t, "shop"); r != "us" { |
| + t.Fatalf("shop's region = %q, want us", r) |
| + } |
| + // 2. The change to uk, paused at its admitted point. |
| + pause := newHAPause(t) |
| + appRegionAdmitted = pause.hook("shop") |
| + t.Cleanup(func() { appRegionAdmitted = nil }) |
| + done := haSend(http.MethodPut, w.appURL("shop", "region"), w.token, map[string]any{"region": "uk"}) |
| + pause.wait(t, done) |
| + // 3. The name is recreated as HA, under the same created_at. |
| + w.recreate(t, "shop", true) |
| + // 4. The new shop's first enable binds pool ha to us. |
| + w.enable(t, "shop", "us") |
| + // 5. The paused change answers Changed and has no side effects. |
| + var r haReply |
| + w.unchanged(t, "paused region change", haAllow{}, func() { |
| + pause.free() |
| + r = <-done |
| + }) |
| + if r.err != nil { |
| + t.Fatal(r.err) |
| + } |
| + wantHARefusal(t, "paused region change", r.status, r.res, http.StatusConflict, appRegionSetCopy.Changed, "") |
| + // 6. The HA app is as its create and enable made it. |
| + if pool, region, err := w.st.AppHAPool(context.Background(), OpsAccountID, "shop"); err != nil || pool != "ha" || region != "us" { |
| + t.Errorf("AppHAPool = %q %q %v, want ha us", pool, region, err) |
| + } |
| + m := w.manifestOf(t, "shop") |
| + if m.Pool != "ha" || !slices.Equal(m.Regions, []string{"us"}) { |
| + t.Errorf("manifest = %+v, want pool ha regions [us]", m) |
| + } |
| + w.enable(t, "shop", "us") |
| + if again := w.manifestOf(t, "shop"); again.Pool != m.Pool || !slices.Equal(again.Regions, m.Regions) || again.Channel != m.Channel { |
| + t.Errorf("the second enable changed the manifest: %+v, was %+v", again, m) |
| + } |
| +} |
| + |
| +// TestPlaceRefusesHAApp (Review Focus 4): carlos apps place refuses a |
| +// born-HA app, to its own fleet and back to the shared one, before and |
| +// after its enable, changing nothing. Controls: the same requests for |
| +// plain apps in the same world answer 200 and write the placement. |
| +func TestPlaceRefusesHAApp(t *testing.T) { |
| + w := haRegionFixture(t, haRegionOpts{Accounts: []string{"bab"}, HistoryInactive: true}) |
| + if _, err := w.st.CreateHAAppInRegion(OpsAccountID, "haapp", "paul@keymail.dev", "us", "chosen", "ha", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, app := range []string{"plain", "plainback"} { |
| + if _, err := w.st.CreateAppIn(OpsAccountID, app, "paul@keymail.dev", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + own := "fleet/" + w.ownFleet |
| + place := func(app, target string) (int, map[string]any) { |
| + return postJSON(t, w.appURL(app, "place"), map[string]any{"target": target}, w.token) |
| + } |
| + refused := func(what string) { |
| + t.Helper() |
| + for _, target := range []string{own, ""} { |
| + var status int |
| + var res map[string]any |
| + w.unchanged(t, what+" to "+target, haAllow{}, func() { status, res = place("haapp", target) }) |
| + wantHARefusal(t, what+" to "+target, status, res, http.StatusConflict, haCopyRegionLocked, "") |
| + if p := w.placementOf(t, "haapp"); p != "" { |
| + t.Errorf("%s to %q: haapp's placement = %q, want none", what, target, p) |
| + } |
| + } |
| + } |
| + |
| + refused("place, before enable") |
| + // Controls: the same requests for plain apps. |
| + if status, res := place("plain", own); status != http.StatusOK { |
| + t.Fatalf("control: plain place = %d %v, want 200", status, res) |
| + } |
| + if p := w.placementOf(t, "plain"); p != own { |
| + t.Fatalf("control: plain's placement = %q, want %q", p, own) |
| + } |
| + if status, res := place("plainback", own); status != http.StatusOK { |
| + t.Fatalf("control: plainback place = %d %v, want 200", status, res) |
| + } |
| + if status, res := place("plainback", ""); status != http.StatusOK { |
| + t.Fatalf("control: plainback clear = %d %v, want 200", status, res) |
| + } |
| + if p := w.placementOf(t, "plainback"); p != "" { |
| + t.Fatalf("control: plainback's placement = %q, want none", p) |
| + } |
| + |
| + // F11: after the enable, too. |
| + w.enable(t, "haapp", "us") |
| + refused("place, after enable") |
| +} |
| + |
| +// TestPlaceRefusesHAAppHistoryActive: the early refusal (the clear answer) |
| +// comes before the place door's hostname-history cut, so a born-HA app |
| +// hears why it is never placed, not that placement is fixed for now. The |
| +// control is NOT a success by design: with the history active the same |
| +// request for a plain app meets the existing cut, exactly as before. |
| +func TestPlaceRefusesHAAppHistoryActive(t *testing.T) { |
| + w := haRegionFixture(t, haRegionOpts{Accounts: []string{"bab"}}) |
| + w.haCreate(t, "haapp", "us", true) |
| + w.haCreate(t, "plain", "us", false) |
| + own := "fleet/" + w.ownFleet |
| + var status int |
| + var res map[string]any |
| + w.unchanged(t, "place, history active", haAllow{}, func() { |
| + status, res = postJSON(t, w.appURL("haapp", "place"), map[string]any{"target": own}, w.token) |
| + }) |
| + wantHARefusal(t, "place, history active", status, res, http.StatusConflict, haCopyRegionLocked, "") |
| + status, res = postJSON(t, w.appURL("plain", "place"), map[string]any{"target": own}, w.token) |
| + wantHARefusal(t, "control: plain place, history active", status, res, http.StatusConflict, msgPlacementFixed, "") |
| +} |
| + |
| +// TestPlacePausedAcrossRecreation (round 1, P1-3): a place admitted for |
| +// plain shop pauses (placeAdmitted) before the account and app locks; shop |
| +// is trashed, purged and recreated (a) as HA: 409 haCopyRegionLocked; (b) |
| +// as another plain app: 409 Changed. Neither writes a placement or anything |
| +// else. Control: the same place, unpaused, on a plain app answers 200 with |
| +// the placement written. |
| +// |
| +// The mutation that makes it fail: remove the re-read under the locks; (a) |
| +// is then still refused by the store (ok false, so Changed) and fails on |
| +// the sentence. Remove the store's conditions as well and the placement is |
| +// written: it fails on the state. |
| +func TestPlacePausedAcrossRecreation(t *testing.T) { |
| + w := haRegionFixture(t, haRegionOpts{Accounts: []string{"bab"}, HistoryInactive: true}) |
| + own := "fleet/" + w.ownFleet |
| + // Control first: the same place, unpaused, for a plain app. |
| + if _, err := w.st.CreateAppIn(OpsAccountID, "control2", "paul@keymail.dev", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + if status, res := postJSON(t, w.appURL("control2", "place"), map[string]any{"target": own}, w.token); status != http.StatusOK { |
| + t.Fatalf("control: place = %d %v, want 200", status, res) |
| + } |
| + if p := w.placementOf(t, "control2"); p != own { |
| + t.Fatalf("control: placement = %q, want %q", p, own) |
| + } |
| + t.Cleanup(func() { placeAdmitted = nil }) |
| + for _, c := range []struct { |
| + name string |
| + ha bool |
| + msg string |
| + }{{"as HA", true, haCopyRegionLocked}, {"as plain", false, appRegionSetCopy.Changed}} { |
| + t.Run(c.name, func(t *testing.T) { |
| + app := "shop" |
| + if !c.ha { |
| + app = "shop2" |
| + } |
| + if _, err := w.st.CreateAppIn(OpsAccountID, app, "paul@keymail.dev", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + pause := newHAPause(t) |
| + placeAdmitted = pause.hook(app) |
| + done := haSend(http.MethodPost, w.appURL(app, "place"), w.token, map[string]any{"target": own}) |
| + pause.wait(t, done) |
| + w.recreate(t, app, c.ha) |
| + var r haReply |
| + w.unchanged(t, "paused place", haAllow{}, func() { |
| + pause.free() |
| + r = <-done |
| + }) |
| + if r.err != nil { |
| + t.Fatal(r.err) |
| + } |
| + wantHARefusal(t, "paused place", r.status, r.res, http.StatusConflict, c.msg, "") |
| + if p := w.placementOf(t, app); p != "" { |
| + t.Errorf("%s's placement = %q, want none", app, p) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// TestStoreSetAppPlacementIf: the conditional placement write lands only on |
| +// the incarnation it was given, and never on an HA row. Control: a plain |
| +// row with its current incarnation is placed. |
| +func TestStoreSetAppPlacementIf(t *testing.T) { |
| + st := haStore(t) |
| + ctx, now := context.Background(), time.Now() |
| + incOf := func(app string) AppIncarnation { |
| + t.Helper() |
| + inc, live, err := st.AppIncarnationContext(ctx, OpsAccountID, app) |
| + if err != nil || !live { |
| + t.Fatal(live, err) |
| + } |
| + return inc |
| + } |
| + placed := func(app string) string { |
| + t.Helper() |
| + p, err := st.AppPlacement(OpsAccountID, app) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return p |
| + } |
| + // A stale incarnation: the name was purged and created again, at the |
| + // same second. |
| + if _, err := st.CreateAppIn(OpsAccountID, "stale", "paul@keymail.dev", now); err != nil { |
| + t.Fatal(err) |
| + } |
| + old := incOf("stale") |
| + if err := st.TrashAppRow(OpsAccountID, "stale", now); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := st.PurgeAppRow(OpsAccountID, "stale"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := st.CreateAppIn(OpsAccountID, "stale", "paul@keymail.dev", now); err != nil { |
| + t.Fatal(err) |
| + } |
| + if ok, err := st.SetAppPlacementIfContext(ctx, OpsAccountID, "stale", old, "fleet/pi"); err != nil || ok { |
| + t.Errorf("stale incarnation = %v %v, want false", ok, err) |
| + } |
| + if p := placed("stale"); p != "" { |
| + t.Errorf("stale's placement = %q, want none", p) |
| + } |
| + // An HA row, with its current incarnation. |
| + if _, err := st.CreateHAAppInRegion(OpsAccountID, "haapp", "paul@keymail.dev", "us", "chosen", "ha", now); err != nil { |
| + t.Fatal(err) |
| + } |
| + if ok, err := st.SetAppPlacementIfContext(ctx, OpsAccountID, "haapp", incOf("haapp"), "fleet/pi"); err != nil || ok { |
| + t.Errorf("HA row = %v %v, want false", ok, err) |
| + } |
| + if p := placed("haapp"); p != "" { |
| + t.Errorf("haapp's placement = %q, want none", p) |
| + } |
| + // Control: the recreated plain row, with its current incarnation. |
| + if ok, err := st.SetAppPlacementIfContext(ctx, OpsAccountID, "stale", incOf("stale"), "fleet/pi"); err != nil || !ok { |
| + t.Errorf("plain row = %v %v, want true", ok, err) |
| + } |
| + if p := placed("stale"); p != "fleet/pi" { |
| + t.Errorf("stale's placement = %q, want fleet/pi", p) |
| + } |
| +} |
| + |
| +// TestSwapConcurrencyRefusedForHAApp: both swap doors refuse a born-HA app |
| +// a swap concurrency above 1, changing nothing; 1, and a mode alone, pass. |
| +// Controls: the same requests for a plain app pass. |
| +func TestSwapConcurrencyRefusedForHAApp(t *testing.T) { |
| + w := haRegionFixture(t, haRegionOpts{Accounts: []string{"bab"}}) |
| + w.haCreate(t, "haapp", "us", true) |
| + w.haCreate(t, "plain", "us", false) |
| + w.enable(t, "haapp", "us") |
| + swap := func(app string, body map[string]any) (int, map[string]any) { |
| + return putJSON(t, w.appURL(app, "swap"), body, w.token) |
| + } |
| + before := w.swapOf(t, "haapp") |
| + var status int |
| + var res map[string]any |
| + w.unchanged(t, "api swap 2", haAllow{}, func() { status, res = swap("haapp", map[string]any{"swap_concurrency": 2}) }) |
| + wantHARefusal(t, "api swap 2", status, res, http.StatusBadRequest, haCopySwapConcurrency, "") |
| + if got := w.swapOf(t, "haapp"); got != before { |
| + t.Errorf("the refused swap changed the policy: %+v, was %+v", got, before) |
| + } |
| + // Controls: the same request with 1, and a mode with no number. |
| + if status, res := swap("haapp", map[string]any{"swap_concurrency": 1}); status != http.StatusOK || res["swap_concurrency"] != float64(1) { |
| + t.Fatalf("control: api swap 1 = %d %v, want 200", status, res) |
| + } |
| + if status, res := swap("haapp", map[string]any{"swap": "hold"}); status != http.StatusOK { |
| + t.Fatalf("control: api swap hold = %d %v, want 200", status, res) |
| + } |
| + if got := w.swapOf(t, "haapp"); got.Swap != appconfig.SwapHold || got.Concurrency != 1 { |
| + t.Fatalf("control: haapp's policy = %+v, want hold, 1", got) |
| + } |
| + // Control: a plain app may run 3 at a time. |
| + if status, res := swap("plain", map[string]any{"swap_concurrency": 3}); status != http.StatusOK || res["swap_concurrency"] != float64(3) { |
| + t.Fatalf("control: plain api swap 3 = %d %v, want 200", status, res) |
| + } |
| + |
| + c, csrf := w.browser(t) |
| + form := func(n string) url.Values { |
| + return url.Values{"csrf": {csrf}, "swap": {"overlap"}, "swap_concurrency": {n}} |
| + } |
| + before = w.swapOf(t, "haapp") |
| + var kind, msg string |
| + w.unchanged(t, "browser swap 2", haAllow{}, func() { kind, msg = w.postFlash(t, c, "/a/bab/apps/haapp/swap", form("2")) }) |
| + if kind != "err" || msg != haCopySwapConcurrency { |
| + t.Errorf("browser swap 2 flash = %q %q, want err %q", kind, msg, haCopySwapConcurrency) |
| + } |
| + if got := w.swapOf(t, "haapp"); got != before { |
| + t.Errorf("the refused browser swap changed the policy: %+v, was %+v", got, before) |
| + } |
| + // Controls: the same form with 1, and with 2 for the plain app. |
| + if kind, msg := w.postFlash(t, c, "/a/bab/apps/haapp/swap", form("1")); kind != "ok" { |
| + t.Fatalf("control: browser swap 1 flash = %q %q, want ok", kind, msg) |
| + } |
| + if kind, msg := w.postFlash(t, c, "/a/bab/apps/plain/swap", form("2")); kind != "ok" { |
| + t.Fatalf("control: plain browser swap 2 flash = %q %q, want ok", kind, msg) |
| + } |
| + if got := w.swapOf(t, "plain"); got.Swap != appconfig.SwapOverlap || got.Concurrency != 2 { |
| + t.Fatalf("control: plain's policy = %+v, want overlap, 2", got) |
| + } |
| +} |