-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
78 lines (66 loc) · 1.84 KB
/
server_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
package pag_test
import (
_ "embed"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
pag "github.com/ndx-technologies/prometheus-aggregation-gateway"
)
func TestServer(t *testing.T) {
config := pag.PromAggGatewayServerConfig{
Metrics: map[string]pag.MetricConfig{
"abc_count": {
Help: "ABC is abc",
Type: pag.Counter,
},
},
Labels: map[string]pag.LabelConfig{
"platform": {Values: []string{"ios", "web"}},
},
MetricAppendPrefix: "ppp_",
}
s := pag.NewPromAggGatewayServer(config)
http.HandleFunc("POST /metrics", s.ConsumeMetrics)
http.HandleFunc("GET /metrics", s.GetMetrics)
t.Run("consume", func(t *testing.T) {
req := httptest.NewRequest("POST", "/metrics", strings.NewReader(`{"metrics":{"abc_count{x=\"11\"}":420,"wrong":11},"labels":{"platform":"ios"}}`))
w := httptest.NewRecorder()
s.ConsumeMetrics(w, req)
resp := w.Result()
if resp.StatusCode != http.StatusOK {
t.Error("expected status OK")
}
})
t.Run("consume no label", func(t *testing.T) {
req := httptest.NewRequest("POST", "/metrics", strings.NewReader(`{"metrics":{"abc_count":11,"wrong":11},"labels":{"platform":"ios"}}`))
w := httptest.NewRecorder()
s.ConsumeMetrics(w, req)
resp := w.Result()
if resp.StatusCode != http.StatusOK {
t.Error("expected status OK")
}
})
t.Run("read", func(t *testing.T) {
req := httptest.NewRequest("GET", "/metric", nil)
w := httptest.NewRecorder()
s.GetMetrics(w, req)
resp := w.Result()
if resp.StatusCode != http.StatusOK {
t.Error("expected status OK")
}
exp := strings.Join([]string{
`# HELP ppp_abc_count ABC is abc`,
`# TYPE ppp_abc_count counter`,
`ppp_abc_count{platform="ios"} 431`,
``,
``,
``,
}, "\n")
body, _ := io.ReadAll(resp.Body)
if string(body) != exp {
t.Error("unexpected body", "got", string(body), "exp", exp)
}
})
}