console: lapse inventory, leftover caps and newest-first selection (plans 4d)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+531
−1
internal/console/plangrace.go+15 −1internal/console/plangrace_test.go+22 −0internal/console/planlapse_inventory.go+242 −0internal/console/planlapse_inventory_test.go+252 −0
diff --git a/internal/console/plangrace.go b/internal/console/plangrace.go| index 5d5def9c7..dbbbda11f 100644 |
| --- a/internal/console/plangrace.go |
| +++ b/internal/console/plangrace.go |
| @@ -16,10 +16,18 @@ const ( |
| capEmail capID = "email" // sends, per UTC day |
| capBandwidth capID = "bandwidth" // bytes, per UTC month |
| capAwakeHours capID = "awake_hours" // hours, per UTC month |
| + capSidecars capID = "sidecars" // workers beyond the per-app cap, summed; lapse only |
| + capCustomDomains capID = "custom_domains" // custom domains on a plan without them; lapse only |
| ) |
| // capOrder is the order caps are listed in, everywhere. |
| -var capOrder = []capID{capRunningThings, capStorage, capEmail, capBandwidth, capAwakeHours} |
| +var capOrder = []capID{capRunningThings, capStorage, capEmail, capBandwidth, capAwakeHours, capSidecars, capCustomDomains} |
| + |
| +// leftoverCap names the caps only a lapse measures (plans 4d): what an |
| +// account kept from a plan it no longer has. Their level is ok (nothing |
| +// left over) or over (anything left over), never warn or ceiling; grace is |
| +// 7 days and they have no period. |
| +func leftoverCap(c capID) bool { return c == capSidecars || c == capCustomDomains } |
| // capLevel is how close to its limit an account is on one cap. |
| type capLevel string |
| @@ -81,6 +89,12 @@ func graceEnd(c capID, first time.Time) time.Time { |
| } |
| func levelOf(c capID, used, limit int64) capLevel { |
| + if leftoverCap(c) { |
| + if used > 0 { |
| + return levelOver |
| + } |
| + return levelOK |
| + } |
| switch { |
| case hasCeiling(c) && used >= 2*limit: |
| return levelCeiling |
diff --git a/internal/console/plangrace_test.go b/internal/console/plangrace_test.go| index c9f2c4b99..2ff51566a 100644 |
| --- a/internal/console/plangrace_test.go |
| +++ b/internal/console/plangrace_test.go |
| @@ -238,3 +238,25 @@ func TestExpiredPeriodIsNotDueOrShown(t *testing.T) { |
| t.Errorf("current row not shown: %v", got) |
| } |
| } |
| + |
| +func TestLeftoverLevels(t *testing.T) { |
| + for _, c := range []struct { |
| + cap capID |
| + used, limit int64 |
| + want capLevel |
| + }{ |
| + {capSidecars, 0, 1, levelOK}, {capSidecars, 1, 1, levelOver}, {capSidecars, 4, 5, levelOver}, |
| + {capSidecars, 400, 1, levelOver}, {capCustomDomains, 0, 0, levelOK}, {capCustomDomains, 2, 0, levelOver}, |
| + } { |
| + st := evaluateCap(nil, c.cap, c.used, c.limit, planNow) |
| + if st.Level != c.want { |
| + t.Errorf("%s used %d limit %d = %s, want %s", c.cap, c.used, c.limit, st.Level, c.want) |
| + } |
| + if st.Level.over() && (!st.GraceEnds.Equal(planNow.Add(7*24*time.Hour)) || st.Period != "") { |
| + t.Errorf("%s: grace ends %v period %q, want planNow+7d and no period", c.cap, st.GraceEnds, st.Period) |
| + } |
| + } |
| + if !leftoverCap(capSidecars) || !leftoverCap(capCustomDomains) || leftoverCap(capRunningThings) { |
| + t.Fatal("leftoverCap classifies wrongly") |
| + } |
| +} |
diff --git a/internal/console/planlapse_inventory.go b/internal/console/planlapse_inventory.go| new file mode 100644 |
| index 000000000..70826be5d |
| --- /dev/null |
| +++ b/internal/console/planlapse_inventory.go |
| @@ -0,0 +1,242 @@ |
| +package console |
| + |
| +import ( |
| + "cmp" |
| + "context" |
| + "errors" |
| + "fmt" |
| + "slices" |
| + "strings" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/plans" |
| + "github.com/carlosframework/platform/internal/provision" |
| +) |
| + |
| +// The kinds of thing a lapse can turn off (plans 4d). Stored in |
| +// plan_lapse_items.kind; keep the strings. |
| +const ( |
| + lapseApp = "app" |
| + lapseInstance = "instance" |
| + lapseSidecar = "sidecar" |
| + lapseDomain = "domain" |
| +) |
| + |
| +// lapseRecordsMax bounds one app's instance records in a lapse inventory; |
| +// more is an error, never a partial count. |
| +const lapseRecordsMax = 1000 |
| + |
| +// lapseThing is one thing a lapse governs. |
| +type lapseThing struct { |
| + Kind, App, Ref string |
| + CreatedAt time.Time |
| + Catchall bool // a domain claimed with its catch-all alias |
| + Keep bool |
| +} |
| + |
| +// lapseInventory is everything a lapsed account runs that its limits |
| +// govern. Things holds apps and instances (the running-things cap). |
| +type lapseInventory struct{ Things, Sidecars, Domains []lapseThing } |
| + |
| +// lapseInventory reads the account's live apps, their instance and worker |
| +// records and its custom domain claims. Any read that fails, and any |
| +// record that does not decode, fails the whole call: unknown is not over. |
| +func (s *Server) lapseInventory(ctx context.Context, acct *Account) (*lapseInventory, error) { |
| + if s.cfg.Releases == nil { |
| + return nil, errors.New("no bucket") |
| + } |
| + apps, err := s.store.LiveAppsCreated(acct.ID) |
| + if err != nil { |
| + return nil, err |
| + } |
| + items, err := s.store.LapseItems(acct.ID) |
| + if err != nil { |
| + return nil, err |
| + } |
| + kept := map[string]int64{} |
| + for _, it := range items { |
| + if it.Keep { |
| + kept[it.Kind+"\x00"+it.Ref] = unixOrZero(it.ThingCreated) |
| + } |
| + } |
| + names := make([]string, 0, len(apps)) |
| + for app := range apps { |
| + names = append(names, app) |
| + } |
| + slices.Sort(names) |
| + inv := &lapseInventory{} |
| + for _, app := range names { |
| + inv.Things = append(inv.Things, lapseThing{Kind: lapseApp, App: app, Ref: app, CreatedAt: apps[app]}) |
| + recs, unreadable, err := provision.ListRecordsStrict(ctx, s.cfg.Releases, acct.Sqid, app, lapseRecordsMax) |
| + if err != nil { |
| + return nil, err |
| + } |
| + if len(unreadable) > 0 { |
| + return nil, fmt.Errorf("%d unreadable instance records in %s", len(unreadable), app) |
| + } |
| + for _, r := range recs { |
| + inv.Things = append(inv.Things, lapseThing{Kind: lapseInstance, App: app, Ref: strings.ToLower(r.Host), CreatedAt: r.CreatedAt}) |
| + } |
| + scs, err := provision.ListSidecarRecordsStrict(ctx, s.cfg.Releases, acct.Sqid, app) |
| + if err != nil { |
| + return nil, err |
| + } |
| + for _, r := range scs { |
| + inv.Sidecars = append(inv.Sidecars, lapseThing{Kind: lapseSidecar, App: app, Ref: strings.ToLower(r.Host), CreatedAt: r.CreatedAt}) |
| + } |
| + } |
| + claims, err := s.claims.List(ctx, s.cfg.Releases) |
| + if err != nil { |
| + return nil, err |
| + } |
| + for _, c := range claims { |
| + host := strings.ToLower(c.Host) |
| + if c.Account != acct.Sqid { |
| + continue |
| + } |
| + if _, live := apps[c.App]; !live || !s.customDomain(strings.TrimPrefix(host, "*."), acct.Sqid) { |
| + continue |
| + } |
| + created, _ := time.Parse(time.RFC3339, c.CreatedAt) |
| + inv.Domains = append(inv.Domains, lapseThing{Kind: lapseDomain, App: c.App, Ref: host, CreatedAt: created, Catchall: c.Catchall}) |
| + } |
| + for _, list := range [][]lapseThing{inv.Things, inv.Sidecars, inv.Domains} { |
| + for i := range list { |
| + if at, ok := kept[list[i].Kind+"\x00"+list[i].Ref]; ok && at == unixOrZero(list[i].CreatedAt) { |
| + list[i].Keep = true |
| + } |
| + } |
| + } |
| + return inv, nil |
| +} |
| + |
| +// measureLeftovers measures the two lapse-only caps from an inventory: the |
| +// workers beyond the per-app cap, summed over apps, and the custom domains |
| +// on a plan without them. A cap the plan does not limit is absent. |
| +func measureLeftovers(inv *lapseInventory, l *plans.Limits) map[capID]capMeasure { |
| + out := map[capID]capMeasure{} |
| + if l.SidecarsPerApp > 0 { |
| + per := map[string]int{} |
| + for _, sc := range inv.Sidecars { |
| + per[sc.App]++ |
| + } |
| + var used int64 |
| + for _, n := range per { |
| + used += int64(max(0, n-l.SidecarsPerApp)) |
| + } |
| + out[capSidecars] = capMeasure{Used: used, Limit: int64(l.SidecarsPerApp)} |
| + } |
| + if !l.CustomDomains { |
| + out[capCustomDomains] = capMeasure{Used: int64(len(inv.Domains)), Limit: 0} |
| + } |
| + return out |
| +} |
| + |
| +// lapseOrder is the order things are offered a place: kept things oldest |
| +// first, then the rest oldest first; ties by kind (apps first), then ref. |
| +func lapseOrder(things []lapseThing) []lapseThing { |
| + out := slices.Clone(things) |
| + kindRank := func(k string) int { |
| + if k == lapseApp { |
| + return 0 |
| + } |
| + return 1 |
| + } |
| + slices.SortStableFunc(out, func(a, b lapseThing) int { |
| + if a.Keep != b.Keep { |
| + if a.Keep { |
| + return -1 |
| + } |
| + return 1 |
| + } |
| + return cmp.Or(a.CreatedAt.Compare(b.CreatedAt), cmp.Compare(kindRank(a.Kind), kindRank(b.Kind)), cmp.Compare(a.Ref, b.Ref)) |
| + }) |
| + return out |
| +} |
| + |
| +// chooseOff is the running things beyond limit: walking lapseOrder, a thing |
| +// stays while the staying count plus its cost fits, where an instance whose |
| +// app is not yet staying costs 2 (keeping an instance keeps its app). The |
| +// rest are off, newest first in effect; an off app takes its instances |
| +// with it. At or under the limit, or with no limit, nothing is off. |
| +func chooseOff(things []lapseThing, limit int) []lapseThing { |
| + if limit <= 0 || len(things) <= limit { |
| + return nil |
| + } |
| + appStays := map[string]bool{} |
| + stays := map[string]bool{} |
| + n := 0 |
| + for _, x := range lapseOrder(things) { |
| + key := x.Kind + "\x00" + x.Ref |
| + if x.Kind == lapseApp && appStays[x.App] { |
| + stays[key] = true |
| + continue |
| + } |
| + cost := 1 |
| + if x.Kind == lapseInstance && !appStays[x.App] { |
| + cost++ |
| + } |
| + if n+cost > limit { |
| + continue |
| + } |
| + n += cost |
| + stays[key] = true |
| + appStays[x.App] = true |
| + } |
| + var off []lapseThing |
| + for _, x := range lapseOrder(things) { |
| + if !stays[x.Kind+"\x00"+x.Ref] { |
| + off = append(off, x) |
| + } |
| + } |
| + return off |
| +} |
| + |
| +// sidecarsOff is each app's workers beyond perApp, chosen the way |
| +// chooseOff chooses: kept first, then oldest; the newest go. |
| +func sidecarsOff(sidecars []lapseThing, perApp int) []lapseThing { |
| + if perApp <= 0 { |
| + return nil |
| + } |
| + seen := map[string]int{} |
| + var off []lapseThing |
| + for _, x := range lapseOrder(sidecars) { |
| + if seen[x.App] >= perApp { |
| + off = append(off, x) |
| + continue |
| + } |
| + seen[x.App]++ |
| + } |
| + return off |
| +} |
| + |
| +// lapseHosts is the host list boxes get for what is off: instance and |
| +// worker hosts, every instance and worker host of an off app, and each off |
| +// domain with "*."+domain for a catch-all. Lower case, sorted, deduped. |
| +func lapseHosts(off []lapseThing, inv *lapseInventory) []string { |
| + var out []string |
| + for _, x := range off { |
| + switch x.Kind { |
| + case lapseInstance, lapseSidecar: |
| + out = append(out, x.Ref) |
| + case lapseApp: |
| + for _, list := range [][]lapseThing{inv.Things, inv.Sidecars} { |
| + for _, y := range list { |
| + if y.App == x.App && y.Kind != lapseApp { |
| + out = append(out, y.Ref) |
| + } |
| + } |
| + } |
| + case lapseDomain: |
| + out = append(out, x.Ref) |
| + if x.Catchall { |
| + out = append(out, "*."+x.Ref) |
| + } |
| + } |
| + } |
| + for i := range out { |
| + out[i] = strings.ToLower(out[i]) |
| + } |
| + slices.Sort(out) |
| + return slices.Compact(out) |
| +} |
diff --git a/internal/console/planlapse_inventory_test.go b/internal/console/planlapse_inventory_test.go| new file mode 100644 |
| index 000000000..3905252ea |
| --- /dev/null |
| +++ b/internal/console/planlapse_inventory_test.go |
| @@ -0,0 +1,252 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "reflect" |
| + "slices" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/domains" |
| + "github.com/carlosframework/platform/internal/plans" |
| + "github.com/carlosframework/platform/internal/provision" |
| +) |
| + |
| +func at(h int) time.Time { return planNow.Add(time.Duration(h) * time.Hour).UTC() } |
| + |
| +func lapseApp_(t *testing.T, fx *consoleFixture, acct *Account, app string, created time.Time) { |
| + t.Helper() |
| + if _, err := fx.st.CreateAppIn(acct.ID, app, "owner@example.com", created); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +func lapseInstanceRec(t *testing.T, fx *consoleFixture, acct *Account, app, host string, created time.Time) { |
| + t.Helper() |
| + b, err := json.Marshal(provision.Record{Schema: 1, Host: host, Slug: app, App: app, Account: acct.Sqid, Channel: "stable", CreatedAt: created}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := fx.rel.Put(context.Background(), provision.RecordKey(acct.Sqid, app, host), b, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +func lapseSidecarRec(t *testing.T, fx *consoleFixture, acct *Account, app, name string, created time.Time) string { |
| + t.Helper() |
| + host := provision.SidecarHost(name, app, acct.Sqid) |
| + b, err := json.Marshal(provision.SidecarRecord{Schema: 1, Name: name, Host: host, App: app, Account: acct.Sqid, Channel: "stable", CreatedAt: created}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := fx.rel.Put(context.Background(), provision.SidecarRecordKey(acct.Sqid, app, name), b, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + return host |
| +} |
| + |
| +func lapseClaim(t *testing.T, fx *consoleFixture, acct *Account, app, host string, catchall bool, created time.Time) { |
| + t.Helper() |
| + b, err := json.Marshal(domains.Claim{Schema: 1, Host: host, Account: acct.Sqid, App: app, Route: "r." + app, |
| + CreatedAt: created.Format(time.RFC3339), Wildcard: catchall, Catchall: catchall}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := fx.rel.Put(context.Background(), domains.ClaimKey(host), b, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +func th(kind, app, ref string, h int) lapseThing { |
| + return lapseThing{Kind: kind, App: app, Ref: ref, CreatedAt: at(h)} |
| +} |
| + |
| +func refsOf(things []lapseThing) []string { |
| + var out []string |
| + for _, x := range things { |
| + out = append(out, x.Ref) |
| + } |
| + slices.Sort(out) |
| + return out |
| +} |
| + |
| +func sampleThings() []lapseThing { |
| + return []lapseThing{ |
| + th(lapseApp, "a", "a", 1), th(lapseInstance, "a", "a1.example.com", 2), th(lapseApp, "b", "b", 3), |
| + th(lapseInstance, "b", "b1.example.com", 4), th(lapseInstance, "a", "a2.example.com", 5), |
| + } |
| +} |
| + |
| +func TestChooseOffNewestFirst(t *testing.T) { |
| + got := refsOf(chooseOff(sampleThings(), 3)) |
| + if want := []string{"a2.example.com", "b1.example.com"}; !reflect.DeepEqual(got, want) { |
| + t.Fatalf("off = %v, want %v", got, want) |
| + } |
| +} |
| + |
| +func TestChooseOffKeepWins(t *testing.T) { |
| + things := sampleThings() |
| + things[3].Keep = true // b1 |
| + got := refsOf(chooseOff(things, 3)) |
| + if want := []string{"a1.example.com", "a2.example.com"}; !reflect.DeepEqual(got, want) { |
| + t.Fatalf("off = %v, want %v", got, want) |
| + } |
| +} |
| + |
| +func TestChooseOffKeepImpliesTheApp(t *testing.T) { |
| + things := sampleThings() |
| + things[4].Keep = true // a2 |
| + got := refsOf(chooseOff(things, 2)) |
| + if want := []string{"a1.example.com", "b", "b1.example.com"}; !reflect.DeepEqual(got, want) { |
| + t.Fatalf("off = %v, want %v (stays a, a2)", got, want) |
| + } |
| +} |
| + |
| +func TestChooseOffUnderTheCapIsNothing(t *testing.T) { |
| + if got := chooseOff(sampleThings(), 5); got != nil { |
| + t.Fatalf("at the cap: %v", got) |
| + } |
| + if got := chooseOff(sampleThings(), 9); got != nil { |
| + t.Fatalf("under the cap: %v", got) |
| + } |
| + if got := chooseOff(sampleThings(), 0); got != nil { |
| + t.Fatalf("no cap: %v", got) |
| + } |
| +} |
| + |
| +func TestSidecarsOffPerApp(t *testing.T) { |
| + sc := []lapseThing{ |
| + th(lapseSidecar, "a", "w1", 1), th(lapseSidecar, "a", "w2", 2), th(lapseSidecar, "a", "w3", 3), |
| + th(lapseSidecar, "b", "v1", 4), |
| + } |
| + if got, want := refsOf(sidecarsOff(sc, 1)), []string{"w2", "w3"}; !reflect.DeepEqual(got, want) { |
| + t.Fatalf("off = %v, want %v", got, want) |
| + } |
| + sc[2].Keep = true |
| + if got, want := refsOf(sidecarsOff(sc, 1)), []string{"w1", "w2"}; !reflect.DeepEqual(got, want) { |
| + t.Fatalf("keeping the newest: off = %v, want %v", got, want) |
| + } |
| + if got := sidecarsOff(sc, 0); got != nil { |
| + t.Fatalf("no cap: %v", got) |
| + } |
| +} |
| + |
| +func TestLapseHostsExpandsAppsAndCatchalls(t *testing.T) { |
| + inv := &lapseInventory{ |
| + Things: append(sampleThings(), th(lapseInstance, "b", "B2.example.com", 6)), |
| + Sidecars: []lapseThing{ |
| + th(lapseSidecar, "a", "w.a.bab.sidecar.internal", 1), th(lapseSidecar, "b", "w.b.bab.sidecar.internal", 1), |
| + }, |
| + Domains: []lapseThing{ |
| + {Kind: lapseDomain, App: "a", Ref: "shop.example.com", Catchall: true}, |
| + {Kind: lapseDomain, App: "a", Ref: "plain.example.org"}, |
| + }, |
| + } |
| + off := []lapseThing{th(lapseApp, "b", "b", 3), th(lapseInstance, "a", "a2.example.com", 5), inv.Domains[0], inv.Domains[1], |
| + th(lapseInstance, "b", "b1.example.com", 4)} |
| + got := lapseHosts(off, inv) |
| + want := []string{"*.shop.example.com", "a2.example.com", "b1.example.com", "b2.example.com", "plain.example.org", |
| + "shop.example.com", "w.b.bab.sidecar.internal"} |
| + if !reflect.DeepEqual(got, want) { |
| + t.Fatalf("hosts = %v\nwant %v", got, want) |
| + } |
| +} |
| + |
| +func TestMeasureLeftovers(t *testing.T) { |
| + inv := &lapseInventory{ |
| + Sidecars: []lapseThing{ |
| + th(lapseSidecar, "a", "w1", 1), th(lapseSidecar, "a", "w2", 2), th(lapseSidecar, "a", "w3", 3), |
| + th(lapseSidecar, "b", "v1", 4), th(lapseSidecar, "b", "v2", 5), |
| + }, |
| + Domains: []lapseThing{{Kind: lapseDomain, Ref: "shop.example.com"}, {Kind: lapseDomain, Ref: "x.example.org"}}, |
| + } |
| + got := measureLeftovers(inv, &plans.Limits{SidecarsPerApp: 1}) |
| + want := map[capID]capMeasure{capSidecars: {Used: 3, Limit: 1}, capCustomDomains: {Used: 2, Limit: 0}} |
| + if !reflect.DeepEqual(got, want) { |
| + t.Fatalf("measures = %v, want %v", got, want) |
| + } |
| + got = measureLeftovers(inv, &plans.Limits{CustomDomains: true}) |
| + if len(got) != 0 { |
| + t.Fatalf("a plan limiting neither: %v", got) |
| + } |
| +} |
| + |
| +func TestLapseInventoryReadsTheAccount(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| + fx.srv.cfg.AppsDomain = "oncarlos.test" |
| + acct := planAccount(t, fx, 501) |
| + lapseApp_(t, fx, acct, "shop", at(1)) |
| + lapseApp_(t, fx, acct, "blog", at(2)) |
| + lapseApp_(t, fx, acct, "gone", at(3)) |
| + if _, err := fx.st.db.Exec(`UPDATE apps SET deleted_at = ? WHERE app = 'gone'`, at(4).Unix()); err != nil { |
| + t.Fatal(err) |
| + } |
| + lapseInstanceRec(t, fx, acct, "shop", "shop-1.example.com", at(5)) |
| + lapseInstanceRec(t, fx, acct, "blog", "blog-1.example.com", at(6)) |
| + lapseInstanceRec(t, fx, acct, "gone", "gone-1.example.com", at(7)) |
| + worker := lapseSidecarRec(t, fx, acct, "shop", "mailer", at(8)) |
| + lapseClaim(t, fx, acct, "shop", "shop.example.com", true, at(9)) |
| + lapseClaim(t, fx, acct, "shop", "shop."+acct.Sqid+".oncarlos.test", false, at(10)) |
| + if err := fx.st.PutLapseItem(context.Background(), lapseItem{AccountID: acct.ID, App: "blog", Kind: lapseInstance, |
| + Ref: "blog-1.example.com", ThingCreated: at(6), Keep: true, UpdatedAt: planNow}); err != nil { |
| + t.Fatal(err) |
| + } |
| + // A keep row for an older incarnation of the same name does not keep. |
| + if err := fx.st.PutLapseItem(context.Background(), lapseItem{AccountID: acct.ID, App: "shop", Kind: lapseInstance, |
| + Ref: "shop-1.example.com", ThingCreated: at(-50), Keep: true, UpdatedAt: planNow}); err != nil { |
| + t.Fatal(err) |
| + } |
| + inv, err := fx.srv.lapseInventory(context.Background(), acct) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + wantThings := []lapseThing{ |
| + th(lapseApp, "blog", "blog", 2), th(lapseInstance, "blog", "blog-1.example.com", 6), |
| + th(lapseApp, "shop", "shop", 1), th(lapseInstance, "shop", "shop-1.example.com", 5), |
| + } |
| + wantThings[1].Keep = true |
| + if !sameThings(inv.Things, wantThings) { |
| + t.Fatalf("things = %+v\nwant %+v", inv.Things, wantThings) |
| + } |
| + if want := []lapseThing{th(lapseSidecar, "shop", worker, 8)}; !sameThings(inv.Sidecars, want) { |
| + t.Fatalf("sidecars = %+v", inv.Sidecars) |
| + } |
| + wantDom := lapseThing{Kind: lapseDomain, App: "shop", Ref: "shop.example.com", CreatedAt: at(9), Catchall: true} |
| + if len(inv.Domains) != 1 || !sameThings(inv.Domains, []lapseThing{wantDom}) { |
| + t.Fatalf("domains = %+v", inv.Domains) |
| + } |
| +} |
| + |
| +func sameThings(got, want []lapseThing) bool { |
| + if len(got) != len(want) { |
| + return false |
| + } |
| + for _, w := range want { |
| + found := false |
| + for _, g := range got { |
| + if g.Kind == w.Kind && g.App == w.App && g.Ref == w.Ref && g.CreatedAt.Equal(w.CreatedAt) && g.Keep == w.Keep && g.Catchall == w.Catchall { |
| + found = true |
| + } |
| + } |
| + if !found { |
| + return false |
| + } |
| + } |
| + return true |
| +} |
| + |
| +func TestLapseInventoryUnreadableRecordIsAnError(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| + acct := planAccount(t, fx, 501) |
| + lapseApp_(t, fx, acct, "shop", at(1)) |
| + lapseInstanceRec(t, fx, acct, "shop", "shop-1.example.com", at(2)) |
| + if _, err := fx.rel.Put(context.Background(), provision.RecordKey(acct.Sqid, "shop", "shop-2.example.com"), []byte(`{not json`), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + if inv, err := fx.srv.lapseInventory(context.Background(), acct); err == nil { |
| + t.Fatalf("an unreadable record gave an inventory: %+v", inv) |
| + } |
| +} |