Skip to content

Commit 177b256

Browse files
committed
Add test case for each resource endpoint
1 parent 141d4fa commit 177b256

9 files changed

Lines changed: 343 additions & 74 deletions

File tree

alertaclient/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ def get_services(self, query=None):
105105
return counts['services']
106106

107107
# Blackouts
108-
def create_blackout(self, environment, service, resource, event, group, tag, start, duration):
108+
def create_blackout(self, environment, service=None, resource=None, event=None, group=None, tags=None, start=None, duration=None):
109109
data = {
110110
'environment': environment,
111111
'service': service,
112112
'resource': resource,
113113
'event': event,
114114
'group': group,
115-
'tags': tag,
115+
'tags': tags,
116116
'startTime': start,
117117
'duration': duration
118118
}

tests/test_alerts.py

Lines changed: 26 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,110 +5,64 @@
55

66
from alertaclient.api import Client
77

8-
try:
9-
from unittest import mock
10-
except ImportError:
11-
import mock
12-
138

149
class AlertTestCase(unittest.TestCase):
1510

1611
def setUp(self):
1712
self.client = Client()
1813

1914
self.alert = """
20-
{
21-
"alert": {
15+
{
16+
"alert": {
2217
"attributes": {
23-
"ip": "127.0.0.1"
18+
"ip": "127.0.0.1",
19+
"notify": false
2420
},
2521
"correlate": [],
26-
"createTime": "2017-10-02T23:51:10.748Z",
22+
"createTime": "2017-10-03T09:12:27.283Z",
2723
"customer": null,
28-
"duplicateCount": 0,
24+
"duplicateCount": 4,
2925
"environment": "Production",
3026
"event": "node_down",
3127
"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",
5034
"previousSeverity": "indeterminate",
5135
"rawData": null,
52-
"receiveTime": "2017-10-02T23:51:10.750Z",
53-
"repeat": false,
36+
"receiveTime": "2017-10-03T09:12:27.289Z",
37+
"repeat": true,
5438
"resource": "web01",
5539
"service": [
56-
"Web",
57-
"App"
40+
"Web",
41+
"App"
5842
],
5943
"severity": "critical",
6044
"status": "open",
6145
"tags": [
62-
"london",
63-
"linux"
46+
"london",
47+
"linux"
6448
],
6549
"text": "",
6650
"timeout": 86400,
6751
"trendIndication": "moreSevere",
6852
"type": "exceptionAlert",
6953
"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+
}
9858
"""
9959

10060
@requests_mock.mock()
10161
def test_alert(self, m):
10262
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+
)
10467
self.assertEqual(alert.value, "4") # values cast to string
10568
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)

tests/test_blackouts.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
import unittest
3+
4+
import requests_mock
5+
6+
from alertaclient.api import Client
7+
8+
9+
class BlackoutTestCase(unittest.TestCase):
10+
11+
def setUp(self):
12+
self.client = Client()
13+
14+
self.blackout = """
15+
{
16+
"blackout": {
17+
"customer": null,
18+
"duration": 300,
19+
"endTime": "2017-10-03T08:26:00.948Z",
20+
"environment": "Production",
21+
"event": "node_down",
22+
"group": "Network",
23+
"href": "http://localhost:8080/blackout/8eb1504f-cb48-433d-854c-b31e06284af9",
24+
"id": "8eb1504f-cb48-433d-854c-b31e06284af9",
25+
"priority": 3,
26+
"remaining": 299,
27+
"resource": "web01",
28+
"service": [
29+
"Web",
30+
"App"
31+
],
32+
"startTime": "2017-10-03T08:21:00.948Z",
33+
"status": "active",
34+
"tags": [
35+
"london",
36+
"linux"
37+
]
38+
},
39+
"id": "8eb1504f-cb48-433d-854c-b31e06284af9",
40+
"status": "ok"
41+
}
42+
"""
43+
44+
@requests_mock.mock()
45+
def test_blackout(self, m):
46+
m.post('http://localhost:8080/blackout', text=self.blackout)
47+
alert = self.client.create_blackout(environment='Production', service=['Web', 'App'], resource='web01', event='node_down', group='Network', tags=["london", "linux"])
48+
self.assertEqual(alert.environment, 'Production')
49+
self.assertEqual(alert.service, ['Web', 'App'])
50+
self.assertEqual(alert.resource, 'web01')
51+
self.assertIn("london", alert.tags)

tests/test_customers.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
import unittest
3+
4+
import requests_mock
5+
6+
from alertaclient.api import Client
7+
8+
9+
class CustomerTestCase(unittest.TestCase):
10+
11+
def setUp(self):
12+
self.client = Client()
13+
14+
self.customer = """
15+
{
16+
"customer": {
17+
"customer": "ACME Corp.",
18+
"href": "http://localhost:8080/customer/9bb97023-186e-4744-a59d-d18f641eee52",
19+
"id": "9bb97023-186e-4744-a59d-d18f641eee52",
20+
"match": "example.com"
21+
},
22+
"id": "9bb97023-186e-4744-a59d-d18f641eee52",
23+
"status": "ok"
24+
}
25+
"""
26+
27+
@requests_mock.mock()
28+
def test_customer(self, m):
29+
m.post('http://localhost:8080/customer', text=self.customer)
30+
customer = self.client.create_customer(customer='ACME Corp.', match='example.com')
31+
self.assertEqual(customer.customer, 'ACME Corp.')
32+
self.assertEqual(customer.match, 'example.com')

