-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathdetector_test.go
281 lines (216 loc) · 9.31 KB
/
detector_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package signalfx
import (
"context"
"net/http"
"net/url"
"strconv"
"testing"
"github.com/signalfx/signalfx-go/detector"
"github.com/stretchr/testify/assert"
)
func TestCreateDetector(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/v2/detector", verifyRequest(t, "POST", true, http.StatusOK, nil, "detector/create_success.json"))
result, err := client.CreateDetector(context.Background(), &detector.CreateUpdateDetectorRequest{
Name: "string",
})
assert.NoError(t, err, "Unexpected error creating detector")
assert.Equal(t, "string", result.Name, "Name does not match")
}
func TestCreateBadDetector(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/v2/detector", verifyRequest(t, "POST", true, http.StatusBadRequest, nil, ""))
result, err := client.CreateDetector(context.Background(), &detector.CreateUpdateDetectorRequest{
Name: "string",
})
assert.Error(t, err, "Should have gotten an error from a bad create")
assert.Nil(t, result, "Should have a null detector on bad create")
}
func TestDeleteDetector(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/v2/detector/string", verifyRequest(t, "DELETE", true, http.StatusNoContent, nil, ""))
err := client.DeleteDetector(context.Background(), "string")
assert.NoError(t, err, "Unexpected error deleting detector")
}
func TestDeleteMissingDetector(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/v2/detector", verifyRequest(t, "POST", true, http.StatusNotFound, nil, ""))
err := client.DeleteDetector(context.Background(), "example")
assert.Error(t, err, "Should have gotten an error from a missing delete")
}
func TestDisableDetector(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/v2/detector/string/disable", verifyRequest(t, "PUT", true, http.StatusNoContent, nil, ""))
err := client.DisableDetector(context.Background(), "string", []string{"example"})
assert.NoError(t, err, "Unexpected error disabling detector")
}
func TestDisableMissingDetector(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/v2/detector/string/disable", verifyRequest(t, "PUT", true, http.StatusNotFound, nil, ""))
err := client.DisableDetector(context.Background(), "string", []string{"example"})
assert.Error(t, err, "Should have gotten an error from a missing disable")
}
func TestEnableDetector(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/v2/detector/string/enable", verifyRequest(t, "PUT", true, http.StatusNoContent, nil, ""))
err := client.EnableDetector(context.Background(), "string", []string{"example"})
assert.NoError(t, err, "Unexpected error disabling detector")
}
func TestEnableMissingDetector(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/v2/detector/string/enable", verifyRequest(t, "PUT", true, http.StatusNotFound, nil, ""))
err := client.EnableDetector(context.Background(), "string", []string{"example"})
assert.Error(t, err, "Should have gotten an error from a missing enable")
}
func TestGetDetector(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/v2/detector/string", verifyRequest(t, "GET", true, http.StatusOK, nil, "detector/get_success.json"))
result, err := client.GetDetector(context.Background(), "string")
assert.NoError(t, err, "Unexpected error getting detector")
assert.Equal(t, result.Name, "string", "Name does not match")
}
func TestGetDetectors(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/v2/detector", verifyRequest(t, "GET", true, http.StatusOK, nil, "detector/get_detectors.json"))
result, err := client.GetDetectors(context.Background(), 1000, "", 0)
assert.NoError(t, err, "Unexpected error getting all detectors")
assert.Equal(t, len(result), 2, "Incorrect number of detectors returned")
assert.Equal(t, result[0].Id, "string", "ID does not match")
assert.Equal(t, result[0].ProgramText, "string", "Program text field does not match")
assert.Equal(t, result[1].Id, "string1", "ID does not match")
}
func TestGetMissingDetector(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/v2/detector/string", verifyRequest(t, "GET", true, http.StatusNotFound, nil, ""))
result, err := client.GetDetector(context.Background(), "string")
assert.Error(t, err, "Should have gotten an error from a missing detector")
assert.Nil(t, result, "Should have gotten a nil result from a missing detector")
}
func TestSearchDetector(t *testing.T) {
teardown := setup()
defer teardown()
limit := 10
name := "foo"
offset := 2
tags := "bar"
params := url.Values{}
params.Add("limit", strconv.Itoa(limit))
params.Add("name", name)
params.Add("offset", strconv.Itoa(offset))
params.Add("tags", tags)
mux.HandleFunc("/v2/detector", verifyRequest(t, "GET", true, http.StatusOK, params, "detector/search_success.json"))
results, err := client.SearchDetectors(context.Background(), limit, name, offset, tags)
assert.NoError(t, err, "Unexpected error search detector")
assert.Equal(t, int32(1), results.Count, "Incorrect number of results")
}
func TestSearchDetectorBad(t *testing.T) {
teardown := setup()
defer teardown()
limit := 10
name := "foo"
offset := 2
tags := "bar"
params := url.Values{}
params.Add("limit", strconv.Itoa(limit))
params.Add("name", name)
params.Add("offset", strconv.Itoa(offset))
params.Add("tags", tags)
mux.HandleFunc("/v2/detector", verifyRequest(t, "GET", true, http.StatusBadRequest, params, ""))
_, err := client.SearchDetectors(context.Background(), limit, name, offset, tags)
assert.Error(t, err, "Unexpected error search detector")
}
func TestUpdateDetector(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/v2/detector/string", verifyRequest(t, "PUT", true, http.StatusOK, nil, "detector/update_success.json"))
result, err := client.UpdateDetector(context.Background(), "string", &detector.CreateUpdateDetectorRequest{
Name: "string",
})
assert.NoError(t, err, "Unexpected error updating detector")
assert.Equal(t, "string", result.Name, "Name does not match")
}
func TestUpdateMissingDetector(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/v2/detector/string", verifyRequest(t, "PUT", true, http.StatusNotFound, nil, ""))
result, err := client.UpdateDetector(context.Background(), "string", &detector.CreateUpdateDetectorRequest{
Name: "string",
})
assert.Error(t, err, "Should have gotten an error from an update on a missing detector")
assert.Nil(t, result, "Should have gotten a nil result from an update on a missing detector")
}
func TestGetDetectorEvents(t *testing.T) {
teardown := setup()
defer teardown()
from := 1557534630000
to := 1557534640000
offset := 12
limit := 2
params := url.Values{}
params.Add("from", strconv.Itoa(from))
params.Add("to", strconv.Itoa(to))
params.Add("offset", strconv.Itoa(offset))
params.Add("limit", strconv.Itoa(limit))
mux.HandleFunc("/v2/detector/string/events", verifyRequest(t, "GET", true, http.StatusOK, params, "detector/get_events.json"))
result, err := client.GetDetectorEvents(context.Background(), "string", from, to, offset, limit)
assert.NoError(t, err, "Unexpected error getting detector")
assert.Equal(t, result[0].AnomalyState, "ANOMALOUS", "AnomalyState does not match")
}
func TestGetDetectorIncidents(t *testing.T) {
teardown := setup()
defer teardown()
offset := 12
limit := 2
params := url.Values{}
params.Add("offset", strconv.Itoa(offset))
params.Add("limit", strconv.Itoa(limit))
mux.HandleFunc("/v2/detector/string/incidents", verifyRequest(t, "GET", true, http.StatusOK, params, "detector/get_incidents.json"))
result, err := client.GetDetectorIncidents(context.Background(), "string", offset, limit)
assert.NoError(t, err, "Unexpected error getting detector")
assert.Equal(t, result[0].AnomalyState, "ANOMALOUS", "AnomalyState does not match")
}
func TestValidateDetector(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/v2/detector/validate", verifyRequest(t, "POST", true, http.StatusNoContent, nil, ""))
err := client.ValidateDetector(context.Background(), &detector.ValidateDetectorRequestModel{
Name: "string",
ProgramText: "signal = data('cpu.utilization').mean(by=['sf_metric', 'sfx_realm']).publish(label='A'); detect(when(A > threshold(10), lasting='2m'), auto_resolve_after='3d').publish('Test detector validation')",
Rules: []*detector.Rule{
&detector.Rule{
Description: "Maximum > 10 for 2m",
DetectLabel: "Test detector validation",
Severity: detector.INFO,
},
},
})
assert.NoError(t, err, "Unexpected error validating detector programText")
}
func TestValidateBadDetector(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/v2/detector/validate", verifyRequest(t, "POST", true, http.StatusBadRequest, nil, ""))
err := client.ValidateDetector(context.Background(), &detector.ValidateDetectorRequestModel{
Name: "string",
ProgramText: "signal = ('cpu.utilization').mean(by=['sf_metric', 'sfx_realm']).publish(label='A'); detect(when(A > threshold(10), lasting='2m'), auto_resolve_after='3d').publish('Test detector validation')",
Rules: []*detector.Rule{
&detector.Rule{
Description: "Maximum > 10 for 2m",
DetectLabel: "Test detector validation",
Severity: detector.INFO,
},
},
})
assert.Error(t, err, "Should have gotten an error from invalid detector")
}