Skip to content

Commit cca74a8

Browse files
Maharachaamotl
authored andcommitted
Added tests for Alerting.
1 parent c0d58ba commit cca74a8

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

test/elements/test_alerting.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import unittest
2+
3+
import requests_mock
4+
5+
from grafana_client import GrafanaApi
6+
7+
ALERTRULE = {
8+
"name": "alert-rule-test",
9+
"id": 2,
10+
"uid": "bUUGqLiVk",
11+
"orgID": 1,
12+
"folderUID": "4",
13+
"ruleGroup": "Any",
14+
"title": "ApiAlerts",
15+
"condition": "B",
16+
"data": [],
17+
"updated": "2022-08-17T13:00:21Z",
18+
"noDataState": "NoData",
19+
"execErrState": "Alerting",
20+
"for": 300000000000,
21+
"labels": {"alertGroup": "any", "region": "eu-west-1"},
22+
}
23+
24+
25+
class AlertingTestCase(unittest.TestCase):
26+
def setUp(self):
27+
self.grafana = GrafanaApi(("admin", "admin"), host="localhost", url_path_prefix="", protocol="http")
28+
29+
@requests_mock.Mocker()
30+
def test_get_alertrule(self, m):
31+
m.get("http://localhost/api/ruler/grafana/api/v1/rules/alert-folder/alert-rule-test", json=ALERTRULE)
32+
response = self.grafana.alerting.get_alertrule("alert-folder", "alert-rule-test")
33+
self.assertEqual(response["uid"], "bUUGqLiVk")
34+
self.assertEqual(response["name"], "alert-rule-test")
35+
36+
@requests_mock.Mocker()
37+
def test_delete_alertrule(self, m):
38+
m.delete("http://localhost/api/ruler/grafana/api/v1/rules/alert-folder/alert-rule-test", json={"uid": "bUUGqLiVk"})
39+
response = self.grafana.alerting.delete_alertrule("alert-folder", "alert-rule-test")
40+
self.assertEqual(response["uid"], "bUUGqLiVk")
41+
42+
@requests_mock.Mocker()
43+
def test_create_alertrule(self, m):
44+
m.post(
45+
"http://localhost/api/ruler/grafana/api/v1/rules/alert-folder",
46+
json=ALERTRULE,
47+
)
48+
49+
response = self.grafana.alerting.create_alertrule("alert-folder", ALERTRULE)
50+
self.assertEqual(response["uid"], "bUUGqLiVk")
51+
self.assertEqual(response["name"], "alert-rule-test")
52+
53+
@requests_mock.Mocker()
54+
def test_update_alertrule(self, m):
55+
m.post(
56+
"http://localhost/api/ruler/grafana/api/v1/rules/alert-folder",
57+
json=ALERTRULE,
58+
)
59+
60+
response = self.grafana.alerting.update_alertrule("alert-folder", alertrule=ALERTRULE)
61+
self.assertEqual(response["uid"], "bUUGqLiVk")

0 commit comments

Comments
 (0)