(t *testing.T)
| 163 | } |
| 164 | |
| 165 | func TestIPVSCollectorResponse(t *testing.T) { |
| 166 | testcases := []struct { |
| 167 | labels string |
| 168 | metricsFile string |
| 169 | }{ |
| 170 | {"<none>", "fixtures/ip_vs_result.txt"}, |
| 171 | {"", "fixtures/ip_vs_result_lbs_none.txt"}, |
| 172 | {"local_port", "fixtures/ip_vs_result_lbs_local_port.txt"}, |
| 173 | {"local_address,local_port", "fixtures/ip_vs_result_lbs_local_address_local_port.txt"}, |
| 174 | } |
| 175 | for _, test := range testcases { |
| 176 | t.Run(test.labels, func(t *testing.T) { |
| 177 | args := []string{"--path.procfs", "fixtures/proc"} |
| 178 | if test.labels != "<none>" { |
| 179 | args = append(args, "--collector.ipvs.backend-labels="+test.labels) |
| 180 | } |
| 181 | if _, err := kingpin.CommandLine.Parse(args); err != nil { |
| 182 | t.Fatal(err) |
| 183 | } |
| 184 | collector, err := NewIPVSCollector(slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 185 | if err != nil { |
| 186 | t.Fatal(err) |
| 187 | } |
| 188 | registry := prometheus.NewRegistry() |
| 189 | registry.MustRegister(miniCollector{c: collector}) |
| 190 | |
| 191 | rw := httptest.NewRecorder() |
| 192 | promhttp.InstrumentMetricHandler(registry, promhttp.HandlerFor(registry, promhttp.HandlerOpts{})).ServeHTTP(rw, &http.Request{}) |
| 193 | |
| 194 | wantMetrics, err := os.ReadFile(test.metricsFile) |
| 195 | if err != nil { |
| 196 | t.Fatalf("unable to read input test file %s: %s", test.metricsFile, err) |
| 197 | } |
| 198 | |
| 199 | wantLines := strings.Split(string(wantMetrics), "\n") |
| 200 | gotLines := strings.Split(string(rw.Body.String()), "\n") |
| 201 | gotLinesIdx := 0 |
| 202 | |
| 203 | // Until the Prometheus Go client library offers better testability |
| 204 | // (https://github.com/prometheus/client_golang/issues/58), we simply compare |
| 205 | // verbatim text-format metrics outputs, but ignore any lines we don't have |
| 206 | // in the fixture. Put differently, we are only testing that each line from |
| 207 | // the fixture is present, in the order given. |
| 208 | wantLoop: |
| 209 | for _, want := range wantLines { |
| 210 | for _, got := range gotLines[gotLinesIdx:] { |
| 211 | if want == got { |
| 212 | // this is a line we are interested in, and it is correct |
| 213 | continue wantLoop |
| 214 | } |
| 215 | gotLinesIdx++ |
| 216 | } |
| 217 | // if this point is reached, the line we want was missing |
| 218 | t.Fatalf("Missing expected output line(s), first missing line is %s", want) |
| 219 | } |
| 220 | }) |
| 221 | } |
| 222 | } |
nothing calls this directly
no test coverage detected