ration/pkg/metrics/metrics_test.go
caelum0x 13abc31645 feat(ration): observability, infra-as-code, security hardening, and test depth
Phase 2 sections 2-6 and 8 of docs/SCALE-EXPANSION.md:

- pkg/telemetry + pkg/metrics: OpenTelemetry trace propagation and
  Prometheus metrics across every service hop (CLI -> agent -> gateway ->
  card-issuance/tenant-registry/auth -> ledger-core), tracing added to
  ledger-core itself via services/ledger-core/src/telemetry.rs.
- infra/: otel-collector + Prometheus + Grafana docker-compose stack, a
  Terraform ECS Fargate module + dev/staging/prod envs (plan-only, no
  apply — real AWS spend is an owner action), and a dev-only mTLS CA
  (infra/dev-ca) wired through pkg/mtls + services/ledger-core/src/mtls.rs.
- services/audit-log (from the prior commit) gets its async mirror writer
  in services/ledger-core/src/audit_mirror.rs, and services/gateway gets
  a per-tenant token-bucket rate limiter (internal/ratelimit).
- Test depth: a Rust proptest suite proving concurrent ApplyEntry never
  breaches the reserve floor (tests/apply_entry_proptest.rs, passing), a Go
  fuzz seed corpus for the policy envelope (rules_fuzz_test.go), a libFuzzer
  harness for the C WAL frame parser (services/wal/fuzz, deliberately not
  wired into `make test` per its own Makefile comment - needs a
  libFuzzer-bundled clang), and a real chaos test
  (scripts/chaos/kill-ledger-mid-write.sh) that SIGKILLs ledger-core
  mid-write under concurrent load and verifies via ListEntries that
  nothing was lost or double-applied.
- docs/formal/ledger.tla: a TLA+ spec of the ledger state machine (balance
  >= reserve_floor safety, HOLD-eventually-resolves liveness) plus a TLC
  model config - not run through TLC in this pass, that's a precise
  documented TODO, not faked.
- docs/adr/0001-0004, docs/RUNBOOK.md, docs/SLO.md, docs/THREAT-MODEL.md.

Verified: `make build && make test` clean (C WAL, Rust incl. the new
proptest suite, all 10 Go modules) from a cold target.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-19 12:25:58 +03:00

74 lines
2.3 KiB
Go

package metrics
import (
"context"
"net/http/httptest"
"strings"
"testing"
"google.golang.org/grpc"
)
func TestBridgeGrantsTotalIncrementsAndExports(t *testing.T) {
m := New("test-service")
m.BridgeGrantsTotal.WithLabelValues("granted").Inc()
m.BridgeGrantsTotal.WithLabelValues("granted").Inc()
m.BridgeGrantsTotal.WithLabelValues("declined").Inc()
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/metrics", nil)
m.Handler().ServeHTTP(rec, req)
body := rec.Body.String()
if !strings.Contains(body, `ration_bridge_grants_total{result="granted",service="test-service"} 2`) {
t.Fatalf("expected granted=2 in /metrics output, got:\n%s", body)
}
if !strings.Contains(body, `ration_bridge_grants_total{result="declined",service="test-service"} 1`) {
t.Fatalf("expected declined=1 in /metrics output, got:\n%s", body)
}
}
func TestReserveFloorHeadroomGaugeSetsPerTenant(t *testing.T) {
m := New("test-service")
m.ReserveFloorHeadroom.WithLabelValues("tenant-a").Set(12345)
m.ReserveFloorHeadroom.WithLabelValues("tenant-b").Set(0)
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/metrics", nil)
m.Handler().ServeHTTP(rec, req)
body := rec.Body.String()
if !strings.Contains(body, `ration_reserve_floor_headroom_minor_units{service="test-service",tenant_id="tenant-a"} 12345`) {
t.Fatalf("expected tenant-a headroom in /metrics output, got:\n%s", body)
}
}
func TestUnaryServerInterceptorRecordsSuccessAndFailure(t *testing.T) {
m := New("test-service")
interceptor := m.UnaryServerInterceptor()
okHandler := func(ctx context.Context, req any) (any, error) { return "ok", nil }
if _, err := interceptor(context.Background(), nil, &grpc.UnaryServerInfo{FullMethod: "/svc/Ok"}, okHandler); err != nil {
t.Fatalf("unexpected error from ok handler: %v", err)
}
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/metrics", nil)
m.Handler().ServeHTTP(rec, req)
body := rec.Body.String()
if !strings.Contains(body, `method="/svc/Ok"`) {
t.Fatalf("expected a request_total series for /svc/Ok, got:\n%s", body)
}
}
func TestServerMountsMetricsEndpoint(t *testing.T) {
m := New("test-service")
srv := m.Server("127.0.0.1:0")
if srv.Handler == nil {
t.Fatal("Server() returned an http.Server with no handler")
}
}