| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/carlosframework/platform/internal/hostclaim" |
| 12 | "github.com/carlosframework/platform/internal/hostops" |
| 13 | "github.com/carlosframework/platform/internal/moves" |
| 14 | ) |
| 15 | |
| 16 | // Plan 6 Task 3 fix round 5, ruling 2: an instance incarnation's move log |
| 17 | // outbox rows go when the incarnation is retired or purged, in that step, |
| 18 | // not when the drainer next finds the claim gone (its drop stays a |
| 19 | // backstop). Another incarnation's rows stay. |
| 20 | |
| 21 | // queueTestMoveLog queues one confirmed row for host, op and incarnation. |
| 22 | func queueTestMoveLog(t *testing.T, st *Store, host, op, name, inc string) { |
| 23 | t.Helper() |
| 24 | body, err := json.Marshal(moves.LogEvent{V: 1, Op: op, Host: host, Event: "x", Identity: name, Actor: "console", At: time.Now().UTC()}) |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | if err := st.QueueMoveLog(host, op, name, inc, body, true, time.Now()); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func moveLogRows(t *testing.T, st *Store, host, op string) int { |
| 34 | t.Helper() |
| 35 | got, err := st.MoveLogPending(host, op) |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | return len(got) |
| 40 | } |
| 41 | |
| 42 | func holderIncarnation(t *testing.T, cx *claimCase, host string) string { |
| 43 | t.Helper() |
| 44 | o, err := hostclaim.Read(context.Background(), cx.rel, host) |
| 45 | if err != nil || o.Absent || o.Claim.Holder == nil { |
| 46 | t.Fatalf("the claim of %s: %+v %v", host, o, err) |
| 47 | } |
| 48 | return o.Claim.Holder.Incarnation |
| 49 | } |
| 50 | |
| 51 | func TestRetiringAnInstancePurgesItsMoveLogRows(t *testing.T) { |
| 52 | cx := newClaimCaseWith(t) |
| 53 | cx.instanceWorld(t) |
| 54 | cx.instance(t, `{"host":"acme.go.tito.io","region":"ie"}`) |
| 55 | cx.quiesce() |
| 56 | inc := holderIncarnation(t, cx, "acme.go.tito.io") |
| 57 | queueTestMoveLog(t, cx.st, "acme.go.tito.io", "mv_retired", "done", inc) |
| 58 | queueTestMoveLog(t, cx.st, "acme.go.tito.io", "mv_earlier", "done", "an-earlier-incarnation") |
| 59 | // A row the writer refuses (the record never named its move) is held, |
| 60 | // not deleted (final fix, ruling 1): the drainer runs once, as the moves |
| 61 | // switch would wire it, and the row stays, held, for the retire's purge. |
| 62 | r := *cx.srv.cfg.HostClaimRecovery |
| 63 | r.MoveLogs = cx.srv.drainMoveLog |
| 64 | if err := cx.srv.cfg.ClaimAuthority.Control.RecoveryPass(context.Background(), r); err != nil { |
| 65 | t.Logf("the recovery pass: %v", err) |
| 66 | } |
| 67 | if got, err := cx.st.MoveLogPending("acme.go.tito.io", "mv_retired"); err != nil || len(got) != 1 || !got[0].Held { |
| 68 | t.Fatalf("the refused row is not held: %+v %v", got, err) |
| 69 | } |
| 70 | if status := claimRetireDoor("/api/cli/apps/bab/titogo/instances/acme.go.tito.io", "acme.go.tito.io")(t, cx); status != http.StatusAccepted { |
| 71 | t.Fatalf("delete answered %d", status) |
| 72 | } |
| 73 | if n := moveLogRows(t, cx.st, "acme.go.tito.io", "mv_retired"); n != 1 { |
| 74 | t.Fatalf("the rows went before the incarnation was retired: %d", n) |
| 75 | } |
| 76 | // The boxes park the instance (world staging: its obligations are |
| 77 | // discharged), and the delete's retry frees the claim and retires the |
| 78 | // incarnation. |
| 79 | for i := 0; i < 3; i++ { |
| 80 | obs, err := hostclaim.Read(context.Background(), cx.rel, "acme.go.tito.io") |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | if obs.Absent || obs.Claim.Free() { |
| 85 | break |
| 86 | } |
| 87 | if obs.Claim.Op.Kind == hostclaim.KindParking && len(obs.Claim.Obligations) != 0 { |
| 88 | if _, err := hostclaim.Update(context.Background(), cx.rel, obs, func(c *hostclaim.Claim) error { c.Obligations = nil; return nil }); err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | } |
| 92 | rawReq(t, http.MethodDelete, cx.ts.URL+"/api/cli/apps/bab/titogo/instances/acme.go.tito.io", cx.bearer, "") |
| 93 | } |
| 94 | if o, err := hostclaim.Read(context.Background(), cx.rel, "acme.go.tito.io"); err != nil || !(o.Absent || o.Claim.Free()) { |
| 95 | t.Fatalf("the instance was not retired: op %+v holder %+v obligations %+v %v", o.Claim.Op, o.Claim.Holder, o.Claim.Obligations, err) |
| 96 | } |
| 97 | if n := moveLogRows(t, cx.st, "acme.go.tito.io", "mv_retired"); n != 0 { |
| 98 | t.Fatalf("%d rows of the retired incarnation remain", n) |
| 99 | } |
| 100 | if n := moveLogRows(t, cx.st, "acme.go.tito.io", "mv_earlier"); n != 1 { |
| 101 | t.Fatalf("another incarnation's rows: %d, want 1", n) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // The app purge retires its instances' incarnations itself (RetirePurgeHosts, |
| 106 | // not this door), and records each in its journal; its last stage drops their |
| 107 | // rows. The fixture world cannot drive a purge of an app with instances to |
| 108 | // completion (its hosts' purge needs the boxes), so the stage is tested on a |
| 109 | // journal. |
| 110 | func TestPurgingAnAppPurgesItsInstancesMoveLogRows(t *testing.T) { |
| 111 | fx := newConsoleFixture(t) |
| 112 | j := hostops.PurgeJournal{Hosts: map[string]hostops.PurgeHost{ |
| 113 | "a.example.com": {Holder: &hostclaim.Holder{Incarnation: "inc-a"}}, |
| 114 | "b.example.com": {Holder: &hostclaim.Holder{Incarnation: "inc-b"}}, |
| 115 | "c.example.com": {}, |
| 116 | }} |
| 117 | queueTestMoveLog(t, fx.st, "a.example.com", "mv_1", "done", "inc-a") |
| 118 | queueTestMoveLog(t, fx.st, "b.example.com", "mv_2", "done", "inc-b") |
| 119 | queueTestMoveLog(t, fx.st, "b.example.com", "mv_3", "done", "inc-b-earlier") |
| 120 | queueTestMoveLog(t, fx.st, "d.example.com", "mv_4", "done", "inc-d") |
| 121 | if err := fx.srv.purgeMoveLogOfPurgedHosts(j); err != nil { |
| 122 | t.Fatal(err) |
| 123 | } |
| 124 | for _, c := range []struct { |
| 125 | host, op string |
| 126 | want int |
| 127 | }{{"a.example.com", "mv_1", 0}, {"b.example.com", "mv_2", 0}, {"b.example.com", "mv_3", 1}, {"d.example.com", "mv_4", 1}} { |
| 128 | if n := moveLogRows(t, fx.st, c.host, c.op); n != c.want { |
| 129 | t.Errorf("%s %s: %d rows, want %d", c.host, c.op, n, c.want) |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Final fix, ruling 2: the purge's own run (Resume) drops the rows of the |
| 135 | // incarnations its journal records, from a retired journal on. The world's |
| 136 | // app has no instance the fixture can purge (an instance's host purge needs |
| 137 | // the boxes), so the journal gains one, already retired, the moment it is |
| 138 | // "retired"; Resume then runs from there to done. |
| 139 | func TestPurgeResumeDropsItsHostsMoveLogRows(t *testing.T) { |
| 140 | cx := newClaimCaseWith(t) |
| 141 | cx.purgeWorld(t) |
| 142 | cx.quiesce() |
| 143 | queueTestMoveLog(t, cx.st, "gone.example.com", "mv_1", "done", "inc-gone") |
| 144 | queueTestMoveLog(t, cx.st, "gone.example.com", "mv_2", "done", "inc-earlier") |
| 145 | v := url.Values{"csrf": {cx.csrf}, "app": {"titogo"}, "confirm": {"titogo"}} |
| 146 | if status, _, out := postGated(t, cx.consoleFixture, cx.browser, cx.ts.URL+"/a/bab/trash/delete", v); status != http.StatusAccepted { |
| 147 | t.Fatalf("purge answered %d: %s", status, out) |
| 148 | } |
| 149 | a, err := hostops.ReadCanonicalAdmission(context.Background(), cx.rel, "bab", "titogo") |
| 150 | if err != nil || a.Value.Op == nil { |
| 151 | t.Fatalf("no accepted purge: %v", err) |
| 152 | } |
| 153 | op, appID := a.Value.Op.ID, a.Value.AppID |
| 154 | cx.publishPurgeCensus(t, op) |
| 155 | injected := false |
| 156 | for i := 0; i < 12 && cx.ownsPurge(t, "bab", "titogo", op); i++ { |
| 157 | if j, tag, err := hostops.ReadPurgeJournal(context.Background(), cx.rel, "bab", "titogo", op, appID); err == nil && j.Phase == "retired" && !injected { |
| 158 | if j.Hosts == nil { |
| 159 | j.Hosts = map[string]hostops.PurgeHost{} |
| 160 | } |
| 161 | j.Hosts["gone.example.com"] = hostops.PurgeHost{Holder: &hostclaim.Holder{Incarnation: "inc-gone"}, Retired: true} |
| 162 | if _, err := cx.srv.cfg.ClaimAuthority.Control.SavePurgeJournal(context.Background(), j, tag); err != nil { |
| 163 | t.Fatalf("record the retired host in the journal: %v", err) |
| 164 | } |
| 165 | injected = true |
| 166 | } |
| 167 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 168 | if err := cx.srv.RunClaimRecoveryPass(ctx); err != nil { |
| 169 | t.Logf("purge %s recovery pass %d: %v", op, i, err) |
| 170 | } |
| 171 | cancel() |
| 172 | } |
| 173 | if !injected { |
| 174 | t.Fatal("the purge never stood at retired") |
| 175 | } |
| 176 | if cx.ownsPurge(t, "bab", "titogo", op) { |
| 177 | t.Fatalf("purge %s did not finish", op) |
| 178 | } |
| 179 | if n := moveLogRows(t, cx.st, "gone.example.com", "mv_1"); n != 0 { |
| 180 | t.Fatalf("%d rows of the purged incarnation remain", n) |
| 181 | } |
| 182 | if n := moveLogRows(t, cx.st, "gone.example.com", "mv_2"); n != 1 { |
| 183 | t.Fatalf("another incarnation's rows: %d, want 1", n) |
| 184 | } |
| 185 | } |
| 186 | |