|
| 1 | +import unittest |
| 2 | + |
| 3 | +from alertaclient.api import Client |
| 4 | + |
| 5 | + |
| 6 | +class AlertTestCase(unittest.TestCase): |
| 7 | + |
| 8 | + def setUp(self): |
| 9 | + self.client = Client(endpoint='http://api:8080/api', key='demo-key') |
| 10 | + |
| 11 | + def test_alert(self): |
| 12 | + id, alert, message = self.client.send_alert( |
| 13 | + environment='Production', resource='web01', event='node_down', correlated=['node_up', 'node_down'], |
| 14 | + service=['Web', 'App'], severity='critical', tags=['london', 'linux'], value=4 |
| 15 | + ) |
| 16 | + self.assertEqual(alert.value, '4') # values cast to string |
| 17 | + self.assertEqual(alert.timeout, 86400) # timeout returned as int |
| 18 | + self.assertIn('london', alert.tags) |
| 19 | + |
| 20 | + def test_blackout(self): |
| 21 | + blackout = self.client.create_blackout( |
| 22 | + environment='Production', service=['Web', 'App'], resource='web01', event='node_down', group='Network', tags=['london', 'linux'] |
| 23 | + ) |
| 24 | + self.assertEqual(blackout.environment, 'Production') |
| 25 | + self.assertEqual(blackout.service, ['Web', 'App']) |
| 26 | + self.assertIn('london', blackout.tags) |
| 27 | + self.assertIn('linux', blackout.tags) |
| 28 | + |
| 29 | + def test_customer(self): |
| 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') |
| 33 | + |
| 34 | + def test_group(self): |
| 35 | + group = self.client.create_group(name='myGroup', text='test group') |
| 36 | + self.assertEqual(group.name, 'myGroup') |
| 37 | + self.assertEqual(group.text, 'test group') |
| 38 | + |
| 39 | + def test_heartbeat(self): |
| 40 | + hb = self.client.heartbeat(origin='app/web01', timeout=10, tags=['london', 'linux']) |
| 41 | + self.assertEqual(hb.origin, 'app/web01') |
| 42 | + self.assertEqual(hb.event_type, 'Heartbeat') |
| 43 | + self.assertEqual(hb.timeout, 10) |
| 44 | + self.assertIn('linux', hb.tags) |
| 45 | + |
| 46 | + def test_history(self): |
| 47 | + hist = self.client.get_history() |
| 48 | + self.assertEqual(hist[0].environment, 'Production') |
| 49 | + self.assertEqual(hist[0].service, ['Web', 'App']) |
| 50 | + self.assertEqual(hist[0].resource, 'web01') |
| 51 | + self.assertIn('london', hist[0].tags) |
| 52 | + self.assertEqual(hist[0].change_type, 'new') |
| 53 | + |
| 54 | + def test_key(self): |
| 55 | + api_key = self.client.create_key( |
| 56 | + username='johndoe@example.com', scopes=['write:alerts', 'admin:keys'], text='Ops API Key' |
| 57 | + ) |
| 58 | + self.assertEqual(api_key.user, 'johndoe@example.com') |
| 59 | + self.assertEqual(sorted(api_key.scopes), sorted(['write:alerts', 'admin:keys'])) |
| 60 | + |
| 61 | + # def test_note(self): |
| 62 | + # n = self.client.alert_note(id='e7020428-5dad-4a41-9bfe-78e9d55cda06', note='this is a test note') |
| 63 | + # self.assertEqual(n.text, 'this is a test note') |
| 64 | + |
| 65 | + def test_permission(self): |
| 66 | + perm = self.client.create_perm(role='websys', scopes=['admin:users', 'admin:keys', 'write']) |
| 67 | + self.assertEqual(perm.match, 'websys') |
| 68 | + self.assertEqual(sorted(perm.scopes), sorted(['admin:users', 'admin:keys', 'write'])) |
| 69 | + |
| 70 | + def test_user(self): |
| 71 | + users = self.client.get_users() |
| 72 | + self.assertEqual(users[0].name, 'admin@alerta.io') |
| 73 | + self.assertEqual(sorted(users[0].roles), sorted(['admin'])) |
| 74 | + self.assertEqual(users[0].status, 'active') |
0 commit comments