Fixes bugs for integration with policy module 23/5923/1
authorBenjamin Diaz <bdiaz@whitestack.com>
Thu, 29 Mar 2018 20:24:09 +0000 (17:24 -0300)
committerBenjamin Diaz <bdiaz@whitestack.com>
Thu, 29 Mar 2018 20:24:09 +0000 (17:24 -0300)
Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
osm_mon/core/message_bus/common_consumer.py
osm_mon/plugins/OpenStack/common.py
osm_mon/test/functional/__init__.py [new file with mode: 0644]
osm_mon/test/functional/test_vim_account.py [new file with mode: 0644]
osm_mon/test/integration/test_alarm_integration.py
osm_mon/test/integration/test_metric_integration.py
osm_mon/test/integration/test_vim_account.py [deleted file]

index e79e98a..0ba003b 100755 (executable)
@@ -77,10 +77,12 @@ aws_access_credentials = AccessCredentials()
 vrops_rcvr = plugin_receiver.PluginReceiver()
 
 
-def get_vim_type(message):
+def get_vim_type(msg):
     """Get the vim type that is required by the message."""
     try:
-        return json.loads(message.value)["vim_type"].lower()
+        vim_uuid = json.loads(msg.value)["vim_uuid"].lower()
+        credentials = database_manager.get_credentials(vim_uuid)
+        return credentials.type
     except Exception as exc:
         log.warn("vim_type is not configured correctly; %s", exc)
     return None
index 0c5e7c6..57acf40 100644 (file)
@@ -76,17 +76,17 @@ class Common(object):
         if req_type == "put":
             response = requests.put(
                 url, data=payload, headers=headers,
-                timeout=1)
+                timeout=10)
         elif req_type == "get":
             response = requests.get(
-                url, params=params, headers=headers, timeout=1)
+                url, params=params, headers=headers, timeout=10)
         elif req_type == "delete":
             response = requests.delete(
-                url, headers=headers, timeout=1)
+                url, headers=headers, timeout=10)
         else:
             response = requests.post(
                 url, data=payload, headers=headers,
-                timeout=1)
+                timeout=10)
 
         # Raises exception if there was an error
         try:
diff --git a/osm_mon/test/functional/__init__.py b/osm_mon/test/functional/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/osm_mon/test/functional/test_vim_account.py b/osm_mon/test/functional/test_vim_account.py
new file mode 100644 (file)
index 0000000..2a7a722
--- /dev/null
@@ -0,0 +1,115 @@
+# -*- coding: utf-8 -*-
+
+# Copyright 2018 Whitestack, LLC
+# *************************************************************
+
+# This file is part of OSM Monitoring module
+# All Rights Reserved to Whitestack, LLC
+
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+
+#         http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+# For those usages not covered by the Apache License, Version 2.0 please
+# contact: bdiaz@whitestack.com or glavado@whitestack.com
+##
+
+"""Test an end to end Openstack vim_account requests."""
+
+import json
+import logging
+import time
+import unittest
+
+from kafka import KafkaConsumer
+from kafka import KafkaProducer
+from kafka.errors import KafkaError
+
+from osm_mon.core.auth import AuthManager
+
+log = logging.getLogger(__name__)
+
+
+class VimAccountTest(unittest.TestCase):
+    def setUp(self):
+        try:
+            self.producer = KafkaProducer(bootstrap_servers='localhost:9092')
+            self.consumer = KafkaConsumer(bootstrap_servers='localhost:9092',
+                                          group_id='osm_mon')
+            self.consumer.subscribe(['vim_account'])
+            self.auth_manager = AuthManager()
+        except KafkaError:
+            self.skipTest('Kafka server not present.')
+
+    @unittest.skip("Correct support for functional tests is pending.")
+    # TODO: Refactor
+    def test_create_edit_delete_vim_account(self):
+        """Test vim_account creation message from KafkaProducer."""
+        # Set-up message, producer and consumer for tests
+        create_payload = {
+            "_id": "test_id",
+            "name": "test_name",
+            "vim_type": "openstack",
+            "vim_url": "auth_url",
+            "vim_user": "user",
+            "vim_password": "password",
+            "vim_tenant_name": "tenant",
+            "config":
+                {
+                    "foo": "bar"
+                }
+        }
+
+        self.producer.send('vim_account', key=b'create', value=json.dumps(create_payload))
+
+        self.producer.flush()
+
+        time.sleep(1)
+        creds = self.auth_manager.get_credentials('test_id')
+        self.assertIsNotNone(creds)
+        self.assertEqual(creds.name, create_payload['name'])
+        self.assertEqual(json.loads(creds.config), create_payload['config'])
+
+        # Set-up message, producer and consumer for tests
+        edit_payload = {
+            "_id": "test_id",
+            "name": "test_name_edited",
+            "vim_type": "openstack",
+            "vim_url": "auth_url",
+            "vim_user": "user",
+            "vim_password": "password",
+            "vim_tenant_name": "tenant",
+            "config":
+                {
+                    "foo_edited": "bar_edited"
+                }
+        }
+
+        self.producer.send('vim_account', key=b'edit', value=json.dumps(edit_payload))
+
+        self.producer.flush()
+
+        time.sleep(1)
+        creds = self.auth_manager.get_credentials('test_id')
+        self.assertEqual(creds.name, edit_payload['name'])
+        self.assertEqual(json.loads(creds.config), edit_payload['config'])
+
+        delete_payload = {
+            "_id": "test_id"
+        }
+
+        self.producer.send('vim_account', key=b'delete', value=json.dumps(delete_payload))
+
+        self.producer.flush()
+
+        time.sleep(1)
+        creds = self.auth_manager.get_credentials('test_id')
+        self.assertIsNone(creds)
index 368cc10..b443d6a 100644 (file)
@@ -32,6 +32,7 @@ from kafka import KafkaProducer
 from kafka.errors import KafkaError
 
 from osm_mon.core.auth import AuthManager
