forked from cloudflare/opentsdb_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
65 lines (56 loc) · 1.42 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
)
func TestHappyPath(t *testing.T) {
var example = fixture{
in: metrics{
{
Metric: "tsd.compaction.duplicates",
Value: "1",
Tags: map[string]string{"host": "foo", "type": "variant1"},
},
{
Metric: "tsd.compaction.duplicates.test",
Value: "2",
Tags: map[string]string{"host": "bar", "type": "variant2"},
},
},
out: []string{
`Desc{fqName: "opentsdb_compaction_duplicates", help: "", constLabels: {}, variableLabels: [type]}`,
`Desc{fqName: "opentsdb_compaction_duplicates_test", help: "", constLabels: {}, variableLabels: [type]}`,
},
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
e := json.NewEncoder(w)
_ = e.Encode(example.in)
}))
defer ts.Close()
e := &collector{target: ts.URL, timeout: 5 * time.Second}
ch := make(chan prometheus.Metric)
go e.Collect(ch)
var metricsFound []string
for m := range ch {
metricsFound = append(metricsFound, m.Desc().String())
if len(metricsFound) == len(example.in) {
break
}
}
if !reflect.DeepEqual(metricsFound, example.out) {
t.Fatalf("\n%s\n\tdoes not match expected output:\n%s",
strings.Join(metricsFound, "\n"),
strings.Join(example.out, "\n"),
)
}
}
type fixture struct {
in metrics
out []string
}