| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "log" |
| 7 | "os" |
| 8 | "strings" |
| 9 | "sync" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/carlosframework/platform/internal/domains" |
| 14 | "github.com/carlosframework/platform/internal/release" |
| 15 | ) |
| 16 | |
| 17 | // tempApp seeds a temporary row in the fixture's ops account and one object |
| 18 | // under its prefix (a shipped site, as far as any listing can tell). |
| 19 | func tempApp(t *testing.T, fx *consoleFixture, app string, expires time.Time) { |
| 20 | t.Helper() |
| 21 | if ok, err := fx.st.CreateTemporaryAppIn(OpsAccountID, app, "paul@keymail.dev", time.Now(), expires); err != nil || !ok { |
| 22 | t.Fatalf("CreateTemporaryAppIn(%s) = %v %v", app, ok, err) |
| 23 | } |
| 24 | if _, err := fx.rel.Put(context.Background(), "apps/"+fx.sqid+"/"+app+"/channels/edge.json", []byte(`{}`), ""); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func lifeOf(t *testing.T, fx *consoleFixture, app string) *AppLife { |
| 30 | t.Helper() |
| 31 | l, err := fx.st.AppLife(OpsAccountID, app) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | return l |
| 36 | } |
| 37 | |
| 38 | func markerOf(t *testing.T, fx *consoleFixture, app string) *release.AppTombstone { |
| 39 | t.Helper() |
| 40 | tomb, err := release.ReadAppTombstone(context.Background(), fx.rel, fx.sqid, app) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | return tomb |
| 45 | } |
| 46 | |
| 47 | func always(v bool) evidenceFunc { return func(context.Context) bool { return v } } |
| 48 | |
| 49 | func TestKeyedLocksSerialiseOneKeyOnly(t *testing.T) { |
| 50 | var l keyedLocks |
| 51 | unlockA := l.lock("bab/tmp-a") |
| 52 | other := make(chan struct{}) |
| 53 | go func() { l.lock("bab/tmp-b")(); close(other) }() |
| 54 | select { |
| 55 | case <-other: |
| 56 | case <-time.After(time.Second): |
| 57 | t.Fatal("a different key waited on tmp-a's lock") |
| 58 | } |
| 59 | var wg sync.WaitGroup |
| 60 | got := make(chan struct{}) |
| 61 | wg.Add(1) |
| 62 | go func() { defer wg.Done(); l.lock("bab/tmp-a")(); close(got) }() |
| 63 | select { |
| 64 | case <-got: |
| 65 | t.Fatal("a second holder of tmp-a's lock did not wait") |
| 66 | case <-time.After(50 * time.Millisecond): |
| 67 | } |
| 68 | unlockA() |
| 69 | wg.Wait() |
| 70 | if len(l.m) != 0 { |
| 71 | t.Fatalf("lock entries left behind: %v", l.m) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestBucketEvidenceIgnoresTrashMarkers(t *testing.T) { |
| 76 | if bucketEvidence(nil) { |
| 77 | t.Error("an empty listing is not evidence") |
| 78 | } |
| 79 | if bucketEvidence([]string{"apps/bab/tmp-a/deleted.json", "apps/bab/x/deleted.json"}) { |
| 80 | t.Error("a listing of trash markers alone is not evidence (spec §2, astra 5.2)") |
| 81 | } |
| 82 | if !bucketEvidence([]string{"apps/bab/tmp-a/deleted.json", "apps/bab/kass/channels/stable.json"}) { |
| 83 | t.Error("one real key is evidence") |
| 84 | } |
| 85 | if isTrashMarkerKey("apps/bab/tmp-a/sub/deleted.json") { |
| 86 | t.Error("a deleted.json below the app prefix is not the marker") |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Spec §9: settleApp, each marker/row/restoring combination. |
| 91 | func TestSettleAppEachCombination(t *testing.T) { |
| 92 | ctx := context.Background() |
| 93 | now := time.Now().Truncate(time.Second) |
| 94 | at := now.Add(-time.Hour) |
| 95 | |
| 96 | t.Run("live row, marker present: trashed with the marker's clock and the 3-day window", func(t *testing.T) { |
| 97 | fx := newConsoleFixture(t) |
| 98 | tempApp(t, fx, "tmp-a", now.Add(29*24*time.Hour)) |
| 99 | release.TrashApp(ctx, fx.rel, fx.sqid, "tmp-a", "paul@keymail.dev", at) |
| 100 | if err := fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "tmp-a", now, always(true)); err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | l := lifeOf(t, fx, "tmp-a") |
| 104 | if l.DeletedAt != at.Unix() || l.PurgeFor != at.Unix() || l.PurgeAfter != at.Unix()+72*3600 { |
| 105 | t.Fatalf("row = %+v", l) |
| 106 | } |
| 107 | }) |
| 108 | t.Run("live permanent row, marker present: trashed, no early purge", func(t *testing.T) { |
| 109 | fx := newConsoleFixture(t) |
| 110 | release.TrashApp(ctx, fx.rel, fx.sqid, "kass", "paul@keymail.dev", at) |
| 111 | fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "kass", now, always(true)) |
| 112 | if l := lifeOf(t, fx, "kass"); l.DeletedAt != at.Unix() || l.PurgeAfter != 0 { |
| 113 | t.Fatalf("row = %+v", l) |
| 114 | } |
| 115 | }) |
| 116 | t.Run("restoring, marker present: step 4 failed, slot released, deadline kept", func(t *testing.T) { |
| 117 | fx := newConsoleFixture(t) |
| 118 | tempApp(t, fx, "tmp-a", now.Add(time.Hour)) |
| 119 | release.TrashApp(ctx, fx.rel, fx.sqid, "tmp-a", "expiry", at) |
| 120 | fx.st.TrashAppRow(OpsAccountID, "tmp-a", at) |
| 121 | fx.st.SetRestoring(OpsAccountID, "tmp-a", true) |
| 122 | fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "tmp-a", now, always(true)) |
| 123 | if l := lifeOf(t, fx, "tmp-a"); l.Restoring || !l.Trashed() || l.PurgeAfter != at.Unix()+72*3600 { |
| 124 | t.Fatalf("row = %+v", l) |
| 125 | } |
| 126 | }) |
| 127 | t.Run("restoring, marker gone, objects present: step 5 finished", func(t *testing.T) { |
| 128 | fx := newConsoleFixture(t) |
| 129 | tempApp(t, fx, "tmp-a", now.Add(time.Hour)) |
| 130 | fx.st.TrashAppRow(OpsAccountID, "tmp-a", at) |
| 131 | fx.st.SetRestoring(OpsAccountID, "tmp-a", true) |
| 132 | fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "tmp-a", now, always(true)) |
| 133 | if l := lifeOf(t, fx, "tmp-a"); l.Trashed() || l.Restoring || l.PurgeAfter != 0 { |
| 134 | t.Fatalf("row = %+v", l) |
| 135 | } |
| 136 | }) |
| 137 | t.Run("trashed, marker gone, objects present: restored elsewhere, expiry bumped", func(t *testing.T) { |
| 138 | fx := newConsoleFixture(t) |
| 139 | tempApp(t, fx, "tmp-a", now.Add(-time.Minute)) |
| 140 | fx.st.TrashAppRow(OpsAccountID, "tmp-a", at) |
| 141 | fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "tmp-a", now, always(true)) |
| 142 | l := lifeOf(t, fx, "tmp-a") |
| 143 | if l.Trashed() || l.ExpiresAt != now.Add(temporaryTTLDefault).Unix() { |
| 144 | t.Fatalf("row = %+v, want live with expiry now+7d", l) |
| 145 | } |
| 146 | }) |
| 147 | t.Run("trashed, no marker, no objects, evidence: purged elsewhere, row freed", func(t *testing.T) { |
| 148 | fx := newConsoleFixture(t) |
| 149 | fx.st.CreateTemporaryAppIn(OpsAccountID, "tmp-a", "paul@keymail.dev", now, now.Add(time.Hour)) |
| 150 | fx.st.TrashAppRow(OpsAccountID, "tmp-a", at) |
| 151 | fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "tmp-a", now, always(true)) |
| 152 | if l := lifeOf(t, fx, "tmp-a"); l != nil { |
| 153 | t.Fatalf("row = %+v, want it gone", l) |
| 154 | } |
| 155 | }) |
| 156 | t.Run("trashed, no marker, no objects, no evidence: left exactly as it is", func(t *testing.T) { |
| 157 | fx := newConsoleFixture(t) |
| 158 | fx.st.CreateTemporaryAppIn(OpsAccountID, "tmp-a", "paul@keymail.dev", now, now.Add(time.Hour)) |
| 159 | fx.st.TrashAppRow(OpsAccountID, "tmp-a", at) |
| 160 | before := *lifeOf(t, fx, "tmp-a") |
| 161 | fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "tmp-a", now, always(false)) |
| 162 | if l := lifeOf(t, fx, "tmp-a"); l == nil || *l != before { |
| 163 | t.Fatalf("row = %+v, want unchanged %+v", l, before) |
| 164 | } |
| 165 | }) |
| 166 | t.Run("live, no marker: nothing to settle", func(t *testing.T) { |
| 167 | fx := newConsoleFixture(t) |
| 168 | tempApp(t, fx, "tmp-a", now.Add(time.Hour)) |
| 169 | before := *lifeOf(t, fx, "tmp-a") |
| 170 | fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "tmp-a", now, always(true)) |
| 171 | if l := lifeOf(t, fx, "tmp-a"); *l != before { |
| 172 | t.Fatalf("row = %+v, want unchanged", l) |
| 173 | } |
| 174 | }) |
| 175 | t.Run("unreadable marker: an error, and nothing changes", func(t *testing.T) { |
| 176 | fx := newConsoleFixture(t) |
| 177 | tempApp(t, fx, "tmp-a", now.Add(time.Hour)) |
| 178 | fx.rel.Put(ctx, release.AppTombstoneKey(fx.sqid, "tmp-a"), []byte("{"), "") |
| 179 | before := *lifeOf(t, fx, "tmp-a") |
| 180 | if err := fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "tmp-a", now, always(true)); err == nil { |
| 181 | t.Fatal("settleApp believed an unparseable marker") |
| 182 | } |
| 183 | if l := lifeOf(t, fx, "tmp-a"); *l != before { |
| 184 | t.Fatalf("row = %+v, want unchanged", l) |
| 185 | } |
| 186 | }) |
| 187 | } |
| 188 | |
| 189 | // Spec §9 "Materialize": a marker found for a temporary row sets the 3-day |
| 190 | // window from the marker's clock; for a permanent row, leaves it 0. |
| 191 | func TestMaterializeSetsTheTemporaryWindowFromTheMarker(t *testing.T) { |
| 192 | st := openTestStore(t) |
| 193 | rel := release.NewDirStore(t.TempDir()) |
| 194 | ctx := t.Context() |
| 195 | now := time.Unix(1700000000, 0).UTC() |
| 196 | id, sqid, err := domains.MintAccount(ctx, rel, "Testco", "paul@keymail.dev", now) |
| 197 | if err != nil { |
| 198 | t.Fatal(err) |
| 199 | } |
| 200 | MaterializeAccounts(ctx, rel, st) |
| 201 | st.CreateTemporaryAppIn(id, "tmp-a", "paul@keymail.dev", now, now.Add(30*24*time.Hour)) |
| 202 | st.CreateAppIn(id, "perm", "paul@keymail.dev", now) |
| 203 | at := now.Add(time.Hour) |
| 204 | for _, app := range []string{"tmp-a", "perm"} { |
| 205 | rel.Put(ctx, "apps/"+sqid+"/"+app+"/channels/edge.json", []byte(`{}`), "") |
| 206 | if err := release.TrashApp(ctx, rel, sqid, app, "paul@keymail.dev", at); err != nil { |
| 207 | t.Fatal(err) |
| 208 | } |
| 209 | } |
| 210 | if err := MaterializeApps(ctx, rel, st); err != nil { |
| 211 | t.Fatal(err) |
| 212 | } |
| 213 | if l, _ := st.AppLife(id, "tmp-a"); l.PurgeFor != at.Unix() || l.PurgeAfter != at.Unix()+72*3600 { |
| 214 | t.Fatalf("temporary row = %+v, want the window from the marker's clock", l) |
| 215 | } |
| 216 | if l, _ := st.AppLife(id, "perm"); l.PurgeAfter != 0 || l.PurgeFor != 0 { |
| 217 | t.Fatalf("permanent row = %+v, want no window", l) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // The tripwire ignores trash markers: a bucket holding nothing else is not |
| 222 | // evidence that anything was purged. |
| 223 | func TestMaterializeHoldsPurgesOnABucketOfMarkers(t *testing.T) { |
| 224 | st := openTestStore(t) |
| 225 | rel := release.NewDirStore(t.TempDir()) |
| 226 | ctx := t.Context() |
| 227 | now := time.Unix(1700000000, 0).UTC() |
| 228 | id, sqid, _ := domains.MintAccount(ctx, rel, "Testco", "paul@keymail.dev", now) |
| 229 | MaterializeAccounts(ctx, rel, st) |
| 230 | st.CreateAppIn(id, "gamma", "paul@keymail.dev", now) |
| 231 | st.TrashAppRow(id, "gamma", now) |
| 232 | release.TrashApp(ctx, rel, sqid, "somebody-else", "paul@keymail.dev", now) |
| 233 | var logged bytes.Buffer |
| 234 | log.SetOutput(&logged) |
| 235 | t.Cleanup(func() { log.SetOutput(os.Stderr) }) |
| 236 | if err := MaterializeApps(ctx, rel, st); err != nil { |
| 237 | t.Fatal(err) |
| 238 | } |
| 239 | if trashed, _ := st.HasTrashedAppIn(id, "gamma"); !trashed { |
| 240 | t.Fatal("a bucket holding only trash markers released a held name") |
| 241 | } |
| 242 | if !strings.Contains(logged.String(), "CARLOS_DEPLOYMENT_BUCKET") { |
| 243 | t.Fatalf("the hold was silent:\n%s", logged.String()) |
| 244 | } |
| 245 | } |
| 246 | |