project id is not sent in webhook

* use set not tuples so you can use assert equal
* use HasLength
* remove the project id in filter
* rearrange the order of assert so refernce and actual are true

Change-Id: Ie41e4ae89354bc2cf88fc179726260d02e130ec8
This commit is contained in:
Eyal 2018-02-20 13:12:02 +02:00
parent a76ad711c6
commit b0f3efb2d3
2 changed files with 42 additions and 43 deletions

View File

@ -17,6 +17,7 @@ from oslo_log import log as logging
import requests import requests
from six.moves import BaseHTTPServer from six.moves import BaseHTTPServer
import socket import socket
from testtools import matchers
from threading import Thread from threading import Thread
import time import time
@ -45,24 +46,23 @@ TYPE_FILTER = '{"vitrage_type": "doctor"}'
FILTER_NO_MATCH = '{"name": "NO MATCH"}' FILTER_NO_MATCH = '{"name": "NO MATCH"}'
NOTIFICATION = 'notification' NOTIFICATION = 'notification'
PAYLOAD = 'payload' PAYLOAD = 'payload'
MAIN_FILTER = (NOTIFICATION, MAIN_FILTER = {NOTIFICATION,
PAYLOAD) PAYLOAD}
DOCTOR_ALARM_FILTER = (VProps.VITRAGE_ID, DOCTOR_ALARM_FILTER = {VProps.VITRAGE_ID,
VProps.RESOURCE, VProps.RESOURCE,
VProps.NAME, VProps.NAME,
VProps.UPDATE_TIMESTAMP, VProps.UPDATE_TIMESTAMP,
VProps.VITRAGE_TYPE, VProps.VITRAGE_TYPE,
VProps.VITRAGE_CATEGORY, VProps.VITRAGE_CATEGORY,
VProps.STATE, VProps.STATE,
VProps.VITRAGE_OPERATIONAL_SEVERITY) VProps.VITRAGE_OPERATIONAL_SEVERITY}
RESOURCE_FILTER = (VProps.VITRAGE_ID, RESOURCE_FILTER = {VProps.VITRAGE_ID,
VProps.ID, VProps.ID,
VProps.NAME, VProps.NAME,
VProps.VITRAGE_CATEGORY, VProps.VITRAGE_CATEGORY,
VProps.UPDATE_TIMESTAMP, VProps.UPDATE_TIMESTAMP,
VProps.VITRAGE_OPERATIONAL_STATE, VProps.VITRAGE_OPERATIONAL_STATE,
VProps.VITRAGE_TYPE, VProps.VITRAGE_TYPE}
VProps.PROJECT_ID)
messages = [] messages = []
@ -113,15 +113,15 @@ class TestWebhook(TestActionsBase):
self._trigger_do_action(TRIGGER_ALARM_1) self._trigger_do_action(TRIGGER_ALARM_1)
# Check event received # Check event received
self.assertEqual(1, len(self.mock_server.requests), self.assertThat(self.mock_server.requests, matchers.HasLength(1),
'Wrong number of notifications for raise alarm') 'Wrong number of notifications for raise alarm')
# Undo # Undo
self._trigger_undo_action(TRIGGER_ALARM_1) self._trigger_undo_action(TRIGGER_ALARM_1)
# Check event undo received # Check event undo received
self.assertEqual(2, len(self.mock_server.requests), self.assertThat(self.mock_server.requests, matchers.HasLength(2),
'Wrong number of notifications for clear alarm') 'Wrong number of notifications for clear alarm')
finally: finally:
self._trigger_undo_action(TRIGGER_ALARM_1) self._trigger_undo_action(TRIGGER_ALARM_1)
@ -145,15 +145,15 @@ class TestWebhook(TestActionsBase):
self._trigger_do_action(TRIGGER_ALARM_1) self._trigger_do_action(TRIGGER_ALARM_1)
# Check event received # Check event received
self.assertEqual(1, len(self.mock_server.requests), self.assertThat(self.mock_server.requests, matchers.HasLength(1),
'Wrong number of notifications for raise alarm') 'Wrong number of notifications for raise alarm')
# Raise another alarm # Raise another alarm
self._trigger_do_action(TRIGGER_ALARM_2) self._trigger_do_action(TRIGGER_ALARM_2)
# Check second event received # Check second event received
self.assertEqual(2, len(self.mock_server.requests), self.assertThat(self.mock_server.requests, matchers.HasLength(2),
'Wrong number of notifications for clear alarm') 'Wrong number of notifications for clear alarm')
finally: finally:
self._trigger_undo_action(TRIGGER_ALARM_1) self._trigger_undo_action(TRIGGER_ALARM_1)
@ -212,15 +212,15 @@ class TestWebhook(TestActionsBase):
self._trigger_do_action(TRIGGER_ALARM_1) self._trigger_do_action(TRIGGER_ALARM_1)
# Check event received # Check event received
self.assertEqual(2, len(self.mock_server.requests), self.assertThat(self.mock_server.requests, matchers.HasLength(2),
'event not posted to all webhooks') 'event not posted to all webhooks')
# Raise another alarm # Raise another alarm
self._trigger_do_action(TRIGGER_ALARM_2) self._trigger_do_action(TRIGGER_ALARM_2)
# Check second event received # Check second event received
self.assertEqual(4, len(self.mock_server.requests), self.assertThat(self.mock_server.requests, matchers.HasLength(4),
'event not posted to all webhooks') 'event not posted to all webhooks')
finally: finally:
self._trigger_undo_action(TRIGGER_ALARM_1) self._trigger_undo_action(TRIGGER_ALARM_1)
@ -244,17 +244,17 @@ class TestWebhook(TestActionsBase):
# (the trigger alarm does not pass the filter). This test verifies # (the trigger alarm does not pass the filter). This test verifies
# that the webhook is called only once for the deduced alarm. # that the webhook is called only once for the deduced alarm.
time.sleep(1) time.sleep(1)
self.assertEqual(1, len(self.mock_server.requests), self.assertThat(self.mock_server.requests, matchers.HasLength(1),
'Wrong number of notifications for deduced alarm') 'Wrong number of notifications for deduced alarm')
# Undo # Undo
self._trigger_undo_action(TRIGGER_ALARM_WITH_DEDUCED) self._trigger_undo_action(TRIGGER_ALARM_WITH_DEDUCED)
# Check event undo received # Check event undo received
time.sleep(1) time.sleep(1)
self.assertEqual(2, len(self.mock_server.requests), self.assertThat(self.mock_server.requests, matchers.HasLength(2),
'Wrong number of notifications for clear deduced ' 'Wrong number of notifications '
'alarm') 'for clear deduced alarm')
finally: finally:
self._trigger_undo_action(TRIGGER_ALARM_WITH_DEDUCED) self._trigger_undo_action(TRIGGER_ALARM_WITH_DEDUCED)
@ -273,16 +273,18 @@ class TestWebhook(TestActionsBase):
self._trigger_do_action(TRIGGER_ALARM_1) self._trigger_do_action(TRIGGER_ALARM_1)
# pre check that correct amount of notifications sent # pre check that correct amount of notifications sent
self.assertEqual(1, len(self.mock_server.requests), self.assertThat(self.mock_server.requests, matchers.HasLength(1),
'Wrong number of notifications for alarm') 'Wrong number of notifications for alarm')
self.assertEqual(1, len(messages), self.assertThat(messages, matchers.HasLength(1),
'Wrong number of messages for alarm') 'Wrong number of messages for alarm')
alarm = ast.literal_eval(messages[0]) alarm = ast.literal_eval(messages[0])
# check that only specified fields are sent for the alarm, # check that only specified fields are sent for the alarm,
# payload and resource # payload and resource
passed_filter = utils.filter_data(alarm, MAIN_FILTER, False) passed_filter = utils.filter_data(alarm,
MAIN_FILTER,
match_filter=False)
self.assertThat(passed_filter, self.assertThat(passed_filter,
IsEmpty(), IsEmpty(),
@ -292,39 +294,36 @@ class TestWebhook(TestActionsBase):
if payload: if payload:
passed_filter = utils.filter_data(payload, passed_filter = utils.filter_data(payload,
DOCTOR_ALARM_FILTER, DOCTOR_ALARM_FILTER,
False) match_filter=False)
self.assertThat(passed_filter, self.assertThat(passed_filter,
IsEmpty(), IsEmpty(),
"Wrong alarm fields sent") "Wrong alarm fields sent")
sent_fields = utils.filter_data(payload, sent_fields = utils.filter_data(payload, DOCTOR_ALARM_FILTER)
DOCTOR_ALARM_FILTER,
True)
self.assertEqual(len(sent_fields), len(DOCTOR_ALARM_FILTER), self.assertEqual(DOCTOR_ALARM_FILTER, sent_fields,
"Some alarm fields not sent") "Some alarm fields not sent")
resource = payload.get(VProps.RESOURCE) resource = payload.get(VProps.RESOURCE)
if resource: if resource:
passed_filter = utils.filter_data(resource, passed_filter = utils.filter_data(resource,
RESOURCE_FILTER, RESOURCE_FILTER,
False) match_filter=False)
self.assertThat(passed_filter, self.assertThat(passed_filter,
IsEmpty(), IsEmpty(),
"Wrong resource fields sent") "Wrong resource fields sent")
sent_fields = utils.filter_data(resource, sent_fields = utils.filter_data(resource, RESOURCE_FILTER)
RESOURCE_FILTER,
True)
self.assertEqual(len(sent_fields), len(RESOURCE_FILTER), self.assertEqual(RESOURCE_FILTER, sent_fields,
"Some resource fields not sent") "Some resource fields not sent")
finally: finally:
self._trigger_undo_action(TRIGGER_ALARM_1) self._trigger_undo_action(TRIGGER_ALARM_1)
def _delete_webhooks(self): @staticmethod
def _delete_webhooks():
webhooks = TempestClients.vitrage().webhook.list() webhooks = TempestClients.vitrage().webhook.list()
for webhook in webhooks: for webhook in webhooks:
TempestClients.vitrage().webhook.delete(webhook['id']) TempestClients.vitrage().webhook.delete(webhook['id'])

View File

@ -171,7 +171,7 @@ def wait_for_status(max_waiting, func, **kwargs):
return False return False
def filter_data(data, filter, match_filter=True): def filter_data(data, filter_, match_filter=True):
if match_filter: if match_filter:
return [k for k in data if k in filter] return {k for k in data if k in filter_}
return [k for k in data if k not in filter] return {k for k in data if k not in filter_}