From: kdhumal Date: Wed, 14 Feb 2018 10:48:42 +0000 (-0800) Subject: Additional mock test cases for vROPs plugin X-Git-Tag: v4.0.0~31^2 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F49%2F5849%2F1;p=osm%2FMON.git Additional mock test cases for vROPs plugin Change-Id: I1d9e585f758736d06abcdb0d3fc06ca475cb4393 Signed-off-by: dhumal --- diff --git a/osm_mon/test/VMware/test_mon_plugin_vrops.py b/osm_mon/test/VMware/test_mon_plugin_vrops.py new file mode 100644 index 0000000..571c0de --- /dev/null +++ b/osm_mon/test/VMware/test_mon_plugin_vrops.py @@ -0,0 +1,199 @@ +# -*- coding: utf-8 -*- + +## +# Copyright 2017-2018 VMware Inc. +# This file is part of ETSI OSM +# All Rights Reserved. +# +# 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: osslegalrouting@vmware.com +## + +""" Mock tests for VMware vROPs Mon plugin """ + +import sys + +import json + +import logging + +import unittest + +import mock + +import requests + +import os + +log = logging.getLogger(__name__) + +sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)),"..","..","..")) + +from osm_mon.plugins.vRealiseOps import mon_plugin_vrops as monPlugin + + +class TestMonPlugin(unittest.TestCase): + """Test class for vROPs Mon Plugin class methods""" + + def setUp(self): + """Setup the tests for plugin_receiver class methods""" + super(TestMonPlugin, self).setUp() + self.mon_plugin = monPlugin.MonPlugin() + + + def test_get_default_Params_valid_metric_alarm_name(self): + """Test get default params method""" + + # Mock valid metric_alarm_name and response + metric_alarm_name = "Average_Memory_Usage_Above_Threshold" + exepcted_return = {'impact': 'risk', 'cancel_cycles': 2, 'adapter_kind': 'VMWARE', 'repeat': False, + 'cancel_period': 300, 'alarm_type': 16, 'vrops_alarm': 'Avg_Mem_Usage_Above_Thr', + 'enabled': True, 'period': 300, 'resource_kind': 'VirtualMachine', 'alarm_subType': 19, + 'action': 'acknowledge', 'evaluation': 2, 'unit': '%'} + + # call get default param function under test + actual_return = self.mon_plugin.get_default_Params(metric_alarm_name) + + # verify return value with expected value + self.assertEqual(exepcted_return, actual_return) + + + def test_get_default_Params_invalid_metric_alarm_name(self): + """Test get default params method-invalid metric alarm""" + + # Mock valid metric_alarm_name and response + metric_alarm_name = "Invalid_Alarm" + exepcted_return = {} + + # call get default param function under test + actual_return = self.mon_plugin.get_default_Params(metric_alarm_name) + + # verify return value with expected value + self.assertEqual(exepcted_return, actual_return) + + + @mock.patch.object(monPlugin.requests, 'post') + def test_create_symptom_valid_req_response(self, m_post): + """Test create symptom method-valid request""" + + # Mock valid symptom params and mock responses + symptom_param = {'threshold_value': 0, 'cancel_cycles': 1, 'adapter_kind_key': 'VMWARE', + 'resource_kind_key': 'VirtualMachine', 'severity': 'CRITICAL', + 'symptom_name': u'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4', + 'operation': 'GT', 'wait_cycles': 1, 'metric_key': 'cpu|usage_average'} + + m_post.return_value.status_code = 201 + m_post.return_value.content = '{"id":"SymptomDefinition-351c23b4-bc3c-4c7b-b4af-1ad90a673c5d",\ + "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\ + "adapterKindKey":"VMWARE","resourceKindKey":"VirtualMachine","waitCycles":1,\ + "cancelCycles":1,"state":{"severity":"CRITICAL","condition":{"type":"CONDITION_HT",\ + "key":"cpu|usage_average","operator":"GT","value":"0.0","valueType":"NUMERIC",\ + "instanced":false,"thresholdType":"STATIC"}}}' + + exepcted_return = "SymptomDefinition-351c23b4-bc3c-4c7b-b4af-1ad90a673c5d" + + # call create symptom method under test + actual_return = self.mon_plugin.create_symptom(symptom_param) + + # verify that mocked method is called + m_post.assert_called() + + # verify return value with expected value + self.assertEqual(exepcted_return, actual_return) + + + @mock.patch.object(monPlugin.requests, 'post') + def test_create_symptom_invalid_req_response(self, m_post): + """Test create symptom method-invalid response""" + + # Mock valid symptom params and invalid mock responses + symptom_param = {'threshold_value': 0, 'cancel_cycles': 1, 'adapter_kind_key': 'VMWARE', + 'resource_kind_key': 'VirtualMachine', 'severity': 'CRITICAL', + 'symptom_name': u'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4', + 'operation': 'GT', 'wait_cycles': 1, 'metric_key': 'cpu|usage_average'} + + m_post.return_value.status_code = 404 + m_post.return_value.content = '404 Not Found' + + exepcted_return = None + + # call create symptom method under test + actual_return = self.mon_plugin.create_symptom(symptom_param) + + # verify that mocked method is called + m_post.assert_called() + + # verify return value with expected value + self.assertEqual(exepcted_return, actual_return) + + + @mock.patch.object(monPlugin.requests, 'post') + def test_create_symptom_incorrect_data(self, m_post): + """Test create symptom method-incorrect data""" + + # Mock valid symptom params and invalid mock responses + symptom_param = {'threshold_value': 0, 'cancel_cycles': 1, 'adapter_kind_key': 'VMWARE', + 'resource_kind_key': 'VirtualMachine', 'severity': 'CRITICAL', + 'symptom_name': u'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4', + 'operation': 'GT', 'metric_key': 'cpu|usage_average'} + + exepcted_return = None + + # call create symptom method under test + actual_return = self.mon_plugin.create_symptom(symptom_param) + + # verify that mocked method is not called + m_post.assert_not_called() + + # verify return value with expected value + self.assertEqual(exepcted_return, actual_return) + + + @mock.patch.object(monPlugin.requests, 'post') + def test_create_alarm_definition_valid_req_response(self, m_post): + """Test create alarm definition method-valid response""" + + # Mock valid alarm params and mock responses + alarm_param = {'description': 'CPU_Utilization_Above_Threshold', 'cancelCycles': 1, 'subType': 19, + 'waitCycles': 1, 'severity': 'CRITICAL', 'impact': 'risk', 'adapterKindKey': 'VMWARE', + 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4', 'resourceKindKey': 'VirtualMachine', + 'symptomDefinitionId': u'SymptomDefinition-25278b06-bff8-4409-a141-9b4e064235df', 'type': 16} + + m_post.return_value.status_code = 201 + m_post.return_value.content = '{"id":"AlertDefinition-d4f21e4b-770a-45d6-b298-022eaf489115",\ + "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\ + "description":"CPU_Utilization_Above_Threshold","adapterKindKey":"VMWARE",\ + "resourceKindKey":"VirtualMachine","waitCycles":1,"cancelCycles":1,"type":16,\ + "subType":19,"states":[{"severity":"CRITICAL","base-symptom-set":{"type":"SYMPTOM_SET",\ + "relation":"SELF","aggregation":"ALL","symptomSetOperator":"AND",\ + "symptomDefinitionIds":["SymptomDefinition-25278b06-bff8-4409-a141-9b4e064235df"]},\ + "impact":{"impactType":"BADGE","detail":"risk"}}]}' + + exepcted_return = "AlertDefinition-d4f21e4b-770a-45d6-b298-022eaf489115" + + # call create alarm definition method under test + actual_return = self.mon_plugin.create_alarm_definition(alarm_param) + + # verify that mocked method is called + m_post.assert_called() + + # verify return value with expected value + self.assertEqual(exepcted_return, actual_return) + + +# For testing purpose +#if __name__ == '__main__': +# unittest.main() + diff --git a/osm_mon/test/VMware/test_plugin_receiver.py b/osm_mon/test/VMware/test_plugin_receiver.py index 07bfa35..1f1e38b 100644 --- a/osm_mon/test/VMware/test_plugin_receiver.py +++ b/osm_mon/test/VMware/test_plugin_receiver.py @@ -24,7 +24,7 @@ """ Mock tests for VMware vROPs plugin recevier """ import sys -sys.path.append("/root/MON/") +#sys.path.append("/root/MON/") import json @@ -36,10 +36,14 @@ import mock import requests -from osm_mon.plugins.vRealiseOps import plugin_receiver as monPluginRec +import os log = logging.getLogger(__name__) +sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)),"..","..","..")) + +from osm_mon.plugins.vRealiseOps import plugin_receiver as monPluginRec + class Message(object): """A class to mock a message object value for alarm and matric requests""" @@ -271,6 +275,7 @@ class TestPluginReceiver(unittest.TestCase): @mock.patch.object(monPluginRec.PluginReceiver, 'verify_metric') def test_consume_update_metric_request_key(self, m_verify_metric, m_publish_update_metric_response): """Test functionality of update metric request key""" + # Mock a message msg = Message() msg.topic = "metric_request" @@ -649,7 +654,179 @@ class TestPluginReceiver(unittest.TestCase): m_publish.assert_called_with(key='delete_metric_response', value=mock.ANY, topic='metric_response') + @mock.patch.object(monPluginRec.MonPlugin, 'get_triggered_alarms_list') + def test_list_alarms(self, m_get_triggered_alarms_list): + """ Test functionality of list alarms method""" + + # Mock list alarm input + list_alarm_input = {'severity': 'CRITICAL', + 'correlation_id': 'e14b203c', + 'alarm_name': 'CPU_Utilization_Above_Threshold', + 'resource_uuid': 'd14b203c'} + + # set return value to mocked method + m_return = m_get_triggered_alarms_list.return_value = [{'status': 'ACTIVE', 'update_date': '2018-01-12T08:34:05', + 'severity': 'CRITICAL', 'resource_uuid': 'e14b203c', + 'cancel_date': '0000-00-00T00:00:00','alarm_instance_uuid': 'd9e3bc84', + 'alarm_uuid': '5714977d', 'vim_type': 'VMware', + 'start_date': '2018-01-12T08:34:05'}, + {'status': 'CANCELED', 'update_date': '2017-12-20T09:37:57', + 'severity': 'CRITICAL', 'resource_uuid': 'e14b203c', + 'cancel_date': '2018-01-12T06:49:19', 'alarm_instance_uuid': 'd3bbeef6', + 'alarm_uuid': '7ba1bf3e', 'vim_type': 'VMware', + 'start_date': '2017-12-20T09:37:57'}] + + # call list alarms method under test + return_value = self.plugin_receiver.list_alarms(list_alarm_input) + + # verify mocked method called with correct params + m_get_triggered_alarms_list.assert_called_with(list_alarm_input) + + # verify list alarm method returns correct list + self.assertEqual(return_value, m_return) + + + @mock.patch.object(monPluginRec.KafkaProducer, 'publish') + def test_publish_list_alarm_response(self, m_publish): + """ Test functionality of publish list alarm response method""" + + # Mock list alarm input + msg_key = 'list_alarm_response' + topic = 'alarm_response' + list_alarm_input = {'alarm_list_request': {'severity': 'CRITICAL', + 'correlation_id': 'e14b203c', + 'alarm_name': 'CPU_Utilization_Above_Threshold', + 'resource_uuid': 'd14b203c'},'vim_type' : 'VMware'} + + triggered_alarm_list = [{'status': 'ACTIVE', 'update_date': '2018-01-12T08:34:05', 'severity': 'CRITICAL', + 'resource_uuid': 'e14b203c', 'cancel_date': '0000-00-00T00:00:00','alarm_instance_uuid': 'd9e3bc84', + 'alarm_uuid': '5714977d', 'vim_type': 'VMware', 'start_date': '2018-01-12T08:34:05'}] + + # call publish list alarm response method under test + self.plugin_receiver.publish_list_alarm_response(triggered_alarm_list, list_alarm_input) + + # verify mocked method called with correct params + m_publish.assert_called_with(key=msg_key,value=mock.ANY, topic=topic) + + + @mock.patch.object(monPluginRec.KafkaProducer, 'publish') + def test_publish_access_update_response(self, m_publish): + """ Test functionality of publish access update response method""" + + # Mock required inputs + access_update_status = True + msg_key = 'vim_access_credentials_response' + topic = 'access_credentials' + access_info_req = {'access_config': {'vrops_password': 'vmware', 'vcloud-site': 'https://192.169.241.105', + 'vrops_user': 'Admin', 'correlation_id': 'e14b203c', 'tenant_id': 'Org2'}, 'vim_type': u'VMware'} + + # call publish access update response method under test + self.plugin_receiver.publish_access_update_response(access_update_status, access_info_req) + + # verify mocked method called with correct params + m_publish.assert_called_with(key=msg_key ,value=mock.ANY, topic=topic) + + + @mock.patch.object(monPluginRec.PluginReceiver, 'write_access_config') + def test_update_access_credentials_successful(self, m_write_access_config): + """ Test functionality of update access credentials-positive case""" + + # Mock access_info + access_info = {'vrops_site':'https://192.169.241.13','vrops_user':'admin', 'vrops_password':'vmware', + 'vcloud-site':'https://192.169.241.15','admin_username':'admin','admin_password':'vmware', + 'vcenter_ip':'192.169.241.13','vcenter_port':'443','vcenter_user':'admin','vcenter_password':'vmware', + 'vim_tenant_name':'Org2','orgname':'Org2','tenant_id':'Org2'} + + # Mock return values + expected_status = m_write_access_config.return_value = True + + # call publish update acccess credentials method under test + actual_status = self.plugin_receiver.update_access_credentials(access_info) + + # check write_access_config called with correct params + m_write_access_config.assert_called_with(access_info) + + # verify update access credentials returns correct status + self.assertEqual(expected_status, actual_status) + + + @mock.patch.object(monPluginRec.PluginReceiver, 'write_access_config') + def test_update_access_credentials_less_config_params(self, m_write_access_config): + """ Test functionality of update access credentials-negative case""" + + # Mock access_info + access_info = {'vrops_site':'https://192.169.241.13','vrops_user':'admin', 'vrops_password':'vmware', + 'vcloud-site':'https://192.169.241.15','admin_username':'admin','admin_password':'vmware', + 'vcenter_ip':'192.169.241.13','vcenter_port':'443','vcenter_user':'admin', + 'vim_tenant_name':'Org2','orgname':'Org2','tenant_id':'Org2'} + + # Mock return values + expected_status = m_write_access_config.return_value = False + + # call publish update acccess credentials method under test + actual_status = self.plugin_receiver.update_access_credentials(access_info) + + # check if mocked method not called + m_write_access_config.assert_not_called() + + # verify update access credentials returns correct status + self.assertEqual(expected_status, actual_status) + + + @mock.patch.object(monPluginRec.PluginReceiver, 'write_access_config') + def test_update_access_credentials_failed(self, m_write_access_config): + """ Test functionality of update access credentials-failed case """ + + # Mock access_info + access_info = {'vrops_site':'https://192.169.241.13','vrops_user':'admin', 'vrops_password':'vmware', + 'vcloud-site':'https://192.169.241.15','admin_username':'admin','admin_password':'vmware', + 'vcenter_ip':'192.169.241.13','vcenter_port':'443','vcenter_user':'admin','vcenter_password':'vmware', + 'vim_tenant_name':'Org2','orgname':'Org2','tenant_id':'Org2'} + + # Mock return values + expected_status = m_write_access_config.return_value = False + + # call publish update acccess credentials method under test + actual_status = self.plugin_receiver.update_access_credentials(access_info) + + # check write_access_config called with correct params + m_write_access_config.assert_called_with(access_info) + + # verify update access credentials returns correct status + self.assertEqual(expected_status, actual_status) + + + def test_write_access_config_successful(self): + """ Test functionality of write access config method-positive case""" + + # Mock access_info + access_info = {'vrops_sit':'https://192.169.241.13','vrops_user':'admin', 'vrops_password':'vmware', + 'vcloud-site':'https://192.169.241.15','admin_username':'admin','admin_password':'vmware', + 'vcenter_ip':'192.169.241.13','vcenter_port':'443','vcenter_user':'admin','vcenter_password':'vmware', + 'vim_tenant_name':'Org2','orgname':'Org2','tenant_id':'Org2'} + + # call write acccess config method under test + actual_status = self.plugin_receiver.write_access_config(access_info) + + # verify write access config returns correct status + self.assertEqual(True, actual_status) + + + def test_write_access_config_failed(self): + """ Test functionality of write access config method-negative case""" + + # Mock access_info + access_info = [] # provided incorrect info to generate error + + # call write acccess config method under test + actual_status = self.plugin_receiver.write_access_config(access_info) + + # verify write access config returns correct status + self.assertEqual(False, actual_status) + + # For testing purpose #if __name__ == '__main__': # unittest.main() +