X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_mon%2Ftest%2Fintegration%2Ftest_alarm_integration.py;h=b443d6a74ed0553779ccf4b695dbf60c61e2159d;hb=77b8b3f70d0bdb8faeeaab4fc8144f1d0a1e4732;hp=13f0fef399d9e2630be30f5da353fabdfd3c48eb;hpb=b3f86c9f43a0eb4d56487118a403c39f979ec042;p=osm%2FMON.git diff --git a/osm_mon/test/integration/test_alarm_integration.py b/osm_mon/test/integration/test_alarm_integration.py index 13f0fef..b443d6a 100644 --- a/osm_mon/test/integration/test_alarm_integration.py +++ b/osm_mon/test/integration/test_alarm_integration.py @@ -31,29 +31,33 @@ from kafka import KafkaConsumer 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 from osm_mon.plugins.OpenStack.common import Common +from keystoneclient.v3 import client log = logging.getLogger(__name__) class AlarmIntegrationTest(unittest.TestCase): def setUp(self): - # Set up common and alarming class instances - self.alarms = alarming.Alarming() - self.openstack_auth = Common() - try: self.producer = KafkaProducer(bootstrap_servers='localhost:9092') self.req_consumer = KafkaConsumer(bootstrap_servers='localhost:9092', - group_id='osm_mon', - consumer_timeout_ms=2000) + auto_offset_reset='earliest', + consumer_timeout_ms=60000) self.req_consumer.subscribe(['alarm_request']) except KafkaError: self.skipTest('Kafka server not present.') + # Set up common and alarming class instances + self.alarms = alarming.Alarming() + self.openstack_auth = Common() + @mock.patch.object(Common, "get_auth_token", mock.Mock()) + @mock.patch.object(Common, "get_endpoint", mock.Mock()) @mock.patch.object(prod, "update_alarm_response") @mock.patch.object(alarming.Alarming, "update_alarm") @mock.patch.object(response.OpenStack_Response, "generate_response") @@ -61,6 +65,7 @@ class AlarmIntegrationTest(unittest.TestCase): """Test Aodh update alarm request message from KafkaProducer.""" # Set-up message, producer and consumer for tests payload = {"vim_type": "OpenSTACK", + "vim_uuid": "test_id", "alarm_update_request": {"correlation_id": 123, "alarm_uuid": "alarm_id", @@ -71,11 +76,10 @@ class AlarmIntegrationTest(unittest.TestCase): for message in self.req_consumer: # Check the vim desired by the message - vim_type = json.loads(message.value)["vim_type"].lower() - if vim_type == "openstack": + if message.key == "update_alarm_request": # Mock a valid alarm update update_alarm.return_value = "alarm_id", True - self.alarms.alarming(message, self.openstack_auth, None) + self.alarms.alarming(message) # A response message is generated and sent via MON's producer resp.assert_called_with( @@ -87,6 +91,9 @@ 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") @mock.patch.object(alarming.Alarming, "configure_alarm") @mock.patch.object(response.OpenStack_Response, "generate_response") @@ -94,6 +101,7 @@ class AlarmIntegrationTest(unittest.TestCase): """Test Aodh create alarm request message from KafkaProducer.""" # Set-up message, producer and consumer for tests payload = {"vim_type": "OpenSTACK", + "vim_uuid": "test_id", "alarm_create_request": {"correlation_id": 123, "alarm_name": "my_alarm", @@ -106,11 +114,10 @@ class AlarmIntegrationTest(unittest.TestCase): for message in self.req_consumer: # Check the vim desired by the message - vim_type = json.loads(message.value)["vim_type"].lower() - if vim_type == "openstack": + if message.key == "create_alarm_request": # Mock a valid alarm creation config_alarm.return_value = "alarm_id", True - self.alarms.alarming(message, self.openstack_auth, None) + self.alarms.alarming(message) # A response message is generated and sent via MON's produce resp.assert_called_with( @@ -122,6 +129,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(prod, "list_alarm_response") @mock.patch.object(alarming.Alarming, "list_alarms") @mock.patch.object(response.OpenStack_Response, "generate_response") @@ -129,6 +138,7 @@ class AlarmIntegrationTest(unittest.TestCase): """Test Aodh list alarm request message from KafkaProducer.""" # Set-up message, producer and consumer for tests payload = {"vim_type": "OpenSTACK", + "vim_uuid": "test_id", "alarm_list_request": {"correlation_id": 123, "resource_uuid": "resource_id", }} @@ -138,13 +148,12 @@ class AlarmIntegrationTest(unittest.TestCase): for message in self.req_consumer: # Check the vim desired by the message - vim_type = json.loads(message.value)["vim_type"].lower() - if vim_type == "openstack": + if message.key == "list_alarm_request": # Mock an empty list generated by the request list_alarm.return_value = [] - self.alarms.alarming(message, self.openstack_auth, None) + self.alarms.alarming(message) - # Resoonse message is generated + # Response message is generated resp.assert_called_with( 'list_alarm_response', alarm_list=[], cor_id=123) @@ -155,6 +164,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, "delete_alarm") @mock.patch.object(prod, "delete_alarm_response") @mock.patch.object(response.OpenStack_Response, "generate_response") @@ -162,6 +173,7 @@ class AlarmIntegrationTest(unittest.TestCase): """Test Aodh delete alarm request message from KafkaProducer.""" # Set-up message, producer and consumer for tests payload = {"vim_type": "OpenSTACK", + "vim_uuid": "test_id", "alarm_delete_request": {"correlation_id": 123, "alarm_uuid": "alarm_id", }} @@ -171,9 +183,8 @@ class AlarmIntegrationTest(unittest.TestCase): for message in self.req_consumer: # Check the vim desired by the message - vim_type = json.loads(message.value)["vim_type"].lower() - if vim_type == "openstack": - self.alarms.alarming(message, self.openstack_auth, None) + if message.key == "delete_alarm_request": + self.alarms.alarming(message) # Response message is generated and sent by MON's producer resp.assert_called_with( @@ -185,11 +196,14 @@ 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.""" # Set-up message, producer and consumer for tests payload = {"vim_type": "OpenSTACK", + "vim_uuid": "test_id", "ack_details": {"alarm_uuid": "alarm_id", }} @@ -198,11 +212,8 @@ class AlarmIntegrationTest(unittest.TestCase): for message in self.req_consumer: # Check the vim desired by the message - vim_type = json.loads(message.value)["vim_type"].lower() - if vim_type == "openstack": - self.alarms.alarming(message, self.openstack_auth, None) - # No response message is sent for and ack request - # Alarm state is updated from alarm -> ok - ack_alarm.assert_called_with(None, None, "alarm_id") + if message.key == "acknowledge_alarm": + self.alarms.alarming(message) return - self.fail("No message received in consumer") + + self.fail("No message received in consumer") \ No newline at end of file