-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzab2_integration_test.go
More file actions
396 lines (368 loc) · 10.4 KB
/
Copy pathzab2_integration_test.go
File metadata and controls
396 lines (368 loc) · 10.4 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"net/http"
"net/url"
// "fmt"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os/exec"
"strings"
"testing"
"time"
// "os"
// "net/url"
)
var server *exec.Cmd
var client *http.Client
var dashboardtests = []struct {
old string
new string
}{
{"ohtu1", "ohtu1"},
{"kivimylly.relex.fi", "kivimylly.relex.fi"},
{"<span class=high>0.813</span>", "<span class=normal>0.813</span>"},
}
var graphtests = []struct {
item string
}{
{"cpu"},
{"mem"},
{"swap"},
}
var basicupdatetests = []struct {
id string
scale string
oldmodel string
newmodel string
oldlower string
newlower string
oldhigh string
newhigh string
oldwarning string
newwarning string
oldcritical string
newcritical string
oldparams string
newparams string
badparams string
}{
{"9", "1",
"1", "1",
"false", "true",
"1.000000", "5.000000",
"0.800000", "15.000000",
"0.900000", "25.000000",
"params\":{\"stop_lower\":-518400,\"stop_upper\":null}",
"{\"stop_lower\":1414380539,\"pre_filter\":\"DailyMax\"}",
"",
},
{"6", "1",
"1", "1",
"true", "true",
"60.000000", "95.000000",
"0.000000", "75.000000",
"0.000000", "35.000000",
"params\":{\"test\":6}",
"{\"stop_lower\":1414380539,\"pre_filter\":\"DailyMax\"}",
"",
},
{"8", "1073741824",
"1", "2",
"false", "true",
"826781184.000000", "500000000.000000",
"885836992.000000", "500000000.000000",
"1181116032.000000", "500000000.000000",
"params\":{\"stop_lower\":1415488908,\"stop_upper\":1416395691}",
"{\"stop_lower\":1414380539,\"pre_filter\":\"DailyMax\"}",
"",
},
{"10", "1",
"1", "1",
"false", "false",
"0.000000", "0.000000",
"0.000000", "0.000000",
"0.000000", "0.000000",
"params\":{\"stop_lower\":1416638813,\"stop_upper\":1417050015,\"predict_length\":30}",
"{\"stop_lower\":1416638813,\"stop_upper\":1417050015,\"predict_length\":30}",
"{ i am not valid json",
},
}
var forecasttests = []struct {
id string
old string
new string
}{
{"6",
"future\":[{\"time\":1418372406,\"val\":99.6706},{\"time\":1418458806,\"val\":99.7462},{\"time\":1418545206,\"val\":99.8219},{\"time\":1418631606,\"val\":99.8975},{\"time\":1418718006,\"val\":99.9731},{\"time\":1418804406,\"val\":100.0487},{\"time\":1418890806,\"val\":100.1243},{\"time\":1418977206,\"val\":100.1999}]",
"future", // habbix fail so doesn't work anyway
},
{"9",
"future\":[{\"time\":1418372405,\"val\":0},{\"time\":1418458805,\"val\":-0.0002},{\"time\":1418545205,\"val\":-0.0004},{\"time\":1418631605,\"val\":-0.0005},{\"time\":1418718005,\"val\":-0.0007},{\"time\":1418804405,\"val\":-0.0009},{\"time\":1418890805,\"val\":-0.0011},{\"time\":1418977205,\"val\":-0.0012}]",
"future",
},
{"8",
"future\":[{\"time\":1418372405,\"val\":8.6340384e+08},{\"time\":1418458805,\"val\":8.6209216e+08},{\"time\":1418545205,\"val\":8.607805e+08},{\"time\":1418631605,\"val\":8.594688e+08},{\"time\":1418718005,\"val\":8.581571e+08},{\"time\":1418804405,\"val\":8.5684544e+08},{\"time\":1418890805,\"val\":8.5553376e+08},{\"time\":1418977205,\"val\":8.542221e+08}]",
"future",
},
}
var wrongurl = []struct {
url string
}{
// {"http://localhost:8080/api/100"},
{"http://localhost:8080/item/ohtufail/cpu"},
{"http://localhost:8080/item/ohtu1/carrot"},
// {"http://localhost:8080/static/zoink.css"},
{"http://localhost:8080/dashbored"},
{"http://localhost:8080/"},
}
func TestWrongUrl(t *testing.T) {
for _, wrong := range wrongurl {
r, err := http.Get(wrong.url)
if err != nil {
t.Errorf(err.Error())
}
if r.StatusCode != 404 {
t.Errorf("found false url:%s with statuscode:%s", wrong.url, r.StatusCode)
}
}
}
func TestItemsFound(t *testing.T) {
for _, item := range graphtests {
r, err := http.Get(fmt.Sprintf("http://localhost:8080/item/ohtu1/%s", item.item))
if err != nil {
t.Errorf(err.Error())
}
if r.StatusCode != 200 {
t.Errorf("could not find item %s, received statuscode: %d", item.item, r.StatusCode)
}
}
}
func TestScales(t *testing.T) {
for _, apiId := range basicupdatetests {
r, err := http.Get(fmt.Sprintf("http://localhost:8080/api/%s", apiId.id))
if err != nil {
t.Errorf(err.Error())
return
}
body, err := ioutil.ReadAll(r.Body)
if err != nil && err != io.EOF {
t.Errorf(err.Error())
}
if !strings.Contains(string(body), fmt.Sprintf("\"scale\":%s", apiId.scale)) {
t.Errorf("wrong or no scale for item %s, should be %s", apiId.id, apiId.scale)
}
}
}
func TestDashBoard(t *testing.T) { dashboarding(t, false) }
func TestParams(t *testing.T) { parametring(t, false) }
func TestThresholds(t *testing.T) { tresholding(t, false) }
func TestForecast(t *testing.T) { forecasting(t, false) }
func TestModel(t *testing.T) { modeling(t, false) }
func TestUpdates(t *testing.T) {
for _, apiId := range basicupdatetests {
form := url.Values{}
if apiId.badparams != "" {
form.Set("params", apiId.badparams)
} else {
form.Set("params", apiId.newparams)
}
form.Set("tr_high", apiId.newhigh)
form.Set("tr_critical", apiId.newcritical)
form.Set("tr_warning", apiId.newwarning)
form.Set("tr_lower", apiId.newlower)
form.Set("model", apiId.newmodel)
_, err := http.PostForm(fmt.Sprintf("http://localhost:8080/api/%s", apiId.id), form)
if err != nil {
t.Errorf("error sending POST to server: %s", err.Error())
}
}
}
func TestPostUpdateDashBoard(t *testing.T) { dashboarding(t, true) }
func TestPostUpdateParams(t *testing.T) { parametring(t, true) }
func TestPostUpdateForecast(t *testing.T) { forecasting(t, true) }
func TestPostUpdateThresholds(t *testing.T) { tresholding(t, true) }
func TestPostUpdateModel(t *testing.T) { modeling(t, true) }
func dashboarding(t *testing.T, post bool) {
r, err := http.Get("http://localhost:8080/dashboard")
if err != nil {
t.Errorf(err.Error())
}
if r.StatusCode != 200 {
t.Errorf("%s", r.StatusCode)
}
body, _ := ioutil.ReadAll(r.Body)
if err != nil && err != io.EOF {
t.Errorf(err.Error())
}
var item string
for _, ts := range dashboardtests {
if post {
item = ts.new
} else {
item = ts.old
}
if !strings.Contains(string(body), fmt.Sprintf(item)) {
t.Errorf("could not find %s from dashboard", item)
}
}
}
func forecasting(t *testing.T, post bool) {
var data string
for _, apiId := range forecasttests {
if post {
data = apiId.new
} else {
data = apiId.old
}
r, err := http.Get(fmt.Sprintf("http://localhost:8080/api/%s", apiId.id))
if err != nil {
t.Errorf(err.Error())
return
}
body, err := ioutil.ReadAll(r.Body)
if err != nil && err != io.EOF {
t.Errorf(err.Error())
}
if !strings.Contains(string(body), fmt.Sprintf(data)) {
t.Errorf("wrong forecast for item %s", apiId.id)
}
}
}
func parametring(t *testing.T, post bool) {
var params string
for _, apiId := range basicupdatetests {
if post {
params = apiId.newparams
} else {
params = apiId.oldparams
}
r, err := http.Get(fmt.Sprintf("http://localhost:8080/api/%s", apiId.id))
if err != nil && err != io.EOF {
t.Errorf(err.Error())
}
body, err := ioutil.ReadAll(r.Body)
if err != nil && err != io.EOF {
t.Errorf(err.Error())
}
if apiId.badparams != "" {
if !strings.Contains(string(body), apiId.oldparams) ||
strings.Contains(string(body), apiId.badparams) {
t.Errorf("wrong params for item %s", apiId.id)
}
} else if !strings.Contains(string(body), params) {
t.Errorf("wrong params for item %s", apiId.id)
}
}
}
func modeling(t *testing.T, post bool) {
var model string
for _, apiId := range basicupdatetests {
if post {
model = apiId.newmodel
} else {
model = apiId.oldmodel
}
r, err := http.Get(fmt.Sprintf("http://localhost:8080/api/%s", apiId.id))
if err != nil && err != io.EOF {
t.Errorf(err.Error())
}
body, err := ioutil.ReadAll(r.Body)
if err != nil && err != io.EOF {
t.Errorf(err.Error())
}
if !strings.Contains(string(body), model) {
t.Errorf("wrong model for item %s", apiId.id)
}
}
}
func tresholding(t *testing.T, post bool) {
var high, critical, warning, lower string
for _, apiId := range basicupdatetests {
if post {
high = apiId.newhigh
critical = apiId.newcritical
warning = apiId.newwarning
lower = apiId.newlower
} else {
high = apiId.oldhigh
critical = apiId.oldcritical
warning = apiId.oldwarning
lower = apiId.oldlower
}
r, err := http.Get(fmt.Sprintf("http://localhost:8080/api/%s", apiId.id))
if err != nil && err != io.EOF {
t.Errorf(err.Error())
}
body, err := ioutil.ReadAll(r.Body)
if err != nil && err != io.EOF {
t.Errorf(err.Error())
}
tresbolding(t, body, "lower", lower, apiId.id)
tresbolding(t, body, "high", high, apiId.id)
tresbolding(t, body, "warning", warning, apiId.id)
tresbolding(t, body, "critical", critical, apiId.id)
}
}
func tresbolding(t *testing.T, body []byte, thing string, value string, id string) {
if !strings.Contains(string(body), fmt.Sprintf("%s\":%s", thing, value)) {
t.Errorf("wrong %s for item %s, should be %s", thing, id, value)
}
}
func TestApi(t *testing.T) {
client = &http.Client{
CheckRedirect: nil,
}
req, err := http.NewRequest("Get", "http://localhost:8080/api/1", nil)
if err != nil {
t.Errorf(err.Error())
}
req.ParseForm()
// req.PostForm.Add("params", "{}")
resp, err := client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
req2, err := http.NewRequest("Get", "http://localhost:8080/api/1", nil)
if err != nil {
t.Errorf(err.Error())
}
req2.ParseForm()
// req2.PostForm.Add("params", "{}")
resp2, err := client.Do(req2)
defer resp2.Body.Close()
body2, _ := ioutil.ReadAll(resp2.Body)
if !bytes.Equal(body, body2) {
t.Errorf("zoink!")
}
}
func TestEnds(t *testing.T) {
err := server.Process.Kill()
if err != nil {
t.Errorf(err.Error())
}
}
func init() {
// uncomment the following to enable automatic reset of test database before testing
// this *will* cause problems with travis, so remember to undo/recomment!
/* dbinit := exec.Command("ssh", "ohtu@83.150.98.77",
"psql multi-axis-test -c 'drop schema public cascade;create schema public;';psql multi-axis-test < test.dump.sql")
err := dbinit.Run()
if err != nil {
log.Fatal(err)
} */
exec.Command("go", "build", "-o", "test_bin", "zab2.go").Run()
server = exec.Command("./test_bin", "-s", "multi-axis-test", "-h", "--config=config_test.yaml")
err := server.Start()
time.Sleep(5 * time.Second)
if err != nil {
log.Fatal(err)
}
}
func checkRun() {
if server.ProcessState.Exited() {
server.Start()
time.Sleep(1 * time.Second)
}
}