|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "log/slog" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/pingular/pingularity/internal/settings" |
| 13 | + "github.com/pingular/pingularity/internal/store" |
| 14 | +) |
| 15 | + |
| 16 | +// A LOADED controller with Quick Setup unanswered and NO offer clock is the |
| 17 | +// late-load window: a retry/SIGHUP Reload flips Loaded()==true and broadcasts |
| 18 | +// the wake BEFORE materializeQuickSetup seeds the offer clock. QuickSetupHold |
| 19 | +// reads the bare zero as "never offered -> release", and monitoringLive used |
| 20 | +// to LATCH that release permanently - a fresh install started probing without |
| 21 | +// first-run consent. The predicate must fail CLOSED there: hold a fresh |
| 22 | +// install, latch nothing, and let the real 48h hold take over once the clock |
| 23 | +// is seeded. |
| 24 | +func TestMonitoringLiveHoldsFreshInstallWithUnseededOfferClock(t *testing.T) { |
| 25 | + ctx := context.Background() |
| 26 | + st, err := store.Open(":memory:") |
| 27 | + if err != nil { |
| 28 | + t.Fatalf("open store: %v", err) |
| 29 | + } |
| 30 | + defer st.Close() |
| 31 | + set, err := settings.New(ctx, st, settings.Values{Monitoring: true}) |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("settings: %v", err) |
| 34 | + } |
| 35 | + // The window's exact state: loaded, offer open, clock unseeded. |
| 36 | + if !set.Loaded() { |
| 37 | + t.Fatal("precondition: controller must be loaded") |
| 38 | + } |
| 39 | + if set.QuickSetupDone() { |
| 40 | + t.Fatal("precondition: quick setup must be unanswered") |
| 41 | + } |
| 42 | + if since, err := set.QuickSetupOfferSinceErr(ctx); err != nil || since != 0 { |
| 43 | + t.Fatalf("precondition: offer clock must be unseeded, got %d, %v", since, err) |
| 44 | + } |
| 45 | + |
| 46 | + live := newMonitoringLiveFn(ctx, set, nil) |
| 47 | + if live() { |
| 48 | + t.Fatal("fresh install with a loaded controller and an unseeded offer clock must HOLD, not probe (consent bypass)") |
| 49 | + } |
| 50 | + // Nothing may have latched in that window: once the clock IS seeded (the |
| 51 | + // materialize step catching up), the normal 48h hold must be in force, not |
| 52 | + // a permanently released latch. |
| 53 | + if err := set.EnsureQuickSetupOffer(ctx, time.Now().Unix()); err != nil { |
| 54 | + t.Fatalf("EnsureQuickSetupOffer: %v", err) |
| 55 | + } |
| 56 | + if live() { |
| 57 | + t.Fatal("hold released during the unseeded window and LATCHED - the seeded offer clock no longer holds") |
| 58 | + } |
| 59 | + // Answering releases for real. |
| 60 | + if err := set.SetQuickSetupDone(ctx, true); err != nil { |
| 61 | + t.Fatalf("SetQuickSetupDone: %v", err) |
| 62 | + } |
| 63 | + if !live() { |
| 64 | + t.Fatal("monitoring must run once quick setup is answered") |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// The fail-closed branch must not over-hold: the same window state on an |
| 69 | +// install whose store already carries operator configuration (it consented |
| 70 | +// long ago; only the answered marker hasn't been materialized yet) keeps |
| 71 | +// monitoring running. |
| 72 | +func TestMonitoringLiveEstablishedInstallRunsWithUnseededOfferClock(t *testing.T) { |
| 73 | + ctx := context.Background() |
| 74 | + st, err := store.Open(":memory:") |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("open store: %v", err) |
| 77 | + } |
| 78 | + defer st.Close() |
| 79 | + // Prior operator configuration = established (see hasPriorConfiguration). |
| 80 | + if err := st.SetSetting(ctx, "monitoring", "true"); err != nil { |
| 81 | + t.Fatalf("seed config: %v", err) |
| 82 | + } |
| 83 | + set, err := settings.New(ctx, st, settings.Values{Monitoring: true}) |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("settings: %v", err) |
| 86 | + } |
| 87 | + if set.QuickSetupDone() { |
| 88 | + t.Fatal("precondition: quick setup must be unanswered") |
| 89 | + } |
| 90 | + if !newMonitoringLiveFn(ctx, set, nil)() { |
| 91 | + t.Fatal("established install must keep monitoring through the unseeded-clock window") |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// The recovery paths (settings-retry loop, SIGHUP) must seed the offer clock |
| 96 | +// BEFORE calling Reload: Reload itself flips Loaded()==true and broadcasts |
| 97 | +// the wake, so a post-Reload seed leaves the latch window open. This drives |
| 98 | +// the pre-seed against a REAL unloaded controller on a REAL working store (a |
| 99 | +// canceled context makes New's initial read fail exactly like a transient |
| 100 | +// store fault, and a live context recovers it). |
| 101 | +func TestQuickSetupOfferPreSeedRunsWhileUnloaded(t *testing.T) { |
| 102 | + st, err := store.Open(":memory:") |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("open store: %v", err) |
| 105 | + } |
| 106 | + defer st.Close() |
| 107 | + dead, cancel := context.WithCancel(context.Background()) |
| 108 | + cancel() |
| 109 | + set, err := settings.New(dead, st, settings.Values{Monitoring: true}) |
| 110 | + if err == nil { |
| 111 | + t.Fatal("settings.New must fail on the canceled context (simulated failed first load)") |
| 112 | + } |
| 113 | + if set.Loaded() { |
| 114 | + t.Fatal("precondition: controller must be unloaded") |
| 115 | + } |
| 116 | + |
| 117 | + ctx := context.Background() |
| 118 | + p := &program{log: slog.New(slog.NewTextHandler(io.Discard, nil))} |
| 119 | + p.seedQuickSetupOfferEarly(ctx, set) |
| 120 | + if set.Loaded() { |
| 121 | + t.Fatal("pre-seed must not flip Loaded - only Reload may") |
| 122 | + } |
| 123 | + if since, err := set.QuickSetupOfferSinceErr(ctx); err != nil { |
| 124 | + t.Fatalf("offer clock read: %v", err) |
| 125 | + } else if since == 0 { |
| 126 | + t.Fatal("pre-seed left the offer clock unseeded on a fresh install - the latch window before materializeQuickSetup stays open") |
| 127 | + } |
| 128 | + // The recovery Reload now finds the clock already there: the hold is in |
| 129 | + // force from the first woken round. |
| 130 | + if err := set.Reload(ctx); err != nil { |
| 131 | + t.Fatalf("Reload: %v", err) |
| 132 | + } |
| 133 | + if newMonitoringLiveFn(ctx, set, nil)() { |
| 134 | + t.Fatal("hold must be in force after a recovered first load") |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// The pre-seed's placement lives in run()'s SIGHUP goroutine and in |
| 139 | +// retrySettingsLoad's loop, neither unit-testable end-to-end - the same gap |
| 140 | +// TestMainWiresFirstRunAndOptsHooks covers, with the same deliberately literal |
| 141 | +// remedy: in both recovery paths the pre-seed must appear, and appear BEFORE |
| 142 | +// the set.Reload call it protects. |
| 143 | +func TestRecoveryPathsPreSeedBeforeReload(t *testing.T) { |
| 144 | + src, err := os.ReadFile("main.go") |
| 145 | + if err != nil { |
| 146 | + t.Fatal(err) |
| 147 | + } |
| 148 | + s := string(src) |
| 149 | + for _, tc := range []struct{ name, anchor string }{ |
| 150 | + {"settings-retry loop", "func (p *program) retrySettingsLoad"}, |
| 151 | + {"SIGHUP handler", "case <-hup:"}, |
| 152 | + } { |
| 153 | + i := strings.Index(s, tc.anchor) |
| 154 | + if i < 0 { |
| 155 | + t.Fatalf("%s: anchor %q not found in main.go", tc.name, tc.anchor) |
| 156 | + } |
| 157 | + rest := s[i:] |
| 158 | + j := strings.Index(rest, "set.Reload(") |
| 159 | + if j < 0 { |
| 160 | + t.Fatalf("%s: no set.Reload call after %q", tc.name, tc.anchor) |
| 161 | + } |
| 162 | + if !strings.Contains(rest[:j], "seedQuickSetupOfferEarly") { |
| 163 | + t.Errorf("%s: seedQuickSetupOfferEarly must run BEFORE set.Reload - Reload flips Loaded and broadcasts, and a post-Reload seed reopens the consent-hold latch window", tc.name) |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments