package api_test import ( "context" "net/http" "strings" "testing" "time" "github.com/google/uuid" "git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi" ) func TestMaintenance(t *testing.T) { e := newEnv(t) token, region := e.platformChecker(t, uniq("eu-")) client := agentapi.NewClient(e.srv.URL, token, uuid.New(), "test").WithHTTPClient(e.srv.Client()) owner := e.browser(t) owner.signup("o@"+uniq("mt")+".test", uniq("mt-")) infra := owner.group("Infra", "") db := owner.group("Databases", infra) other := owner.group("Other", "") st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{ "name": "Postgres", "type": "tcp", "target": "db.example.com:5432", "interval_seconds": 3600, "confirmations": 1, "regions": []string{region}, "group_id": db, }) if st != http.StatusCreated { t.Fatalf("create monitor = %d %v", st, body) } monitor := uuid.MustParse(body["id"].(string)) now := time.Now().UTC() window := map[string]any{ "title": "DB upgrade", "description": "Upgrade to **Postgres 18**.", "start": now.Add(-10 * time.Minute).Format("2006-01-02T15:04"), "time_zone": "UTC", "duration_minutes": 60, "group_ids": []string{infra}, } for name, tc := range map[string]struct { change map[string]any code string }{ "time zone": {map[string]any{"time_zone": "Mars/Olympus"}, "invalid_time_zone"}, "no targets": {map[string]any{"group_ids": []string{}}, "maintenance_without_targets"}, "overlapping": {map[string]any{"recurrence": "daily", "duration_minutes": 25 * 60}, "maintenance_overlaps"}, } { req := map[string]any{} for k, v := range window { req[k] = v } for k, v := range tc.change { req[k] = v } st, body := owner.do("POST", "/api/v1/org/maintenance", req) expect(t, name, st, body, http.StatusUnprocessableEntity, tc.code) } st, body = owner.do("POST", "/api/v1/org/maintenance", window) if st != http.StatusCreated || body["active"] != true { t.Fatalf("create maintenance = %d %v", st, body) } windowID := body["id"].(string) // A failure during maintenance of a parent group opens no incident. report := func(ok bool) { t.Helper() if err := client.Report(context.Background(), []agentapi.Result{{MonitorID: monitor, ScheduledAt: time.Now(), StartedAt: time.Now(), OK: ok}}); err != nil { t.Fatal(err) } } report(false) report(false) if _, inc := owner.do("GET", "/api/v1/org/incidents", nil); len(inc["incidents"].([]any)) != 0 { t.Fatalf("incident opened during maintenance: %v", inc) } _, _, dash := owner.page("/") if !strings.Contains(dash, `title="maintenance"`) && !strings.Contains(dash, `title="Wartung"`) { t.Error("dashboard does not show the monitor under maintenance") } if _, _, page := owner.page("/maintenance"); !strings.Contains(page, "Postgres 18") { t.Error("maintenance page does not render the Markdown description") } // Status pages show the component under maintenance. slug := uniq("mtp-") _, sp := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": slug, "title": "Acme", "description": "See [docs](https://docs.example)"}) pageID := sp["id"].(string) _, m2 := owner.do("POST", "/api/v1/org/monitors", map[string]any{ "name": "Replica", "type": "tcp", "target": "db2.example.com:5432", "interval_seconds": 3600, "regions": []string{region}, "group_id": db, }) owner.do("PUT", "/api/v1/org/statuspages/"+pageID+"/monitors", map[string]any{"monitors": []map[string]any{ {"monitor_id": monitor.String(), "section": "", "label": "Database"}, {"monitor_id": m2["id"].(string), "section": "", "label": "Replica"}, }}) owner.do("PUT", "/api/v1/org/statuspages/"+pageID, map[string]any{"slug": slug, "title": "Acme", "description": "See [docs](https://docs.example)", "published": true}) snap, err := e.statuspageClient(t, nil).Page(context.Background(), "", slug) if err != nil { t.Fatal(err) } // One occurrence covering two components is listed once. if len(snap.Maintenance) != 1 || strings.Join(snap.Maintenance[0].Components, ",") != "Database,Replica" { t.Fatalf("snapshot maintenance = %+v", snap.Maintenance) } if got := snap.WithMaintenance(time.Now()); got.Status != agentapi.PageUnderMaintenance || got.Sections[0].Components[0].Status != agentapi.PageUnderMaintenance { t.Errorf("WithMaintenance() = %s / %+v", got.Status, got.Sections) } if got := snap.WithMaintenance(time.Now().Add(2 * time.Hour)); got.Status == agentapi.PageUnderMaintenance { t.Error("page still under maintenance after it ended") } // Members limited to another group may not plan maintenance here. scopedEmail := "s@" + uniq("mt") + ".test" scoped := e.joinVia(t, owner.invite(scopedEmail, owner.roleID("viewer"))) st, body = owner.do("POST", "/api/v1/org/members/"+owner.userID(scopedEmail)+"/roles", map[string]any{ "role_id": owner.roleID("editor"), "resource_type": "monitor_group", "resource_id": other, }) expect(t, "assign scoped editor", st, body, http.StatusCreated, "") st, body = scoped.do("POST", "/api/v1/org/maintenance", window) expect(t, "scoped editor targets foreign group", st, body, http.StatusForbidden, "forbidden") st, body = scoped.do("DELETE", "/api/v1/org/maintenance/"+windowID, nil) expect(t, "scoped editor deletes foreign maintenance", st, body, http.StatusForbidden, "forbidden") w2 := map[string]any{} for k, v := range window { w2[k] = v } w2["group_ids"] = []string{other} w2["recurrence"], w2["weekdays"] = "weekly", []string{"1", "4"} st, body = scoped.do("POST", "/api/v1/org/maintenance", w2) if st != http.StatusCreated || body["next"] == nil { t.Errorf("scoped editor plans weekly maintenance for own group = %d %v", st, body) } // Without maintenance the failure alerts again. st, body = owner.do("DELETE", "/api/v1/org/maintenance/"+windowID, nil) expect(t, "delete maintenance", st, body, http.StatusNoContent, "") report(false) if _, inc := owner.do("GET", "/api/v1/org/incidents", nil); len(inc["incidents"].([]any)) != 1 { t.Errorf("no incident after the maintenance ended: %v", inc) } } func TestEditMaintenanceReplacesTargets(t *testing.T) { e := newEnv(t) _, region := e.platformChecker(t, uniq("eu-")) owner := e.browser(t) owner.signup("o@"+uniq("me")+".test", uniq("me-")) a, b := owner.group("A", ""), owner.group("B", "") _, m := owner.do("POST", "/api/v1/org/monitors", map[string]any{ "name": "M", "type": "tcp", "target": "example.com:443", "interval_seconds": 3600, "regions": []string{region}, }) req := map[string]any{ "title": "Patch", "start": "2030-01-01T02:00", "time_zone": "Europe/Berlin", "duration_minutes": 30, "recurrence": "monthly", "until": "2030-12-31", "group_ids": []string{a}, } _, body := owner.do("POST", "/api/v1/org/maintenance", req) id := body["id"].(string) req["group_ids"], req["monitor_ids"], req["title"] = []string{b}, []string{m["id"].(string)}, "Patch v2" st, body := owner.do("PUT", "/api/v1/org/maintenance/"+id, req) if st != http.StatusOK || body["title"] != "Patch v2" { t.Fatalf("update = %d %v", st, body) } _, list := owner.do("GET", "/api/v1/org/maintenance", nil) got := list["maintenance"].([]any)[0].(map[string]any) if mustJSON(got["group_ids"]) != `["`+b+`"]` || len(got["monitor_ids"].([]any)) != 1 || got["until"] != "2030-12-31" { t.Errorf("after update = %v", got) } next, _ := time.Parse(time.RFC3339, got["next"].(map[string]any)["start"].(string)) if want := time.Date(2030, 1, 1, 1, 0, 0, 0, time.UTC); !next.Equal(want) { t.Errorf("next start = %v, want 02:00 Berlin = %v", next, want) } if _, _, page := owner.page("/maintenance?edit=" + id); !strings.Contains(page, `value="Patch v2"`) { t.Error("edit form is not prefilled") } }