-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
67 lines (57 loc) · 1.54 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
66
67
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func Test(t *testing.T) {
t.Log("this test requires tailscaled and node_exporter on port 9100")
cfg, err := loadConfig("./testdata/config.yaml")
if err != nil {
t.Fatal(err)
}
h, err := newHandler(cfg)
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/node-exporter", nil)
h.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Errorf("invalid status code %d", res.StatusCode)
}
ct := res.Header.Get("Content-Type")
if !strings.Contains(ct, "application/json") {
t.Errorf("response should be json but %s", ct)
}
var resp []*httpSD
if err := json.NewDecoder(res.Body).Decode(&resp); err != nil {
t.Fatal(err)
}
for _, sd := range resp {
for _, target := range sd.Targets {
if target == "" {
t.Error("target should not be empty")
}
if !strings.HasSuffix(target, ":9100") {
t.Error("target should have node_exporter port as suffix")
}
}
if sd.Labels["__meta_tailscale_device_id"] == "" {
t.Error("label __meta_tailscale_device_id should not be empty")
}
if sd.Labels["__meta_tailscale_device_dns_name"] == "" {
t.Error("label __meta_tailscale_device_dns_name should not be empty")
}
if sd.Labels["__meta_tailscale_device_ipv4"] == "" {
t.Error("label __meta_tailscale_device_ipv4 should not be empty")
}
}
}
type httpSD struct {
Targets []string `json:"targets"`
Labels map[string]string `json:"labels"`
}