-
Notifications
You must be signed in to change notification settings - Fork 14
/
plugin_test.go
63 lines (51 loc) · 2.25 KB
/
plugin_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
package availability_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/slok/sloth-common-sli-plugins/plugins/slok-go-http-metrics/availability"
)
func TestSLIPlugin(t *testing.T) {
tests := map[string]struct {
meta map[string]string
labels map[string]string
options map[string]string
expQuery string
expErr bool
}{
"Having a wrong filter, it should fail.": {
options: map[string]string{"filter": "something="},
expErr: true,
},
"Not having a filter should fail.": {
expErr: true,
},
"Having a filter should return the query with the filters.": {
options: map[string]string{"filter": `k1="v2",k2="v2"`},
expQuery: "\nsum(rate(http_request_duration_seconds_count{ k1=\"v2\",k2=\"v2\",code=~\"(5..|429)\" }[{{.window}}]))\n/\nsum(rate(http_request_duration_seconds_count{ k1=\"v2\",k2=\"v2\" }[{{.window}}]))\n",
},
"Having a filter with `{}` should return the query with the filters.": {
options: map[string]string{"filter": `{k1="v2",k2="v2"}`},
expQuery: "\nsum(rate(http_request_duration_seconds_count{ k1=\"v2\",k2=\"v2\",code=~\"(5..|429)\" }[{{.window}}]))\n/\nsum(rate(http_request_duration_seconds_count{ k1=\"v2\",k2=\"v2\" }[{{.window}}]))\n",
},
"Having a filter with `,` should return the query with the filters.": {
options: map[string]string{"filter": `k1="v2",k2="v2",`},
expQuery: "\nsum(rate(http_request_duration_seconds_count{ k1=\"v2\",k2=\"v2\",code=~\"(5..|429)\" }[{{.window}}]))\n/\nsum(rate(http_request_duration_seconds_count{ k1=\"v2\",k2=\"v2\" }[{{.window}}]))\n",
},
"Having a filter with `{}` and `,` should return the query with the filters.": {
options: map[string]string{"filter": `{k1="v2",k2="v2",}`},
expQuery: "\nsum(rate(http_request_duration_seconds_count{ k1=\"v2\",k2=\"v2\",code=~\"(5..|429)\" }[{{.window}}]))\n/\nsum(rate(http_request_duration_seconds_count{ k1=\"v2\",k2=\"v2\" }[{{.window}}]))\n",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
gotQuery, err := availability.SLIPlugin(context.TODO(), test.meta, test.labels, test.options)
if test.expErr {
assert.Error(err)
} else if assert.NoError(err) {
assert.Equal(test.expQuery, gotQuery)
}
})
}
}