package api_test import ( "context" "net/http" "net/http/httptest" "strconv" "strings" "sync/atomic" "testing" "time" ) func TestJSONCheck(t *testing.T) { e := newEnv(t) ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) go e.api.RunScheduler(ctx) var depth atomic.Int64 depth.Store(3) api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("X-Api-Key") != "s3cret-key" { w.WriteHeader(http.StatusUnauthorized) return } w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"status":"ok","queue":{"depth":` + itoa(depth.Load()) + `}}`)) })) defer api.Close() tok, region := e.platformChecker(t, uniq("eu-")) e.startChecker(t, tok) owner := e.browser(t) owner.signup("o@"+uniq("jc")+".test", uniq("jc-")) st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{ "name": "Queue", "type": "json", "target": api.URL, "interval_seconds": 10, "timeout_ms": 2000, "confirmations": 1, "regions": []string{region}, "config": map[string]any{ "headers": map[string]string{"X-Api-Key": "s3cret-key"}, "assertions": []map[string]string{ {"path": "status", "op": "==", "value": "ok"}, {"path": "queue.depth", "op": "<", "value": "10"}, {"path": "", "op": "==", "value": ""}, }, }, }) if st != http.StatusCreated { t.Fatalf("create json monitor = %d %v", st, body) } latest := func() map[string]any { _, list := owner.do("GET", "/api/v1/org/monitors", nil) for _, m := range list["monitors"].([]any) { if l := m.(map[string]any)["latest"].([]any); len(l) > 0 { return l[0].(map[string]any) } } return nil } waitFor(t, 15*time.Second, "a successful JSON check", func() bool { l := latest() return l != nil && l["ok"] == true }) depth.Store(42) waitFor(t, 25*time.Second, "a failed assertion", func() bool { l := latest() return l != nil && l["ok"] == false && strings.Contains(asString(l["error"]), "queue.depth is 42, expected < 10") }) // Readers see the condition, not the credential. viewer := e.joinVia(t, owner.invite("v@"+uniq("jc")+".test", owner.roleID("viewer"))) _, vl := viewer.do("GET", "/api/v1/org/monitors", nil) if raw := mustJSON(vl); strings.Contains(raw, "s3cret-key") || !strings.Contains(raw, "queue.depth") { t.Errorf("viewer sees %s", raw) } if _, ol := owner.do("GET", "/api/v1/org/monitors", nil); !strings.Contains(mustJSON(ol), "s3cret-key") { t.Error("the editor does not see the header to edit it") } } func itoa(n int64) string { return strconv.FormatInt(n, 10) } func asString(v any) string { s, _ := v.(string) return s } func TestPrometheusMonitor(t *testing.T) { e := newEnv(t) ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) go e.api.RunScheduler(ctx) prom := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte(`{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"api"},"value":[1,"0"]}]}}`)) })) defer prom.Close() tok, region := e.platformChecker(t, uniq("eu-")) e.startChecker(t, tok) owner := e.browser(t) owner.signup("o@"+uniq("pm")+".test", uniq("pm-")) st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{ "name": "API targets", "type": "prometheus", "target": prom.URL, "interval_seconds": 10, "timeout_ms": 2000, "confirmations": 1, "regions": []string{region}, "config": map[string]any{"query": `up{job="api"}`, "op": "==", "threshold": 1}, }) if st != http.StatusCreated { t.Fatalf("create prometheus monitor = %d %v", st, body) } waitFor(t, 15*time.Second, "an incident for the down target", func() bool { _, b := owner.do("GET", "/api/v1/org/incidents", nil) return len(b["incidents"].([]any)) == 1 }) _, list := owner.do("GET", "/api/v1/org/monitors", nil) if !strings.Contains(mustJSON(list), `up{job=\"api\"} = 0, expected == 1`) { t.Errorf("result lacks the failing series: %s", mustJSON(list)) } }