@@ -2,13 +2,47 @@ package gapi
2
2
3
3
import (
4
4
"encoding/json"
5
+ "strings"
5
6
"testing"
6
7
"time"
7
8
8
9
"github.com/gobs/pretty"
9
10
)
10
11
11
12
func TestAlertRules (t * testing.T ) {
13
+ mockData := strings .Repeat (getAlertRulesJSON + "," , 1000 ) // make 1000 alertRules.
14
+ mockData = "[" + mockData [:len (mockData )- 1 ] + "]" // remove trailing comma; make a json list.
15
+
16
+ // This creates 1000 + 1000 + 1 (2001, 3 calls) worth of alertRules.
17
+
18
+ client := gapiTestToolsFromCalls (t , []mockServerCall {
19
+ {200 , mockData },
20
+ {200 , mockData },
21
+ {200 , "[" + getAlertRulesJSON + "]" },
22
+ })
23
+
24
+ const dashCount = 2001
25
+
26
+ alertRules , err := client .AlertRules ()
27
+ if err != nil {
28
+ t .Fatal (err )
29
+ }
30
+
31
+ t .Log (pretty .PrettyFormat (alertRules ))
32
+
33
+ if len (alertRules ) != dashCount {
34
+ t .Errorf ("Length of returned folders should be %d" , dashCount )
35
+ }
36
+
37
+ if alertRules [0 ].UID != "123abcd" || alertRules [0 ].Title != "Always in alarm" {
38
+ t .Error ("Not correctly parsing returned alertRules." )
39
+ }
40
+ if alertRules [dashCount - 1 ].UID != "123abcd" || alertRules [dashCount - 1 ].Title != "Always in alarm" {
41
+ t .Error ("Not correctly parsing returned alertRules." )
42
+ }
43
+ }
44
+
45
+ func TestAlertRule (t * testing.T ) {
12
46
t .Run ("get alert rule succeeds" , func (t * testing.T ) {
13
47
client := gapiTestTools (t , 200 , getAlertRuleJSON )
14
48
@@ -161,7 +195,18 @@ const writeAlertRuleJSON = `
161
195
"for": "1m"
162
196
}
163
197
`
164
-
198
+ const getAlertRulesJSON = `{
199
+ "conditions": "A",
200
+ "data": [{"datasourceUid":"-100","model":{"conditions":[{"evaluator":{"params":[0,0],"type":"gt"},"operator":{"type":"and"},"query":{"params":[]},"reducer":{"params":[],"type":"avg"},"type":"query"}],"datasource":{"type":"__expr__","uid":"__expr__"},"expression":"1 == 1","hide":false,"intervalMs":1000,"maxDataPoints":43200,"refId":"A","type":"math"},"queryType":"","refId":"A","relativeTimeRange":{"from":0,"to":0}}],
201
+ "execErrState": "OK",
202
+ "folderUID": "project_test",
203
+ "noDataState": "OK",
204
+ "orgId": 1,
205
+ "uid": "123abcd",
206
+ "ruleGroup": "eval_group_1",
207
+ "title": "Always in alarm",
208
+ "for": "1m"
209
+ }`
165
210
const getAlertRuleJSON = `
166
211
{
167
212
"conditions": "A",
0 commit comments