1 # -*- coding: utf-8 -*-
4 # Copyright 2017-2018 VMware Inc.
5 # This file is part of ETSI OSM
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: osslegalrouting@vmware.com
24 """ Mock tests for VMware vROPs Mon plugin """
40 log
= logging
.getLogger(__name__
)
42 sys
.path
.append(os
.path
.join(os
.path
.dirname(os
.path
.abspath(__file__
)),"..","..",".."))
44 from osm_mon
.plugins
.vRealiseOps
import mon_plugin_vrops
as monPlugin
47 class TestMonPlugin(unittest
.TestCase
):
48 """Test class for vROPs Mon Plugin class methods"""
51 """Setup the tests for plugin_receiver class methods"""
52 super(TestMonPlugin
, self
).setUp()
53 self
.mon_plugin
= monPlugin
.MonPlugin()
56 def test_get_default_Params_valid_metric_alarm_name(self
):
57 """Test get default params method"""
59 # Mock valid metric_alarm_name and response
60 metric_alarm_name
= "Average_Memory_Usage_Above_Threshold"
61 exepcted_return
= {'impact': 'risk', 'cancel_cycles': 2, 'adapter_kind': 'VMWARE', 'repeat': False,
62 'cancel_period': 300, 'alarm_type': 16, 'vrops_alarm': 'Avg_Mem_Usage_Above_Thr',
63 'enabled': True, 'period': 300, 'resource_kind': 'VirtualMachine', 'alarm_subType': 19,
64 'action': 'acknowledge', 'evaluation': 2, 'unit': '%'}
66 # call get default param function under test
67 actual_return
= self
.mon_plugin
.get_default_Params(metric_alarm_name
)
69 # verify return value with expected value
70 self
.assertEqual(exepcted_return
, actual_return
)
73 def test_get_default_Params_invalid_metric_alarm_name(self
):
74 """Test get default params method-invalid metric alarm"""
76 # Mock valid metric_alarm_name and response
77 metric_alarm_name
= "Invalid_Alarm"
80 # call get default param function under test
81 actual_return
= self
.mon_plugin
.get_default_Params(metric_alarm_name
)
83 # verify return value with expected value
84 self
.assertEqual(exepcted_return
, actual_return
)
87 @mock.patch
.object(monPlugin
.requests
, 'post')
88 def test_create_symptom_valid_req_response(self
, m_post
):
89 """Test create symptom method-valid request"""
91 # Mock valid symptom params and mock responses
92 symptom_param
= {'threshold_value': 0, 'cancel_cycles': 1, 'adapter_kind_key': 'VMWARE',
93 'resource_kind_key': 'VirtualMachine', 'severity': 'CRITICAL',
94 'symptom_name': u
'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
95 'operation': 'GT', 'wait_cycles': 1, 'metric_key': 'cpu|usage_average'}
97 m_post
.return_value
.status_code
= 201
98 m_post
.return_value
.content
= '{"id":"SymptomDefinition-351c23b4-bc3c-4c7b-b4af-1ad90a673c5d",\
99 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
100 "adapterKindKey":"VMWARE","resourceKindKey":"VirtualMachine","waitCycles":1,\
101 "cancelCycles":1,"state":{"severity":"CRITICAL","condition":{"type":"CONDITION_HT",\
102 "key":"cpu|usage_average","operator":"GT","value":"0.0","valueType":"NUMERIC",\
103 "instanced":false,"thresholdType":"STATIC"}}}'
105 exepcted_return
= "SymptomDefinition-351c23b4-bc3c-4c7b-b4af-1ad90a673c5d"
107 # call create symptom method under test
108 actual_return
= self
.mon_plugin
.create_symptom(symptom_param
)
110 # verify that mocked method is called
111 m_post
.assert_called()
113 # verify return value with expected value
114 self
.assertEqual(exepcted_return
, actual_return
)
117 @mock.patch
.object(monPlugin
.requests
, 'post')
118 def test_create_symptom_invalid_req_response(self
, m_post
):
119 """Test create symptom method-invalid response"""
121 # Mock valid symptom params and invalid mock responses
122 symptom_param
= {'threshold_value': 0, 'cancel_cycles': 1, 'adapter_kind_key': 'VMWARE',
123 'resource_kind_key': 'VirtualMachine', 'severity': 'CRITICAL',
124 'symptom_name': u
'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
125 'operation': 'GT', 'wait_cycles': 1, 'metric_key': 'cpu|usage_average'}
127 m_post
.return_value
.status_code
= 404
128 m_post
.return_value
.content
= '404 Not Found'
130 exepcted_return
= None
132 # call create symptom method under test
133 actual_return
= self
.mon_plugin
.create_symptom(symptom_param
)
135 # verify that mocked method is called
136 m_post
.assert_called()
138 # verify return value with expected value
139 self
.assertEqual(exepcted_return
, actual_return
)
142 @mock.patch
.object(monPlugin
.requests
, 'post')
143 def test_create_symptom_incorrect_data(self
, m_post
):
144 """Test create symptom method-incorrect data"""
146 # Mock valid symptom params and invalid mock responses
147 symptom_param
= {'threshold_value': 0, 'cancel_cycles': 1, 'adapter_kind_key': 'VMWARE',
148 'resource_kind_key': 'VirtualMachine', 'severity': 'CRITICAL',
149 'symptom_name': u
'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
150 'operation': 'GT', 'metric_key': 'cpu|usage_average'}
152 exepcted_return
= None
154 # call create symptom method under test
155 actual_return
= self
.mon_plugin
.create_symptom(symptom_param
)
157 # verify that mocked method is not called
158 m_post
.assert_not_called()
160 # verify return value with expected value
161 self
.assertEqual(exepcted_return
, actual_return
)
164 @mock.patch
.object(monPlugin
.requests
, 'post')
165 def test_create_alarm_definition_valid_req_response(self
, m_post
):
166 """Test create alarm definition method-valid response"""
168 # Mock valid alarm params and mock responses
169 alarm_param
= {'description': 'CPU_Utilization_Above_Threshold', 'cancelCycles': 1, 'subType': 19,
170 'waitCycles': 1, 'severity': 'CRITICAL', 'impact': 'risk', 'adapterKindKey': 'VMWARE',
171 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4', 'resourceKindKey': 'VirtualMachine',
172 'symptomDefinitionId': u
'SymptomDefinition-25278b06-bff8-4409-a141-9b4e064235df', 'type': 16}
174 m_post
.return_value
.status_code
= 201
175 m_post
.return_value
.content
= '{"id":"AlertDefinition-d4f21e4b-770a-45d6-b298-022eaf489115",\
176 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
177 "description":"CPU_Utilization_Above_Threshold","adapterKindKey":"VMWARE",\
178 "resourceKindKey":"VirtualMachine","waitCycles":1,"cancelCycles":1,"type":16,\
179 "subType":19,"states":[{"severity":"CRITICAL","base-symptom-set":{"type":"SYMPTOM_SET",\
180 "relation":"SELF","aggregation":"ALL","symptomSetOperator":"AND",\
181 "symptomDefinitionIds":["SymptomDefinition-25278b06-bff8-4409-a141-9b4e064235df"]},\
182 "impact":{"impactType":"BADGE","detail":"risk"}}]}'
184 exepcted_return
= "AlertDefinition-d4f21e4b-770a-45d6-b298-022eaf489115"
186 # call create alarm definition method under test
187 actual_return
= self
.mon_plugin
.create_alarm_definition(alarm_param
)
189 # verify that mocked method is called
190 m_post
.assert_called()
192 # verify return value with expected value
193 self
.assertEqual(exepcted_return
, actual_return
)
196 # For testing purpose
197 #if __name__ == '__main__':