| 1 | package openmetrics |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "compress/gzip" |
| 6 | "context" |
| 7 | "io" |
| 8 | "net/http" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 13 | ) |
| 14 | |
| 15 | type roundTripFunc func(*http.Request) (*http.Response, error) |
| 16 | |
| 17 | func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { |
| 18 | return f(req) |
| 19 | } |
| 20 | |
| 21 | func newTestHTTPClient(fn roundTripFunc) *http.Client { |
| 22 | return &http.Client{Transport: fn} |
| 23 | } |
| 24 | |
| 25 | func TestFetchSeries(t *testing.T) { |
| 26 | payload := strings.Join([]string{ |
| 27 | "# HELP test_metric A test metric", |
| 28 | "# TYPE test_metric gauge", |
| 29 | "test_metric{label=\"value\"} 42", |
| 30 | "another_metric 7", |
| 31 | }, "\n") |
| 32 | |
| 33 | httpClient := newTestHTTPClient(func(r *http.Request) (*http.Response, error) { |
| 34 | if r.Header.Get("Accept") == "" { |
| 35 | t.Fatalf("expected Accept header to be set") |
| 36 | } |
| 37 | if r.Header.Get("Accept-Encoding") != "gzip" { |
| 38 | t.Fatalf("expected Accept-Encoding gzip header") |
| 39 | } |
| 40 | return &http.Response{ |
| 41 | StatusCode: http.StatusOK, |
| 42 | Body: io.NopCloser(strings.NewReader(payload)), |
| 43 | Header: make(http.Header), |
| 44 | }, nil |
| 45 | }) |
| 46 | |
| 47 | client, err := NewClientWithHTTP(Config{HTTPConfig: web.HTTPConfig{RequestConfig: web.RequestConfig{URL: "https://example.com/metrics"}}}, httpClient) |
| 48 | if err != nil { |
| 49 | t.Fatalf("NewClientWithHTTP failed: %v", err) |
| 50 | } |
| 51 | |
| 52 | series, err := client.FetchSeries(context.Background(), nil) |
| 53 | if err != nil { |
| 54 | t.Fatalf("FetchSeries failed: %v", err) |
| 55 | } |
| 56 | |
| 57 | if len(series) != 2 { |
| 58 | t.Fatalf("expected 2 series entries, got %d", len(series)) |
| 59 | } |
| 60 | |
| 61 | if series[0].Name() != "another_metric" || series[0].Value != 7 { |
| 62 | t.Fatalf("unexpected first sample: %+v", series[0]) |
| 63 | } |
| 64 | |
| 65 | if series[1].Name() != "test_metric" || series[1].Value != 42 { |
| 66 | t.Fatalf("unexpected second sample: %+v", series[1]) |
| 67 | } |
| 68 | |
| 69 | if got := series[1].Labels.Get("label"); got != "value" { |
| 70 | t.Fatalf("expected label \"value\", got %q", got) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestFetchSeriesGzip(t *testing.T) { |
| 75 | raw := strings.Join([]string{ |
| 76 | "metric_one 1", |
| 77 | "metric_two 2", |
| 78 | }, "\n") |
| 79 | |
| 80 | var buf bytes.Buffer |
| 81 | gz := gzip.NewWriter(&buf) |
| 82 | if _, err := gz.Write([]byte(raw)); err != nil { |
| 83 | t.Fatalf("failed to compress payload: %v", err) |
| 84 | } |
| 85 | if err := gz.Close(); err != nil { |
| 86 | t.Fatalf("failed to close gzip writer: %v", err) |
| 87 | } |
| 88 | |
| 89 | httpClient := newTestHTTPClient(func(r *http.Request) (*http.Response, error) { |
| 90 | resp := &http.Response{ |
| 91 | StatusCode: http.StatusOK, |
| 92 | Body: io.NopCloser(bytes.NewReader(buf.Bytes())), |
| 93 | Header: make(http.Header), |
| 94 | } |
| 95 | resp.Header.Set("Content-Encoding", "gzip") |
| 96 | return resp, nil |
| 97 | }) |
| 98 | |
| 99 | client, err := NewClientWithHTTP(Config{HTTPConfig: web.HTTPConfig{RequestConfig: web.RequestConfig{URL: "https://example.com/metrics"}}}, httpClient) |
| 100 | if err != nil { |
| 101 | t.Fatalf("NewClientWithHTTP failed: %v", err) |
| 102 | } |
| 103 | |
| 104 | series, err := client.FetchSeries(context.Background(), nil) |
| 105 | if err != nil { |
| 106 | t.Fatalf("FetchSeries failed: %v", err) |
| 107 | } |
| 108 | |
| 109 | if len(series) != 2 { |
| 110 | t.Fatalf("expected 2 series entries, got %d", len(series)) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func TestFetchSeriesHTTPError(t *testing.T) { |
| 115 | httpClient := newTestHTTPClient(func(r *http.Request) (*http.Response, error) { |
| 116 | return &http.Response{ |
| 117 | StatusCode: http.StatusBadGateway, |
| 118 | Body: io.NopCloser(strings.NewReader("bad gateway")), |
| 119 | }, nil |
| 120 | }) |
| 121 | |
| 122 | client, err := NewClientWithHTTP(Config{HTTPConfig: web.HTTPConfig{RequestConfig: web.RequestConfig{URL: "https://example.com/metrics"}}}, httpClient) |
| 123 | if err != nil { |
| 124 | t.Fatalf("NewClientWithHTTP failed: %v", err) |
| 125 | } |
| 126 | |
| 127 | if _, err := client.FetchSeries(context.Background(), nil); err == nil { |
| 128 | t.Fatalf("expected error for HTTP status") |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestFetchSeriesCancellation(t *testing.T) { |
| 133 | httpClient := newTestHTTPClient(func(r *http.Request) (*http.Response, error) { |
| 134 | <-r.Context().Done() |
| 135 | return nil, r.Context().Err() |
| 136 | }) |
| 137 | |
| 138 | client, err := NewClientWithHTTP(Config{HTTPConfig: web.HTTPConfig{RequestConfig: web.RequestConfig{URL: "https://example.com/metrics"}}}, httpClient) |
| 139 | if err != nil { |
| 140 | t.Fatalf("NewClientWithHTTP failed: %v", err) |
| 141 | } |
| 142 | |
| 143 | ctx, cancel := context.WithCancel(context.Background()) |
| 144 | cancel() |
| 145 | |
| 146 | if _, err := client.FetchSeries(ctx, nil); err == nil { |
| 147 | t.Fatalf("expected cancellation error") |
| 148 | } |
| 149 | } |