package promq import ( "net/http" "net/http/httptest" "strings" "testing" "time" ) func server(t *testing.T, body string) *Client { t.Helper() srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte(body)) })) t.Cleanup(srv.Close) return New(srv.URL, time.Second) } func TestSeriesAreNamedByTheLabelAsked(t *testing.T) { c := server(t, `{"status":"success","data":{"resultType":"matrix","result":[ {"metric":{"__name__":"llmgw_pool_saturation","model":"qwen"}, "values":[[1700000000,"0.2"],[1700000015,"0.8"]]}]}}`) got, err := c.RangeQuery(t.Context(), "llmgw_pool_saturation", "model", time.Hour, time.Minute) if err != nil { t.Fatal(err) } if len(got) != 1 || got[0].Name != "qwen" { t.Fatalf("expected one series named for its model, got %+v", got) } if got[0].Last() != 0.8 || got[0].Max() != 0.8 { t.Errorf("points did not decode: %+v", got[0].Points) } } func TestASeriesMissingThatLabelIsStillDrawn(t *testing.T) { c := server(t, `{"status":"success","data":{"result":[ {"metric":{"__name__":"llmgw_replicas"},"values":[[1700000000,"3"]]}]}}`) got, _ := c.RangeQuery(t.Context(), "x", "model", time.Hour, time.Minute) // A legend reading "{}" helps nobody, and dropping the line hides data. if len(got) != 1 || got[0].Name != "llmgw_replicas" { t.Errorf("expected a fallback name, got %+v", got) } } func TestBusiestSeriesComeFirst(t *testing.T) { c := server(t, `{"status":"success","data":{"result":[ {"metric":{"model":"quiet"},"values":[[1,"0.1"]]}, {"metric":{"model":"busy"},"values":[[1,"9"]]}]}}`) got, _ := c.RangeQuery(t.Context(), "x", "model", time.Hour, time.Minute) // The first colours go to the lines that matter, and whatever is folded // into "other" is then genuinely the tail. if got[0].Name != "busy" { t.Errorf("expected the busiest first, got %+v", got) } } func TestASampleThatIsNotANumberIsSkippedRatherThanZero(t *testing.T) { c := server(t, `{"status":"success","data":{"result":[ {"metric":{"model":"m"},"values":[[1,"1.5"],[2,"NaN"],[3,"2.5"]]}]}}`) got, _ := c.RangeQuery(t.Context(), "x", "model", time.Hour, time.Minute) // Drawn as zero it would put a line on the floor where there is no data, // which reads as an outage. if len(got[0].Points) != 2 { t.Errorf("expected the NaN dropped, got %+v", got[0].Points) } } func TestAnEmptySeriesIsNotReturned(t *testing.T) { c := server(t, `{"status":"success","data":{"result":[{"metric":{"model":"m"},"values":[]}]}}`) if got, _ := c.RangeQuery(t.Context(), "x", "model", time.Hour, time.Minute); len(got) != 0 { t.Errorf("a series with no points is not a line, got %+v", got) } } func TestAQueryPrometheusRefusesSaysWhy(t *testing.T) { c := server(t, `{"status":"error","errorType":"bad_data","error":"parse error at char 1"}`) _, err := c.RangeQuery(t.Context(), "!!", "model", time.Hour, time.Minute) if err == nil || !strings.Contains(err.Error(), "parse error") { t.Errorf("expected the reason to survive, got %v", err) } } func TestNoAddressMeansNoClient(t *testing.T) { // A deployment with no Prometheus is an ordinary one. The dashboard leaves // the panels out rather than showing an error where a chart should be. var c *Client = New(" ", 0) if c != nil { t.Fatal("an empty address should build no client") } got, err := c.RangeQuery(t.Context(), "x", "model", time.Hour, time.Minute) if err != nil || got != nil { t.Errorf("a nil client should answer nothing, quietly: %v %v", got, err) } }