New namespaced CRD KumaStatusPage (uptime.renner.uno/v1alpha1) and a reconciler that owns the Kuma status page completely: config, domain names and the full group/monitor list. Sections select KumaMonitors in any namespace by label; monitors are ordered by displayName, then namespace/name, a monitor goes to the first matching section only, and monitors without a Kuma ID are left out and counted in status. The reconciler adopts an existing page with the slug, writes only when the managed content differs (no write per resync), reuses Kuma group IDs by position so saves do not churn them, reverts UI edits on resync, deletes the page on resource deletion, and reports Ready=False/Conflict for a younger resource claiming the same slug. Every KumaMonitor change to labels, displayName, monitor ID or deletion enqueues all pages. kumaclient gains a StatusPages interface (Breml + Fake). The socket API returns no group list, so groups are read from Kuma's public GET /api/status-page/<slug>, as the bootstrap did. Fields kuma-sync does not manage (logo, custom CSS, analytics, RSS title, auto refresh interval) are written back unchanged. autoRefreshInterval is not in the spec: go-uptime-kuma-client v0.6.2 (latest) never sends it on save. The Ansible bootstrap no longer creates or edits status pages; the existing page is left in place for kuma-sync to adopt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
68 lines
2.4 KiB
Go
68 lines
2.4 KiB
Go
package kumaclient
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
func TestStatusPageSameContent(t *testing.T) {
|
|
base := StatusPage{
|
|
ID: 1, Slug: "s", Title: "T", Theme: "auto", DomainNames: []string{"b.example", "a.example"},
|
|
Groups: []StatusGroup{{ID: 3, Name: "G", Monitors: []PageMonitor{{ID: 1}, {ID: 2}}}},
|
|
}
|
|
same := StatusPage{
|
|
ID: 9, Slug: "s", Title: "T", Theme: "auto", DomainNames: []string{"a.example", " b.example", "", "a.example"},
|
|
Groups: []StatusGroup{{ID: 0, Name: "G", Monitors: []PageMonitor{{ID: 1}, {ID: 2}}}},
|
|
}
|
|
if !base.SameContent(same) {
|
|
t.Error("IDs, domain order, blanks and duplicates must not count")
|
|
}
|
|
emptyGroup := StatusPage{Groups: []StatusGroup{{Name: "G", Monitors: []PageMonitor{}}}}
|
|
if !emptyGroup.SameContent(StatusPage{Groups: []StatusGroup{{Name: "G"}}}) {
|
|
t.Error("empty and nil monitor lists must be equal")
|
|
}
|
|
diffs := map[string]func(p *StatusPage){
|
|
"monitor order": func(p *StatusPage) { p.Groups[0].Monitors = []PageMonitor{{ID: 2}, {ID: 1}} },
|
|
"show url": func(p *StatusPage) { p.Groups[0].Monitors[0].ShowURL = true },
|
|
"group name": func(p *StatusPage) { p.Groups[0].Name = "H" },
|
|
"extra group": func(p *StatusPage) { p.Groups = append(p.Groups, StatusGroup{Name: "H"}) },
|
|
"domain": func(p *StatusPage) { p.DomainNames = []string{"a.example"} },
|
|
"footer": func(p *StatusPage) { p.FooterText = "f" },
|
|
"cert expiry": func(p *StatusPage) { p.ShowCertificateExpiry = true },
|
|
}
|
|
for name, mutate := range diffs {
|
|
p := base.clone()
|
|
mutate(&p)
|
|
if base.SameContent(p) {
|
|
t.Errorf("%s: change not detected", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTruthy(t *testing.T) {
|
|
for _, c := range []struct {
|
|
v any
|
|
want bool
|
|
}{{nil, false}, {false, false}, {true, true}, {float64(0), false}, {float64(1), true}, {"", false}} {
|
|
if got := truthy(c.v); got != c.want {
|
|
t.Errorf("truthy(%#v) = %v", c.v, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPageNotFound(t *testing.T) {
|
|
for _, msg := range []string{
|
|
"get status page s: getStatusPage: No slug?",
|
|
"save status page: saveStatusPage: No slug?",
|
|
"delete status page: deleteStatusPage: Status Page is not found",
|
|
} {
|
|
if err := pageNotFound("s", errors.New(msg)); !errors.Is(err, ErrStatusPageNotFound) {
|
|
t.Errorf("%q not mapped", msg)
|
|
}
|
|
}
|
|
other := errors.New("save status page: saveStatusPage: Invalid Slug")
|
|
if err := pageNotFound("s", other); errors.Is(err, ErrStatusPageNotFound) {
|
|
t.Error("other error mapped to not found")
|
|
}
|
|
}
|