-
Notifications
You must be signed in to change notification settings - Fork 13
/
plugin_test.go
102 lines (89 loc) · 2.81 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package availability_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/slok/sloth-common-sli-plugins/plugins/traefik/v1/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
}{
"Without backend, should fail.": {
options: map[string]string{},
expErr: true,
},
"An invalid backend query, should fail.": {
options: map[string]string{"backend_regex": "([xyz"},
expErr: true,
},
"An empty backend query, should fail.": {
options: map[string]string{"backend_regex": ""},
expErr: true,
},
"Not having a filter and with backend should return a valid query.": {
options: map[string]string{"backend_regex": "github.com/slok/sloth/?"},
expQuery: `
(
sum(rate(traefik_backend_requests_total{ backend=~"github.com/slok/sloth/?",code=~"(5..|429)" }[{{.window}}]))
/
(sum(rate(traefik_backend_requests_total{ backend=~"github.com/slok/sloth/?" }[{{.window}}])) > 0)
) OR on() vector(0)
`,
},
"Having a filter and with backend should return a valid query.": {
options: map[string]string{
"filter": `k1="v2",k2="v2"`,
"backend_regex": "github.com/slok/sloth/?",
},
expQuery: `
(
sum(rate(traefik_backend_requests_total{ k1="v2",k2="v2",backend=~"github.com/slok/sloth/?",code=~"(5..|429)" }[{{.window}}]))
/
(sum(rate(traefik_backend_requests_total{ k1="v2",k2="v2",backend=~"github.com/slok/sloth/?" }[{{.window}}])) > 0)
) OR on() vector(0)
`,
},
"Filter should be sanitized with ','.": {
options: map[string]string{
"filter": `k1="v2",k2="v2",`,
"backend_regex": "github.com/slok/sloth/?",
},
expQuery: `
(
sum(rate(traefik_backend_requests_total{ k1="v2",k2="v2",backend=~"github.com/slok/sloth/?",code=~"(5..|429)" }[{{.window}}]))
/
(sum(rate(traefik_backend_requests_total{ k1="v2",k2="v2",backend=~"github.com/slok/sloth/?" }[{{.window}}])) > 0)
) OR on() vector(0)
`,
},
"Filter should be sanitized with '{'.": {
options: map[string]string{
"filter": `{k1="v2",k2="v2",},`,
"backend_regex": "github.com/slok/sloth/?",
},
expQuery: `
(
sum(rate(traefik_backend_requests_total{ k1="v2",k2="v2",backend=~"github.com/slok/sloth/?",code=~"(5..|429)" }[{{.window}}]))
/
(sum(rate(traefik_backend_requests_total{ k1="v2",k2="v2",backend=~"github.com/slok/sloth/?" }[{{.window}}])) > 0)
) OR on() vector(0)
`,
},
}
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)
}
})
}
}