package console import ( "bytes" "context" "log" "os" "strings" "sync" "testing" "time" "github.com/carlosframework/platform/internal/domains" "github.com/carlosframework/platform/internal/release" ) // tempApp seeds a temporary row in the fixture's ops account and one object // under its prefix (a shipped site, as far as any listing can tell). func tempApp(t *testing.T, fx *consoleFixture, app string, expires time.Time) { t.Helper() if ok, err := fx.st.CreateTemporaryAppIn(OpsAccountID, app, "paul@keymail.dev", time.Now(), expires); err != nil || !ok { t.Fatalf("CreateTemporaryAppIn(%s) = %v %v", app, ok, err) } if _, err := fx.rel.Put(context.Background(), "apps/"+fx.sqid+"/"+app+"/channels/edge.json", []byte(`{}`), ""); err != nil { t.Fatal(err) } } func lifeOf(t *testing.T, fx *consoleFixture, app string) *AppLife { t.Helper() l, err := fx.st.AppLife(OpsAccountID, app) if err != nil { t.Fatal(err) } return l } func markerOf(t *testing.T, fx *consoleFixture, app string) *release.AppTombstone { t.Helper() tomb, err := release.ReadAppTombstone(context.Background(), fx.rel, fx.sqid, app) if err != nil { t.Fatal(err) } return tomb } func always(v bool) evidenceFunc { return func(context.Context) bool { return v } } func TestKeyedLocksSerialiseOneKeyOnly(t *testing.T) { var l keyedLocks unlockA := l.lock("bab/tmp-a") other := make(chan struct{}) go func() { l.lock("bab/tmp-b")(); close(other) }() select { case <-other: case <-time.After(time.Second): t.Fatal("a different key waited on tmp-a's lock") } var wg sync.WaitGroup got := make(chan struct{}) wg.Add(1) go func() { defer wg.Done(); l.lock("bab/tmp-a")(); close(got) }() select { case <-got: t.Fatal("a second holder of tmp-a's lock did not wait") case <-time.After(50 * time.Millisecond): } unlockA() wg.Wait() if len(l.m) != 0 { t.Fatalf("lock entries left behind: %v", l.m) } } func TestBucketEvidenceIgnoresTrashMarkers(t *testing.T) { if bucketEvidence(nil) { t.Error("an empty listing is not evidence") } if bucketEvidence([]string{"apps/bab/tmp-a/deleted.json", "apps/bab/x/deleted.json"}) { t.Error("a listing of trash markers alone is not evidence (spec §2, astra 5.2)") } if !bucketEvidence([]string{"apps/bab/tmp-a/deleted.json", "apps/bab/kass/channels/stable.json"}) { t.Error("one real key is evidence") } if isTrashMarkerKey("apps/bab/tmp-a/sub/deleted.json") { t.Error("a deleted.json below the app prefix is not the marker") } } // Spec §9: settleApp, each marker/row/restoring combination. func TestSettleAppEachCombination(t *testing.T) { ctx := context.Background() now := time.Now().Truncate(time.Second) at := now.Add(-time.Hour) t.Run("live row, marker present: trashed with the marker's clock and the 3-day window", func(t *testing.T) { fx := newConsoleFixture(t) tempApp(t, fx, "tmp-a", now.Add(29*24*time.Hour)) release.TrashApp(ctx, fx.rel, fx.sqid, "tmp-a", "paul@keymail.dev", at) if err := fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "tmp-a", now, always(true)); err != nil { t.Fatal(err) } l := lifeOf(t, fx, "tmp-a") if l.DeletedAt != at.Unix() || l.PurgeFor != at.Unix() || l.PurgeAfter != at.Unix()+72*3600 { t.Fatalf("row = %+v", l) } }) t.Run("live permanent row, marker present: trashed, no early purge", func(t *testing.T) { fx := newConsoleFixture(t) release.TrashApp(ctx, fx.rel, fx.sqid, "kass", "paul@keymail.dev", at) fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "kass", now, always(true)) if l := lifeOf(t, fx, "kass"); l.DeletedAt != at.Unix() || l.PurgeAfter != 0 { t.Fatalf("row = %+v", l) } }) t.Run("restoring, marker present: step 4 failed, slot released, deadline kept", func(t *testing.T) { fx := newConsoleFixture(t) tempApp(t, fx, "tmp-a", now.Add(time.Hour)) release.TrashApp(ctx, fx.rel, fx.sqid, "tmp-a", "expiry", at) fx.st.TrashAppRow(OpsAccountID, "tmp-a", at) fx.st.SetRestoring(OpsAccountID, "tmp-a", true) fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "tmp-a", now, always(true)) if l := lifeOf(t, fx, "tmp-a"); l.Restoring || !l.Trashed() || l.PurgeAfter != at.Unix()+72*3600 { t.Fatalf("row = %+v", l) } }) t.Run("restoring, marker gone, objects present: step 5 finished", func(t *testing.T) { fx := newConsoleFixture(t) tempApp(t, fx, "tmp-a", now.Add(time.Hour)) fx.st.TrashAppRow(OpsAccountID, "tmp-a", at) fx.st.SetRestoring(OpsAccountID, "tmp-a", true) fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "tmp-a", now, always(true)) if l := lifeOf(t, fx, "tmp-a"); l.Trashed() || l.Restoring || l.PurgeAfter != 0 { t.Fatalf("row = %+v", l) } }) t.Run("trashed, marker gone, objects present: restored elsewhere, expiry bumped", func(t *testing.T) { fx := newConsoleFixture(t) tempApp(t, fx, "tmp-a", now.Add(-time.Minute)) fx.st.TrashAppRow(OpsAccountID, "tmp-a", at) fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "tmp-a", now, always(true)) l := lifeOf(t, fx, "tmp-a") if l.Trashed() || l.ExpiresAt != now.Add(temporaryTTLDefault).Unix() { t.Fatalf("row = %+v, want live with expiry now+7d", l) } }) t.Run("trashed, no marker, no objects, evidence: purged elsewhere, row freed", func(t *testing.T) { fx := newConsoleFixture(t) fx.st.CreateTemporaryAppIn(OpsAccountID, "tmp-a", "paul@keymail.dev", now, now.Add(time.Hour)) fx.st.TrashAppRow(OpsAccountID, "tmp-a", at) fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "tmp-a", now, always(true)) if l := lifeOf(t, fx, "tmp-a"); l != nil { t.Fatalf("row = %+v, want it gone", l) } }) t.Run("trashed, no marker, no objects, no evidence: left exactly as it is", func(t *testing.T) { fx := newConsoleFixture(t) fx.st.CreateTemporaryAppIn(OpsAccountID, "tmp-a", "paul@keymail.dev", now, now.Add(time.Hour)) fx.st.TrashAppRow(OpsAccountID, "tmp-a", at) before := *lifeOf(t, fx, "tmp-a") fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "tmp-a", now, always(false)) if l := lifeOf(t, fx, "tmp-a"); l == nil || *l != before { t.Fatalf("row = %+v, want unchanged %+v", l, before) } }) t.Run("live, no marker: nothing to settle", func(t *testing.T) { fx := newConsoleFixture(t) tempApp(t, fx, "tmp-a", now.Add(time.Hour)) before := *lifeOf(t, fx, "tmp-a") fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "tmp-a", now, always(true)) if l := lifeOf(t, fx, "tmp-a"); *l != before { t.Fatalf("row = %+v, want unchanged", l) } }) t.Run("unreadable marker: an error, and nothing changes", func(t *testing.T) { fx := newConsoleFixture(t) tempApp(t, fx, "tmp-a", now.Add(time.Hour)) fx.rel.Put(ctx, release.AppTombstoneKey(fx.sqid, "tmp-a"), []byte("{"), "") before := *lifeOf(t, fx, "tmp-a") if err := fx.srv.settleApp(ctx, OpsAccountID, fx.sqid, "tmp-a", now, always(true)); err == nil { t.Fatal("settleApp believed an unparseable marker") } if l := lifeOf(t, fx, "tmp-a"); *l != before { t.Fatalf("row = %+v, want unchanged", l) } }) } // Spec §9 "Materialize": a marker found for a temporary row sets the 3-day // window from the marker's clock; for a permanent row, leaves it 0. func TestMaterializeSetsTheTemporaryWindowFromTheMarker(t *testing.T) { st := openTestStore(t) rel := release.NewDirStore(t.TempDir()) ctx := t.Context() now := time.Unix(1700000000, 0).UTC() id, sqid, err := domains.MintAccount(ctx, rel, "Testco", "paul@keymail.dev", now) if err != nil { t.Fatal(err) } MaterializeAccounts(ctx, rel, st) st.CreateTemporaryAppIn(id, "tmp-a", "paul@keymail.dev", now, now.Add(30*24*time.Hour)) st.CreateAppIn(id, "perm", "paul@keymail.dev", now) at := now.Add(time.Hour) for _, app := range []string{"tmp-a", "perm"} { rel.Put(ctx, "apps/"+sqid+"/"+app+"/channels/edge.json", []byte(`{}`), "") if err := release.TrashApp(ctx, rel, sqid, app, "paul@keymail.dev", at); err != nil { t.Fatal(err) } } if err := MaterializeApps(ctx, rel, st); err != nil { t.Fatal(err) } if l, _ := st.AppLife(id, "tmp-a"); l.PurgeFor != at.Unix() || l.PurgeAfter != at.Unix()+72*3600 { t.Fatalf("temporary row = %+v, want the window from the marker's clock", l) } if l, _ := st.AppLife(id, "perm"); l.PurgeAfter != 0 || l.PurgeFor != 0 { t.Fatalf("permanent row = %+v, want no window", l) } } // The tripwire ignores trash markers: a bucket holding nothing else is not // evidence that anything was purged. func TestMaterializeHoldsPurgesOnABucketOfMarkers(t *testing.T) { st := openTestStore(t) rel := release.NewDirStore(t.TempDir()) ctx := t.Context() now := time.Unix(1700000000, 0).UTC() id, sqid, _ := domains.MintAccount(ctx, rel, "Testco", "paul@keymail.dev", now) MaterializeAccounts(ctx, rel, st) st.CreateAppIn(id, "gamma", "paul@keymail.dev", now) st.TrashAppRow(id, "gamma", now) release.TrashApp(ctx, rel, sqid, "somebody-else", "paul@keymail.dev", now) var logged bytes.Buffer log.SetOutput(&logged) t.Cleanup(func() { log.SetOutput(os.Stderr) }) if err := MaterializeApps(ctx, rel, st); err != nil { t.Fatal(err) } if trashed, _ := st.HasTrashedAppIn(id, "gamma"); !trashed { t.Fatal("a bucket holding only trash markers released a held name") } if !strings.Contains(logged.String(), "CARLOS_DEPLOYMENT_BUCKET") { t.Fatalf("the hold was silent:\n%s", logged.String()) } }