moves: offline run witnessed against the real console doors (Task 7)
carlos move offline run through moveMain drives a move to done against a real console.Server, the boxes played by their reports and woken only by the doors' serial bumps; a box failing in staging, sealing or landing ends in abort, finish, exit 3.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+286
−2
offline_move_e2e_test.go+286 −2
diff --git a/offline_move_e2e_test.go b/offline_move_e2e_test.go| index fafc6285c..ecf3a2fb2 100644 |
| --- a/offline_move_e2e_test.go |
| +++ b/offline_move_e2e_test.go |
| @@ -6,6 +6,7 @@ import ( |
| "crypto/ecdh" |
| "encoding/base64" |
| "encoding/json" |
| + "io" |
| "net/http" |
| "net/http/httptest" |
| "path/filepath" |
| @@ -35,6 +36,11 @@ type offlineE2E struct { |
| // publish writes one more box's edges file and status snapshot; |
| // shared false is a box that serves only a named pool. |
| publish func(box, placement, aws, bucket string, shared bool) |
| + // tap, when set, sees every request the console gets, its body read, |
| + // before the console serves it. |
| + tap func(method, path string, body []byte) |
| + // token is the operator's CLI token. |
| + token, url string |
| } |
| func newOfflineE2E(t *testing.T, caps []string) *offlineE2E { |
| @@ -71,7 +77,15 @@ func newOfflineE2E(t *testing.T, caps []string) *offlineE2E { |
| fp := appconfig.KeyFingerprint(secrets.PublicKey()) |
| var srv *console.Server |
| - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { srv.ServeHTTP(w, r) })) |
| + var e *offlineE2E |
| + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + if e != nil && e.tap != nil { |
| + body, _ := io.ReadAll(r.Body) |
| + r.Body = io.NopCloser(bytes.NewReader(body)) |
| + e.tap(r.Method, r.URL.Path, body) |
| + } |
| + srv.ServeHTTP(w, r) |
| + })) |
| t.Cleanup(ts.Close) |
| srv, err = console.NewServer(console.Config{ |
| Origin: ts.URL, |
| @@ -161,7 +175,8 @@ func newOfflineE2E(t *testing.T, caps []string) *offlineE2E { |
| } |
| } |
| - return &offlineE2E{host: host, rel: rel, run: run, put: put, srv: srv, publish: publish} |
| + e = &offlineE2E{host: host, rel: rel, run: run, put: put, srv: srv, publish: publish, token: token, url: ts.URL} |
| + return e |
| } |
| // TestMoveOfflineAgainstTheRealDoors drives `carlos move offline start` and |
| @@ -491,3 +506,272 @@ func TestMoveOfflineFlipStepsOverAPoolBox(t *testing.T) { |
| wantLines(t, out, "Ready to open", " forward carlos move offline open --host "+host) |
| wantNoLine(t, out, "could not take", "uk-a-pool") |
| } |
| + |
| +// offlineBoxScript plays ie-box (source, stager and sealer) and uk-box |
| +// (target) for `carlos move offline run` against the real console: on each |
| +// serial bump the console makes, it does the boxes' work for the record's |
| +// state once, writing the reports and the box transitions (sealed, landed) |
| +// where and how the real boxes write them. Nothing else wakes it, so a |
| +// move only goes on when a door announced it. |
| +type offlineBoxScript struct { |
| + t *testing.T |
| + e *offlineE2E |
| + failIn string // the state whose box work reports failed |
| + serial int64 // the serial the boxes last acted on |
| + wakes int |
| +} |
| + |
| +func (b *offlineBoxScript) record() *provision.Record { |
| + b.t.Helper() |
| + rec, err := provision.GetRecord(context.Background(), b.e.rel, release.OpsAccount, "shop", b.e.host) |
| + if err != nil { |
| + b.t.Fatal(err) |
| + } |
| + return rec |
| +} |
| + |
| +// poll is the boxes' fast-converge read of the serial: a bump since the |
| +// last one runs their work once. |
| +func (b *offlineBoxScript) poll() { |
| + b.t.Helper() |
| + doc, _, err := release.ReadSerial(context.Background(), b.e.rel) |
| + if err != nil { |
| + b.t.Fatal(err) |
| + } |
| + if doc.Serial == b.serial { |
| + return |
| + } |
| + b.serial = doc.Serial |
| + b.wakes++ |
| + b.work() |
| +} |
| + |
| +// work is one pass of both boxes over the record as it stands. |
| +func (b *offlineBoxScript) work() { |
| + t := b.t |
| + t.Helper() |
| + ctx := context.Background() |
| + rec := b.record() |
| + mv := rec.Move |
| + if mv == nil { |
| + return |
| + } |
| + id, host := mv.ID, b.e.host |
| + failed := mv.State == b.failIn |
| + sealed := map[string]moves.Check{} |
| + for _, it := range moves.OfflineSourced() { |
| + sealed[it.ID] = moves.Check{OK: true, Reason: "ok"} |
| + } |
| + landed := map[string]moves.Check{} |
| + for _, it := range moves.OfflineOwned(moves.OfflineOwnerLanding) { |
| + landed[it.ID] = moves.Check{OK: true, Reason: "ok"} |
| + } |
| + notHeld := func(r *moves.OfflineReport) { r.Held, r.Fenced, r.Quiesced = false, false, false } |
| + switch mv.State { |
| + case provision.OfflineStaging: |
| + b.e.put(id, "ie-box", moves.OfflineRoleStager, moves.OfflinePhaseStaged, func(r *moves.OfflineReport) { |
| + notHeld(r) |
| + r.StageRequest = *mv.StagedAt |
| + r.ReplicaObjects, r.ReplicaBytes, r.StagedObjects, r.StagedBytes, r.RoundObjects, r.RoundBytes = 288, 72_000_000, 288, 72_000_000, 3, 4096 |
| + if failed { |
| + r.Phase, r.StageError = moves.OfflinePhaseFailed, "the replica could not be listed" |
| + } |
| + }) |
| + case provision.OfflineStopping: |
| + b.e.put(id, "ie-box", moves.OfflineRoleSource, moves.OfflinePhaseStopped, nil) |
| + if mv.StagedAt != nil { |
| + b.e.put(id, "ie-box", moves.OfflineRoleStager, moves.OfflinePhaseStopped, notHeld) |
| + } |
| + case provision.OfflineSealing: |
| + if failed { |
| + b.e.put(id, "ie-box", moves.OfflineRoleSealer, moves.OfflinePhaseFailed, func(r *moves.OfflineReport) { |
| + r.Items = map[string]moves.Check{"db": {Reason: "the final copy did not verify"}} |
| + }) |
| + return |
| + } |
| + seal, err := json.Marshal(moves.OfflineReport{V: moves.OfflineReportVersion, Op: id, Host: host, Box: "ie-box", Role: moves.OfflineRoleSealer, |
| + Phase: moves.OfflinePhaseSealed, At: time.Now().UTC(), Items: sealed, FinalTXID: 7, TombstoneOwner: moves.TombstoneOwner(id)}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := b.e.rel.Put(ctx, moves.OfflineSealKey(host, id), seal, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + b.e.put(id, "ie-box", moves.OfflineRoleSealer, moves.OfflinePhaseSealed, func(r *moves.OfflineReport) { |
| + r.Items, r.TombstoneOwner, r.TombstoneUntil = sealed, moves.TombstoneOwner(id), time.Now().Add(moves.TombstoneTTL) |
| + }) |
| + b.e.put(id, "ie-box", moves.OfflineRoleSource, moves.OfflinePhaseStopped, nil) |
| + if _, err := provision.SetRecordMove(ctx, b.e.rel, release.OpsAccount, "shop", host, |
| + provision.OfflineSealedBy(id, "ie-box", provision.MoveSource{FinalTXID: 7, Evidence: moves.OfflineSealEvidence(host, id, seal)})); err != nil { |
| + t.Fatal(err) |
| + } |
| + case provision.OfflineLanding: |
| + if failed { |
| + b.e.put(id, "uk-box", moves.OfflineRoleTarget, moves.OfflinePhaseFailed, func(r *moves.OfflineReport) { |
| + r.Items = map[string]moves.Check{"config": {OK: false, Reason: "the app settings differ"}} |
| + }) |
| + return |
| + } |
| + b.e.put(id, "uk-box", moves.OfflineRoleTarget, moves.OfflinePhaseLanded, func(r *moves.OfflineReport) { r.Items = landed }) |
| + if _, err := provision.SetRecordMove(ctx, b.e.rel, release.OpsAccount, "shop", host, provision.OfflineLandedBy(id, "uk")); err != nil { |
| + t.Fatal(err) |
| + } |
| + case provision.OfflineOpen: |
| + b.e.put(id, "uk-box", moves.OfflineRoleTarget, moves.OfflinePhaseOpened, func(r *moves.OfflineReport) { notHeld(r); r.Items = landed }) |
| + b.e.put(id, "ie-box", moves.OfflineRoleSource, moves.OfflinePhaseParked, nil) |
| + case provision.OfflineFinishing: |
| + b.e.put(id, "ie-box", moves.OfflineRoleSealer, moves.OfflinePhaseLifted, notHeld) |
| + b.e.put(id, "ie-box", moves.OfflineRoleSource, moves.OfflinePhaseGone, notHeld) |
| + case provision.OfflineReverting: |
| + b.e.put(id, "ie-box", moves.OfflineRoleSource, moves.OfflinePhaseReverted, notHeld) |
| + if mv.Sealer != "" { |
| + b.e.put(id, "ie-box", moves.OfflineRoleSealer, moves.OfflinePhaseReverted, notHeld) |
| + } |
| + if mv.StagedAt != nil { |
| + b.e.put(id, "ie-box", moves.OfflineRoleStager, moves.OfflinePhaseReverted, notHeld) |
| + } |
| + } |
| +} |
| + |
| +// runOffline runs `carlos move offline run --to uk <args>` through moveMain |
| +// against the real console, the boxes woken by its serial between polls. |
| +// The run's clock is virtual: each poll is 2 s of it and no wall time. |
| +func (e *offlineE2E) runOffline(t *testing.T, boxes *offlineBoxScript, args ...string) (int, string) { |
| + t.Helper() |
| + var out bytes.Buffer |
| + clock := time.Now() |
| + deps := moveDeps{ |
| + session: func(string) (*apiSession, error) { |
| + return &apiSession{c: newConsoleClient(e.url, e.token), console: e.url, address: "paul@keymail.dev"}, nil |
| + }, |
| + euid: func() int { return 1000 }, |
| + poll: time.Millisecond, |
| + now: func() time.Time { return clock }, |
| + after: func(d time.Duration) <-chan time.Time { |
| + clock = clock.Add(d) |
| + boxes.poll() |
| + fired := make(chan time.Time, 1) |
| + fired <- clock |
| + return fired |
| + }, |
| + probe: func(_ context.Context, url string) int { |
| + if url != "https://"+e.host+"/" { |
| + t.Errorf("probed %q", url) |
| + } |
| + return 200 |
| + }, |
| + interrupts: func() (<-chan struct{}, func()) { return nil, func() {} }, |
| + } |
| + args = append(append([]string{"offline", "run", "--to", "uk"}, args...), "--host", e.host, "--account", release.OpsAccount, "--app", "shop") |
| + ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second) |
| + defer cancel() |
| + code := moveMain(ctx, args, &out, deps) |
| + return code, out.String() |
| +} |
| + |
| +// offlineRunPosts taps the console for the POSTs the run sends, as |
| +// "<door> <id>" (door "start" for the offline door itself). |
| +func offlineRunPosts(e *offlineE2E) *[]string { |
| + var posts []string |
| + base := "/api/cli/apps/" + release.OpsAccount + "/shop/instances/" + e.host + "/move/offline" |
| + e.tap = func(method, path string, body []byte) { |
| + if method != http.MethodPost || !strings.HasPrefix(path, base) { |
| + return |
| + } |
| + door := strings.TrimPrefix(strings.TrimPrefix(path, base), "/") |
| + if door == "" { |
| + door = "start" |
| + } |
| + var b struct { |
| + ID string `json:"id"` |
| + } |
| + _ = json.Unmarshal(body, &b) |
| + posts = append(posts, strings.TrimSpace(door+" "+b.ID)) |
| + } |
| + return &posts |
| +} |
| + |
| +// TestMoveOfflineRunAgainstTheRealDoors (plan 2026-11-15-offline-move-run, |
| +// Task 7): `carlos move offline run` through moveMain carries a move from |
| +// nothing to done against a real console.Server, the boxes played by the |
| +// reports they write and woken only by the doors' serial bumps. A failure |
| +// the target reports before open ends in abort, finish and exit 3, with |
| +// open never sent. |
| +func TestMoveOfflineRunAgainstTheRealDoors(t *testing.T) { |
| + ctx := context.Background() |
| + t.Run("done", func(t *testing.T) { |
| + e := newOfflineE2E(t, []string{"region-ha-1", "offline-move-1", "offline-stage-1"}) |
| + posts := offlineRunPosts(e) |
| + boxes := &offlineBoxScript{t: t, e: e} |
| + code, out := e.runOffline(t, boxes) |
| + if code != 0 { |
| + t.Fatalf("exit %d:\n%s", code, out) |
| + } |
| + rec, err := provision.GetRecord(ctx, e.rel, release.OpsAccount, "shop", e.host) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if rec.Move != nil || rec.LastMove == nil || rec.LastMove.Outcome != provision.OfflineOutcomeDone || rec.Region != "uk" { |
| + t.Fatalf("after the run: region %q, move %+v, last %+v", rec.Region, rec.Move, rec.LastMove) |
| + } |
| + id := rec.LastMove.ID |
| + want := []string{"stage", "start " + id, "seal " + id, "flip " + id, "open " + id, "finish " + id, "finish " + id} |
| + if strings.Join(*posts, ",") != strings.Join(want, ",") { |
| + t.Fatalf("POSTs %q, want %q\n%s", *posts, want, out) |
| + } |
| + wantLines(t, out, "Moving "+e.host+" from ie to uk. Move "+id+".") |
| + if !strings.Contains(out, "Moved. "+e.host+" is served from uk.") { |
| + t.Fatalf("no moved line:\n%s", out) |
| + } |
| + for _, verb := range []string{"stage", "start", "seal", "flip", "open", "finish"} { |
| + if !strings.Contains(out, " "+verb+strings.Repeat(" ", 7-len(verb))+" sent, accepted") { |
| + t.Errorf("no %s line:\n%s", verb, out) |
| + } |
| + } |
| + if _, _, err := e.rel.Get(ctx, moves.OfflineActiveKey); err == nil { |
| + t.Error("the one-move lock is still held after finish") |
| + } |
| + }) |
| + // A box that reports failed before open: the stager's round, the |
| + // sealer's copy, the target's landing. |
| + for _, c := range []struct { |
| + state, said string |
| + doors []string |
| + }{ |
| + {provision.OfflineStaging, "", []string{"stage", "abort", "finish"}}, |
| + {provision.OfflineSealing, "", []string{"stage", "start", "seal", "abort", "finish"}}, |
| + {provision.OfflineLanding, "uk could not take " + "acme.shop.example.com" + ": the app settings differ.", []string{"stage", "start", "seal", "flip", "abort", "finish"}}, |
| + } { |
| + t.Run("a box fails in "+c.state, func(t *testing.T) { |
| + e := newOfflineE2E(t, []string{"region-ha-1", "offline-move-1", "offline-stage-1"}) |
| + posts := offlineRunPosts(e) |
| + boxes := &offlineBoxScript{t: t, e: e, failIn: c.state} |
| + code, out := e.runOffline(t, boxes) |
| + if code != 3 { |
| + t.Fatalf("exit %d:\n%s", code, out) |
| + } |
| + rec, err := provision.GetRecord(ctx, e.rel, release.OpsAccount, "shop", e.host) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if rec.Move != nil || rec.LastMove == nil || rec.LastMove.Outcome != provision.OfflineOutcomeAborted || rec.Region != "ie" { |
| + t.Fatalf("after the run: region %q, move %+v, last %+v", rec.Region, rec.Move, rec.LastMove) |
| + } |
| + id := rec.LastMove.ID |
| + want := []string{"stage"} |
| + for _, d := range c.doors[1:] { |
| + want = append(want, d+" "+id) |
| + } |
| + if strings.Join(*posts, ",") != strings.Join(want, ",") { |
| + t.Fatalf("POSTs %q, want %q\n%s", *posts, want, out) |
| + } |
| + wantLines(t, out, e.host+" is back in ie and serving. Nothing was lost.") |
| + if c.state != provision.OfflineStaging { |
| + wantLines(t, out, " abort Moving it back to ie.") |
| + } |
| + if c.said != "" && !strings.Contains(out, c.said) { |
| + t.Fatalf("the box's reason is not said:\n%s", out) |
| + } |
| + }) |
| + } |
| +} |