This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
alert_test.go
131 lines (106 loc) · 2.77 KB
/
alert_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
package gapi
import (
"net/url"
"strings"
"testing"
"github.com/gobs/pretty"
)
const (
alertsJSON = `[{
"id": 1,
"dashboardId": 1,
"dashboardUId": "ABcdEFghij",
"dashboardSlug": "sensors",
"panelId": 1,
"name": "fire place sensor",
"state": "alerting",
"newStateDate": "2018-05-14T05:55:20+02:00",
"evalDate": "0001-01-01T00:00:00Z",
"evalData": null,
"executionError": "",
"url": "http://grafana.com/dashboard/db/sensors"
}]`
alertJSON = `{
"id": 1,
"dashboardId": 1,
"dashboardUId": "ABcdEFghij",
"dashboardSlug": "sensors",
"panelId": 1,
"name": "fire place sensor",
"state": "alerting",
"message": "Someone is trying to break in through the fire place",
"newStateDate": "2018-05-14T05:55:20+02:00",
"evalDate": "0001-01-01T00:00:00Z",
"executionError": "",
"url": "http://grafana.com/dashboard/db/sensors"
}`
pauseAlertJSON = `{
"alertId": 1,
"state": "Paused",
"message": "alert paused"
}`
)
func TestAlerts(t *testing.T) {
server, client := gapiTestTools(200, alertsJSON)
defer server.Close()
params := url.Values{}
params.Add("dashboardId", "123")
as, err := client.Alerts(params)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(as))
if as[0].ID != 1 {
t.Error("alerts response should contain alerts with an ID")
}
}
func TestAlerts_500(t *testing.T) {
server, client := gapiTestTools(500, alertsJSON)
defer server.Close()
params := url.Values{}
params.Add("dashboardId", "123")
_, err := client.Alerts(params)
if !strings.Contains(err.Error(), "status: 500") {
t.Errorf("expected error to contain 'status: 500'; got: %s", err.Error())
}
}
func TestAlert(t *testing.T) {
server, client := gapiTestTools(200, alertJSON)
defer server.Close()
res, err := client.Alert(1)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
if res.ID != 1 {
t.Error("alert response should contain the ID of the queried alert")
}
}
func TestAlert_500(t *testing.T) {
server, client := gapiTestTools(500, alertJSON)
defer server.Close()
_, err := client.Alert(1)
if !strings.Contains(err.Error(), "status: 500") {
t.Errorf("expected error to contain 'status: 500'; got: %s", err.Error())
}
}
func TestPauseAlert(t *testing.T) {
server, client := gapiTestTools(200, pauseAlertJSON)
defer server.Close()
res, err := client.PauseAlert(1)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
if res.State != "Paused" {
t.Error("pause alert response should contain the correct response message")
}
}
func TestPauseAlert_500(t *testing.T) {
server, client := gapiTestTools(500, pauseAlertJSON)
defer server.Close()
_, err := client.PauseAlert(1)
if !strings.Contains(err.Error(), "status: 500") {
t.Errorf("expected error to contain 'status: 500'; got: %s", err.Error())
}
}