-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics_test.go
57 lines (49 loc) · 1.42 KB
/
metrics_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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus/testutil"
)
func TestMetrics(t *testing.T) {
ctx := context.Background()
logger := log.New(os.Stderr, "", log.LstdFlags)
db := fmt.Sprintf("file:%s?cache=shared&mode=memory&_foreign_keys=on", filepath.Join(t.TempDir(), "metrics.db"))
st, err := newStorage(ctx, logger, db)
if err != nil {
t.Fatalf("creating storage: %v", err)
}
c := newMetricsCollector(logger, st)
// Test with empty database first (i.e. handles sql.ErrNoRows correctly)
if want, got := 2, testutil.CollectAndCount(c); got != want {
t.Fatalf("want %d metrics, got %d", want, got)
}
om := owntracksMessage{}
if err := json.Unmarshal([]byte(egOwntracksLocation), &om); err != nil {
t.Fatal(err)
}
if err := st.AddOTLocation(ctx, om); err != nil {
t.Fatalf("AddOTLocation: %v", err)
}
if _, err := st.Upsert4sqCheckin(ctx, fsqCheckin{ID: "abcdef", CreatedAt: int(time.Now().Unix())}); err != nil {
t.Fatalf("Upsert4sqCheckin: %v", err)
}
if want, got := 2, testutil.CollectAndCount(c); got != want {
t.Fatalf("want %d metrics, got %d", want, got)
}
lint, err := testutil.CollectAndLint(c)
if err != nil {
t.Fatalf("CollectAndLint: %v", err)
}
for _, prob := range lint {
t.Errorf("lint: %s: %s", prob.Metric, prob.Text)
}
if len(lint) > 0 {
t.Fatal("lint problems detected")
}
}