-
Notifications
You must be signed in to change notification settings - Fork 5
/
observatory_exporter_test.go
90 lines (76 loc) · 2.29 KB
/
observatory_exporter_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
)
func TestScrape(t *testing.T) {
c := NewCollector(DefaultApiURL)
metrics, err := c.Scrape("google.com", false)
if err != nil {
t.Fatalf("Scrape returned an error: %s", err)
}
expected := []string{
"tls_enabled",
"cert_is_trusted",
"cert_expiry_date",
"cert_start_date",
"compatibility_level",
"score",
"grade",
}
for _, k := range expected {
if _, ok := metrics[k]; !ok {
t.Fatalf("Missing metrics %s", k)
}
}
}
func readGauge(m prometheus.Metric) float64 {
pb := &dto.Metric{}
m.Write(pb)
return pb.GetGauge().GetValue()
}
func TestMetricsExport(t *testing.T) {
targetURL := "dummy-url.com"
cache := NewCache()
e := NewExporter(cache)
tomorrow := float64(time.Now().Unix()) + (time.Hour * 24).Seconds()
yesterday := float64(time.Now().Unix()) - (time.Hour * 24).Seconds()
// ordering is important.
metrics := Metrics{}
metrics["cert_expiry_date"] = tomorrow
metrics["cert_is_trusted"] = 0
metrics["cert_start_date"] = yesterday
metrics["compatibility_level"] = 1
metrics["grade"] = 3
metrics["score"] = 85
metrics["tls_enabled"] = 1
cache.Write(targetURL, metrics)
ch := make(chan prometheus.Metric)
go func() {
defer close(ch)
e.Collect(ch)
}()
if expect, got := metrics["cert_expiry_date"], readGauge(<-ch); expect != got {
t.Errorf("cert_expiry_date: expected %f, got %f", expect, got)
}
if expect, got := metrics["cert_is_trusted"], readGauge(<-ch); expect != got {
t.Errorf("cert_is_trusted: expected %f, got %f", expect, got)
}
if expect, got := metrics["cert_start_date"], readGauge(<-ch); expect != got {
t.Errorf("cert_start_date: expected %f, got %f", expect, got)
}
if expect, got := metrics["compatibility_level"], readGauge(<-ch); expect != got {
t.Errorf("compatibility_level: expected %f, got %f", expect, got)
}
if expect, got := metrics["grade"], readGauge(<-ch); expect != got {
t.Errorf("grade: expected %f, got %f", expect, got)
}
if expect, got := metrics["score"], readGauge(<-ch); expect != got {
t.Errorf("score: expected %f, got %f", expect, got)
}
if expect, got := metrics["tls_enabled"], readGauge(<-ch); expect != got {
t.Errorf("tls_enabled: expected %f, got %f", expect, got)
}
}