| index 306f9122b..1c80eed3b 100644 |
| --- a/internal/console/planlapse_test.go |
| +++ b/internal/console/planlapse_test.go |
| @@ -2,18 +2,23 @@ package console |
| |
| import ( |
| "context" |
| + "fmt" |
| "net/http" |
| "os" |
| "path/filepath" |
| + "reflect" |
| + "slices" |
| "strings" |
| "testing" |
| "time" |
| |
| "github.com/carlosframework/platform/internal/plans" |
| + "github.com/carlosframework/platform/internal/provision" |
| ) |
| |
| // lapseCatalog is the example catalog plus "hobby", a second unpaid plan |
| -// with a lower running-things cap than free. |
| +// with a lower running-things cap than free, and "mini", a paid plan with |
| +// a cap of 2 and a "one" pack adding one running thing. |
| func lapseCatalog(t *testing.T) *plans.Catalog { |
| t.Helper() |
| b, err := os.ReadFile(exampleCatalogPath) |
| @@ -21,7 +26,10 @@ func lapseCatalog(t *testing.T) *plans.Catalog { |
| t.Fatal(err) |
| } |
| s := strings.Replace(string(b), `"plans": {`, `"plans": { |
| - "hobby": {"paid": false, "limits": {"running_things": 2, "sidecars_per_app": 1, "idle_seconds": 90, "memory_tier": "small", "min_schedule_seconds": 900}},`, 1) |
| + "hobby": {"paid": false, "limits": {"running_things": 2, "sidecars_per_app": 1, "idle_seconds": 90, "memory_tier": "small", "min_schedule_seconds": 900}}, |
| + "mini": {"paid": true, "limits": {"running_things": 2, "sidecars_per_app": 1, "idle_seconds": 90, "memory_tier": "small", "min_schedule_seconds": 900}, "packs": ["one"]},`, 1) |
| + s = strings.Replace(s, `"packs": {`, `"packs": { |
| + "one": {"adds": {"running_things": 1}, "max_quantity": 20},`, 1) |
| c, err := plans.ParseCatalog([]byte(s)) |
| if err != nil { |
| t.Fatal(err) |
| @@ -54,6 +62,11 @@ func TestApplyAccountPlanMarksALapse(t *testing.T) { |
| fx := newConsoleFixture(t) |
| fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| acct := planAccount(t, fx, 501) |
| + // More running things than free covers, so the lapse outlives the |
| + // call's own re-converge (plans 4d task 5). |
| + for i, app := range []string{"a", "b", "c", "d"} { |
| + lapseApp_(t, fx, acct, app, at(-100+i)) |
| + } |
| applyPlan(t, fx, acct, "personal", nil, nil, planNow.Add(-time.Hour)) |
| if got := lapseSince(t, fx, acct); !got.IsZero() { |
| t.Fatalf("free -> personal marked a lapse at %v", got) |
| @@ -62,9 +75,10 @@ func TestApplyAccountPlanMarksALapse(t *testing.T) { |
| if got := lapseSince(t, fx, acct); !got.Equal(planNow) { |
| t.Fatalf("personal -> free: lapse_since = %v, want %v", got, planNow) |
| } |
| + // Paying again covers everything: the lapse ends in the same call. |
| applyPlan(t, fx, acct, "personal", nil, nil, planNow.Add(time.Hour)) |
| - if got := lapseSince(t, fx, acct); !got.Equal(planNow) { |
| - t.Fatalf("free -> personal moved lapse_since to %v", got) |
| + if got := lapseSince(t, fx, acct); !got.IsZero() { |
| + t.Fatalf("free -> personal left lapse_since at %v", got) |
| } |
| } |
| |
| @@ -72,6 +86,9 @@ func TestApplyAccountPlanPackReductionIsALapse(t *testing.T) { |
| fx := newConsoleFixture(t) |
| fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| acct := planAccount(t, fx, 501) |
| + for i := 0; i < 26; i++ { |
| + lapseApp_(t, fx, acct, fmt.Sprintf("app%02d", i), at(-100+i)) |
| + } |
| applyPlan(t, fx, acct, "personal", map[string]int{"apps": 2}, nil, planNow.Add(-time.Hour)) |
| applyPlan(t, fx, acct, "personal", nil, nil, planNow) |
| if got := lapseSince(t, fx, acct); !got.Equal(planNow) { |
| @@ -171,3 +188,299 @@ func TestApplyAccountPlanWithoutACatalogWritesOnlyThePlan(t *testing.T) { |
| t.Fatalf("row without a catalog = %+v", row) |
| } |
| } |
| + |
| +// lapseFixture is an account on personal with five running things (apps |
| +// shop and blog, instances shop-1, blog-1 and shop-2, oldest first) and |
| +// owners reachable by a fake mailer. Moving it to free (cap 3) is a lapse. |
| +func lapseFixture(t *testing.T) (*consoleFixture, *Account, *fakeConsoleMail) { |
| + t.Helper() |
| + fx := newConsoleFixture(t) |
| + fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| + mailer := &fakeConsoleMail{ready: true} |
| + fx.srv.consoleMail = mailer |
| + fx.srv.sendFederated = nil |
| + acct := planAccount(t, fx, 501) |
| + lapseApp_(t, fx, acct, "shop", at(-100)) |
| + lapseInstanceRec(t, fx, acct, "shop", "shop-1.example.com", at(-99)) |
| + lapseApp_(t, fx, acct, "blog", at(-98)) |
| + lapseInstanceRec(t, fx, acct, "blog", "blog-1.example.com", at(-97)) |
| + lapseInstanceRec(t, fx, acct, "shop", "shop-2.example.com", at(-96)) |
| + applyPlan(t, fx, acct, "personal", nil, nil, planNow.Add(-time.Hour)) |
| + return fx, acct, mailer |
| +} |
| + |
| +const week = 7 * 24 * time.Hour |
| + |
| +func disabledOf(t *testing.T, fx *consoleFixture, acct *Account) []string { |
| + t.Helper() |
| + row, err := fx.st.AccountPlan(acct.ID) |
| + if err != nil || row == nil { |
| + t.Fatalf("plan row: %+v, %v", row, err) |
| + } |
| + return row.Disabled |
| +} |
| + |
| +func offItems(t *testing.T, fx *consoleFixture, acct *Account) []string { |
| + t.Helper() |
| + items, err := fx.st.LapseItems(acct.ID) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var out []string |
| + for _, it := range items { |
| + if !it.DisabledAt.IsZero() { |
| + out = append(out, it.Ref) |
| + } |
| + } |
| + slices.Sort(out) |
| + return out |
| +} |
| + |
| +func publishedDisabled(t *testing.T, fx *consoleFixture, acct *Account) []string { |
| + t.Helper() |
| + r, _ := readLimits(t, fx, acct.Sqid) |
| + if r == nil { |
| + t.Fatal("no limits record") |
| + } |
| + return r.Disabled |
| +} |
| + |
| +// lapseAndTurnOff moves the fixture to free at planNow (notice goes out by |
| +// email at once) and runs the pass after grace: the two newest are off. |
| +func lapseAndTurnOff(t *testing.T, fx *consoleFixture, acct *Account) { |
| + t.Helper() |
| + applyPlan(t, fx, acct, "free", nil, nil, planNow) |
| + fx.srv.planGracePass(context.Background(), planNow.Add(week)) |
| + if got, want := disabledOf(t, fx, acct), []string{"blog-1.example.com", "shop-2.example.com"}; !slices.Equal(got, want) { |
| + t.Fatalf("after grace: disabled = %v, want %v", got, want) |
| + } |
| +} |
| + |
| +func TestLapseGraceTurnsNothingOff(t *testing.T) { |
| + fx, acct, _ := lapseFixture(t) |
| + applyPlan(t, fx, acct, "free", nil, nil, planNow) |
| + fx.srv.planGracePass(context.Background(), planNow.Add(week-time.Minute)) |
| + if got := disabledOf(t, fx, acct); got != nil { |
| + t.Fatalf("in grace: disabled = %v", got) |
| + } |
| + if got := offItems(t, fx, acct); got != nil { |
| + t.Fatalf("in grace: items off = %v", got) |
| + } |
| +} |
| + |
| +func TestLapseAfterGraceTurnsTheNewestOff(t *testing.T) { |
| + fx, acct, _ := lapseFixture(t) |
| + lapseAndTurnOff(t, fx, acct) |
| + if got, want := offItems(t, fx, acct), []string{"blog-1.example.com", "shop-2.example.com"}; !slices.Equal(got, want) { |
| + t.Fatalf("items off = %v, want %v", got, want) |
| + } |
| + fx.srv.planLimitsPass(context.Background(), planNow.Add(week+time.Minute)) |
| + if got, want := publishedDisabled(t, fx, acct), []string{"blog-1.example.com", "shop-2.example.com"}; !slices.Equal(got, want) { |
| + t.Fatalf("Record.Disabled = %v, want %v", got, want) |
| + } |
| + evs, err := fx.st.Events(acct.ID, "blog", 20) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + found := false |
| + for _, e := range evs { |
| + if e.Action == "plan.turned_off" && strings.Contains(e.Detail, "blog-1.example.com") { |
| + found = true |
| + } |
| + } |
| + if !found { |
| + t.Fatalf("no plan.turned_off audit on blog: %+v", evs) |
| + } |
| +} |
| + |
| +func TestLapseWaitsForNotice(t *testing.T) { |
| + fx, acct, _ := lapseFixture(t) |
| + fx.srv.consoleMail = nil // no email: notice only as recorded below |
| + applyPlan(t, fx, acct, "free", nil, nil, planNow) |
| + st, ok := capRow(t, fx, acct, capRunningThings) |
| + if !ok || !st.NoticeAt.IsZero() { |
| + t.Fatalf("running_things row %+v (present %v), want no notice yet", st, ok) |
| + } |
| + if err := fx.st.RecordCapNotice(acct.ID, []capState{st}, planNow.Add(week-71*time.Hour)); err != nil { |
| + t.Fatal(err) |
| + } |
| + fx.srv.planGracePass(context.Background(), planNow.Add(week)) |
| + if got := disabledOf(t, fx, acct); got != nil { |
| + t.Fatalf("notice 71h old: disabled = %v", got) |
| + } |
| +} |
| + |
| +func TestApplyAccountPlanTurnsThingsBackOnAtOnce(t *testing.T) { |
| + fx, acct, _ := lapseFixture(t) |
| + lapseAndTurnOff(t, fx, acct) |
| + applyPlan(t, fx, acct, "personal", nil, nil, planNow.Add(week+time.Hour)) |
| + if got := disabledOf(t, fx, acct); got != nil { |
| + t.Fatalf("after paying again: disabled = %v", got) |
| + } |
| + if got := offItems(t, fx, acct); got != nil { |
| + t.Fatalf("after paying again: items off = %v", got) |
| + } |
| + if got := publishedDisabled(t, fx, acct); got != nil { |
| + t.Fatalf("record not republished in the same call: Disabled = %v", got) |
| + } |
| +} |
| + |
| +func TestFurtherFallWaitsForItsOwnGrace(t *testing.T) { |
| + fx, acct, _ := lapseFixture(t) |
| + fx.srv.cfg.PlanCatalog = lapseCatalog(t) |
| + // personal -> mini + 2 one-packs (cap 4): after grace the newest is off. |
| + applyPlan(t, fx, acct, "mini", map[string]int{"one": 2}, nil, planNow) |
| + fx.srv.planGracePass(context.Background(), planNow.Add(week)) |
| + if got, want := disabledOf(t, fx, acct), []string{"shop-2.example.com"}; !slices.Equal(got, want) { |
| + t.Fatalf("after the first grace: disabled = %v, want %v", got, want) |
| + } |
| + // A pack reduced again (cap 3) is a new fall with its own grace. |
| + applyPlan(t, fx, acct, "mini", map[string]int{"one": 1}, nil, planNow.Add(week+time.Hour)) |
| + if got := lapseSince(t, fx, acct); !got.Equal(planNow.Add(week + time.Hour)) { |
| + t.Fatalf("lapse_since = %v, want the new fall", got) |
| + } |
| + fx.srv.planGracePass(context.Background(), planNow.Add(2*week)) |
| + if got, want := disabledOf(t, fx, acct), []string{"shop-2.example.com"}; !slices.Equal(got, want) { |
| + t.Fatalf("in the new grace: disabled = %v, want only what was off %v", got, want) |
| + } |
| + fx.srv.planGracePass(context.Background(), planNow.Add(2*week+time.Hour)) |
| + if got, want := disabledOf(t, fx, acct), []string{"blog-1.example.com", "shop-2.example.com"}; !slices.Equal(got, want) { |
| + t.Fatalf("after the new grace: disabled = %v, want %v", got, want) |
| + } |
| +} |
| + |
| +func TestDeletingAThingBringsAnotherBack(t *testing.T) { |
| + fx, acct, _ := lapseFixture(t) |
| + lapseAndTurnOff(t, fx, acct) |
| + if err := fx.rel.Delete(context.Background(), provision.RecordKey(acct.Sqid, "shop", "shop-1.example.com")); err != nil { |
| + t.Fatal(err) |
| + } |
| + fx.srv.planGracePass(context.Background(), planNow.Add(week+time.Hour)) |
| + if got, want := disabledOf(t, fx, acct), []string{"shop-2.example.com"}; !slices.Equal(got, want) { |
| + t.Fatalf("after a delete: disabled = %v, want %v", got, want) |
| + } |
| + if got, want := offItems(t, fx, acct), []string{"shop-2.example.com"}; !slices.Equal(got, want) { |
| + t.Fatalf("after a delete: items off = %v, want %v", got, want) |
| + } |
| +} |
| + |
| +func TestLapseUnchangedWhenInventoryUnreadable(t *testing.T) { |
| + fx, acct, _ := lapseFixture(t) |
| + lapseAndTurnOff(t, fx, acct) |
| + before, _ := fx.st.LapseItems(acct.ID) |
| + if _, err := fx.rel.Put(context.Background(), provision.RecordKey(acct.Sqid, "blog", "blog-9.example.com"), []byte(`{not json`), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + // Even a raise that would turn everything back on changes nothing while |
| + // the inventory cannot be read. |
| + fx.srv.planGracePass(context.Background(), planNow.Add(week+time.Hour)) |
| + if got, want := disabledOf(t, fx, acct), []string{"blog-1.example.com", "shop-2.example.com"}; !slices.Equal(got, want) { |
| + t.Fatalf("unreadable inventory: disabled = %v, want %v", got, want) |
| + } |
| + if after, _ := fx.st.LapseItems(acct.ID); !reflect.DeepEqual(after, before) { |
| + t.Fatalf("items changed: %+v -> %+v", before, after) |
| + } |
| +} |
| + |
| +func TestNoLapseWithoutAFall(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| + fx.srv.consoleMail = &fakeConsoleMail{ready: true} |
| + fx.srv.sendFederated = nil |
| + acct := planAccount(t, fx, 501) |
| + for i, app := range []string{"a", "b", "c", "d", "e"} { |
| + lapseApp_(t, fx, acct, app, at(-100+i)) |
| + } |
| + applyPlan(t, fx, acct, "free", nil, nil, planNow) |
| + fx.srv.planGracePass(context.Background(), planNow.Add(week)) |
| + fx.srv.planGracePass(context.Background(), planNow.Add(2*week)) |
| + if st, ok := capRow(t, fx, acct, capRunningThings); !ok || !afterGraceDue(st, planNow.Add(2*week)) { |
| + t.Fatalf("fixture: running_things %+v not past grace and notice", st) |
| + } |
| + if got := disabledOf(t, fx, acct); got != nil { |
| + t.Fatalf("never paid: disabled = %v", got) |
| + } |
| + if got := offItems(t, fx, acct); got != nil { |
| + t.Fatalf("never paid: items off = %v", got) |
| + } |
| +} |
| + |
| +func TestSponsoredAccountsNeverTurnedOff(t *testing.T) { |
| + fx, acct, _ := lapseFixture(t) |
| + lapseAndTurnOff(t, fx, acct) |
| + // The operator sponsors the account at the same figure: not eligible. |
| + if _, err := fx.st.db.Exec(`UPDATE account_plans SET source = ? WHERE account_id = ?`, PlanSourceSponsored, acct.ID); err != nil { |
| + t.Fatal(err) |
| + } |
| + fx.srv.planGracePass(context.Background(), planNow.Add(week+time.Hour)) |
| + if got := disabledOf(t, fx, acct); got != nil { |
| + t.Fatalf("sponsored: disabled = %v", got) |
| + } |
| + if got := offItems(t, fx, acct); got != nil { |
| + t.Fatalf("sponsored: items off = %v", got) |
| + } |
| + // And the operator door's way: a sponsored assignment turns things on |
| + // in the same call. |
| + fx2, acct2, _ := lapseFixture(t) |
| + lapseAndTurnOff(t, fx2, acct2) |
| + applyPlan(t, fx2, acct2, "free", nil, &plans.Limits{RunningThings: 3}, planNow.Add(week+time.Hour)) |
| + if got := disabledOf(t, fx2, acct2); got != nil { |
| + t.Fatalf("sponsored through the door: disabled = %v", got) |
| + } |
| +} |
| + |
| +func TestCustomDomainsTurnedOffAfterGrace(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| + fx.srv.cfg.AppsDomain = "oncarlos.test" |
| + fx.srv.consoleMail = &fakeConsoleMail{ready: true} |
| + fx.srv.sendFederated = nil |
| + acct := planAccount(t, fx, 501) |
| + lapseApp_(t, fx, acct, "shop", at(-100)) |
| + lapseInstanceRec(t, fx, acct, "shop", "shop-1.example.com", at(-99)) |
| + lapseClaim(t, fx, acct, "shop", "shop.example.com", true, at(-98)) |
| + lapseClaim(t, fx, acct, "shop", "shop."+acct.Sqid+".oncarlos.test", false, at(-97)) |
| + applyPlan(t, fx, acct, "personal", nil, nil, planNow.Add(-time.Hour)) |
| + applyPlan(t, fx, acct, "free", nil, nil, planNow) |
| + if got := disabledOf(t, fx, acct); got != nil { |
| + t.Fatalf("in grace: disabled = %v", got) |
| + } |
| + fx.srv.planGracePass(context.Background(), planNow.Add(week)) |
| + if got, want := disabledOf(t, fx, acct), []string{"*.shop.example.com", "shop.example.com"}; !slices.Equal(got, want) { |
| + t.Fatalf("after grace: disabled = %v, want %v", got, want) |
| + } |
| +} |
| + |
| +func TestLapseClearsWhenNothingIsLeft(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| + acct := planAccount(t, fx, 501) |
| + lapseApp_(t, fx, acct, "shop", at(-100)) |
| + applyPlan(t, fx, acct, "personal", nil, nil, planNow.Add(-time.Hour)) |
| + if err := fx.st.MarkLapse(acct.ID, planNow.Add(-time.Minute)); err != nil { |
| + t.Fatal(err) |
| + } |
| + fx.srv.planGracePass(context.Background(), planNow) |
| + if got := lapseSince(t, fx, acct); !got.IsZero() { |
| + t.Fatalf("within its limits: lapse_since = %v, want cleared", got) |
| + } |
| +} |
| + |
| +func TestLapseWithoutACatalogUnchanged(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + acct := planAccount(t, fx, 501) |
| + lapseApp_(t, fx, acct, "shop", at(-100)) |
| + applyPlan(t, fx, acct, "personal", nil, nil, planNow.Add(-time.Hour)) |
| + applyPlan(t, fx, acct, "free", nil, nil, planNow) |
| + fx.srv.planGracePass(context.Background(), planNow.Add(week)) |
| + row, _ := fx.st.AccountPlan(acct.ID) |
| + if !row.LapseSince.IsZero() || row.Disabled != nil { |
| + t.Fatalf("no catalog: row %+v", row) |
| + } |
| + if items, _ := fx.st.LapseItems(acct.ID); items != nil { |
| + t.Fatalf("no catalog: items %+v", items) |
| + } |
| + if r, _ := readLimits(t, fx, acct.Sqid); r != nil { |
| + t.Fatalf("no catalog: record %+v", r) |
| + } |
| +} |