-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdo-backup_test.go
422 lines (331 loc) · 17.5 KB
/
do-backup_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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package main
import (
"fmt"
"net/http"
"os"
"strings"
"testing"
"github.com/jarcoal/httpmock"
"path"
)
//TODO: Create a function to validate the existence of a set of files instead of writing that code in every test.
//TODO: Create a function to clean up a set of files you pass it.
//TODO: Create some sub functions for these so that we can be more specific on what fails each test. I.E. NoResponder should't cause us to fail Accept Header, body, etc.
//TODO: Create some tests which include the following flags: timeout, tag, title, starred, force, verbose
func TestBackupDashboards(t *testing.T) {
*flagServerURL = "http://noserver.nodomain.com:3123"
*flagServerKey = "thisisnotreallyanapikey"
*flagVerbose = true
*flagApplyFor = "dashboards"
*flagDir = "/var/tmp/testDBOutDir"
argCommand = "backup"
// Some variables to track the results of the test
// Accept header.
acceptCorrect := false
// Content type header.
cTypeCorrect := false
// Track how many times the API was called.
numRequests := 0
// Were any requests made to other URIs?
wrongUriRequests := false
// Set up httpmock
httpmock.Activate()
defer httpmock.DeactivateAndReset()
//TODO: Break this up into multiple functions so that the NoResponder doesn't cause us to fail Accept Header, body, etc.
// Create a responder for /api/search which resturns the list of dashboards (and a tiny bit more.)
httpmock.RegisterResponder("GET", *flagServerURL + "/api/search",
func(req *http.Request) (*http.Response, error) {
numRequests++
// This is the kind of stuff we see in a request. I don't think it's neccessary to check things like the host and URL but maybe.
//Request headers:
//map[Content-Type:[application/json] User-Agent:[autograf] Authorization:[Bearer thisisnotreallyanapikey] Accept:[application/json]]
//Request Host: noserver.nodomain.com:3123
//Request Method: GET
//Request Protocol: HTTP/1.1
//Request RemoteAddr:
//Request URI:
//Request URL: http://noserver.nodomain.com:3123/api/datasources
if strings.Contains(req.Header.Get("Accept"), "application/json") {
acceptCorrect = true
}
if strings.Contains(req.Header.Get("Content-type"), "application/json") {
cTypeCorrect = true
}
return httpmock.NewStringResponse(200,
`[
{
"id": 31,
"title": "Test Dashboard 1",
"uri": "db/test-dashboard-1",
"type": "dash-db",
"tags": [
"tagone",
"tagtwo"
],
"isStarred": false
},
{
"id": 32,
"title": "Test Dashboard 2",
"uri": "db/test-dashboard-2",
"type": "dash-db",
"tags": [],
"isStarred": false
},
{
"id": 33,
"title": "Test Dashboard 3",
"uri": "db/test-dashboard-3",
"type": "dash-db",
"tags": [],
"isStarred": false
}
]
`), nil
},
)
// TODO: Tune these dashboards so that they reflect more Grafana features which is the whole point of having three different ones.
// Create a responder for Test Dashboard 1
httpmock.RegisterResponder("GET", *flagServerURL + "/api/dashboards/db/test-dashboard-1",
func(req *http.Request) (*http.Response, error) {
numRequests++
if strings.Contains(req.Header.Get("Accept"), "application/json") {
acceptCorrect = true
}
if strings.Contains(req.Header.Get("Content-type"), "application/json") {
cTypeCorrect = true
}
return httpmock.NewStringResponse(200,
`{"meta":{"type":"db","canSave":true,"canEdit":true,"canStar":true,"slug":"test-dashboard-1","expires":"0001-01-01T00:00:00Z","created":"2017-11-24T09:05:33-08:00","updated":"2017-11-24T09:35:26-08:00","updatedBy":"admin","createdBy":"admin","version":3},"dashboard":{"annotations":{"list":[]},"editable":true,"gnetId":null,"graphTooltip":0,"hideControls":false,"id":31,"links":[],"rows":[{"collapse":false,"height":250,"panels":[{"aliasColors":{},"bars":false,"dashLength":10,"dashes":false,"datasource":"prometheus-test","fill":1,"id":2,"legend":{"avg":false,"current":false,"max":false,"min":false,"show":true,"total":false,"values":false},"lines":true,"linewidth":1,"links":[],"nullPointMode":"null","percentage":false,"pointradius":5,"points":false,"renderer":"flot","seriesOverrides":[],"spaceLength":10,"span":12,"stack":false,"steppedLine":false,"targets":[{"expr":"( \t100 \t* \t( \t\t1 \t\t- \t\tavg( \t\t\tirate( \t\t\t\tnode_cpu{job=\"node\",mode=\"idle\",Team=~\"$Team\"}[5m] \t\t\t) \t\t) BY (instance) \t) )","format":"time_series","intervalFactor":2,"legendFormat":"{{ instance }}","refId":"A","step":20}],"thresholds":[],"timeFrom":null,"timeShift":null,"title":"CPU by Team","tooltip":{"shared":true,"sort":0,"value_type":"individual"},"type":"graph","xaxis":{"buckets":null,"mode":"time","name":null,"show":true,"values":[]},"yaxes":[{"format":"short","label":null,"logBase":1,"max":null,"min":null,"show":true},{"format":"short","label":null,"logBase":1,"max":null,"min":null,"show":true}]}],"repeat":null,"repeatIteration":null,"repeatRowId":null,"showTitle":false,"title":"Dashboard Row","titleSize":"h6"}],"schemaVersion":14,"style":"dark","tags":["tagone","tagtwo"],"templating":{"list":[{"allValue":null,"current":{"text":"ISLab","value":"ISLab"},"datasource":"prometheus-test","hide":0,"includeAll":false,"label":null,"multi":false,"name":"Team","options":[],"query":"label_values(node_boot_time, Team)","refresh":2,"regex":"","sort":0,"tagValuesQuery":"","tags":[],"tagsQuery":"","type":"query","useTags":false}]},"time":{"from":"now-6h","to":"now"},"timepicker":{"refresh_intervals":["5s","10s","30s","1m","5m","15m","30m","1h","2h","1d"],"time_options":["5m","15m","1h","6h","12h","24h","2d","7d","30d"]},"timezone":"","title":"Test Dashboard 1","version":3}}`), nil
},
)
// Create a responder for Test Dashboard 2
httpmock.RegisterResponder("GET", *flagServerURL + "/api/dashboards/db/test-dashboard-2",
func(req *http.Request) (*http.Response, error) {
numRequests++
if strings.Contains(req.Header.Get("Accept"), "application/json") {
acceptCorrect = true
}
if strings.Contains(req.Header.Get("Content-type"), "application/json") {
cTypeCorrect = true
}
return httpmock.NewStringResponse(200,
`{"meta":{"type":"db","canSave":true,"canEdit":true,"canStar":true,"slug":"test-dashboard-2","expires":"0001-01-01T00:00:00Z","created":"2017-11-24T09:36:03-08:00","updated":"2017-11-24T09:36:03-08:00","updatedBy":"admin","createdBy":"admin","version":1},"dashboard":{"annotations":{"list":[]},"editable":true,"gnetId":null,"graphTooltip":0,"hideControls":false,"id":32,"links":[],"rows":[{"collapse":false,"height":"250px","panels":[{"content":"# This is a Title","id":1,"links":[],"mode":"markdown","span":12,"title":"A Panel Title","type":"text"}],"repeat":null,"repeatIteration":null,"repeatRowId":null,"showTitle":false,"title":"Dashboard Row","titleSize":"h6"}],"schemaVersion":14,"style":"dark","tags":[],"templating":{"list":[]},"time":{"from":"now-6h","to":"now"},"timepicker":{"refresh_intervals":["5s","10s","30s","1m","5m","15m","30m","1h","2h","1d"],"time_options":["5m","15m","1h","6h","12h","24h","2d","7d","30d"]},"timezone":"","title":"Test Dashboard 2","version":1}}`), nil
},
)
// Create a responder for Test Dashboard 3
httpmock.RegisterResponder("GET", *flagServerURL + "/api/dashboards/db/test-dashboard-3",
func(req *http.Request) (*http.Response, error) {
numRequests++
if strings.Contains(req.Header.Get("Accept"), "application/json") {
acceptCorrect = true
}
if strings.Contains(req.Header.Get("Content-type"), "application/json") {
cTypeCorrect = true
}
return httpmock.NewStringResponse(200,
`{"meta":{"type":"db","canSave":true,"canEdit":true,"canStar":true,"slug":"test-dashboard-3","expires":"0001-01-01T00:00:00Z","created":"2017-11-24T09:36:44-08:00","updated":"2017-11-24T09:38:25-08:00","updatedBy":"admin","createdBy":"admin","version":2},"dashboard":{"annotations":{"list":[]},"editable":true,"gnetId":null,"graphTooltip":0,"hideControls":false,"id":33,"links":[],"rows":[{"collapse":false,"height":"250px","panels":[{"content":"# This is also a title","id":1,"links":[],"mode":"markdown","span":12,"title":"Still the Panel Title","type":"text"}],"repeat":null,"repeatIteration":null,"repeatRowId":null,"showTitle":false,"title":"Dashboard Row","titleSize":"h6"},{"collapse":false,"height":250,"panels":[{"cacheTimeout":null,"colorBackground":false,"colorValue":false,"colors":["rgba(245, 54, 54, 0.9)","rgba(237, 129, 40, 0.89)","rgba(50, 172, 45, 0.97)"],"datasource":"Promt2Local","format":"none","gauge":{"maxValue":100,"minValue":0,"show":false,"thresholdLabels":false,"thresholdMarkers":true},"id":2,"interval":null,"links":[],"mappingType":1,"mappingTypes":[{"name":"value to text","value":1},{"name":"range to text","value":2}],"maxDataPoints":100,"nullPointMode":"connected","nullText":null,"postfix":"","postfixFontSize":"50%","prefix":"","prefixFontSize":"50%","rangeMaps":[{"from":"null","text":"N/A","to":"null"}],"span":12,"sparkline":{"fillColor":"rgba(31, 118, 189, 0.18)","full":false,"lineColor":"rgb(31, 120, 193)","show":false},"tableColumn":"","targets":[{"expr":"( \t100 \t* \t( \t\t1 \t\t- \t\tavg( \t\t\tirate( \t\t\t\tnode_cpu{job=\"node\",mode=\"idle\",Team=~\"$Team\"}[5m] \t\t\t) \t\t) BY (instance) \t) )","format":"time_series","intervalFactor":2,"legendFormat":"{{ instance }}","refId":"A","step":600}],"thresholds":"","title":"Singlestat Panel Title","type":"singlestat","valueFontSize":"80%","valueMaps":[{"op":"=","text":"N/A","value":"null"}],"valueName":"avg"}],"repeat":null,"repeatIteration":null,"repeatRowId":null,"showTitle":false,"title":"Dashboard Row","titleSize":"h6"}],"schemaVersion":14,"style":"dark","tags":[],"templating":{"list":[]},"time":{"from":"now-6h","to":"now"},"timepicker":{"refresh_intervals":["5s","10s","30s","1m","5m","15m","30m","1h","2h","1d"],"time_options":["5m","15m","1h","6h","12h","24h","2d","7d","30d"]},"timezone":"","title":"Test Dashboard 3","version":2}}`), nil
},
)
httpmock.RegisterNoResponder(
func(req *http.Request) (*http.Response, error) {
wrongUriRequests = true
// Uncomment for troubleshooting.
fmt.Printf("Request headers: \n%v\n", req.Header)
fmt.Printf("Request Host: %s\n", req.Host)
fmt.Printf("Request Method: %s\n", req.Method)
fmt.Printf("Request Protocol: %s\n", req.Proto)
fmt.Printf("Request RemoteAddr: %s\n", req.RemoteAddr)
fmt.Printf("Request URI: %s\n", req.RequestURI)
fmt.Printf("Request URL: %s\n", req.URL)
// Uncomment for moar troubleshooting
//// Get a string out of the io.ReadCloser
//buf := new(bytes.Buffer)
//buf.ReadFrom(req.Body)
//reqBody := buf.String() // Does a complete copy of the bytes in the buffer.
//
//fmt.Printf("Request body: \n%s\n", reqBody)
// Uncomment for the mostest troubleshooting
fmt.Printf("Unexpected Request: \n%s\n", req)
return httpmock.NewStringResponse(500, `{ "message": "Unexpected request" }`), nil
},
)
// Trigger the actual backup
doBackup(serverInstance, applyFor, matchDashboard)
if acceptCorrect != true {
t.Error("Accept header was invalid.")
//t.Fail()
}
// There should be 4 requests. One for search and one for each of the 3 dashboards.
if numRequests != 4 {
t.Errorf("The /api/search URI was called an incorrect number of times. Actual requests %d", numRequests)
}
if wrongUriRequests != false {
t.Error("Request made to an unexpected URI. See the log for details.")
}
// Check the output files and confirm that they are correct.
//TODO: Confirm that they are correct instead of just confirming that they exist.
//name := "FileOrDir"
fi, err := os.Stat(*flagDir)
if err != nil {
fmt.Println(err)
return
}
if !fi.Mode().IsDir() {
t.Error("Output directory does not exist or is not a directory.")
}
if _, err := os.Stat(path.Join(*flagDir, "test-dashboard-1.db.json")); os.IsNotExist(err) {
t.Error("test-dashboard-1 output file does not exist.")
}
if _, err := os.Stat(path.Join(*flagDir, "test-dashboard-2.db.json")); os.IsNotExist(err) {
t.Error("test-dashboard-2 output file does not exist.")
}
if _, err := os.Stat(path.Join(*flagDir, "test-dashboard-3.db.json")); os.IsNotExist(err) {
t.Error("test-dashboard-3 output file does not exist.")
}
//TODO: Compare the newly created files to the ones in testdata. Even better parse the JSON and check for specific values.
// Cleanup the output files. Clean up each file and directory explicitly so that we don't accidentally rm -rf something important.
err = os.Remove(path.Join(*flagDir, "test-dashboard-1.db.json"))
err = os.Remove(path.Join(*flagDir, "test-dashboard-2.db.json"))
err = os.Remove(path.Join(*flagDir, "test-dashboard-3.db.json"))
err = os.Remove(*flagDir)
if err != nil {
t.Errorf("Unable to clean up output file %s", err)
}
}
//TODO: Create multiple tests which test things like sending multiple files, etc.
func TestBackupDatasources(t *testing.T) {
*flagServerURL = "http://noserver.nodomain.com:3123"
*flagServerKey = "thisisnotreallyanapikey"
*flagApplyFor = "datasources"
*flagDir = "/var/tmp/testDSOutDir"
argCommand = "backup"
// Some variables to track the results of the test
// Accept header.
acceptCorrect := false
// Content type header.
cTypeCorrect := false
// Track how many times the API was called.
numRequests := 0
// Were any requests made to other URIs?
wrongUriRequests := false
// Set up httpmock
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// Create a responder which will respond with valid JSON and check what was posted to us for validity.
httpmock.RegisterResponder("GET", *flagServerURL + "/api/datasources",
func(req *http.Request) (*http.Response, error) {
numRequests++
// This is the kind of stuff we see in a request. I don't think it's neccessary to check things like the host and URL but maybe.
//Request headers:
//map[Content-Type:[application/json] User-Agent:[autograf] Authorization:[Bearer thisisnotreallyanapikey] Accept:[application/json]]
//Request Host: noserver.nodomain.com:3123
//Request Method: GET
//Request Protocol: HTTP/1.1
//Request RemoteAddr:
//Request URI:
//Request URL: http://noserver.nodomain.com:3123/api/datasources
if strings.Contains(req.Header.Get("Accept"), "application/json") {
acceptCorrect = true
}
if strings.Contains(req.Header.Get("Content-type"), "application/json") {
cTypeCorrect = true
}
return httpmock.NewStringResponse(200,
`[
{
"id": 10,
"orgId": 1,
"name": "prometheus-test",
"type": "prometheus",
"typeLogoUrl": "public/app/plugins/datasource/prometheus/img/prometheus_logo.svg",
"access": "direct",
"url": "http://prometheus-test.example.com:9090",
"password": "",
"user": "",
"database": "",
"basicAuth": false,
"isDefault": false,
"jsonData": {}
},
{
"id": 8,
"orgId": 1,
"name": "Promt2Local",
"type": "prometheus",
"typeLogoUrl": "public/app/plugins/datasource/prometheus/img/prometheus_logo.svg",
"access": "direct",
"url": "http://prometheus-test.example.com:9090",
"password": "",
"user": "",
"database": "",
"basicAuth": false,
"isDefault": false,
"jsonData": {}
}
]`), nil
},
)
httpmock.RegisterNoResponder(
func(req *http.Request) (*http.Response, error) {
wrongUriRequests = true
// Uncomment for troubleshooting.
fmt.Printf("Request headers: \n%v\n", req.Header)
fmt.Printf("Request Host: %s\n", req.Host)
fmt.Printf("Request Method: %s\n", req.Method)
fmt.Printf("Request Protocol: %s\n", req.Proto)
fmt.Printf("Request RemoteAddr: %s\n", req.RemoteAddr)
fmt.Printf("Request URI: %s\n", req.RequestURI)
fmt.Printf("Request URL: %s\n", req.URL)
// Uncomment for moar troubleshooting
//// Get a string out of the io.ReadCloser
//buf := new(bytes.Buffer)
//buf.ReadFrom(req.Body)
//reqBody := buf.String() // Does a complete copy of the bytes in the buffer.
//
//fmt.Printf("Request body: \n%s\n", reqBody)
// Uncomment for the mostest troubleshooting
fmt.Printf("Unexpected Request: \n%s\n", req)
return httpmock.NewStringResponse(500, `{ "message": "Unexpected request" }`), nil
},
)
doBackup(serverInstance, applyFor, matchDashboard)
if acceptCorrect != true {
t.Error("Accept header was invalid.")
//t.Fail()
}
if numRequests != 1 {
t.Errorf("The /api/datasources URI was called an incorrect number of times. Actual requests %d", numRequests)
}
if wrongUriRequests != false {
t.Error("Request made to an unexpected URI. See the log for details.")
}
// Check the output files and confirm that they are correct.
//TODO: Confirm that they are correct instead of just confirming that they exist.
//name := "FileOrDir"
fi, err := os.Stat(*flagDir)
if err != nil {
fmt.Println(err)
return
}
if !fi.Mode().IsDir() {
t.Error("Output directory does not exist or is not a directory.")
}
if _, err := os.Stat(path.Join(*flagDir, "prometheus-test.ds.1.json")); os.IsNotExist(err) {
t.Error("prometheus-test output file does not exist.")
}
if _, err := os.Stat(path.Join(*flagDir, "promt2local.ds.1.json")); os.IsNotExist(err) {
t.Error("prom2local file does not exist.")
}
// Cleanup the output files. Clean up each file and directory explicitly so that we don't accidentally rm -rf something important.
err = os.Remove(path.Join(*flagDir, "promt2local.ds.1.json"))
err = os.Remove(path.Join(*flagDir, "prometheus-test.ds.1.json"))
err = os.Remove(*flagDir)
if err != nil {
t.Errorf("Unable to clean up output file %s", err)
}
}
//TODO: Change t.Log to t.Error when ready to implement this.
func TestRestoreUsers(t *testing.T) {
t.Log("Test Restore Users not yet implemented because restoring users is not yet implemented.")
}