(t *testing.T)
| 27 | ) |
| 28 | |
| 29 | func TestPush(t *testing.T) { |
| 30 | var ( |
| 31 | lastMethod string |
| 32 | lastBody []byte |
| 33 | lastPath string |
| 34 | lastHeader http.Header |
| 35 | ) |
| 36 | |
| 37 | // Fake a Pushgateway that responds with 202 to DELETE and with 200 in |
| 38 | // all other cases. |
| 39 | pgwOK := httptest.NewServer( |
| 40 | http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 41 | lastMethod = r.Method |
| 42 | lastHeader = r.Header |
| 43 | var err error |
| 44 | lastBody, err = io.ReadAll(r.Body) |
| 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | lastPath = r.URL.EscapedPath() |
| 49 | w.Header().Set("Content-Type", `text/plain; charset=utf-8`) |
| 50 | if r.Method == http.MethodDelete { |
| 51 | w.WriteHeader(http.StatusAccepted) |
| 52 | return |
| 53 | } |
| 54 | w.WriteHeader(http.StatusOK) |
| 55 | }), |
| 56 | ) |
| 57 | defer pgwOK.Close() |
| 58 | |
| 59 | // Fake a Pushgateway that always responds with 500. |
| 60 | pgwErr := httptest.NewServer( |
| 61 | http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 62 | http.Error(w, "fake error", http.StatusInternalServerError) |
| 63 | }), |
| 64 | ) |
| 65 | defer pgwErr.Close() |
| 66 | |
| 67 | metric1 := prometheus.NewCounter(prometheus.CounterOpts{ |
| 68 | Name: "testname1", |
| 69 | Help: "testhelp1", |
| 70 | }) |
| 71 | metric2 := prometheus.NewGauge(prometheus.GaugeOpts{ |
| 72 | Name: "testname2", |
| 73 | Help: "testhelp2", |
| 74 | ConstLabels: prometheus.Labels{"foo": "bar", "dings": "bums"}, |
| 75 | }) |
| 76 | |
| 77 | reg := prometheus.NewRegistry() |
| 78 | reg.MustRegister(metric1) |
| 79 | reg.MustRegister(metric2) |
| 80 | |
| 81 | mfs, err := reg.Gather() |
| 82 | if err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | |
| 86 | buf := &bytes.Buffer{} |
nothing calls this directly
no test coverage detected