-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrafana_test.go
103 lines (90 loc) · 2.65 KB
/
grafana_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
package main
import (
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestIndex(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
index(w, req)
resp := w.Result()
assert.Equal(t, 200, resp.StatusCode)
}
func handleAnnotationRequest(payload string, fn AnnotationHandler) *http.Response {
reqBody := strings.NewReader(payload)
req := httptest.NewRequest("POST", "/", reqBody)
w := httptest.NewRecorder()
ds := &DataSource{}
ds.HandleAnnotation(fn)
handler := withDataSource(http.HandlerFunc(annotations), *ds)
handler.ServeHTTP(w, req)
return w.Result()
}
func TestAnnotationsInvalidRequest(t *testing.T) {
resp := handleAnnotationRequest("invalid", nil)
body, _ := ioutil.ReadAll(resp.Body)
assert.Equal(t, 400, resp.StatusCode)
assert.Equal(t, "decode error invalid character 'i' looking for beginning of value\n", string(body))
}
func TestAnnotationsValidRequest(t *testing.T) {
resp := handleAnnotationRequest(`
{
"annotation": {
"name": "Deploys",
"Datasource": "My datasource",
"IconColor": "red",
"Enable": true,
"Query": "query"
},
"range": {
"from": "2020-01-02T12:00:00.0Z",
"to": "2020-01-07T20:00:00.0Z"
}
}
`, func(req AnnotationsRequest) ([]AnnotationsResponse, error) {
assert.Equal(t, Annotation{Name: "Deploys", Datasource: "My datasource",
IconColor: "red", Enable: true, Query: "query"}, req.Annotation)
assert.Equal(t, time.Date(2020, 1, 2, 12, 0, 0, 0, time.UTC), req.Range.From)
assert.Equal(t, time.Date(2020, 1, 7, 20, 0, 0, 0, time.UTC), req.Range.To)
res := []AnnotationsResponse{
AnnotationsResponse{
Annotation: req.Annotation,
Time: 123123,
Title: "skynet activation",
},
}
return res, nil
})
body, _ := ioutil.ReadAll(resp.Body)
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, `[{"annotation":{"name":"Deploys","datasource":"My datasource","iconColor":"red","enable":true,"query":"query"},"time":123123,"title":"skynet activation"}]
`, string(body))
}
func TestAnnotationsValidRequestErrorResponse(t *testing.T) {
resp := handleAnnotationRequest(`
{
"annotation": {
"name": "Deploys",
"Datasource": "My datasource",
"IconColor": "red",
"Enable": true,
"Query": "query"
},
"range": {
"from": "2020-01-02T12:00:00.0Z",
"to": "2020-01-07T20:00:00.0Z"
}
}
`, func(req AnnotationsRequest) ([]AnnotationsResponse, error) {
return nil, errors.New("something bad happened")
})
body, _ := ioutil.ReadAll(resp.Body)
assert.Equal(t, 400, resp.StatusCode)
assert.Equal(t, "something bad happened\n", string(body))
}