+from osm_mon.core.database import DatabaseManager
 from osm_mon.core.message_bus.producer import KafkaProducer as prod
 from osm_mon.plugins.OpenStack import response
 from osm_mon.plugins.OpenStack.Aodh import alarming
@@ -46,7 +47,8 @@ class AlarmIntegrationTest(unittest.TestCase):
         try:
             self.producer = KafkaProducer(bootstrap_servers='localhost:9092')
             self.req_consumer = KafkaConsumer(bootstrap_servers='localhost:9092',
-                                              consumer_timeout_ms=5000)
+                                              auto_offset_reset='earliest',
+                                              consumer_timeout_ms=60000)
             self.req_consumer.subscribe(['alarm_request'])
         except KafkaError:
             self.skipTest('Kafka server not present.')
@@ -89,6 +91,7 @@ class AlarmIntegrationTest(unittest.TestCase):
                 return
         self.fail("No message received in consumer")
 
+    @mock.patch.object(DatabaseManager, "save_alarm", mock.Mock())
     @mock.patch.object(Common, "get_auth_token", mock.Mock())
     @mock.patch.object(Common, "get_endpoint", mock.Mock())
     @mock.patch.object(prod, "create_alarm_response")
@@ -193,6 +196,8 @@ class AlarmIntegrationTest(unittest.TestCase):
                 return
         self.fail("No message received in consumer")
 
+    @mock.patch.object(Common, "get_auth_token", mock.Mock())
+    @mock.patch.object(Common, "get_endpoint", mock.Mock())
     @mock.patch.object(alarming.Alarming, "update_alarm_state")
     def test_ack_alarm_req(self, ack_alarm):
         """Test Aodh acknowledge alarm request message from KafkaProducer."""
@@ -204,4 +209,11 @@ class AlarmIntegrationTest(unittest.TestCase):
 
         self.producer.send('alarm_request', key="acknowledge_alarm",
                            value=json.dumps(payload))
-        self.producer.flush()
+
+        for message in self.req_consumer:
+            # Check the vim desired by the message
+            if message.key == "acknowledge_alarm":
+                self.alarms.alarming(message)
+                return
+
+        self.fail("No message received in consumer")
\ No newline at end of file
index 6b32a12..66f4f0a 100644 (file)
@@ -55,7 +55,7 @@ class MetricIntegrationTest(unittest.TestCase):
             self.producer = KafkaProducer(bootstrap_servers='localhost:9092')
             self.req_consumer = KafkaConsumer(bootstrap_servers='localhost:9092',
                                               auto_offset_reset='earliest',
-                                              consumer_timeout_ms=2000)
+                                              consumer_timeout_ms=60000)
             self.req_consumer.subscribe(['metric_request'])
         except KafkaError:
             self.skipTest('Kafka server not present.')
