package main import ( "net/http" "net/url" "regexp" "strings" "testing" ) func TestApplyExporter(t *testing.T) { s := mustParse(t) if f := s.exporter(); f.Enabled || f.Port != "9100" || len(f.Zones) != 0 { t.Fatalf("defaults: %+v", f) } for _, c := range []struct { name string form ExporterForm bad string // the field that is refused }{ {"port 0", ExporterForm{Port: "0"}, "port"}, {"not a number", ExporterForm{Port: "nine"}, "port"}, {"the web UI's port", ExporterForm{Enabled: true, Port: "443"}, "port"}, {"the internet", ExporterForm{Enabled: true, Port: "9100", Zones: []string{"wan"}}, "zones"}, {"a zone that does not exist", ExporterForm{Enabled: true, Port: "9100", Zones: []string{"nowhere"}}, "zones"}, } { err := mustParse(t).applyExporter(c.form) fe, ok := err.(FormErrors) if !ok || fe[c.bad] == "" { t.Errorf("%s: %v", c.name, err) } } if err := s.applyExporter(ExporterForm{Enabled: true, Port: "9200", Zones: []string{"main", "vpn"}}); err != nil { t.Fatal(err) } f := s.exporter() if !f.Enabled || f.Port != "9200" || strings.Join(f.Zones, ",") != "main,vpn" { t.Fatalf("saved: %+v", f) } // The default port is not written, and a form that says nothing removes the section. if err := s.applyExporter(ExporterForm{Enabled: true, Port: "9100", Zones: []string{"main"}}); err != nil { t.Fatal(err) } if n := obj(obj(obj(s, "router"), "metrics"), "nodeExporter"); n["port"] != nil || n["enable"] != true { t.Fatalf("%v", n) } if err := s.applyExporter(ExporterForm{Port: ""}); err != nil { t.Fatal(err) } if _, ok := obj(s, "router")["metrics"]; ok { t.Fatalf("an empty section is left behind: %v", obj(s, "router")) } } func TestExporterPage(t *testing.T) { a, sys := newTestApp(t) h := a.routes() cookie := do(h, "POST", "/login", url.Values{"password": {"hunter2hunter2"}}, nil).Result().Cookies()[0] body := do(h, "GET", "/prometheus", nil, cookie).Body.String() for _, want := range []string{`value="main"`, `value="vpn"`, `/metrics`, "node_exporter", `href="/prometheus" aria-current="page"`} { if !strings.Contains(body, want) { t.Errorf("the page lacks %q:\n%s", want, body) } } if strings.Contains(body, `value="wan"`) { t.Error("wan is offered") } token := regexp.MustCompile(`name="csrf" value="([^"]+)"`).FindStringSubmatch(body)[1] form := url.Values{"csrf": {token}, "enabled": {"on"}, "port": {"9100"}, "zone": {"nowhere"}} if rec := do(h, "POST", "/prometheus", form, cookie); rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), "unknown zone nowhere") { t.Fatalf("unknown zone: %d\n%s", rec.Code, rec.Body) } form["zone"] = []string{"main", "vpn"} if rec := do(h, "POST", "/prometheus", form, cookie); rec.Code != http.StatusSeeOther { t.Fatalf("save: %d\n%s", rec.Code, rec.Body) } s, _ := loadSettings(sys.settingsPath()) if f := s.exporter(); !f.Enabled || strings.Join(f.Zones, ",") != "main,vpn" { t.Fatalf("not saved: %+v", f) } body = do(h, "GET", "/prometheus", nil, cookie).Body.String() if !strings.Contains(body, `value="main" checked`) || !strings.Contains(body, "router-node") { t.Fatalf("the saved choice is not shown:\n%s", body) } }