package api_test import ( "net/url" "strings" "testing" "time" "github.com/v10networks/nmon/internal/api" ) func countersURL(id, kind string, from, to time.Time) string { return "/api/v1/graph/counters?iface=" + id + "&kind=" + kind + "&from=" + url.QueryEscape(iso(from)) + "&to=" + url.QueryEscape(iso(to)) } func TestGraphCountersQueryTheErrorCountersAsRates(t *testing.T) { e, i := newGraphEnv(t, &graphVM{hotVal: rising}) var g api.CounterGraphResponse rec := e.getJSON(t, countersURL(i.ID, "errors", t0.Add(-time.Hour), t0), &g) assertNoLeaks(t, "counters", rec.Body.Bytes()) if g.Kind != "errors" || g.Unit != "errors/s" || g.StepSeconds != 60 || g.Clamped || g.Iface.ID != i.ID { t.Fatalf("response: %+v", g) } if len(g.Series.In) != 61 || len(g.Series.Out) != 61 || g.Series.In[60].TS != t0.Unix() { t.Fatalf("series: %d in, %d out", len(g.Series.In), len(g.Series.Out)) } qs := e.q.queries() if len(qs) != 2 || qs[0].store != "hot" || qs[0].expr != `rate(ifInErrors{iface="`+i.ID+`"}[600s])` || qs[1].expr != `rate(ifOutErrors{iface="`+i.ID+`"}[600s])` { t.Errorf("queries: %+v", qs) } } func TestGraphCountersDiscardsAndHotOnlyClamp(t *testing.T) { e, i := newGraphEnv(t, &graphVM{hotVal: rising}) var g api.CounterGraphResponse e.getJSON(t, countersURL(i.ID, "discards", t0.Add(-90*24*time.Hour), t0), &g) if g.Kind != "discards" || !g.Clamped || !g.From.After(t0.Add(-90*24*time.Hour)) { t.Fatalf("a range older than hot retention must be clamped: %+v", g) } for _, q := range e.q.queries() { if q.store != "hot" || !strings.Contains(q.expr, "Discards") { t.Errorf("query: %+v", q) } } isProblem(t, e.do("GET", countersURL(i.ID, "packets", t0.Add(-time.Hour), t0), ""), 422) isProblem(t, e.do("GET", countersURL(i.ID, "errors", t0, t0.Add(-time.Hour)), ""), 422) isProblem(t, e.do("GET", countersURL("01ARZ3NDEKTSV4RRFFQ69G5FAV", "errors", t0.Add(-time.Hour), t0), ""), 404) }