@@ -116,8 +116,6 @@ class MetricIntegrationTest(unittest.TestCase):
                            value=json.dumps(payload))
 
         for message in self.req_consumer:
-            # Check the vim desired by the message
-            vim_type = json.loads(message.value)["vim_type"].lower()
             if message.key == "delete_metric_request":
                 # Metric has been deleted
                 del_metric.return_value = True
diff --git a/osm_mon/test/integration/test_vim_account.py b/osm_mon/test/integration/test_vim_account.py
deleted file mode 100644 (file)
index e84b3cb..0000000
+++ /dev/null
@@ -1,115 +0,0 @@
-# -*- coding: utf-8 -*-
-
-# Copyright 2018 Whitestack, LLC
-# *************************************************************
-
-# This file is part of OSM Monitoring module
-# All Rights Reserved to Whitestack, LLC
-
-# Licensed under the Apache License, Version 2.0 (the "License"); you may
-# not use this file except in compliance with the License. You may obtain
-# a copy of the License at
-
-#         http://www.apache.org/licenses/LICENSE-2.0
-
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-# License for the specific language governing permissions and limitations
-# under the License.
-
-# For those usages not covered by the Apache License, Version 2.0 please
-# contact: bdiaz@whitestack.com or glavado@whitestack.com
-##
-
-"""Test an end to end Openstack vim_account requests."""
-
-import json
-import logging
-import time
-import unittest
-
-from kafka import KafkaConsumer
-from kafka import KafkaProducer
-from kafka.errors import KafkaError
-
-from osm_mon.core.auth import AuthManager
-
-log = logging.getLogger(__name__)
-
-
-class VimAccountTest(unittest.TestCase):
-    def setUp(self):
-        try:
-            self.producer = KafkaProducer(bootstrap_servers='localhost:9092')
-            self.consumer = KafkaConsumer(bootstrap_servers='localhost:9092',
-                                          group_id='osm_mon')
-            self.consumer.subscribe(['vim_account'])
-            self.auth_manager = AuthManager()
-        except KafkaError:
-            self.skipTest('Kafka server not present.')
-
-    # TODO: REFACTOR. This test requires common_consumer running. Needs to be changed so it does not.
-    @unittest.skip("Needs refactoring.")
-    def test_create_edit_delete_vim_account(self):
-        """Test vim_account creation message from KafkaProducer."""
-        # Set-up message, producer and consumer for tests
-        create_payload = {
-            "_id": "test_id",
-            "name": "test_name",
-            "vim_type": "openstack",
-            "vim_url": "auth_url",
-            "vim_user": "user",
-            "vim_password": "password",
-            "vim_tenant_name": "tenant",
-            "config":
-                {
-                    "foo": "bar"
-                }
-        }
-
-        self.producer.send('vim_account', key=b'create', value=json.dumps(create_payload))
-
-        self.producer.flush()
-
-        time.sleep(1)
-        creds = self.auth_manager.get_credentials(create_payload['_id'])
-        self.assertIsNotNone(creds)
-        self.assertEqual(creds.name, create_payload['name'])
-        self.assertEqual(json.loads(creds.config), create_payload['config'])
-
-        # Set-up message, producer and consumer for tests
-        edit_payload = {
-            "_id": "test_id",
-            "name": "test_name_edited",
-            "vim_type": "openstack",
-            "vim_url": "auth_url",
-            "vim_user": "user",
-            "vim_password": "password",
-            "vim_tenant_name": "tenant",
-            "config":
-                {
-                    "foo_edited": "bar_edited"
-                }
-        }
-
-        self.producer.send('vim_account', key=b'edit', value=json.dumps(edit_payload))
-
-        self.producer.flush()
-
-        time.sleep(1)
-        creds = self.auth_manager.get_credentials(edit_payload['_id'])
-        self.assertEqual(creds.name, edit_payload['name'])
-        self.assertEqual(json.loads(creds.config), edit_payload['config'])
-
-        delete_payload = {
-            "_id": "test_id"
-        }
-
-        self.producer.send('vim_account', key=b'delete', value=json.dumps(delete_payload))
-
-        self.producer.flush()
-
-        time.sleep(1)
-        creds = self.auth_manager.get_credentials(delete_payload['_id'])
-        self.assertIsNone(creds)