tests/test_heartbeats.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
import unittest
3+
4+
import requests_mock
5+
6+
from alertaclient.api import Client
7+
8+
9+
class HeartbeatTestCase(unittest.TestCase):
10+
11+
def setUp(self):
12+
self.client = Client()
13+
14+
self.heartbeat = """
15+
{
16+
"heartbeat": {
17+
"createTime": "2017-10-02T23:54:05.214Z",
18+
"customer": null,
19+
"href": "http://localhost:8080/heartbeat/4a0b87cd-9786-48f8-9994-59a9209ff0b2",
20+
"id": "4a0b87cd-9786-48f8-9994-59a9209ff0b2",
21+
"latency": 0.0,
22+
"origin": "app/web01",
23+
"receiveTime": "2017-10-02T23:54:05.214Z",
24+
"since": 0,
25+
"status": "ok",
26+
"tags": [
27+
"london",
28+
"linux"
29+
],
30+
"timeout": 10,
31+
"type": "Heartbeat"
32+
},
33+
"id": "4a0b87cd-9786-48f8-9994-59a9209ff0b2",
34+
"status": "ok"
35+
}
36+
"""
37+
38+
@requests_mock.mock()
39+
def test_heartbeat(self, m):
40+
m.post('http://localhost:8080/heartbeat', text=self.heartbeat)
41+
hb = self.client.heartbeat(origin='app/web01', timeout=10, tags=["london", "linux"])
42+
self.assertEqual(hb.origin, 'app/web01')
43+
self.assertEqual(hb.event_type, 'Heartbeat')
44+
self.assertEqual(hb.timeout, 10)
45+
self.assertIn("linux", hb.tags)

tests/test_history.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
import unittest
3+
4+
import requests_mock
5+
6+
from alertaclient.api import Client
7+
8+
9+
class HistoryTestCase(unittest.TestCase):
10+
11+
def setUp(self):
12+
self.client = Client()
13+
14+
self.history = """
15+
{
16+
"history": [
17+
{
18+
"attributes": {
19+
"ip": "127.0.0.1",
20+
"notify": false
21+
},
22+
"customer": null,
23+
"environment": "Production",
24+
"event": "node_down",
25+
"group": "Misc",
26+
"href": "http://localhost:8080/alert/e7020428-5dad-4a41-9bfe-78e9d55cda06",
27+
"id": "e7020428-5dad-4a41-9bfe-78e9d55cda06",
28+
"origin": "alertad/fdaa33ca.local",
29+
"resource": "web01",
30+
"service": [
31+
"Web",
32+
"App"
33+
],
34+
"severity": "critical",
35+
"tags": [
36+
"london",
37+
"linux"
38+
],
39+
"text": "",
40+
"type": "severity",
41+
"updateTime": "2017-10-03T09:12:27.283Z",
42+
"value": "4"
43+
}
44+
],
45+
"status": "ok",
46+
"total": 1
47+
}
48+
"""
49+
50+
@requests_mock.mock()
51+
def test_history(self, m):
52+
m.get('http://localhost:8080/alerts/history', text=self.history)
53+
hist = self.client.get_history()
54+
self.assertEqual(hist[0].environment, 'Production')
55+
self.assertEqual(hist[0].service, ['Web', 'App'])
56+
self.assertEqual(hist[0].resource, 'web01')
57+
self.assertIn("london", hist[0].tags)

tests/test_keys.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
import unittest
3+
4+
import requests_mock
5+
6+
from alertaclient.api import Client
7+
8+
9+
class ApiKeyTestCase(unittest.TestCase):
10+
11+
def setUp(self):
12+
self.client = Client()
13+
14+
self.key = """
15+
{
16+
"data": {
17+
"count": 0,
18+
"customer": null,
19+
"expireTime": "2018-10-03T08:36:14.651Z",
20+
"href": "http://localhost:8080/key/BpSG0Ck5JCqk5TJiuBSLAWuTs03QKc_527T5cDtw",
21+
"id": "f4203347-d1b2-4f56-b5e9-6de97cf2d8ae",
22+
"key": "BpSG0Ck5JCqk5TJiuBSLAWuTs03QKc_527T5cDtw",
23+
"lastUsedTime": null,
24+
"scopes": [
25+
"write:alerts",
26+
"admin:keys"
27+
],
28+
"text": "Ops kAPI Key",
29+
"type": "read-write",
30+
"user": "johndoe@example.com"
31+
},
32+
"key": "BpSG0Ck5JCqk5TJiuBSLAWuTs03QKc_527T5cDtw",
33+
"status": "ok"
34+
}
35+
"""
36+
37+
@requests_mock.mock()
38+
def test_key(self, m):
39+
m.post('http://localhost:8080/key', text=self.key)
40+
api_key = self.client.create_key(username='johndoe@example.com', scopes=['write:alerts', 'admin:keys'], text='Ops API Key')
41+
self.assertEqual(api_key.user, 'johndoe@example.com')
42+
self.assertEqual(sorted(api_key.scopes),sorted(['write:alerts', 'admin:keys']))

0 commit comments

Comments
 (0)