package api_test import ( "context" "net/http" "strings" "testing" "time" "github.com/google/uuid" "github.com/jackc/pgx/v5" "git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi" "git.zb-server.de/ZB-Server/uptimesebi/internal/config" "git.zb-server.de/ZB-Server/uptimesebi/internal/notify/notifytest" ) func TestUncheckedMonitorsAndRegionsWithoutChecker(t *testing.T) { mail := notifytest.NewSMTP(t) e := newEnvWith(t, func(c *config.API) { c.SMTPURL, c.SignupVerify, c.RegionAlertAfter = mail.URL(), "off", 3*time.Minute }) ctx := context.Background() owner := e.browser(t) ownerEmail := "o@" + uniq("rw") + ".test" owner.signup(ownerEmail, uniq("rw-")) // An organization's own region: its owners hear about it. regionName := uniq("Office ") _, reg := owner.do("POST", "/api/v1/org/regions", map[string]any{"key": uniq("office-"), "name": regionName}) region := reg["id"].(string) regionID := uuid.MustParse(region) st, agent := owner.do("POST", "/api/v1/org/agents", map[string]any{"kind": "checker", "name": "office-1", "region_id": region}) if st != http.StatusCreated { t.Fatalf("create agent = %d %v", st, agent) } tok := agent["token"].(string) mailed := func(to, want string) { t.Helper() waitFor(t, 5*time.Second, "a mail saying "+want, func() bool { for _, m := range mail.Messages() { if strings.Contains(strings.Join(m.To, ","), to) && strings.Contains(m.Data, want) && strings.Contains(m.Data, regionName) { return true } } return false }) } // A monitor whose only region last reported ten minutes ago is not // up: nobody is checking it. _, mon := owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": "Shop", "type": "http", "target": "https://example.test", "interval_seconds": 60, "regions": []string{region}}) id := mon["id"].(string) org := uuid.MustParse(owner.orgID()) if err := e.asSystem(t, func(tx pgx.Tx) error { if _, err := tx.Exec(ctx, `UPDATE monitors SET created_at = now() - interval '1 hour' WHERE id = $1`, id); err != nil { return err } _, err := tx.Exec(ctx, `INSERT INTO check_results (time, org_id, monitor_id, region, checker_id, ok, latency_ms) VALUES (now() - interval '10 minutes', $1, $2, $3, $4, true, 10)`, org, id, region, uuid.New()) return err }); err != nil { t.Fatal(err) } _, _, dash := owner.page("/") for _, want := range []string{"not being checked", "1 monitor is not being checked"} { if !strings.Contains(dash, want) { t.Errorf("dashboard lacks %q", want) } } if _, _, detail := owner.page("/monitors/" + id); !strings.Contains(detail, "no data since") || !strings.Contains(detail, "1 monitor is not being checked") { t.Error("the monitor page does not say the region has no recent data") } // On a status page it shows as unknown, not as operational. slug := uniq("rw-") _, page := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": slug, "title": "Shop status"}) owner.do("PUT", "/api/v1/org/statuspages/"+page["id"].(string)+"/monitors", map[string]any{"monitors": []map[string]any{{"monitor_id": id}}}) owner.do("PUT", "/api/v1/org/statuspages/"+page["id"].(string), map[string]any{"slug": slug, "title": "Shop status", "published": true}) snap, err := e.statuspageClient(t, &org).Page(ctx, "", slug) if err != nil || len(snap.Sections) != 1 || snap.Sections[0].Components[0].Status != agentapi.PageUnknown { t.Errorf("status page = %+v, %v; want the component unknown", snap.Sections, err) } // The region has a checker but no live instance: the watch notes it at // once and tells the owners once it has lasted. now := time.Now() if err := e.api.WatchRegionsOf(ctx, now, regionID); err != nil { t.Fatal(err) } if _, _, dash := owner.page("/"); !strings.Contains(dash, "Region "+regionName+" has had no active checker since") { t.Error("the dashboard does not name the region without a checker") } if err := e.api.WatchRegionsOf(ctx, now.Add(5*time.Minute), regionID); err != nil { t.Fatal(err) } mailed(ownerEmail, "has no active checker") sent := len(mail.Messages()) if err := e.api.WatchRegionsOf(ctx, now.Add(10*time.Minute), regionID); err != nil { t.Fatal(err) } time.Sleep(200 * time.Millisecond) if n := countMails(mail.Messages()[sent:], regionName); n != 0 { t.Errorf("the region was announced %d more times", n) } // A checker coming back is announced too. e.startChecker(t, tok) waitFor(t, 15*time.Second, "the checker to count as live", func() bool { _ = e.api.WatchRegionsOf(ctx, time.Now(), regionID) for _, m := range mail.Messages() { if strings.Contains(m.Data, "is checking again") && strings.Contains(m.Data, regionName) { return true } } return false }) if _, _, dash := owner.page("/"); strings.Contains(dash, "Region "+regionName+" has had no active checker") { t.Error("the dashboard still names the region") } } func countMails(ms []notifytest.Message, about string) int { n := 0 for _, m := range ms { if strings.Contains(m.Data, about) { n++ } } return n }