-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathplugin_test.go
110 lines (96 loc) · 3.28 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
103
104
105
106
107
108
109
110
package availability_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/slok/sloth-common-sli-plugins/plugins/istio/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 namespace, should fail.": {
options: map[string]string{"service": "test"},
expErr: true,
},
"An empty namespace query, should fail.": {
options: map[string]string{"namespace": "", "service": "test"},
expErr: true,
},
"Without service, should fail.": {
options: map[string]string{"namespace": "default"},
expErr: true,
},
"An empty service query, should fail.": {
options: map[string]string{"namespace": "default", "service": ""},
expErr: true,
},
"Not having a filter and with namespace + service should return a valid query.": {
options: map[string]string{"namespace": "default", "service": "test"},
expQuery: `
(
sum(rate(istio_requests_total{ destination_service_name="test",destination_service_namespace="default",response_code=~"(5..|429)" }[{{.window}}]))
/
(sum(rate(istio_requests_total{ destination_service_name="test",destination_service_namespace="default" }[{{.window}}])) > 0)
) OR on() vector(0)
`,
},
"Having a filter, with namespace and service should return a valid query.": {
options: map[string]string{
"filter": `k1="v2",k2="v2"`,
"namespace": "default",
"service": "test",
},
expQuery: `
(
sum(rate(istio_requests_total{ k1="v2",k2="v2",destination_service_name="test",destination_service_namespace="default",response_code=~"(5..|429)" }[{{.window}}]))
/
(sum(rate(istio_requests_total{ k1="v2",k2="v2",destination_service_name="test",destination_service_namespace="default" }[{{.window}}])) > 0)
) OR on() vector(0)
`,
},
"Filter should be sanitized with ','.": {
options: map[string]string{
"filter": `k1="v2",k2="v2",`,
"namespace": "default",
"service": "test",
},
expQuery: `
(
sum(rate(istio_requests_total{ k1="v2",k2="v2",destination_service_name="test",destination_service_namespace="default",response_code=~"(5..|429)" }[{{.window}}]))
/
(sum(rate(istio_requests_total{ k1="v2",k2="v2",destination_service_name="test",destination_service_namespace="default" }[{{.window}}])) > 0)
) OR on() vector(0)
`,
},
"Filter should be sanitized with '{'.": {
options: map[string]string{
"filter": `{k1="v2",k2="v2",},`,
"namespace": "default",
"service": "test",
},
expQuery: `
(
sum(rate(istio_requests_total{ k1="v2",k2="v2",destination_service_name="test",destination_service_namespace="default",response_code=~"(5..|429)" }[{{.window}}]))
/
(sum(rate(istio_requests_total{ k1="v2",k2="v2",destination_service_name="test",destination_service_namespace="default" }[{{.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)
}
})
}
}