Notification Engine for Monasca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_webhook_notification.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # (C) Copyright 2014-2016 Hewlett Packard Enterprise Development LP
  2. # Copyright 2017 Fujitsu LIMITED
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  13. # implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import mock
  17. import requests
  18. import six
  19. import ujson as json
  20. from monasca_notification import notification as m_notification
  21. from monasca_notification.plugins import webhook_notifier
  22. from tests import base
  23. if six.PY2:
  24. import Queue as queue
  25. else:
  26. import queue
  27. def alarm(metrics):
  28. return {"tenantId": "0",
  29. "alarmId": "0",
  30. "alarmDefinitionId": 0,
  31. "alarmName": "test Alarm",
  32. "alarmDescription": "test Alarm description",
  33. "oldState": "OK",
  34. "newState": "ALARM",
  35. "severity": "LOW",
  36. "link": "some-link",
  37. "lifecycleState": "OPEN",
  38. "stateChangeReason": "I am alarming!",
  39. "timestamp": 1429023453632,
  40. "metrics": metrics}
  41. class requestsResponse(object):
  42. def __init__(self, status):
  43. self.status_code = status
  44. class TestWebhook(base.PluginTestCase):
  45. def setUp(self):
  46. super(TestWebhook, self).setUp(webhook_notifier.register_opts)
  47. self.trap = queue.Queue()
  48. def tearDown(self):
  49. super(TestWebhook, self).tearDown()
  50. self.assertTrue(self.trap.empty())
  51. def _http_post_200(self, url, data, headers, **kwargs):
  52. self.trap.put(url)
  53. self.trap.put(data)
  54. self.trap.put(headers)
  55. r = requestsResponse(200)
  56. return r
  57. def _http_post_404(self, url, data, headers, **kwargs):
  58. r = requestsResponse(404)
  59. return r
  60. def _http_post_exception(self, url, data, headers, **kwargs):
  61. self.trap.put("timeout %s" % kwargs["timeout"])
  62. raise requests.exceptions.Timeout
  63. @mock.patch('monasca_notification.plugins.webhook_notifier.requests')
  64. def notify(self, http_func, mock_requests):
  65. mock_log = mock.MagicMock()
  66. mock_log.warn = self.trap.put
  67. mock_log.error = self.trap.put
  68. mock_log.exception = self.trap.put
  69. mock_requests.post = http_func
  70. webhook = webhook_notifier.WebhookNotifier(mock_log)
  71. metric = []
  72. metric_data = {'dimensions': {'hostname': 'foo1', 'service': 'bar1'}}
  73. metric.append(metric_data)
  74. alarm_dict = alarm(metric)
  75. notification = m_notification.Notification(0, 'webhook', 'webhook notification',
  76. 'http://mock:3333/', 0, 0, alarm_dict)
  77. self.trap.put(webhook.send_notification(notification))
  78. def test_webhook_good_http_response(self):
  79. """webhook 200
  80. """
  81. self.notify(self._http_post_200)
  82. url = self.trap.get(timeout=1)
  83. data = self.trap.get(timeout=1)
  84. headers = self.trap.get(timeout=1)
  85. self.assertEqual(url, "http://mock:3333/")
  86. self.maxDiff = None
  87. # timestamp is in milliseconds while alarm_timestamp is in seconds
  88. self.assertEqual(json.loads(data),
  89. {"metrics": [{"dimensions": {"hostname": "foo1", "service": "bar1"}}], "alarm_id": "0",
  90. "state": "ALARM", "alarm_timestamp": 1429023453, "tenant_id": "0",
  91. "old_state": "OK", "alarm_description": "test Alarm description",
  92. "message": "I am alarming!", "alarm_definition_id": 0, "alarm_name": "test Alarm"})
  93. self.assertEqual(headers, {'content-type': 'application/json'})
  94. return_value = self.trap.get()
  95. self.assertTrue(return_value)
  96. def test_webhook_bad_http_response(self):
  97. """webhook bad response
  98. """
  99. self.notify(self._http_post_404)
  100. error = self.trap.get()
  101. self.assertNotRegex(error, "alarm_id.: .test Alarm")
  102. self.assertNotRegex(error, "content-type.: .application/json")
  103. self.assertRegex(error, "HTTP code 404")
  104. self.assertRegex(error, "post on URL http://mock:3333/")
  105. return_value = self.trap.get()
  106. self.assertFalse(return_value)
  107. def test_webhook_timeout_exception_on_http_response(self):
  108. """webhook timeout exception
  109. """
  110. self.conf_override(group='webhook_notifier', timeout=50)
  111. self.notify(self._http_post_exception)
  112. result = self.trap.get()
  113. self.assertEqual(result, "timeout 50")
  114. result = self.trap.get()
  115. self.assertNotRegex(result, "alarm_id.: .test Alarm")
  116. self.assertNotRegex(result, "content-type.: .application/json")
  117. self.assertRegex(result, "Error trying to post on URL http://mock:3333/")
  118. return_value = self.trap.get()
  119. self.assertFalse(return_value)