| index 08543cbfd..ac95a3f01 100644 |
| --- a/internal/host/telemetry_pass_test.go |
| +++ b/internal/host/telemetry_pass_test.go |
| @@ -4,16 +4,169 @@ import ( |
| "context" |
| "errors" |
| "maps" |
| + "net/http" |
| + "net/http/httptest" |
| + "os" |
| + "path/filepath" |
| "slices" |
| "sync" |
| "testing" |
| "time" |
| |
| + "github.com/carlosframework/platform/internal/analytics" |
| + "github.com/carlosframework/platform/internal/edge" |
| "github.com/carlosframework/platform/internal/registry" |
| "github.com/carlosframework/platform/internal/release" |
| "github.com/carlosframework/platform/internal/telemetry" |
| ) |
| |
| +type telemetryBlockingStore struct { |
| + release.Store |
| + key string |
| + entered chan struct{} |
| + resume chan struct{} |
| +} |
| + |
| +func (s *telemetryBlockingStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + if key == s.key { |
| + close(s.entered) |
| + select { |
| + case <-s.resume: |
| + case <-ctx.Done(): |
| + return nil, "", ctx.Err() |
| + } |
| + } |
| + return s.Store.Get(ctx, key) |
| +} |
| + |
| +type telemetryObservations struct{ obs []analytics.Obs } |
| + |
| +func (m *telemetryObservations) Observe(ob analytics.Obs) { m.obs = append(m.obs, ob) } |
| + |
| +func TestTelemetryPassRecordOnlyRejectsOlderPublication(t *testing.T) { |
| + for _, armed := range []bool{false, true} { |
| + name := "disarmed" |
| + if armed { |
| + name = "rearmed" |
| + } |
| + t.Run(name, func(t *testing.T) { |
| + t.Setenv("CARLOS_EDGE_PROTECT", "block") |
| + ctx, cancel := context.WithCancel(context.Background()) |
| + defer cancel() |
| + s := newCICountingStore(t) |
| + reg := edgesTestRegistry(t) |
| + if err := reg.Upsert(registry.Route{ |
| + Host: "shop.example.com", Kind: "static", Account: "bab", App: "shop", Status: "ready", |
| + }); err != nil { |
| + t.Fatal(err) |
| + } |
| + e, err := edge.New(reg, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + root := t.TempDir() |
| + if err := os.MkdirAll(filepath.Join(root, "shop", "current"), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + edge.WithLocalStatic(root)(e) |
| + if err := e.Reload(); err != nil { |
| + t.Fatal(err) |
| + } |
| + meter := &telemetryObservations{} |
| + e.EnableAnalytics(meter) |
| + prevA, prevP, prevO := analyticsSink.Load(), protectionSink.Load(), assetOwnersSource.Load() |
| + prevW := armedChangeWatch.Load() |
| + t.Cleanup(func() { |
| + analyticsSink.Store(prevA) |
| + protectionSink.Store(prevP) |
| + assetOwnersSource.Store(prevO) |
| + armedChangeWatch.Store(prevW) |
| + recordOnlyLast.reset() |
| + }) |
| + recordOnlyLast.reset() |
| + setAnalyticsSink(e.SetAnalyticsOn) |
| + setProtectionSink(e.SetProtectionRecordOnly) |
| + setAssetOwnersSource(func() []registry.Route { |
| + return []registry.Route{{Account: "bdk", App: "cart"}} |
| + }) |
| + armedChangeWatch.Store(nil) |
| + if armed { |
| + armedChangeWatch.Store(release.NewChangeWatch(s)) |
| + } |
| + setOn := func(on bool) { |
| + t.Helper() |
| + if err := telemetry.Update(ctx, s, telemetry.AppKey("bab", "shop"), func(st *telemetry.Settings) { |
| + st.Analytics = &telemetry.Analytics{Count: telemetry.CountOff} |
| + st.Protection = nil |
| + if on { |
| + st.Analytics.Count = telemetry.CountEdge |
| + st.Protection = &telemetry.Protection{Mode: telemetry.ProtectRecordOnly} |
| + } |
| + }); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + setOn(false) |
| + // The far asset read follows shop's read, so the older pass holds |
| + // shop's off values while the newer pass reads and publishes on. |
| + blocked := &telemetryBlockingStore{ |
| + Store: s, key: telemetry.AppKey("bdk", "cart"), |
| + entered: make(chan struct{}), resume: make(chan struct{}), |
| + } |
| + done := make(chan struct{}) |
| + go func() { |
| + defer close(done) |
| + telemetryPass(ctx, blocked, reg) |
| + }() |
| + defer func() { |
| + cancel() |
| + <-done |
| + }() |
| + <-blocked.entered |
| + setOn(true) |
| + if armed { |
| + // Replacing the watch gives the newer pass its own reader; |
| + // publication ordering must outlive either reader's mutex. |
| + armedChangeWatch.Store(release.NewChangeWatch(s)) |
| + } |
| + telemetryPass(ctx, s, reg) |
| + assertOn := func() { |
| + t.Helper() |
| + w := httptest.NewRecorder() |
| + r := httptest.NewRequest("GET", "http://shop.example.com/.env", nil) |
| + n := len(meter.obs) |
| + e.ServeHTTP(w, r) |
| + if w.Code != http.StatusNotFound || w.Body.String() != "404 page not found\n" || w.Header().Get("X-Carlos-Ref") != "" { |
| + t.Errorf("record-only request = %d %q, want static 404 without a protection reference", w.Code, w.Body.String()) |
| + } |
| + if len(meter.obs) != n+1 { |
| + t.Errorf("analytics observations = %d, want %d", len(meter.obs), n+1) |
| + } |
| + recordOnlyLast.mu.Lock() |
| + remembered := recordOnlyLast.m["bab/shop"] |
| + recordOnlyLast.mu.Unlock() |
| + if !remembered { |
| + t.Error("remembered record-only value is off, want on") |
| + } |
| + } |
| + assertOn() |
| + close(blocked.resume) |
| + <-done |
| + assertOn() |
| + // A failed read must reuse the newer remembered value too. |
| + armedChangeWatch.Store(nil) |
| + failed := &keysFailingStore{ciCountingStore: s} |
| + failed.failing(telemetry.AppKey("bab", "shop")) |
| + telemetryPass(ctx, failed, reg) |
| + w := httptest.NewRecorder() |
| + e.ServeHTTP(w, httptest.NewRequest("GET", "http://shop.example.com/.env", nil)) |
| + if w.Code != http.StatusNotFound || w.Body.String() != "404 page not found\n" || w.Header().Get("X-Carlos-Ref") != "" { |
| + t.Errorf("failed read undid record-only: %d %q", w.Code, w.Body.String()) |
| + } |
| + }) |
| + } |
| +} |
| + |
| func TestTelemetrySwitchesResolvePerRoute(t *testing.T) { |
| ctx := context.Background() |
| s := release.NewDirStore(t.TempDir()) |