-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_test.go
88 lines (82 loc) · 2.15 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
prom_dto "github.com/prometheus/client_model/go"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
func TestSetupGauges(t *testing.T) {
reg := prometheus.NewRegistry()
metrics := []metricConfiguration{
{
Name: "test_name",
Help: "some help",
},
}
require.NoError(t, setupGauges(reg, metrics))
families, err := reg.Gather()
require.NoError(t, err)
require.Len(t, families, 1)
fam := families[0]
require.Equal(t, "jira_test_name", *fam.Name)
require.Equal(t, "some help", *fam.Help)
require.Equal(t, "GAUGE", fam.Type.Enum().String())
}
func TestCheck(t *testing.T) {
log := logrus.New()
log.SetLevel(logrus.ErrorLevel)
// With no metrics defines, don't do anything but end if the context is
// cancelled.
t.Run("no-metrics", func(t *testing.T) {
httpClient := &http.Client{}
ctx, cancel := context.WithCancel(context.Background())
cfg := &configuration{
BaseURL: "",
Login: "login",
Password: "password",
}
go func() {
time.Sleep(time.Second)
cancel()
}()
check(ctx, log, cfg, httpClient)
})
// Test the happy case where we get data back from the server.
t.Run("working-metric", func(t *testing.T) {
httpClient := &http.Client{}
reg := prometheus.NewRegistry()
ctx, cancel := context.WithCancel(context.Background())
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"total": 5}`)
cancel()
}))
defer srv.Close()
cfg := &configuration{
BaseURL: srv.URL,
Login: "login",
Password: "password",
Metrics: []metricConfiguration{
{
Name: "test",
Help: "test",
JQL: "project = TEST",
ParsedInterval: time.Second,
},
},
}
require.NoError(t, setupGauges(reg, cfg.Metrics))
check(ctx, log, cfg, httpClient)
results := make(chan prometheus.Metric, 2)
cfg.Metrics[0].Gauge.Collect(results)
result := <-results
val := prom_dto.Metric{}
result.Write(&val)
require.Equal(t, float64(5), *val.Gauge.Value)
})
}