This repository was archived by the owner on Oct 2, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathserver_test.go
More file actions
82 lines (73 loc) · 2.16 KB
/
Copy pathserver_test.go
File metadata and controls
82 lines (73 loc) · 2.16 KB
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
package metrics_test
import (
"context"
"io/ioutil"
"net"
goHttp "net/http"
"strings"
"testing"
"github.com/containerssh/http"
"github.com/containerssh/log"
"github.com/containerssh/service"
"github.com/stretchr/testify/assert"
"github.com/containerssh/metrics"
)
func TestFetchMetrics(t *testing.T) {
geoip := &geoIpLookupProvider{}
logger := log.NewTestLogger(t)
m := metrics.New(geoip)
counter, err := m.CreateCounter("test", "pc", "Test metric")
assert.Nil(t, err)
s, err := metrics.NewServer(
metrics.Config{
ServerConfiguration: http.ServerConfiguration{
Listen: "127.0.0.1:8080",
},
Enable: true,
Path: "/metrics",
},
m,
logger,
)
assert.Nil(t, err)
lifecycle := service.NewLifecycle(s)
ready := make(chan struct{}, 1)
lifecycle.OnRunning(func(s service.Service, l service.Lifecycle) {
ready <- struct{}{}
})
go func() {
err := lifecycle.Run()
assert.Nil(t, err)
}()
defer func() {
lifecycle.Stop(context.Background())
}()
<-ready
bodyLines := callServer(t)
assert.Contains(t, bodyLines, "# HELP test Test metric")
assert.Contains(t, bodyLines, "# TYPE test counter")
assert.Contains(t, bodyLines, "# UNIT test pc")
assert.Equal(t, "# EOF", bodyLines[len(bodyLines)-2])
counter.Increment()
bodyLines = callServer(t)
// TODO this relies on an implementation detail and should be done nicer. Fix it the first time it breaks.
assert.Contains(t, bodyLines, "test 1.000000")
counter.Increment()
bodyLines = callServer(t)
// TODO this relies on an implementation detail and should be done nicer. Fix it the first time it breaks.
assert.Contains(t, bodyLines, "test 2.000000")
counter2, err := m.CreateCounterGeo("test2", "pc", "Test metric 2")
assert.Nil(t, err)
counter2.Increment(net.ParseIP("127.0.0.1"))
bodyLines = callServer(t)
assert.Contains(t, bodyLines, "test2{country=\"XX\"} 1.000000")
}
func callServer(t *testing.T) []string {
metricsResult, err := goHttp.Get("http://127.0.0.1:8080/metrics")
assert.Nil(t, err)
bodyBytes, err := ioutil.ReadAll(metricsResult.Body)
assert.Nil(t, err)
assert.Nil(t, metricsResult.Body.Close())
bodyLines := strings.Split(string(bodyBytes), "\n")
return bodyLines
}