|
5 | 5 |
|
6 | 6 | from alertaclient.api import Client |
7 | 7 |
|
8 | | -try: |
9 | | - from unittest import mock |
10 | | -except ImportError: |
11 | | - import mock |
12 | | - |
13 | 8 |
|
14 | 9 | class AlertTestCase(unittest.TestCase): |
15 | 10 |
|
16 | 11 | def setUp(self): |
17 | 12 | self.client = Client() |
18 | 13 |
|
19 | 14 | self.alert = """ |
20 | | - { |
21 | | - "alert": { |
| 15 | + { |
| 16 | + "alert": { |
22 | 17 | "attributes": { |
23 | | - "ip": "127.0.0.1" |
| 18 | + "ip": "127.0.0.1", |
| 19 | + "notify": false |
24 | 20 | }, |
25 | 21 | "correlate": [], |
26 | | - "createTime": "2017-10-02T23:51:10.748Z", |
| 22 | + "createTime": "2017-10-03T09:12:27.283Z", |
27 | 23 | "customer": null, |
28 | | - "duplicateCount": 0, |
| 24 | + "duplicateCount": 4, |
29 | 25 | "environment": "Production", |
30 | 26 | "event": "node_down", |
31 | 27 | "group": "Misc", |
32 | | - "history": [ |
33 | | - { |
34 | | - "event": "node_down", |
35 | | - "href": "http://localhost:8080/alert/f12a96f5-64a3-4aaa-aaaa-1f3ffe2078e5", |
36 | | - "id": "f12a96f5-64a3-4aaa-aaaa-1f3ffe2078e5", |
37 | | - "severity": "critical", |
38 | | - "status": null, |
39 | | - "text": "", |
40 | | - "type": "severity", |
41 | | - "updateTime": "2017-10-02T23:51:10.748Z", |
42 | | - "value": null |
43 | | - } |
44 | | - ], |
45 | | - "href": "http://localhost:8080/alert/f12a96f5-64a3-4aaa-aaaa-1f3ffe2078e5", |
46 | | - "id": "f12a96f5-64a3-4aaa-aaaa-1f3ffe2078e5", |
47 | | - "lastReceiveId": "f12a96f5-64a3-4aaa-aaaa-1f3ffe2078e5", |
48 | | - "lastReceiveTime": "2017-10-02T23:51:10.750Z", |
49 | | - "origin": "alertad/fdaa33ca.home", |
| 28 | + "history": [], |
| 29 | + "href": "http://localhost:8080/alert/e7020428-5dad-4a41-9bfe-78e9d55cda06", |
| 30 | + "id": "e7020428-5dad-4a41-9bfe-78e9d55cda06", |
| 31 | + "lastReceiveId": "534ced13-ddb0-435e-8f94-a38691719683", |
| 32 | + "lastReceiveTime": "2017-10-03T09:15:06.156Z", |
| 33 | + "origin": "alertad/fdaa33ca.local", |
50 | 34 | "previousSeverity": "indeterminate", |
51 | 35 | "rawData": null, |
52 | | - "receiveTime": "2017-10-02T23:51:10.750Z", |
53 | | - "repeat": false, |
| 36 | + "receiveTime": "2017-10-03T09:12:27.289Z", |
| 37 | + "repeat": true, |
54 | 38 | "resource": "web01", |
55 | 39 | "service": [ |
56 | | - "Web", |
57 | | - "App" |
| 40 | + "Web", |
| 41 | + "App" |
58 | 42 | ], |
59 | 43 | "severity": "critical", |
60 | 44 | "status": "open", |
61 | 45 | "tags": [ |
62 | | - "london", |
63 | | - "linux" |
| 46 | + "london", |
| 47 | + "linux" |
64 | 48 | ], |
65 | 49 | "text": "", |
66 | 50 | "timeout": 86400, |
67 | 51 | "trendIndication": "moreSevere", |
68 | 52 | "type": "exceptionAlert", |
69 | 53 | "value": "4" |
70 | | - }, |
71 | | - "id": "f12a96f5-64a3-4aaa-aaaa-1f3ffe2078e5", |
72 | | - "status": "ok" |
73 | | - } |
74 | | - """ |
75 | | - |
76 | | - self.heartbeat = """ |
77 | | - { |
78 | | - "heartbeat": { |
79 | | - "createTime": "2017-10-02T23:54:05.214Z", |
80 | | - "customer": null, |
81 | | - "href": "http://localhost:8080/heartbeat/4a0b87cd-9786-48f8-9994-59a9209ff0b2", |
82 | | - "id": "4a0b87cd-9786-48f8-9994-59a9209ff0b2", |
83 | | - "latency": 0.0, |
84 | | - "origin": "app/web01", |
85 | | - "receiveTime": "2017-10-02T23:54:05.214Z", |
86 | | - "since": 0, |
87 | | - "status": "ok", |
88 | | - "tags": [ |
89 | | - "london", |
90 | | - "linux" |
91 | | - ], |
92 | | - "timeout": 10, |
93 | | - "type": "Heartbeat" |
94 | | - }, |
95 | | - "id": "4a0b87cd-9786-48f8-9994-59a9209ff0b2", |
96 | | - "status": "ok" |
97 | | - } |
| 54 | + }, |
| 55 | + "id": "e7020428-5dad-4a41-9bfe-78e9d55cda06", |
| 56 | + "status": "ok" |
| 57 | + } |
98 | 58 | """ |
99 | 59 |
|
100 | 60 | @requests_mock.mock() |
101 | 61 | def test_alert(self, m): |
102 | 62 | m.post('http://localhost:8080/alert', text=self.alert) |
103 | | - alert = self.client.send_alert(resource='web01', event='node_down', tags=["london", "linux"], value=4) |
| 63 | + alert = self.client.send_alert( |
| 64 | + environment='Production', resource='web01', event='node_down', correlated=['node_up', 'node_down'], |
| 65 | + service=['Web', 'App'], severity='critical', tags=["london", "linux"], value=4 |
| 66 | + ) |
104 | 67 | self.assertEqual(alert.value, "4") # values cast to string |
105 | 68 | self.assertIn("london", alert.tags) |
106 | | - |
107 | | - @requests_mock.mock() |
108 | | - def test_heartbeat(self, m): |
109 | | - m.post('http://localhost:8080/heartbeat', text=self.heartbeat) |
110 | | - hb = self.client.heartbeat(origin='app/web01', timeout=10, tags=["london", "linux"]) |
111 | | - self.assertEqual(hb.origin, 'app/web01') |
112 | | - self.assertEqual(hb.event_type, 'Heartbeat') |
113 | | - self.assertEqual(hb.timeout, 10) |
114 | | - self.assertIn("linux", hb.tags) |
0 commit comments