| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/plans" |
| 9 | ) |
| 10 | |
| 11 | func readMarker(t *testing.T, fx *consoleFixture) (*plans.Marker, string) { |
| 12 | t.Helper() |
| 13 | m, etag, err := plans.GetMarker(context.Background(), fx.rel) |
| 14 | if err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | return m, etag |
| 18 | } |
| 19 | |
| 20 | func TestPublishPlansMarkerOffAndAbsentWritesNothing(t *testing.T) { |
| 21 | fx := newConsoleFixture(t) |
| 22 | fx.srv.PublishPlansMarker(context.Background()) |
| 23 | if m, _ := readMarker(t, fx); m != nil { |
| 24 | t.Fatalf("wrote a marker with no catalog: %+v", m) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func TestPublishPlansMarkerEnables(t *testing.T) { |
| 29 | fx := newConsoleFixture(t) |
| 30 | fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| 31 | fx.srv.PublishPlansMarker(context.Background()) |
| 32 | m, _ := readMarker(t, fx) |
| 33 | if m == nil || !m.Enabled || m.CatalogID != fx.srv.cfg.PlanCatalog.ID() { |
| 34 | t.Fatalf("marker %+v", m) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestPublishPlansMarkerIsIdempotent(t *testing.T) { |
| 39 | fx := newConsoleFixture(t) |
| 40 | fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| 41 | fx.srv.PublishPlansMarker(context.Background()) |
| 42 | _, first := readMarker(t, fx) |
| 43 | fx.srv.PublishPlansMarker(context.Background()) |
| 44 | if _, second := readMarker(t, fx); second != first { |
| 45 | t.Fatal("an up-to-date marker was rewritten") |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestPublishPlansMarkerFollowsACatalogChange(t *testing.T) { |
| 50 | fx := newConsoleFixture(t) |
| 51 | fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| 52 | fx.srv.PublishPlansMarker(context.Background()) |
| 53 | next := bumpedCatalog(t, "2026-10-22-v2", "4") |
| 54 | fx.srv.cfg.PlanCatalog = next |
| 55 | fx.srv.PublishPlansMarker(context.Background()) |
| 56 | if m, _ := readMarker(t, fx); m.CatalogID != next.ID() { |
| 57 | t.Fatalf("marker %+v, want catalog %s", m, next.ID()) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestPublishPlansMarkerDisablesWhenCatalogRemoved(t *testing.T) { |
| 62 | fx := newConsoleFixture(t) |
| 63 | fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| 64 | fx.srv.PublishPlansMarker(context.Background()) |
| 65 | fx.srv.cfg.PlanCatalog = nil |
| 66 | fx.srv.PublishPlansMarker(context.Background()) |
| 67 | if m, _ := readMarker(t, fx); m == nil || m.Enabled { |
| 68 | t.Fatalf("marker %+v, want disabled", m) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Astra plans-2 finding 1: a bucket failure at start must not leave the |
| 73 | // marker in the wrong state for the life of the console. The worker retries |
| 74 | // until the marker is right. |
| 75 | func TestPublishPlansMarkerRetriesUntilDisabled(t *testing.T) { |
| 76 | fx := newConsoleFixture(t) |
| 77 | fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| 78 | fx.srv.PublishPlansMarker(context.Background()) |
| 79 | defer func(d time.Duration) { plansMarkerRetry = d }(plansMarkerRetry) |
| 80 | plansMarkerRetry = time.Millisecond |
| 81 | fp := &planFailingPuts{Store: fx.rel} |
| 82 | fp.n.Store(3) // the first three writes fail |
| 83 | fx.srv.cfg.Releases = fp |
| 84 | fx.srv.cfg.PlanCatalog = nil |
| 85 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 86 | defer cancel() |
| 87 | fx.srv.PublishPlansMarker(ctx) |
| 88 | fx.srv.cfg.Releases = fp.Store |
| 89 | if m, _ := readMarker(t, fx); m == nil || m.Enabled { |
| 90 | t.Fatalf("marker %+v after transient write failures, want disabled", m) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func TestPublishPlansMarkerStopsWithItsContext(t *testing.T) { |
| 95 | fx := newConsoleFixture(t) |
| 96 | fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| 97 | defer func(d time.Duration) { plansMarkerRetry = d }(plansMarkerRetry) |
| 98 | plansMarkerRetry = time.Millisecond |
| 99 | fp := &planFailingPuts{Store: fx.rel} |
| 100 | fp.n.Store(1 << 30) // never succeeds |
| 101 | fx.srv.cfg.Releases = fp |
| 102 | ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) |
| 103 | defer cancel() |
| 104 | done := make(chan struct{}) |
| 105 | go func() { fx.srv.PublishPlansMarker(ctx); close(done) }() |
| 106 | select { |
| 107 | case <-done: |
| 108 | case <-time.After(5 * time.Second): |
| 109 | t.Fatal("PublishPlansMarker kept retrying after its context ended") |
| 110 | } |
| 111 | } |
| 112 | |