Skip to content

Commit

Permalink
fix: Allow unack when status set to ack in plugin (#1642)
Browse files Browse the repository at this point in the history
* Allow unack when status set to ack in plugin

* Fix race condition for blackout test
  • Loading branch information
satterly committed Dec 5, 2021
1 parent e10f7dd commit f3c51f3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
19 changes: 14 additions & 5 deletions alerta/models/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,27 @@ def get_status_and_value(self):

def _get_hist_info(self, action=None):
h_loop = self.get_alert_history(alert=self)
if not h_loop:
return None, None, None, None

current_status = h_loop[0].status
current_value = h_loop[0].value

if len(h_loop) == 1:
return h_loop[0].status, h_loop[0].value, None, None
return current_status, current_value, None, None
if action == ChangeType.unack:
find = ChangeType.ack
elif action == ChangeType.unshelve:
find = ChangeType.shelve
else:
find = None
for h, h_next in zip(h_loop, h_loop[1:]):
if not find or h.change_type == find:
return h_loop[0].status, h_loop[0].value, h_next.status, h_next.timeout
return None, None, None, None

if find:
for h, h_next in zip(h_loop, h_loop[1:]):
if h.change_type == find:
return current_status, current_value, h_next.status, h_next.timeout

return current_status, current_value, h_loop[1].status, h_loop[1].timeout

# de-duplicate an alert
def deduplicate(self, duplicate_of) -> 'Alert':
Expand Down
2 changes: 1 addition & 1 deletion tests/test_blackouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ def test_custom_notify(self):
alert_receive_time = data['alert']['receiveTime']

# wait for blackout to expire
time.sleep(1)
time.sleep(2)

# resend duplicate alert now that blackout has expired
response = self.client.post('/alert', data=json.dumps(self.prod_alert), headers=self.headers)
Expand Down
57 changes: 57 additions & 0 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from alerta.app import create_app, db, plugins
from alerta.exceptions import AlertaException, InvalidAction
from alerta.models.enums import Status
from alerta.plugins import PluginBase


Expand Down Expand Up @@ -413,6 +414,41 @@ def test_add_and_remove_tags(self):
del plugins.plugins['action1']
del plugins.plugins['action2']

def test_custom_ack(self):

plugins.plugins['ack1'] = CustAckPlugin1()

# create alert
response = self.client.post('/alert', json=self.critical_alert, headers=self.headers)
self.assertEqual(response.status_code, 201)
data = json.loads(response.data.decode('utf-8'))

alert_id = data['id']

# shelve and unshelve the alert to create some history
response = self.client.put('/alert/' + alert_id + '/action', json={'action': 'shelve'}, headers=self.headers)
response = self.client.put('/alert/' + alert_id + '/action', json={'action': 'unshelve'}, headers=self.headers)

response = self.client.put('/alert/' + alert_id + '/action', json={'action': 'ack1'}, headers=self.headers)
self.assertEqual(response.status_code, 200)

response = self.client.get('/alert/' + alert_id)
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['alert']['severity'], 'critical')
self.assertEqual(data['alert']['status'], 'ack')

response = self.client.put('/alert/' + alert_id + '/action', json={'action': 'unack'}, headers=self.headers)
self.assertEqual(response.status_code, 200)

response = self.client.get('/alert/' + alert_id)
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['alert']['severity'], 'critical')
self.assertEqual(data['alert']['status'], 'open')

del plugins.plugins['ack1']


class OldPlugin1(PluginBase):

Expand Down Expand Up @@ -622,6 +658,27 @@ def delete(self, alert, **kwargs):
return True


class CustAckPlugin1(PluginBase):

def pre_receive(self, alert, **kwargs):
return alert

def post_receive(self, alert, **kwargs):
return

def status_change(self, alert, status, text, **kwargs):
return alert, status, text

def take_action(self, alert, action: str, text: str, **kwargs):
if not action == 'ack1':
return
alert.status = Status.Ack
return alert

def delete(self, alert, **kwargs):
return True


class Teapot(PluginBase):

def pre_receive(self, alert, **kwargs):
Expand Down

0 comments on commit f3c51f3

Please sign in to comment.