|
| 1 | +// Copyright (c) The Thanos Authors. |
| 2 | +// Licensed under the Apache License 2.0. |
| 3 | + |
| 4 | +// Package promclient offers helper client function for various API endpoints. |
| 5 | + |
| 6 | +package promclient |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "net/http" |
| 12 | + "net/http/httptest" |
| 13 | + "net/url" |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/efficientgo/core/testutil" |
| 17 | +) |
| 18 | + |
| 19 | +func TestExternalLabels(t *testing.T) { |
| 20 | + for _, tc := range []struct { |
| 21 | + name string |
| 22 | + response string |
| 23 | + err bool |
| 24 | + labels map[string]string |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "invalid payload", |
| 28 | + response: `{`, |
| 29 | + err: true, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "unknown scrape protocol", |
| 33 | + response: `{"status":"success","data":{"yaml":"global:\n scrape_interval: 1m\n scrape_timeout: 10s\n scrape_protocols:\n - OpenMetricsText1.0.0\n - OpenMetricsText0.0.1\n - PrometheusText1.0.0\n - PrometheusText0.0.4\n - UnknownScrapeProto\n evaluation_interval: 1m\n external_labels:\n az: \"1\"\n region: eu-west\nruntime:\n gogc: 75\n"}}`, |
| 34 | + labels: map[string]string{ |
| 35 | + "region": "eu-west", |
| 36 | + "az": "1", |
| 37 | + }, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "no external labels", |
| 41 | + response: `{"status":"success","data":{"yaml":"global:\n scrape_interval: 1m\n scrape_timeout: 10s\n scrape_protocols:\n - OpenMetricsText1.0.0\n - OpenMetricsText0.0.1\n - PrometheusText1.0.0\n - PrometheusText0.0.4\n - UnknownScrapeProto\n evaluation_interval: 1m\nruntime:\n gogc: 75\n"}}`, |
| 42 | + labels: map[string]string{}, |
| 43 | + }, |
| 44 | + } { |
| 45 | + t.Run(tc.name, func(t *testing.T) { |
| 46 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 47 | + fmt.Fprintln(w, tc.response) |
| 48 | + })) |
| 49 | + defer ts.Close() |
| 50 | + |
| 51 | + u, err := url.Parse(ts.URL) |
| 52 | + testutil.Ok(t, err) |
| 53 | + |
| 54 | + ext, err := NewDefaultClient().ExternalLabels(context.Background(), u) |
| 55 | + if tc.err { |
| 56 | + testutil.NotOk(t, err) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + testutil.Ok(t, err) |
| 61 | + testutil.Equals(t, len(tc.labels), ext.Len()) |
| 62 | + for k, v := range tc.labels { |
| 63 | + testutil.Equals(t, v, ext.Get(k)) |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
0 commit comments