Implements multivim support in the OpenStack plugin
[osm/MON.git] / osm_mon / test / integration / test_notify_alarm.py
index 519624f..db21c4e 100644 (file)
@@ -26,9 +26,10 @@ import json
 import logging
 
 import socket
+import unittest
 
-from BaseHTTPServer import BaseHTTPRequestHandler
-from BaseHTTPServer import HTTPServer
+from six.moves.BaseHTTPServer import BaseHTTPRequestHandler
+from six.moves.BaseHTTPServer import HTTPServer
 
 from threading import Thread
 
@@ -72,7 +73,7 @@ class MockNotifierHandler(BaseHTTPRequestHandler):
 
     def do_GET(self):
         """Mock functionality for GET request."""
-#        self.send_response(requests.codes.ok)
+        #        self.send_response(requests.codes.ok)
         self._set_headers()
         pass
 
@@ -86,22 +87,22 @@ class MockNotifierHandler(BaseHTTPRequestHandler):
     def notify_alarm(self, values):
         """Mock the notify_alarm functionality to generate a valid response."""
         config = Config.instance()
-        config.read_environ("aodh")
+        config.read_environ()
         self._alarming = Alarming()
         self._common = Common()
         self._response = OpenStack_Response()
         self._producer = KafkaProducer('alarm_response')
         alarm_id = values['alarm_id']
 
-        auth_token = self._common._authenticate()
-        endpoint = self._common.get_endpoint("alarming")
+        auth_token = Common.get_auth_token('test_id')
+        endpoint = Common.get_endpoint('alarming', 'test_id')
 
         # If authenticated generate and send response message
-        if (auth_token is not None and endpoint is not None):
+        if auth_token is not None and endpoint is not None:
             url = "{}/v2/alarms/%s".format(endpoint) % alarm_id
 
             # Get the resource_id of the triggered alarm and the date
-            result = self._common._perform_request(
+            result = Common.perform_request(
                 url, auth_token, req_type="get")
             alarm_details = json.loads(result.text)
             gnocchi_rule = alarm_details['gnocchi_resources_threshold_rule']
@@ -152,38 +153,39 @@ def test_do_get():
     assert response.ok
 
 
-@mock.patch.object(KafkaProducer, "notify_alarm")
-@mock.patch.object(OpenStack_Response, "generate_response")
-@mock.patch.object(Common, "_perform_request")
-@mock.patch.object(Common, "get_endpoint")
-@mock.patch.object(Common, "_authenticate")
-def test_post_notify_alarm(auth, endpoint, perf_req, resp, notify):
-    """Integration test for notify_alarm."""
-    url = 'http://localhost:{port}/users'.format(port=mock_server_port)
-    payload = {"severity": "critical",
-               "alarm_name": "my_alarm",
-               "current": "current_state",
-               "alarm_id": "my_alarm_id",
-               "reason": "Threshold has been broken",
-               "reason_data": {"count": 1,
-                               "most_recent": "null",
-                               "type": "threshold",
-                               "disposition": "unknown"},
-               "previous": "previous_state"}
-
-    # Mock authenticate and request response for testing
-    auth.return_value = "my_auth_token"
-    endpoint.return_value = "my_endpoint"
-    perf_req.return_value = MockResponse(valid_get_resp)
-
-    # Generate a post reqest for testing
-    requests.post(url, json.dumps(payload))
-
-    # A response message is generated with the following details
-    resp.assert_called_with(
-        "notify_alarm", a_id="my_alarm_id", r_id="my_resource_id",
-        sev="critical", date='dd-mm-yyyy 00:00', state="current_state",
-        vim_type="OpenStack")
-
-    # Reponse message is sent back to the SO via MON's producer
-    notify.assert_called_with("notify_alarm", mock.ANY, "alarm_response")
+class AlarmNotificationTest(unittest.TestCase):
+    @mock.patch.object(KafkaProducer, "notify_alarm")
+    @mock.patch.object(OpenStack_Response, "generate_response")
+    @mock.patch.object(Common, "perform_request")
+    @mock.patch.object(Common, "get_endpoint")
+    @mock.patch.object(Common, "get_auth_token")
+    def test_post_notify_alarm(self, auth, endpoint, perf_req, resp, notify):
+        """Integration test for notify_alarm."""
+        url = 'http://localhost:{port}/users'.format(port=mock_server_port)
+        payload = {"severity": "critical",
+                   "alarm_name": "my_alarm",
+                   "current": "current_state",
+                   "alarm_id": "my_alarm_id",
+                   "reason": "Threshold has been broken",
+                   "reason_data": {"count": 1,
+                                   "most_recent": "null",
+                                   "type": "threshold",
+                                   "disposition": "unknown"},
+                   "previous": "previous_state"}
+
+        # Mock authenticate and request response for testing
+        auth.return_value = "my_auth_token"
+        endpoint.return_value = "my_endpoint"
+        perf_req.return_value = MockResponse(valid_get_resp)
+
+        # Generate a post request for testing
+        response = requests.post(url, json.dumps(payload))
+        self.assertEqual(response.status_code, 200)
+        # A response message is generated with the following details
+        resp.assert_called_with(
+            "notify_alarm", a_id="my_alarm_id", r_id="my_resource_id",
+            sev="critical", date='dd-mm-yyyy 00:00', state="current_state",
+            vim_type="OpenStack")
+
+        # Response message is sent back to the SO via MON's producer
+        notify.assert_called_with("notify_alarm", mock.ANY, "alarm_response")