spb/internetpressure
Public
TypeScript 36.3%
Python 31.8%
Go 18%
JavaScript 9.8%
Shell 1.9%
SQL 1.4%
CSS 0.5%
1package health23import (4 "encoding/json"5 "net/http/httptest"6 "strings"7 "testing"89 "internetpressure.io/probe-agent/internal/protocol"10)1112func TestEndpoints(t *testing.T) {13 s := New("ca-qc-01", "0.1.0", []string{"http", "dns"}, Gauges{14 Buffered: func() int { return 3 }, SpoolBytes: func() int64 { return 1024 },15 ClockOffsetMs: func() int64 { return -14 }, Targets: func() int { return 7 },16 })17 s.Record(protocol.Measurement{Kind: "http", OK: true})18 s.Record(protocol.Measurement{Kind: "dns", OK: false, Error: protocol.ErrDNSTimeout})19 s.Record(protocol.Measurement{Kind: "ping", OK: false})20 s.RecordFlushFailure()2122 rr := httptest.NewRecorder()23 s.Handler().ServeHTTP(rr, httptest.NewRequest("GET", "/healthz", nil))24 var body map[string]any25 if err := json.Unmarshal(rr.Body.Bytes(), &body); err != nil {26 t.Fatal(err)27 }28 if body["ok"] != true || body["probe_id"] != "ca-qc-01" || body["buffered"].(float64) != 3 ||29 body["spool_bytes"].(float64) != 1024 || body["targets"].(float64) != 7 || body["clock_offset_ms"].(float64) != -14 {30 t.Fatalf("healthz: %v", body)31 }32 for _, k := range []string{"version", "last_flush", "last_config"} {33 if _, ok := body[k]; !ok {34 t.Errorf("healthz missing %s", k)35 }36 }3738 rr = httptest.NewRecorder()39 s.Handler().ServeHTTP(rr, httptest.NewRequest("GET", "/metrics", nil))40 m := rr.Body.String()41 for _, want := range []string{42 `ip_probe_measurements_total{kind="http"} 1`, `ip_probe_measurements_total{kind="dns"} 1`,43 `ip_probe_errors_total{code="dns_timeout"} 1`, `ip_probe_errors_total{code="other"} 1`,44 "ip_probe_buffered 3", "ip_probe_spool_bytes 1024", "ip_probe_flush_failures_total 1",45 "ip_probe_clock_offset_ms -14", "ip_probe_targets 7",46 } {47 if !strings.Contains(m, want) {48 t.Errorf("metrics missing %q\n%s", want, m)49 }50 }5152 h := s.Snapshot()53 if h.MeasurementsTotal != 3 || h.ErrorsTotal != 2 || h.Buffered != 3 || h.OS == "" || h.RSSMb <= 0 || h.Goroutines <= 0 {54 t.Fatalf("snapshot: %+v", h)55 }56}57