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 sys
.path
.append(os
.path
.join(os
.path
.dirname(os
.path
.abspath(__file__
)),"..","..",".."))
42 from osm_mon
.plugins
.vRealiseOps
import mon_plugin_vrops
as monPlugin
44 from pyvcloud
.vcd
.client
import Client
47 class TestMonPlugin(unittest
.TestCase
):
48 """Test class for vROPs Mon Plugin class methods"""
51 """Setup the tests for Mon Plugin class methods"""
52 super(TestMonPlugin
, self
).setUp()
53 self
.mon_plugin
= monPlugin
.MonPlugin()
54 # create client object
55 self
.vca
= Client('test', verify_ssl_certs
=False)
57 self
.session
= requests
.Session()
59 def test_get_default_Params_valid_metric_alarm_name(self
):
60 """Test get default params method"""
62 # Mock valid metric_alarm_name and response
63 metric_alarm_name
= "Average_Memory_Usage_Above_Threshold"
64 expected_return
= {'impact': 'risk', 'cancel_cycles': 2, 'adapter_kind': 'VMWARE',
65 'repeat': False,'cancel_period': 300, 'alarm_type': 16,
66 'vrops_alarm': 'Avg_Mem_Usage_Above_Thr','enabled': True, 'period': 300,
67 'resource_kind': 'VirtualMachine', 'alarm_subType': 19,
68 'action': 'acknowledge', 'evaluation': 2, 'unit': '%'}
70 # call get default param function under test
71 actual_return
= self
.mon_plugin
.get_default_Params(metric_alarm_name
)
73 # verify return value with expected value
74 self
.assertEqual(expected_return
, actual_return
)
77 def test_get_default_Params_invalid_metric_alarm_name(self
):
78 """Test get default params method invalid metric alarm"""
80 # Mock valid metric_alarm_name and response
81 metric_alarm_name
= "Invalid_Alarm"
84 # call get default param function under test
85 actual_return
= self
.mon_plugin
.get_default_Params(metric_alarm_name
)
87 # verify return value with expected value
88 self
.assertEqual(expected_return
, actual_return
)
91 @mock.patch
.object(monPlugin
.requests
, 'post')
92 def test_create_symptom_valid_req_response(self
, m_post
):
93 """Test create symptom method-valid request"""
95 # Mock valid symptom params and mock responses
96 symptom_param
= {'threshold_value': 0, 'cancel_cycles': 1, 'adapter_kind_key': 'VMWARE',
97 'resource_kind_key': 'VirtualMachine', 'severity': 'CRITICAL',
98 'symptom_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
99 'operation': 'GT', 'wait_cycles': 1, 'metric_key': 'cpu|usage_average'}
101 m_post
.return_value
.status_code
= 201
102 m_post
.return_value
.content
= \
103 '{"id":"SymptomDefinition-351c23b4-bc3c-4c7b-b4af-1ad90a673c5d",\
104 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
105 "adapterKindKey":"VMWARE","resourceKindKey":"VirtualMachine","waitCycles":1,\
106 "cancelCycles":1,"state":{"severity":"CRITICAL","condition":{"type":"CONDITION_HT",\
107 "key":"cpu|usage_average","operator":"GT","value":"0.0","valueType":"NUMERIC",\
108 "instanced":false,"thresholdType":"STATIC"}}}'
110 expected_return
= "SymptomDefinition-351c23b4-bc3c-4c7b-b4af-1ad90a673c5d"
112 # call create symptom method under test
113 actual_return
= self
.mon_plugin
.create_symptom(symptom_param
)
115 # verify that mocked method is called
116 m_post
.assert_called()
118 # verify return value with expected value
119 self
.assertEqual(expected_return
, actual_return
)
122 @mock.patch
.object(monPlugin
.requests
, 'post')
123 def test_create_symptom_invalid_req_response(self
, m_post
):
124 """Test create symptom method-invalid response"""
126 # Mock valid symptom params and invalid mock responses
127 symptom_param
= {'threshold_value': 0, 'cancel_cycles': 1, 'adapter_kind_key': 'VMWARE',
128 'resource_kind_key': 'VirtualMachine', 'severity': 'CRITICAL',
129 'symptom_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
130 'operation': 'GT', 'wait_cycles': 1, 'metric_key': 'cpu|usage_average'}
132 m_post
.return_value
.status_code
= 404
133 m_post
.return_value
.content
= '404 Not Found'
135 expected_return
= None
137 # call create symptom method under test
138 actual_return
= self
.mon_plugin
.create_symptom(symptom_param
)
140 # verify that mocked method is called
141 m_post
.assert_called()
143 # verify return value with expected value
144 self
.assertEqual(expected_return
, actual_return
)
147 @mock.patch
.object(monPlugin
.requests
, 'post')
148 def test_create_symptom_incorrect_data(self
, m_post
):
149 """Test create symptom method-incorrect data"""
151 # Mock valid symptom params and invalid mock responses
152 symptom_param
= {'threshold_value': 0, 'cancel_cycles': 1, 'adapter_kind_key': 'VMWARE',
153 'resource_kind_key': 'VirtualMachine', 'severity': 'CRITICAL',
154 'symptom_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
155 'operation': 'GT', 'metric_key': 'cpu|usage_average'}
157 expected_return
= None
159 # call create symptom method under test
160 actual_return
= self
.mon_plugin
.create_symptom(symptom_param
)
162 # verify that mocked method is not called
163 m_post
.assert_not_called()
165 # verify return value with expected value
166 self
.assertEqual(expected_return
, actual_return
)
169 @mock.patch
.object(monPlugin
.requests
, 'post')
170 def test_create_alarm_definition_valid_req_response(self
, m_post
):
171 """Test create alarm definition method-valid response"""
173 # Mock valid alarm params and mock responses
174 alarm_param
= {'description': 'CPU_Utilization_Above_Threshold', 'cancelCycles': 1, 'subType': 19,
175 'waitCycles': 1, 'severity': 'CRITICAL', 'impact': 'risk', 'adapterKindKey': 'VMWARE',
176 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
177 'resourceKindKey': 'VirtualMachine', 'type': 16,
178 'symptomDefinitionId': 'SymptomDefinition-25278b06-bff8-4409-a141-9b4e064235df'}
180 m_post
.return_value
.status_code
= 201
181 m_post
.return_value
.content
= \
182 '{"id":"AlertDefinition-d4f21e4b-770a-45d6-b298-022eaf489115",\
183 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
184 "description":"CPU_Utilization_Above_Threshold","adapterKindKey":"VMWARE",\
185 "resourceKindKey":"VirtualMachine","waitCycles":1,"cancelCycles":1,"type":16,\
186 "subType":19,"states":[{"severity":"CRITICAL","base-symptom-set":{"type":"SYMPTOM_SET",\
187 "relation":"SELF","aggregation":"ALL","symptomSetOperator":"AND",\
188 "symptomDefinitionIds":["SymptomDefinition-25278b06-bff8-4409-a141-9b4e064235df"]},\
189 "impact":{"impactType":"BADGE","detail":"risk"}}]}'
191 expected_return
= "AlertDefinition-d4f21e4b-770a-45d6-b298-022eaf489115"
193 # call create alarm definition method under test
194 actual_return
= self
.mon_plugin
.create_alarm_definition(alarm_param
)
196 # verify that mocked method is called
197 m_post
.assert_called()
199 # verify return value with expected value
200 self
.assertEqual(expected_return
, actual_return
)
203 @mock.patch
.object(monPlugin
.requests
, 'post')
204 def test_create_alarm_definition_invalid_req_response(self
, m_post
):
205 """Test create alarm definition method-invalid response"""
207 # Mock valid alarm params and mock responses
208 alarm_param
= {'description': 'CPU_Utilization_Above_Threshold', 'cancelCycles': 1, 'subType': 19,
209 'waitCycles': 1, 'severity': 'CRITICAL', 'impact': 'risk', 'adapterKindKey': 'VMWARE',
210 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
211 'resourceKindKey': 'VirtualMachine', 'type': 16,
212 'symptomDefinitionId': 'SymptomDefinition-25278b06-bff8-4409-a141-9b4e064235df'}
214 m_post
.return_value
.status_code
= 404
215 m_post
.return_value
.content
= '404 Not Found'
217 expected_return
= None
219 # call create alarm definition method under test
220 actual_return
= self
.mon_plugin
.create_alarm_definition(alarm_param
)
222 # verify that mocked method is called
223 m_post
.assert_called()
225 # verify return value with expected value
226 self
.assertEqual(expected_return
, actual_return
)
229 @mock.patch
.object(monPlugin
.requests
, 'post')
230 def test_create_alarm_definition_incorrect_data(self
, m_post
):
231 """Test create alarm definition method-incorrect data"""
233 # Mock incorrect alarm param
234 alarm_param
= {'description': 'CPU_Utilization_Above_Threshold', 'cancelCycles': 1, 'subType': 19,
235 'waitCycles': 1, 'severity': 'CRITICAL', 'impact': 'risk', 'adapterKindKey': 'VMWARE',
236 'symptomDefinitionId': 'SymptomDefinition-25278b06-bff8-4409-a141-9b4e064235df', 'type': 16}
237 expected_return
= None
239 # call create symptom method under test
240 actual_return
= self
.mon_plugin
.create_alarm_definition(alarm_param
)
242 # verify that mocked method is not called
243 m_post
.assert_not_called()
245 # verify return value with expected value
246 self
.assertEqual(expected_return
, actual_return
)
249 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
250 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
251 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
252 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
253 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
254 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
255 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
256 def test_configure_alarm_valid_req(self
, m_get_default_Params
,\
257 m_get_alarm_defination_by_name
,\
259 m_create_alarm_definition
,\
261 m_get_vm_resource_id
,\
262 m_create_alarm_notification_rule
):
263 """Test configure alarm valid request creating alarm"""
265 #Mock input configuration dictionary
266 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
267 'alarm_name': 'CPU_Utilization_Above_Threshold',
268 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
269 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
270 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
271 'operation': 'GT', 'unit': '%',
272 'description': 'CPU_Utilization_Above_Threshold'}
274 #symptom parameters to be passed for symptom creation
275 symptom_params
= {'threshold_value': 0,
277 'adapter_kind_key': 'VMWARE',
278 'resource_kind_key': 'VirtualMachine',
279 'severity': 'CRITICAL',
280 'symptom_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
283 'metric_key': 'cpu|usage_average'}
285 #alarm parameters to be passed for alarm creation
286 alarm_params
= {'description': 'CPU_Utilization_Above_Threshold',
287 'cancelCycles': 1, 'subType': 19,
288 'waitCycles': 1, 'severity': 'CRITICAL',
289 'impact': 'risk', 'adapterKindKey': 'VMWARE',
290 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
291 'resourceKindKey': 'VirtualMachine',
292 'symptomDefinitionId': 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804',
295 vm_moref_id
= 'vm-6626'
296 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
297 alarm_def
= 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
298 resource_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
299 alarm_def_uuid
= '0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
301 #Mock default Parameters for alarm & metric configuration
302 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
303 'adapter_kind': 'VMWARE', 'repeat': False,
304 'cancel_period': 300, 'alarm_type': 16,
305 'vrops_alarm': 'CPU_Utilization_Above_Thr',
306 'enabled': True, 'period': 300,
307 'resource_kind': 'VirtualMachine',
308 'alarm_subType': 19, 'action': 'acknowledge',
309 'evaluation': 1, 'unit': 'msec'},
310 {'metric_key': 'cpu|usage_average', 'unit': '%'}
313 #set mocked function return values
314 m_get_alarm_defination_by_name
.return_value
= []
315 m_create_symptom
.return_value
= 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804'
316 m_create_alarm_definition
.return_value
= 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
317 m_get_vm_moref_id
.return_value
= vm_moref_id
318 m_get_vm_resource_id
.return_value
= 'ac87622f-b761-40a0-b151-00872a2a456e'
319 m_create_alarm_notification_rule
.return_value
= 'f37900e7-dd01-4383-b84c-08f519530d71'
321 #Call configure_alarm method under test
322 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
324 #Verify that mocked methods are called with correct parameters
325 self
.assertEqual(m_get_default_Params
.call_count
, 2)
326 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
327 m_create_symptom
.assert_called_with(symptom_params
)
328 m_create_alarm_definition
.assert_called_with(alarm_params
)
329 m_get_vm_moref_id
.assert_called_with(config_dict
['resource_uuid'])
330 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
331 m_create_alarm_notification_rule
.assert_called_with(vrops_alarm_name
, alarm_def
, resource_id
)
333 #Verify return value with expected value of alarm_def_uuid
334 self
.assertEqual(return_value
, alarm_def_uuid
)
337 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
338 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
339 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
340 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
341 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
342 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
343 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
344 def test_configure_alarm_invalid_alarm_name_req(self
, m_get_default_Params
,\
345 m_get_alarm_defination_by_name
,\
347 m_create_alarm_definition
,\
349 m_get_vm_resource_id
,\
350 m_create_alarm_notification_rule
):
351 """Test configure alarm invalid test: for invalid alarm name"""
353 #Mock input configuration dictionary
354 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
355 'alarm_name': 'CPU_Utilization_Above_Threshold',
356 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
357 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
358 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
359 'operation': 'GT', 'unit': '%',
360 'description': 'CPU_Utilization_Above_Threshold'}
362 alarm_def_uuid
= None
364 #Mock default Parameters return value to None
365 m_get_default_Params
.return_value
= {}
367 #Call configure_alarm method under test
368 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
370 #Verify that mocked methods are called with correct parameters
371 m_get_default_Params
.assert_called_once()
372 m_get_alarm_defination_by_name
.assert_not_called()
373 m_create_symptom
.assert_not_called()
374 m_create_alarm_definition
.assert_not_called()
375 m_get_vm_moref_id
.assert_not_called()
376 m_get_vm_resource_id
.assert_not_called()
377 m_create_alarm_notification_rule
.assert_not_called()
379 #Verify return value with expected value i.e. None
380 self
.assertEqual(return_value
, alarm_def_uuid
)
383 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
384 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
385 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
386 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
387 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
388 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
389 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
390 def test_configure_alarm_invalid_metric_name_req(self
, m_get_default_Params
,\
391 m_get_alarm_defination_by_name
,\
393 m_create_alarm_definition
,\
395 m_get_vm_resource_id
,\
396 m_create_alarm_notification_rule
):
397 """Test configure alarm invalid test: for invalid metric name"""
399 #Mock input configuration dictionary
400 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
401 'alarm_name': 'CPU_Utilization_Above_Threshold',
402 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
403 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
404 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
405 'operation': 'GT', 'unit': '%',
406 'description': 'CPU_Utilization_Above_Threshold'}
408 alarm_def_uuid
= None
410 #Mock default Parameters return values for metrics to None
411 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
412 'adapter_kind': 'VMWARE', 'repeat': False,
413 'cancel_period': 300, 'alarm_type': 16,
414 'vrops_alarm': 'CPU_Utilization_Above_Thr',
415 'enabled': True, 'period': 300,
416 'resource_kind': 'VirtualMachine',
417 'alarm_subType': 19, 'action': 'acknowledge',
418 'evaluation': 1, 'unit': 'msec'},
422 #Call configure_alarm method under test
423 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
425 #Verify that mocked methods are called with correct parameters
426 self
.assertEqual(m_get_default_Params
.call_count
, 2)
427 m_get_alarm_defination_by_name
.assert_not_called()
428 m_create_symptom
.assert_not_called()
429 m_create_alarm_definition
.assert_not_called()
430 m_get_vm_moref_id
.assert_not_called()
431 m_get_vm_resource_id
.assert_not_called()
432 m_create_alarm_notification_rule
.assert_not_called()
434 #Verify return value with expected value i.e. None
435 self
.assertEqual(return_value
, alarm_def_uuid
)
437 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
438 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
439 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
440 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
441 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
442 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
443 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
444 def test_configure_alarm_invalid_already_exists(self
, m_get_default_Params
,\
445 m_get_alarm_defination_by_name
,\
447 m_create_alarm_definition
,\
449 m_get_vm_resource_id
,\
450 m_create_alarm_notification_rule
):
451 """Test configure alarm invalid test: for alarm that already exists"""
453 #Mock input configuration dictionary
454 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
455 'alarm_name': 'CPU_Utilization_Above_Threshold',
456 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
457 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
458 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
459 'operation': 'GT', 'unit': '%',
460 'description': 'CPU_Utilization_Above_Threshold'}
462 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
463 alarm_def_uuid
= None
465 #Mock default Parameters for alarm & metric configuration
466 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
467 'adapter_kind': 'VMWARE', 'repeat': False,
468 'cancel_period': 300, 'alarm_type': 16,
469 'vrops_alarm': 'CPU_Utilization_Above_Thr',
470 'enabled': True, 'period': 300,
471 'resource_kind': 'VirtualMachine',
472 'alarm_subType': 19, 'action': 'acknowledge',
473 'evaluation': 1, 'unit': 'msec'},
474 {'metric_key': 'cpu|usage_average', 'unit': '%'}
476 #set mocked function return value
477 m_get_alarm_defination_by_name
.return_value
= ['mocked_alarm_CPU_Utilization_Above_Thr']
480 #Call configure_alarm method under test
481 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
483 #Verify that mocked methods are called with correct parameters
484 self
.assertEqual(m_get_default_Params
.call_count
, 2)
485 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
486 m_create_symptom
.assert_not_called()
487 m_create_alarm_definition
.assert_not_called()
488 m_get_vm_moref_id
.assert_not_called()
489 m_get_vm_resource_id
.assert_not_called()
490 m_create_alarm_notification_rule
.assert_not_called()
492 #Verify return value with expected value of alarm_def_uuid
493 self
.assertEqual(return_value
, alarm_def_uuid
)
496 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
497 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
498 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
499 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
500 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
501 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
502 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
503 def test_configure_alarm_failed_symptom_creation(self
, m_get_default_Params
,\
504 m_get_alarm_defination_by_name
,\
506 m_create_alarm_definition
,\
508 m_get_vm_resource_id
,\
509 m_create_alarm_notification_rule
):
510 """Test configure alarm: failed to create symptom"""
512 #Mock input configuration dictionary
513 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
514 'alarm_name': 'CPU_Utilization_Above_Threshold',
515 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
516 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
517 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
518 'operation': 'GT', 'unit': '%',
519 'description': 'CPU_Utilization_Above_Threshold'}
521 #symptom parameters to be passed for symptom creation
522 symptom_params
= {'threshold_value': 0,
524 'adapter_kind_key': 'VMWARE',
525 'resource_kind_key': 'VirtualMachine',
526 'severity': 'CRITICAL',
527 'symptom_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
530 'metric_key': 'cpu|usage_average'}
531 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
532 alarm_def_uuid
= None
534 #Mock default Parameters for alarm & metric configuration
535 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
536 'adapter_kind': 'VMWARE', 'repeat': False,
537 'cancel_period': 300, 'alarm_type': 16,
538 'vrops_alarm': 'CPU_Utilization_Above_Thr',
539 'enabled': True, 'period': 300,
540 'resource_kind': 'VirtualMachine',
541 'alarm_subType': 19, 'action': 'acknowledge',
542 'evaluation': 1, 'unit': 'msec'},
543 {'metric_key': 'cpu|usage_average', 'unit': '%'}
545 #set mocked function return values
546 m_get_alarm_defination_by_name
.return_value
= []
547 m_create_symptom
.return_value
= None
549 #Call configure_alarm method under test
550 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
552 #Verify that mocked methods are called with correct parameters
553 self
.assertEqual(m_get_default_Params
.call_count
, 2)
554 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
555 m_create_symptom
.assert_called_with(symptom_params
)
556 m_create_alarm_definition
.assert_not_called()
557 m_get_vm_moref_id
.assert_not_called()
558 m_get_vm_resource_id
.assert_not_called()
559 m_create_alarm_notification_rule
.assert_not_called()
561 #Verify return value with expected value of alarm_def_uuid
562 self
.assertEqual(return_value
, alarm_def_uuid
)
565 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
566 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
567 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
568 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
569 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
570 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
571 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
572 def test_configure_alarm_failed_alert_creation(self
, m_get_default_Params
,\
573 m_get_alarm_defination_by_name
,\
575 m_create_alarm_definition
,\
577 m_get_vm_resource_id
,\
578 m_create_alarm_notification_rule
):
579 """Test configure alarm: failed to create alert in vROPs"""
581 #Mock input configuration dictionary
582 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
583 'alarm_name': 'CPU_Utilization_Above_Threshold',
584 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
585 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
586 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
587 'operation': 'GT', 'unit': '%',
588 'description': 'CPU_Utilization_Above_Threshold'}
590 #symptom parameters to be passed for symptom creation
591 symptom_params
= {'threshold_value': 0,
593 'adapter_kind_key': 'VMWARE',
594 'resource_kind_key': 'VirtualMachine',
595 'severity': 'CRITICAL',
596 'symptom_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
599 'metric_key': 'cpu|usage_average'}
601 #alarm parameters to be passed for alarm creation
602 alarm_params
= {'description': 'CPU_Utilization_Above_Threshold',
603 'cancelCycles': 1, 'subType': 19,
604 'waitCycles': 1, 'severity': 'CRITICAL',
605 'impact': 'risk', 'adapterKindKey': 'VMWARE',
606 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
607 'resourceKindKey': 'VirtualMachine',
608 'symptomDefinitionId': 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804',
611 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
612 alarm_def_uuid
= None
614 #Mock default Parameters for alarm & metric configuration
615 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
616 'adapter_kind': 'VMWARE', 'repeat': False,
617 'cancel_period': 300, 'alarm_type': 16,
618 'vrops_alarm': 'CPU_Utilization_Above_Thr',
619 'enabled': True, 'period': 300,
620 'resource_kind': 'VirtualMachine',
621 'alarm_subType': 19, 'action': 'acknowledge',
622 'evaluation': 1, 'unit': 'msec'},
623 {'metric_key': 'cpu|usage_average', 'unit': '%'}
625 #set mocked function return values
626 m_get_alarm_defination_by_name
.return_value
= []
627 m_create_symptom
.return_value
= 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804'
628 m_create_alarm_definition
.return_value
= None
630 #Call configure_alarm method under test
631 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
633 #Verify that mocked methods are called with correct parameters
634 self
.assertEqual(m_get_default_Params
.call_count
, 2)
635 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
636 m_create_symptom
.assert_called_with(symptom_params
)
637 m_create_alarm_definition
.assert_called_with(alarm_params
)
638 m_get_vm_moref_id
.assert_not_called()
639 m_get_vm_resource_id
.assert_not_called()
640 m_create_alarm_notification_rule
.assert_not_called()
642 #Verify return value with expected value of alarm_def_uuid
643 self
.assertEqual(return_value
, alarm_def_uuid
)
646 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
647 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
648 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
649 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
650 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
651 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
652 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
653 def test_configure_alarm_failed_to_get_vm_moref_id(self
, m_get_default_Params
,\
654 m_get_alarm_defination_by_name
,\
656 m_create_alarm_definition
,\
658 m_get_vm_resource_id
,\
659 m_create_alarm_notification_rule
):
660 """Test configure alarm: failed to get vm_moref_id"""
662 #Mock input configuration dictionary
663 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
664 'alarm_name': 'CPU_Utilization_Above_Threshold',
665 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
666 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
667 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
668 'operation': 'GT', 'unit': '%',
669 'description': 'CPU_Utilization_Above_Threshold'}
671 #symptom parameters to be passed for symptom creation
672 symptom_params
= {'threshold_value': 0,
674 'adapter_kind_key': 'VMWARE',
675 'resource_kind_key': 'VirtualMachine',
676 'severity': 'CRITICAL',
677 'symptom_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
680 'metric_key': 'cpu|usage_average'}
682 #alarm parameters to be passed for alarm creation
683 alarm_params
= {'description': 'CPU_Utilization_Above_Threshold',
684 'cancelCycles': 1, 'subType': 19,
685 'waitCycles': 1, 'severity': 'CRITICAL',
686 'impact': 'risk', 'adapterKindKey': 'VMWARE',
687 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
688 'resourceKindKey': 'VirtualMachine',
689 'symptomDefinitionId': 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804',
692 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
693 alarm_def_uuid
= None
695 #Mock default Parameters for alarm & metric configuration
696 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
697 'adapter_kind': 'VMWARE', 'repeat': False,
698 'cancel_period': 300, 'alarm_type': 16,
699 'vrops_alarm': 'CPU_Utilization_Above_Thr',
700 'enabled': True, 'period': 300,
701 'resource_kind': 'VirtualMachine',
702 'alarm_subType': 19, 'action': 'acknowledge',
703 'evaluation': 1, 'unit': 'msec'},
704 {'metric_key': 'cpu|usage_average', 'unit': '%'}
706 #set mocked function return values
707 m_get_alarm_defination_by_name
.return_value
= []
708 m_create_symptom
.return_value
= 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804'
709 m_create_alarm_definition
.return_value
= 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
710 m_get_vm_moref_id
.return_value
= None
712 #Call configure_alarm method under test
713 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
715 #Verify that mocked methods are called with correct parameters
716 self
.assertEqual(m_get_default_Params
.call_count
, 2)
717 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
718 m_create_symptom
.assert_called_with(symptom_params
)
719 m_create_alarm_definition
.assert_called_with(alarm_params
)
720 m_get_vm_moref_id
.assert_called_with(config_dict
['resource_uuid'])
721 m_get_vm_resource_id
.assert_not_called()
722 m_create_alarm_notification_rule
.assert_not_called()
724 #Verify return value with expected value of alarm_def_uuid
725 self
.assertEqual(return_value
, alarm_def_uuid
)
728 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
729 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
730 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
731 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
732 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
733 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
734 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
735 def test_configure_alarm_failed_to_get_vm_resource_id(self
, m_get_default_Params
,\
736 m_get_alarm_defination_by_name
,\
738 m_create_alarm_definition
,\
740 m_get_vm_resource_id
,\
741 m_create_alarm_notification_rule
):
742 """Test configure alarm: failed to get vm resource_id"""
744 #Mock input configuration dictionary
745 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
746 'alarm_name': 'CPU_Utilization_Above_Threshold',
747 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
748 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
749 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
750 'operation': 'GT', 'unit': '%',
751 'description': 'CPU_Utilization_Above_Threshold'}
753 #symptom parameters to be passed for symptom creation
754 symptom_params
= {'threshold_value': 0,
756 'adapter_kind_key': 'VMWARE',
757 'resource_kind_key': 'VirtualMachine',
758 'severity': 'CRITICAL',
759 'symptom_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
762 'metric_key': 'cpu|usage_average'}
764 #alarm parameters to be passed for alarm creation
765 alarm_params
= {'description': 'CPU_Utilization_Above_Threshold',
766 'cancelCycles': 1, 'subType': 19,
767 'waitCycles': 1, 'severity': 'CRITICAL',
768 'impact': 'risk', 'adapterKindKey': 'VMWARE',
769 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
770 'resourceKindKey': 'VirtualMachine',
771 'symptomDefinitionId': 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804',
774 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
775 vm_moref_id
= 'vm-6626'
776 alarm_def_uuid
= None
778 #Mock default Parameters for alarm & metric configuration
779 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
780 'adapter_kind': 'VMWARE', 'repeat': False,
781 'cancel_period': 300, 'alarm_type': 16,
782 'vrops_alarm': 'CPU_Utilization_Above_Thr',
783 'enabled': True, 'period': 300,
784 'resource_kind': 'VirtualMachine',
785 'alarm_subType': 19, 'action': 'acknowledge',
786 'evaluation': 1, 'unit': 'msec'},
787 {'metric_key': 'cpu|usage_average', 'unit': '%'}
789 #set mocked function return values
790 m_get_alarm_defination_by_name
.return_value
= []
791 m_create_symptom
.return_value
= 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804'
792 m_create_alarm_definition
.return_value
= 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
793 m_get_vm_moref_id
.return_value
= vm_moref_id
794 m_get_vm_resource_id
.return_value
= None
796 #Call configure_alarm method under test
797 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
799 #Verify that mocked methods are called with correct parameters
800 self
.assertEqual(m_get_default_Params
.call_count
, 2)
801 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
802 m_create_symptom
.assert_called_with(symptom_params
)
803 m_create_alarm_definition
.assert_called_with(alarm_params
)
804 m_get_vm_moref_id
.assert_called_with(config_dict
['resource_uuid'])
805 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
806 m_create_alarm_notification_rule
.assert_not_called()
808 #Verify return value with expected value of alarm_def_uuid
809 self
.assertEqual(return_value
, alarm_def_uuid
)
812 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
813 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
814 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
815 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
816 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
817 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
818 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
819 def test_configure_alarm_failed_to_create_alarm_notification_rule(self
, m_get_default_Params
,\
820 m_get_alarm_defination_by_name
,\
822 m_create_alarm_definition
,\
824 m_get_vm_resource_id
,\
825 m_create_alarm_notification_rule
):
826 """Test configure alarm: failed to create alarm notification rule"""
828 #Mock input configuration dictionary
829 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
830 'alarm_name': 'CPU_Utilization_Above_Threshold',
831 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
832 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
833 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
834 'operation': 'GT', 'unit': '%',
835 'description': 'CPU_Utilization_Above_Threshold'}
837 #symptom parameters to be passed for symptom creation
838 symptom_params
= {'threshold_value': 0,
840 'adapter_kind_key': 'VMWARE',
841 'resource_kind_key': 'VirtualMachine',
842 'severity': 'CRITICAL',
843 'symptom_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
846 'metric_key': 'cpu|usage_average'}
848 #alarm parameters to be passed for alarm creation
849 alarm_params
= {'description': 'CPU_Utilization_Above_Threshold',
850 'cancelCycles': 1, 'subType': 19,
851 'waitCycles': 1, 'severity': 'CRITICAL',
852 'impact': 'risk', 'adapterKindKey': 'VMWARE',
853 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
854 'resourceKindKey': 'VirtualMachine',
855 'symptomDefinitionId': 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804',
858 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
859 vm_moref_id
= 'vm-6626'
860 alarm_def
= 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
861 resource_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
862 alarm_def_uuid
= None
864 #Mock default Parameters for alarm & metric configuration
865 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
866 'adapter_kind': 'VMWARE', 'repeat': False,
867 'cancel_period': 300, 'alarm_type': 16,
868 'vrops_alarm': 'CPU_Utilization_Above_Thr',
869 'enabled': True, 'period': 300,
870 'resource_kind': 'VirtualMachine',
871 'alarm_subType': 19, 'action': 'acknowledge',
872 'evaluation': 1, 'unit': 'msec'},
873 {'metric_key': 'cpu|usage_average', 'unit': '%'}
875 #set mocked function return values
876 m_get_alarm_defination_by_name
.return_value
= []
877 m_create_symptom
.return_value
= 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804'
878 m_create_alarm_definition
.return_value
= 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
879 m_get_vm_moref_id
.return_value
= vm_moref_id
880 m_get_vm_resource_id
.return_value
= 'ac87622f-b761-40a0-b151-00872a2a456e'
881 m_create_alarm_notification_rule
.return_value
= None
883 #Call configure_alarm method under test
884 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
886 #Verify that mocked methods are called with correct parameters
887 self
.assertEqual(m_get_default_Params
.call_count
, 2)
888 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
889 m_create_symptom
.assert_called_with(symptom_params
)
890 m_create_alarm_definition
.assert_called_with(alarm_params
)
891 m_get_vm_moref_id
.assert_called_with(config_dict
['resource_uuid'])
892 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
893 m_create_alarm_notification_rule
.assert_called_with(vrops_alarm_name
, alarm_def
, resource_id
)
895 #Verify return value with expected value of alarm_def_uuid
896 self
.assertEqual(return_value
, alarm_def_uuid
)
899 @mock.patch
.object(monPlugin
.requests
, 'get')
900 def test_get_alarm_defination_details_valid_rest_req_response(self
, m_get
):
901 """Test get_alarm_defination_details: For a valid REST request response"""
903 alarm_uuid
= '9a6d8a14-9f25-4d81-bf91-4d773497444d'
905 #Set mocked function's return values
906 m_get
.return_value
.status_code
= 200
907 m_get
.return_value
.content
= '{"id":"AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d",\
908 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
909 "description":"CPU_Utilization_Above_Threshold",\
910 "adapterKindKey":"VMWARE","resourceKindKey":"VirtualMachine",\
911 "waitCycles":1,"cancelCycles":1,"type":16,"subType":19,\
912 "states":[{"severity":"CRITICAL","base-symptom-set":{"type":"SYMPTOM_SET",\
913 "relation":"SELF","aggregation":"ALL","symptomSetOperator":"AND",\
914 "symptomDefinitionIds":["SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278"]},\
915 "impact":{"impactType":"BADGE","detail":"risk"}}]}'
917 expected_alarm_details
= {'adapter_kind': 'VMWARE',
918 'symptom_definition_id': 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278',
919 'alarm_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
920 'alarm_id': 'AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d',
921 'resource_kind': 'VirtualMachine', 'type': 16, 'sub_type': 19}
923 expected_alarm_details_json
= {'states':
925 {'impactType':'BADGE', 'detail':'risk'},'severity':'CRITICAL',
926 'base-symptom-set': {'symptomDefinitionIds':\
927 ['SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'],
928 'relation': 'SELF', 'type': 'SYMPTOM_SET',
929 'aggregation':'ALL', 'symptomSetOperator': 'AND'}}],
930 'adapterKindKey': 'VMWARE',
931 'description': 'CPU_Utilization_Above_Threshold',
932 'type': 16, 'cancelCycles': 1, 'resourceKindKey': 'VirtualMachine',
933 'subType': 19, 'waitCycles': 1,
934 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d',
935 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'}
937 #Call get_alarm_defination_details method under test
938 alarm_details_json
, alarm_details
= self
.mon_plugin
.get_alarm_defination_details(alarm_uuid
)
940 #Verify that mocked method is called
941 m_get
.assert_called()
943 #Verify return value with expected value
944 self
.assertEqual(expected_alarm_details
, alarm_details
)
945 self
.assertEqual(expected_alarm_details_json
, alarm_details_json
)
948 @mock.patch
.object(monPlugin
.requests
, 'get')
949 def test_get_alarm_defination_details_invalid_rest_req_response(self
, m_get
):
950 """Test get_alarm_defination_details: For an invalid REST request response"""
952 alarm_uuid
= '9a6d8a14-9f25-4d81-bf91-4d773497444d'
954 #Set mocked function's return values
955 m_get
.return_value
.status_code
= 404
956 m_get
.return_value
.content
= '{"message": "No such AlertDefinition - \
957 AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444.",\
958 "httpStatusCode": 404,"apiErrorCode": 404}'
960 expected_alarm_details
= None
961 expected_alarm_details_json
= None
963 #Call get_alarm_defination_details method under test
964 alarm_details_json
, alarm_details
= self
.mon_plugin
.get_alarm_defination_details(alarm_uuid
)
966 #verify that mocked method is called
967 m_get
.assert_called()
969 #Verify return value with expected value
970 self
.assertEqual(expected_alarm_details
, alarm_details
)
971 self
.assertEqual(expected_alarm_details_json
, alarm_details_json
)
974 @mock.patch
.object(monPlugin
.requests
, 'get')
975 def test_get_alarm_defination_by_name_valid_rest_req_response(self
, m_get
):
976 """Test get_alarm_defination_by_name: For a valid REST request response"""
978 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
980 #Set mocked function's return values
981 m_get
.return_value
.status_code
= 200
982 m_get
.return_value
.content
= '{"pageInfo": {"totalCount": 1,"page": 0,"pageSize": 1000},\
984 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
985 "rel": "SELF","name": "current"},\
986 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
987 "rel": "RELATED","name": "first"},\
988 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
989 "rel": "RELATED","name": "last"}],\
990 "alertDefinitions": [{\
991 "id": "AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d",\
992 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
993 "description": "CPU_Utilization_Above_Threshold",\
994 "adapterKindKey": "VMWARE","resourceKindKey": "VirtualMachine",\
995 "waitCycles": 1,"cancelCycles": 1,"type": 16,"subType": 19,\
996 "states": [{"impact": {"impactType": "BADGE","detail": "risk"},\
997 "severity": "CRITICAL",\
998 "base-symptom-set": {"type": "SYMPTOM_SET",\
999 "relation": "SELF","aggregation": "ALL",\
1000 "symptomSetOperator": "AND",\
1001 "symptomDefinitionIds": [\
1002 "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278"]}}]\
1005 #Expected return match list
1006 Exp_alert_match_list
= [{'states':
1007 [{'impact': {'impactType': 'BADGE', 'detail': 'risk'},
1008 'severity': 'CRITICAL',
1009 'base-symptom-set': {
1010 'symptomDefinitionIds': \
1011 ['SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'],
1013 'type': 'SYMPTOM_SET',
1014 'aggregation': 'ALL',
1015 'symptomSetOperator': 'AND'}
1017 'adapterKindKey': 'VMWARE',
1018 'description': 'CPU_Utilization_Above_Threshold',
1021 'resourceKindKey': 'VirtualMachine',
1022 'subType': 19, 'waitCycles': 1,
1023 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d',
1025 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
1028 #Call get_alarm_defination_by_name method under test
1029 alert_match_list
= self
.mon_plugin
.get_alarm_defination_by_name(alarm_name
)
1031 #Verify that mocked method is called
1032 m_get
.assert_called()
1034 #Verify return value with expected value
1035 self
.assertEqual(Exp_alert_match_list
, alert_match_list
)
1038 @mock.patch
.object(monPlugin
.requests
, 'get')
1039 def test_get_alarm_defination_by_name_no_valid_alarm_found(self
, m_get
):
1040 """Test get_alarm_defination_by_name: With no valid alarm found in returned list"""
1042 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda5'
1044 #Set mocked function's return values
1045 m_get
.return_value
.status_code
= 200
1046 m_get
.return_value
.content
= '{"pageInfo": {"totalCount": 1,"page": 0,"pageSize": 1000},\
1048 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
1049 "rel": "SELF","name": "current"},\
1050 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
1051 "rel": "RELATED","name": "first"},\
1052 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
1053 "rel": "RELATED","name": "last"}],\
1054 "alertDefinitions": [{\
1055 "id": "AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d",\
1056 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1057 "description": "CPU_Utilization_Above_Threshold",\
1058 "adapterKindKey": "VMWARE","resourceKindKey": "VirtualMachine",\
1059 "waitCycles": 1,"cancelCycles": 1,"type": 16,"subType": 19,\
1060 "states": [{"impact": {"impactType": "BADGE","detail": "risk"},\
1061 "severity": "CRITICAL",\
1062 "base-symptom-set": {"type": "SYMPTOM_SET",\
1063 "relation": "SELF","aggregation": "ALL",\
1064 "symptomSetOperator": "AND",\
1065 "symptomDefinitionIds": [\
1066 "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278"]}}]\
1069 #Expected return match list
1070 Exp_alert_match_list
= []
1072 #Call get_alarm_defination_by_name method under test
1073 alert_match_list
= self
.mon_plugin
.get_alarm_defination_by_name(alarm_name
)
1075 #Verify that mocked method is called
1076 m_get
.assert_called()
1078 #Verify return value with expected value
1079 self
.assertEqual(Exp_alert_match_list
, alert_match_list
)
1082 @mock.patch
.object(monPlugin
.requests
, 'put')
1083 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_symptom_defination_details')
1084 def test_update_symptom_defination_valid_symptom_req_response(self
,\
1085 m_get_symptom_defination_details
,\
1087 """Test update_symptom_defination: With valid REST response, update symptom"""
1089 #Expected symptom to be updated
1090 symptom_defination_id
= 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'
1091 new_alarm_config
= {'severity':"CRITICAL",
1093 'threshold_value':5,
1094 'alarm_uuid':'9a6d8a14-9f25-4d81-bf91-4d773497444d'
1097 #Set mocked function's return values
1098 m_get_symptom_defination_details
.return_value
= {
1099 "id": "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278",
1100 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",
1101 "adapterKindKey": "VMWARE",
1102 "resourceKindKey": "VirtualMachine",
1105 "state": {"severity": "CRITICAL",
1107 "type": "CONDITION_HT",
1108 "key": "cpu|usage_average","operator": "GT","value": "0.0",
1109 "valueType": "NUMERIC","instanced": False,
1110 "thresholdType": "STATIC"}
1114 m_put
.return_value
.status_code
= 200
1115 m_put
.return_value
.content
= '{\
1116 "id":"SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278",\
1117 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1118 "adapterKindKey":"VMWARE","resourceKindKey":"VirtualMachine","waitCycles":1,\
1121 "severity":"CRITICAL",\
1123 "type":"CONDITION_HT","key":"cpu|usage_average","operator":"GT","value":"5.0",\
1124 "valueType":"NUMERIC","instanced":False,"thresholdType":"STATIC"}}}'
1126 #Call update_symptom_defination method under test
1127 symptom_uuid
= self
.mon_plugin
.update_symptom_defination(symptom_defination_id
,\
1130 #Verify that mocked method is called with required parameters
1131 m_get_symptom_defination_details
.assert_called_with(symptom_defination_id
)
1132 m_put
.assert_called()
1134 #Verify return value with expected value
1135 self
.assertEqual(symptom_defination_id
, symptom_uuid
)
1138 @mock.patch
.object(monPlugin
.requests
, 'put')
1139 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_symptom_defination_details')
1140 def test_update_symptom_defination_invalid_symptom_req_response(self
,\
1141 m_get_symptom_defination_details
,\
1143 """Test update_symptom_defination: If invalid REST response received, return None"""
1145 #Expected symptom to be updated
1146 symptom_defination_id
= 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'
1147 new_alarm_config
= {'severity':"CRITICAL",
1149 'threshold_value':5,
1150 'alarm_uuid':'9a6d8a14-9f25-4d81-bf91-4d773497444d'
1153 #Set mocked function's return values
1154 m_get_symptom_defination_details
.return_value
= {
1155 "id": "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278",
1156 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",
1157 "adapterKindKey": "VMWARE",
1158 "resourceKindKey": "VirtualMachine",
1161 "state": {"severity": "CRITICAL",
1163 "type": "CONDITION_HT",
1164 "key": "cpu|usage_average","operator": "GT","value": "0.0",
1165 "valueType": "NUMERIC","instanced": False,
1166 "thresholdType": "STATIC"}
1170 m_put
.return_value
.status_code
= 500
1171 m_put
.return_value
.content
= '{\
1172 "message": "Internal Server error, cause unknown.",\
1173 "moreInformation": [\
1174 {"name": "errorMessage",\
1175 "value": "Symptom Definition CPU_Utilization_Above_Thr-e14b203c-\
1176 6bf2-4e2f-a91c-8c19d240eda4 does not exist and hence cannot be updated."},\
1177 {"name": "localizedMessage",\
1178 "value": "Symptom Definition CPU_Utilization_Above_Thr-e14b203c-\
1179 6bf2-4e2f-a91c-8c19d240eda4 does not exist and hence cannot be updated.;"}],\
1180 "httpStatusCode": 500,"apiErrorCode": 500}'
1182 #Call update_symptom_defination method under test
1183 symptom_uuid
= self
.mon_plugin
.update_symptom_defination(symptom_defination_id
,\
1186 #Verify that mocked method is called with required parameters
1187 m_get_symptom_defination_details
.assert_called_with(symptom_defination_id
)
1188 m_put
.assert_called()
1190 #Verify return value with expected value
1191 self
.assertEqual(symptom_uuid
, None)
1194 @mock.patch
.object(monPlugin
.requests
, 'put')
1195 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_symptom_defination_details')
1196 def test_update_symptom_defination_failed_to_get_symptom_defination(self
,\
1197 m_get_symptom_defination_details
,\
1199 """Test update_symptom_defination: if fails to get symptom_defination returns None"""
1201 #Expected symptom to be updated
1202 symptom_defination_id
= 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'
1203 new_alarm_config
= {'severity':"CRITICAL",
1205 'threshold_value':5,
1206 'alarm_uuid':'9a6d8a14-9f25-4d81-bf91-4d773497444d'
1209 #Set mocked function's return values
1210 m_get_symptom_defination_details
.return_value
= None
1212 #Call update_symptom_defination method under test
1213 symptom_uuid
= self
.mon_plugin
.update_symptom_defination(symptom_defination_id
,\
1216 #Verify that mocked method is called with required parameters
1217 m_get_symptom_defination_details
.assert_called_with(symptom_defination_id
)
1218 m_put
.assert_not_called()
1220 #Verify return value with expected value
1221 self
.assertEqual(symptom_uuid
, None)
1224 @mock.patch
.object(monPlugin
.requests
, 'get')
1225 def test_get_symptom_defination_details_valid_req_response(self
,m_get
):
1226 """Test update_symptom_defination: With valid REST response symptom is created"""
1228 #Expected symptom to be updated
1229 symptom_uuid
= 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'
1231 #Set mocked function's return values
1232 m_get
.return_value
.status_code
= 200
1233 m_get
.return_value
.content
= '{\
1234 "id": "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278",\
1235 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1236 "adapterKindKey": "VMWARE","resourceKindKey": "VirtualMachine","waitCycles": 1,\
1237 "cancelCycles": 1,"state": {"severity": "CRITICAL","condition": {"type": "CONDITION_HT",\
1238 "key": "cpu|usage_average","operator": "GT","value": "6.0","valueType": "NUMERIC",\
1239 "instanced": false,"thresholdType": "STATIC"}}}'
1240 expected_symptom_details
= {\
1241 "id": "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278",\
1242 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1243 "adapterKindKey": "VMWARE","resourceKindKey": "VirtualMachine","waitCycles": 1,\
1244 "cancelCycles": 1,"state": {"severity": "CRITICAL","condition": {"type": "CONDITION_HT",\
1245 "key": "cpu|usage_average","operator": "GT","value": "6.0","valueType": "NUMERIC",\
1246 "instanced": False,"thresholdType": "STATIC"}}}
1248 #Call update_symptom_defination method under test
1249 symptom_details
= self
.mon_plugin
.get_symptom_defination_details(symptom_uuid
)
1251 #Verify that mocked method is called with required parameters
1252 m_get
.assert_called()
1254 #Verify return value with expected value
1255 self
.assertEqual(expected_symptom_details
, symptom_details
)
1258 @mock.patch
.object(monPlugin
.requests
, 'get')
1259 def test_get_symptom_defination_details_invalid_req_response(self
,m_get
):
1260 """Test update_symptom_defination: if invalid REST response received return None"""
1262 #Expected symptom to be updated
1263 symptom_uuid
= 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'
1265 #Set mocked function's return values
1266 m_get
.return_value
.status_code
= 404
1267 m_get
.return_value
.content
= '{"message": "No such SymptomDefinition\
1268 - SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278.",\
1269 "httpStatusCode": 404,"apiErrorCode": 404}'
1271 expected_symptom_details
= None
1273 #Call update_symptom_defination method under test
1274 symptom_details
= self
.mon_plugin
.get_symptom_defination_details(symptom_uuid
)
1276 #Verify that mocked method is called with required parameters
1277 m_get
.assert_called()
1279 #Verify return value with expected value
1280 self
.assertEqual(expected_symptom_details
, symptom_details
)
1283 @mock.patch
.object(monPlugin
.requests
, 'get')
1284 def test_get_symptom_defination_details_symptom_uuid_not_provided(self
,m_get
):
1285 """Test update_symptom_defination: if required symptom uuid is not provided"""
1287 #Expected symptom to be updated
1289 expected_symptom_details
= None
1291 #Call update_symptom_defination method under test
1292 symptom_details
= self
.mon_plugin
.get_symptom_defination_details(symptom_uuid
)
1294 #Verify that mocked method is called with required parameters
1295 m_get
.assert_not_called()
1297 #Verify return value with expected value
1298 self
.assertEqual(expected_symptom_details
, symptom_details
)
1301 @mock.patch
.object(monPlugin
.requests
, 'put')
1302 def test_reconfigure_alarm_valid_req_response(self
, m_put
):
1303 """Test reconfigure_alarm: for valid REST response"""
1305 #Set input parameters to reconfigure_alarm
1306 alarm_details_json
= {
1307 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d',
1308 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
1309 'description': 'CPU_Utilization_Above_Threshold', 'adapterKindKey': 'VMWARE',
1310 'states':[{'impact':{'impactType':'BADGE', 'detail':'risk'}, 'severity':'CRITICAL',
1311 'base-symptom-set':{
1312 'symptomDefinitionIds':['SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'],
1313 'relation': 'SELF','type': 'SYMPTOM_SET', 'aggregation':'ALL',
1314 'symptomSetOperator': 'AND'}}],
1315 'type': 16, 'cancelCycles': 1, 'resourceKindKey': 'VirtualMachine','subType': 19,
1318 new_alarm_config
= {'severity':'WARNING',
1319 'description': 'CPU_Utilization_Above_Threshold_Warning'}
1321 #Set mocked function's return values
1322 m_put
.return_value
.status_code
= 200
1323 m_put
.return_value
.content
= '{"id":"AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d",\
1324 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1325 "description":"CPU_Utilization_Above_Threshold_Warning","adapterKindKey":"VMWARE",\
1326 "resourceKindKey":"VirtualMachine","waitCycles":1,"cancelCycles":1,"type":16,\
1327 "subType":19,"states":[{"severity":"WARNING","base-symptom-set":{"type":"SYMPTOM_SET",\
1328 "relation":"SELF","aggregation":"ALL","symptomSetOperator":"AND",\
1329 "symptomDefinitionIds":["SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278"]},\
1330 "impact":{"impactType":"BADGE","detail":"risk"}}]}'
1332 #Expected alarm_def_uuid to be returned
1333 expected_alarm_def_uuid
= '9a6d8a14-9f25-4d81-bf91-4d773497444d'
1335 #Call reconfigure_alarm method under test
1336 alarm_def_uuid
= self
.mon_plugin
.reconfigure_alarm(alarm_details_json
, new_alarm_config
)
1338 #Verify that mocked method is called with required parameters
1339 m_put
.assert_called()
1341 #Verify return value with expected value
1342 self
.assertEqual(expected_alarm_def_uuid
, alarm_def_uuid
)
1345 @mock.patch
.object(monPlugin
.requests
, 'put')
1346 def test_reconfigure_alarm_invalid_req_response(self
, m_put
):
1347 """Test reconfigure_alarm: for invalid REST response, return None"""
1349 #Set input parameters to reconfigure_alarm
1350 alarm_details_json
= {
1351 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d',
1352 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
1353 'description': 'CPU_Utilization_Above_Threshold', 'adapterKindKey': 'VMWARE',
1354 'states':[{'impact':{'impactType':'BADGE', 'detail':'risk'}, 'severity':'CRITICAL',
1355 'base-symptom-set':{
1356 'symptomDefinitionIds':['SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'],
1357 'relation': 'SELF','type': 'SYMPTOM_SET', 'aggregation':'ALL',
1358 'symptomSetOperator': 'AND'}}],
1359 'type': 16, 'cancelCycles': 1, 'resourceKindKey': 'VirtualMachine','subType': 19,
1362 new_alarm_config
= {'severity':'WARNING',
1363 'description': 'CPU_Utilization_Above_Threshold_Warning'}
1365 #Set mocked function's return values
1366 m_put
.return_value
.status_code
= 500
1367 m_put
.return_value
.content
= '{"message": "Internal Server error, cause unknown.",\
1368 "moreInformation": [{"name": "errorMessage",\
1369 "value": "Cannot update Alert Definition CPU_Utilization_Above_Thr-\
1370 e14b203c-6bf2-4e2f-a91c-8c19d240eda4 since it does not exist"},\
1371 {"name": "localizedMessage",\
1372 "value": "Cannot update Alert Definition CPU_Utilization_Above_Thr-\
1373 e14b203c-6bf2-4e2f-a91c-8c19d240eda4 since it does not exist;"}],\
1374 "httpStatusCode": 500,"apiErrorCode": 500}'
1376 #Expected alarm_def_uuid to be returned
1377 expected_alarm_def_uuid
= None
1379 #Call reconfigure_alarm method under test
1380 alarm_def_uuid
= self
.mon_plugin
.reconfigure_alarm(alarm_details_json
, new_alarm_config
)
1382 #Verify that mocked method is called with required parameters
1383 m_put
.assert_called()
1385 #Verify return value with expected value
1386 self
.assertEqual(expected_alarm_def_uuid
, alarm_def_uuid
)
1389 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_symptom_definition')
1390 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_alarm_defination')
1391 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_notification_rule')
1392 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
1393 def test_delete_alarm_configuration_successful_alarm_deletion(self
,\
1394 m_get_alarm_defination_details
,\
1395 m_delete_notification_rule
,\
1396 m_delete_alarm_defination
,\
1397 m_delete_symptom_definition
):
1398 """Test delete_alarm_configuration: for successful alarm deletion, return alarm uuid"""
1400 #Set input parameters to delete_alarm_configuration
1401 delete_alarm_req_dict
= {'alarm_uuid':'9a6d8a14-9f25-4d81-bf91-4d773497444d'}
1403 #Set mocked function's return values
1404 alarm_details_json
= {
1405 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1406 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1407 'symptomDefinitionIds':['SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278']}
1409 'alarm_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1410 'alarm_id':'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1411 'symptom_definition_id':'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'}
1413 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
1414 m_delete_notification_rule
.return_value
= '989e7293-d78d-4405-92e30ec4f247'
1415 m_delete_alarm_defination
.return_value
= alarm_details
['alarm_id']
1416 m_delete_symptom_definition
.return_value
= alarm_details
['symptom_definition_id']
1418 #Call reconfigure_alarm method under test
1419 alarm_uuid
= self
.mon_plugin
.delete_alarm_configuration(delete_alarm_req_dict
)
1421 #Verify that mocked method is called with required parameters
1422 m_get_alarm_defination_details
.assert_called_with(delete_alarm_req_dict
['alarm_uuid'])
1423 m_delete_notification_rule
.assert_called_with(alarm_details
['alarm_name'])
1424 m_delete_alarm_defination
.assert_called_with(alarm_details
['alarm_id'])
1425 m_delete_symptom_definition
.assert_called_with(alarm_details
['symptom_definition_id'])
1427 #Verify return value with expected value
1428 self
.assertEqual(alarm_uuid
, delete_alarm_req_dict
['alarm_uuid'])
1431 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_symptom_definition')
1432 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_alarm_defination')
1433 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_notification_rule')
1434 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
1435 def test_delete_alarm_configuration_failed_to_get_alarm_defination(self
,\
1436 m_get_alarm_defination_details
,\
1437 m_delete_notification_rule
,\
1438 m_delete_alarm_defination
,\
1439 m_delete_symptom_definition
):
1440 """Test delete_alarm_configuration: if failed to get alarm definition, return None"""
1442 #Set input parameters to delete_alarm_configuration
1443 delete_alarm_req_dict
= {'alarm_uuid':'9a6d8a14-9f25-4d81-bf91-4d773497444d'}
1445 #Set mocked function's return values
1446 alarm_details_json
= None
1447 alarm_details
= None
1449 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
1451 #Call reconfigure_alarm method under test
1452 alarm_uuid
= self
.mon_plugin
.delete_alarm_configuration(delete_alarm_req_dict
)
1454 #Verify that mocked method is called with required parameters
1455 m_get_alarm_defination_details
.assert_called_with(delete_alarm_req_dict
['alarm_uuid'])
1456 m_delete_notification_rule
.assert_not_called()
1457 m_delete_alarm_defination
.assert_not_called()
1458 m_delete_symptom_definition
.assert_not_called()
1460 #Verify return value with expected value
1461 self
.assertEqual(alarm_uuid
, None)
1464 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_symptom_definition')
1465 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_alarm_defination')
1466 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_notification_rule')
1467 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
1468 def test_delete_alarm_configuration_failed_to_delete_notification_rule(self
,\
1469 m_get_alarm_defination_details
,\
1470 m_delete_notification_rule
,\
1471 m_delete_alarm_defination
,\
1472 m_delete_symptom_definition
):
1473 """Test delete_alarm_configuration: if failed to delete notification rule, return None"""
1475 #Set input parameters to delete_alarm_configuration
1476 delete_alarm_req_dict
= {'alarm_uuid':'9a6d8a14-9f25-4d81-bf91-4d773497444d'}
1478 #Set mocked function's return values
1479 alarm_details_json
= {
1480 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1481 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1482 'symptomDefinitionIds':['SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278']}
1484 'alarm_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1485 'alarm_id':'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1486 'symptom_definition_id':'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'}
1488 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
1489 m_delete_notification_rule
.return_value
= None
1491 #Call reconfigure_alarm method under test
1492 alarm_uuid
= self
.mon_plugin
.delete_alarm_configuration(delete_alarm_req_dict
)
1494 #Verify that mocked method is called with required parameters
1495 m_get_alarm_defination_details
.assert_called_with(delete_alarm_req_dict
['alarm_uuid'])
1496 m_delete_notification_rule
.assert_called_with(alarm_details
['alarm_name'])
1497 m_delete_alarm_defination
.assert_not_called()
1498 m_delete_symptom_definition
.assert_not_called()
1500 #Verify return value with expected value
1501 self
.assertEqual(alarm_uuid
, None)
1504 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_symptom_definition')
1505 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_alarm_defination')
1506 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_notification_rule')
1507 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
1508 def test_delete_alarm_configuration_failed_to_delete_alarm_defination(self
,\
1509 m_get_alarm_defination_details
,\
1510 m_delete_notification_rule
,\
1511 m_delete_alarm_defination
,\
1512 m_delete_symptom_definition
):
1513 """Test delete_alarm_configuration: if failed to delete alarm definition, return None"""
1515 #Set input parameters to delete_alarm_configuration
1516 delete_alarm_req_dict
= {'alarm_uuid':'9a6d8a14-9f25-4d81-bf91-4d773497444d'}
1518 #Set mocked function's return values
1519 alarm_details_json
= {
1520 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1521 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1522 'symptomDefinitionIds':['SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278']}
1524 'alarm_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1525 'alarm_id':'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1526 'symptom_definition_id':'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'}
1528 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
1529 m_delete_notification_rule
.return_value
= '989e7293-d78d-4405-92e30ec4f247'
1530 m_delete_alarm_defination
.return_value
= None
1532 #Call reconfigure_alarm method under test
1533 alarm_uuid
= self
.mon_plugin
.delete_alarm_configuration(delete_alarm_req_dict
)
1535 #Verify that mocked method is called with required parameters
1536 m_get_alarm_defination_details
.assert_called_with(delete_alarm_req_dict
['alarm_uuid'])
1537 m_delete_notification_rule
.assert_called_with(alarm_details
['alarm_name'])
1538 m_delete_alarm_defination
.assert_called_with(alarm_details
['alarm_id'])
1539 m_delete_symptom_definition
.assert_not_called()
1541 #Verify return value with expected value
1542 self
.assertEqual(alarm_uuid
, None)
1545 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_symptom_definition')
1546 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_alarm_defination')
1547 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_notification_rule')
1548 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
1549 def test_delete_alarm_configuration_failed_to_delete_symptom_definition(self
,\
1550 m_get_alarm_defination_details
,\
1551 m_delete_notification_rule
,\
1552 m_delete_alarm_defination
,\
1553 m_delete_symptom_definition
):
1554 """Test delete_alarm_configuration: if failed to delete symptom definition, return None"""
1556 #Set input parameters to delete_alarm_configuration
1557 delete_alarm_req_dict
= {'alarm_uuid':'9a6d8a14-9f25-4d81-bf91-4d773497444d'}
1559 #Set mocked function's return values
1560 alarm_details_json
= {
1561 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1562 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1563 'symptomDefinitionIds':['SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278']}
1565 'alarm_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1566 'alarm_id':'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1567 'symptom_definition_id':'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'}
1569 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
1570 m_delete_notification_rule
.return_value
= '989e7293-d78d-4405-92e30ec4f247'
1571 m_delete_alarm_defination
.return_value
= alarm_details
['alarm_id']
1572 m_delete_symptom_definition
.return_value
= None
1574 #Call reconfigure_alarm method under test
1575 alarm_uuid
= self
.mon_plugin
.delete_alarm_configuration(delete_alarm_req_dict
)
1577 #Verify that mocked method is called with required parameters
1578 m_get_alarm_defination_details
.assert_called_with(delete_alarm_req_dict
['alarm_uuid'])
1579 m_delete_notification_rule
.assert_called_with(alarm_details
['alarm_name'])
1580 m_delete_alarm_defination
.assert_called_with(alarm_details
['alarm_id'])
1581 m_delete_symptom_definition
.assert_called_with(alarm_details
['symptom_definition_id'])
1583 #Verify return value with expected value
1584 self
.assertEqual(alarm_uuid
, None)
1587 @mock.patch
.object(monPlugin
.requests
, 'delete')
1588 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_notification_rule_id_by_alarm_name')
1589 def test_delete_notification_rule_successful_deletion_req_response(self
,\
1590 m_get_notification_rule_id_by_alarm_name
,\
1592 """Test delete_notification_rule: Valid notification rule is deleted & returns rule_id"""
1594 #Set input parameters to delete_notification_rule
1595 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4'
1597 #Set mocked function's return values
1598 m_get_notification_rule_id_by_alarm_name
.return_value
= '8db86441-71d8-4830-9e1a-a90be3776d12'
1599 m_delete
.return_value
.status_code
= 204
1601 #Call delete_notification_rule method under test
1602 rule_id
= self
.mon_plugin
.delete_notification_rule(alarm_name
)
1604 #Verify that mocked method is called with required parameters
1605 m_get_notification_rule_id_by_alarm_name
.assert_called_with(alarm_name
)
1606 m_delete
.assert_called()
1608 #Verify return value with expected value
1609 self
.assertEqual(rule_id
, '8db86441-71d8-4830-9e1a-a90be3776d12')
1612 @mock.patch
.object(monPlugin
.requests
, 'delete')
1613 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_notification_rule_id_by_alarm_name')
1614 def test_delete_notification_rule_failed_to_get_notification_rule_id(self
,\
1615 m_get_notification_rule_id_by_alarm_name
,\
1617 """Test delete_notification_rule: if notification rule is not found, returns None"""
1619 #Set input parameters to delete_notification_rule
1620 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4'
1622 #Set mocked function's return values
1623 m_get_notification_rule_id_by_alarm_name
.return_value
= None
1625 #Call delete_notification_rule method under test
1626 rule_id
= self
.mon_plugin
.delete_notification_rule(alarm_name
)
1628 #Verify that mocked method is called with required parameters
1629 m_get_notification_rule_id_by_alarm_name
.assert_called_with(alarm_name
)
1630 m_delete
.assert_not_called()
1632 # verify return value with expected value
1633 self
.assertEqual(rule_id
, None)
1636 @mock.patch
.object(monPlugin
.requests
, 'delete')
1637 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_notification_rule_id_by_alarm_name')
1638 def test_delete_notification_rule_invalid_deletion_req_response(self
,\
1639 m_get_notification_rule_id_by_alarm_name
,\
1641 """Test delete_notification_rule: If an invalid response is received, returns None"""
1643 #Set input parameters to delete_notification_rule
1644 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4'
1646 #Set mocked function's return values
1647 m_get_notification_rule_id_by_alarm_name
.return_value
= '8db86441-71d8-4830-9e1a-a90be3776d12'
1648 m_delete
.return_value
.status_code
= 404
1650 #Call delete_notification_rule method under test
1651 rule_id
= self
.mon_plugin
.delete_notification_rule(alarm_name
)
1653 #Verify that mocked method is called with required parameters
1654 m_get_notification_rule_id_by_alarm_name
.assert_called_with(alarm_name
)
1655 m_delete
.assert_called()
1657 #Verify return value with expected value
1658 self
.assertEqual(rule_id
, None)
1661 @mock.patch
.object(monPlugin
.requests
, 'get')
1662 def test_get_notification_rule_id_by_alarm_name_valid_req_response(self
,m_get
):
1663 """Test get_notification_rule_id_by_alarm_name: A valid request response received,
1664 returns notification_id
1667 #Set input parameters to get_notification_rule_id_by_alarm_name
1668 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
1670 #Set mocked function's return values
1671 m_get
.return_value
.status_code
= 200
1672 m_get
.return_value
.content
= '{\
1673 "pageInfo": {"totalCount": 0,"page": 0,"pageSize": 1000},\
1675 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1676 "rel": "SELF","name": "current"},\
1677 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1678 "rel": "RELATED","name": "first"},\
1679 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1680 "rel": "RELATED","name": "last"}],\
1681 "notification-rule": [{\
1682 "id": "2b86fa23-0c15-445c-a2b1-7bd725c46f59",\
1683 "name": "notify_CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1684 "pluginId": "03053f51-f829-438d-993d-cc33a435d76a",\
1685 "links": [{"href": "/suite-api/api/notifications/rules/2b86fa23-0c15-445c-a2b1-7bd725c46f59",\
1686 "rel": "SELF","name": "linkToSelf"}]}]}'
1688 #Call get_notification_rule_id_by_alarm_name method under test
1689 notification_id
= self
.mon_plugin
.get_notification_rule_id_by_alarm_name(alarm_name
)
1691 #Verify that mocked method is called with required parameters
1692 m_get
.assert_called()
1694 #Verify return value with expected value
1695 self
.assertEqual(notification_id
, '2b86fa23-0c15-445c-a2b1-7bd725c46f59')
1698 @mock.patch
.object(monPlugin
.requests
, 'get')
1699 def test_get_notification_rule_id_by_alarm_name_invalid_req_response(self
,m_get
):
1700 """Test get_notification_rule_id_by_alarm_name: If an invalid response received,\
1704 #Set input parameters to delete_alarm_configuration
1705 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
1707 #Set mocked function's return values
1708 m_get
.return_value
.status_code
= 404
1710 #Call get_notification_rule_id_by_alarm_name method under test
1711 notification_id
= self
.mon_plugin
.get_notification_rule_id_by_alarm_name(alarm_name
)
1713 #Verify that mocked method is called with required parameters
1714 m_get
.assert_called()
1716 #Verify return value with expected value
1717 self
.assertEqual(notification_id
, None)
1720 @mock.patch
.object(monPlugin
.requests
, 'get')
1721 def test_get_notification_rule_id_by_alarm_name_rule_not_found(self
,m_get
):
1722 """Test get_notification_rule_id_by_alarm_name: If a notification rule is not found,
1726 #Set input parameters to delete_alarm_configuration
1727 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda'
1729 #Set mocked function's return values
1730 m_get
.return_value
.status_code
= 200
1731 m_get
.return_value
.content
= '{\
1732 "pageInfo": {"totalCount": 0,"page": 0,"pageSize": 1000},\
1734 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1735 "rel": "SELF","name": "current"},\
1736 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1737 "rel": "RELATED","name": "first"},\
1738 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1739 "rel": "RELATED","name": "last"}],\
1740 "notification-rule": [{\
1741 "id": "2b86fa23-0c15-445c-a2b1-7bd725c46f59",\
1742 "name": "notify_CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1743 "pluginId": "03053f51-f829-438d-993d-cc33a435d76a",\
1744 "links": [{"href": "/suite-api/api/notifications/rules/2b86fa23-0c15-445c-a2b1-7bd725c46f59",\
1745 "rel": "SELF","name": "linkToSelf"}]}]}'
1747 #Call get_notification_rule_id_by_alarm_name method under test
1748 notification_id
= self
.mon_plugin
.get_notification_rule_id_by_alarm_name(alarm_name
)
1750 #Verify that mocked method is called with required parameters
1751 m_get
.assert_called()
1753 #Verify return value with expected value
1754 self
.assertEqual(notification_id
, None)
1757 @mock.patch
.object(monPlugin
.requests
, 'delete')
1758 def test_delete_alarm_defination_valid_req_response(self
,m_delete
):
1759 """Test delete_alarm_defination: A valid request response received,
1763 #Set input parameters to delete_alarm_definition
1764 alarm_definition_id
= 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d'
1766 #Set mocked function's return values
1767 m_delete
.return_value
.status_code
= 204
1769 #Call delete_alarm_defination method under test
1770 actual_alarm_id
= self
.mon_plugin
.delete_alarm_defination(alarm_definition_id
)
1772 #Verify that mocked method is called with required parameters
1773 m_delete
.assert_called()
1775 #Verify return value with expected value
1776 self
.assertEqual(actual_alarm_id
, alarm_definition_id
)
1779 @mock.patch
.object(monPlugin
.requests
, 'delete')
1780 def test_delete_alarm_defination_invalid_req_response(self
,m_delete
):
1781 """Test delete_alarm_defination: If an invalid request response received,
1785 #Set input parameters to delete_alarm_definition
1786 alarm_definition_id
= 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d'
1788 #Set mocked function's return values
1789 m_delete
.return_value
.status_code
= 404
1791 #Call delete_alarm_defination method under test
1792 actual_alarm_id
= self
.mon_plugin
.delete_alarm_defination(alarm_definition_id
)
1794 #Verify that mocked method is called with required parameters
1795 m_delete
.assert_called()
1797 #Verify return value with expected value
1798 self
.assertEqual(actual_alarm_id
, None)
1801 @mock.patch
.object(monPlugin
.requests
, 'delete')
1802 def test_delete_symptom_definition_valid_req_response(self
,m_delete
):
1803 """Test delete_symptom_definition: A valid request response received,
1807 #Set input parameters to delete_symptom_definition
1808 symptom_definition_id
= 'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'
1810 #Set mocked function's return values
1811 m_delete
.return_value
.status_code
= 204
1813 #Call delete_symptom_definition method under test
1814 actual_symptom_id
= self
.mon_plugin
.delete_symptom_definition(symptom_definition_id
)
1816 #Verify that mocked method is called with required parameters
1817 m_delete
.assert_called()
1819 #Verify return value with expected value
1820 self
.assertEqual(actual_symptom_id
, symptom_definition_id
)
1823 @mock.patch
.object(monPlugin
.requests
, 'delete')
1824 def test_delete_symptom_definition_invalid_req_response(self
,m_delete
):
1825 """Test delete_symptom_definition: If an invalid request response received,
1829 #Set input parameters to delete_symptom_definition
1830 symptom_definition_id
= 'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'
1832 #Set mocked function's return values
1833 m_delete
.return_value
.status_code
= 404
1835 #Call delete_symptom_definition method under test
1836 actual_symptom_id
= self
.mon_plugin
.delete_symptom_definition(symptom_definition_id
)
1838 #Verify that mocked method is called with required parameters
1839 m_delete
.assert_called()
1841 #Verify return value with expected value
1842 self
.assertEqual(actual_symptom_id
, None)
1845 @mock.patch
.object(monPlugin
.requests
, 'post')
1846 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
1847 def test_configure_rest_plugin_valid_plugin_id(self
, m_check_if_plugin_configured
, m_post
):
1848 """Test configure rest plugin method-valid plugin id"""
1850 # mock return values
1851 expected_return
= m_check_if_plugin_configured
.return_value
= "mock_pluginid"
1853 # call configure rest plugin method under test
1854 actual_return
= self
.mon_plugin
.configure_rest_plugin()
1856 # verify that mocked method is called
1857 m_check_if_plugin_configured
.assert_called()
1858 m_post
.assert_not_called()
1860 # verify return value with expected value
1861 self
.assertEqual(expected_return
, actual_return
)
1864 @mock.patch
.object(monPlugin
.MonPlugin
,'enable_rest_plugin')
1865 @mock.patch
.object(monPlugin
.requests
, 'post')
1866 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
1867 def est_configure_rest_plugin_invalid_plugin_id(self
, m_check_if_plugin_configured
, m_post
, m_enable_rest_plugin
):
1868 """Test configure rest plugin method-invalid plugin id"""
1870 # mock return values
1871 m_check_if_plugin_configured
.return_value
= None # not configured
1872 m_post
.return_value
.status_code
= 201 #success
1873 m_post
.return_value
.content
= '{"pluginTypeId":"RestPlugin","pluginId":"1ef15663-9739-49fe-8c41-022bcc9f690c",\
1874 "name":"MON_module_REST_Plugin","version":1518693747871,"enabled":false,\
1875 "configValues":[{"name":"Url","value":"https://MON.lxd:8080/notify/"},\
1876 {"name":"Content-type","value":"application/json"},{"name":"Certificate",\
1877 "value":"AA:E7:3E:A5:34:E0:25:FB:28:84:3B:74:B2:18:74:C0:C3:E8:26:50"},\
1878 {"name":"ConnectionCount","value":"20"}]}'
1880 m_enable_rest_plugin
.return_value
= True #success
1881 expected_return
= '1ef15663-9739-49fe-8c41-022bcc9f690c'
1883 # call configure rest plugin method under test
1884 actual_return
= self
.mon_plugin
.configure_rest_plugin()
1886 # verify that mocked method is called
1887 m_check_if_plugin_configured
.assert_called()
1888 m_post
.assert_called()
1889 m_enable_rest_plugin
.assert_called_with('1ef15663-9739-49fe-8c41-022bcc9f690c','MON_module_REST_Plugin')
1891 # verify return value with expected value
1892 self
.assertEqual(expected_return
, actual_return
)
1895 @mock.patch
.object(monPlugin
.MonPlugin
,'enable_rest_plugin')
1896 @mock.patch
.object(monPlugin
.requests
, 'post')
1897 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
1898 def est_configure_rest_plugin_failed_to_enable_plugin(self
, m_check_if_plugin_configured
, m_post
, m_enable_rest_plugin
):
1899 """Test configure rest plugin method-failed to enable plugin case"""
1901 # mock return values
1902 m_check_if_plugin_configured
.return_value
= None # not configured
1903 m_post
.return_value
.status_code
= 201 #success
1904 m_post
.return_value
.content
= '{"pluginTypeId":"RestPlugin","pluginId":"1ef15663-9739-49fe-8c41-022bcc9f690c",\
1905 "name":"MON_module_REST_Plugin","version":1518693747871,"enabled":false,\
1906 "configValues":[{"name":"Url","value":"https://MON.lxd:8080/notify/"},\
1907 {"name":"Content-type","value":"application/json"},{"name":"Certificate",\
1908 "value":"AA:E7:3E:A5:34:E0:25:FB:28:84:3B:74:B2:18:74:C0:C3:E8:26:50"},\
1909 {"name":"ConnectionCount","value":"20"}]}'
1911 m_enable_rest_plugin
.return_value
= False #return failure
1912 expected_return
= None
1914 # call configure rest plugin method under test
1915 actual_return
= self
.mon_plugin
.configure_rest_plugin()
1917 # verify that mocked method is called
1918 m_check_if_plugin_configured
.assert_called()
1919 m_post
.assert_called()
1920 m_enable_rest_plugin
.assert_called_with('1ef15663-9739-49fe-8c41-022bcc9f690c','MON_module_REST_Plugin')
1922 # verify return value with expected value
1923 self
.assertEqual(expected_return
, actual_return
)
1926 @mock.patch
.object(monPlugin
.requests
, 'get')
1927 def test_check_if_plugin_configured_valid_req_response(self
, m_get
):
1928 """Test check if plugin configured method-valid request response"""
1930 plugin_name
= 'MON_module_REST_Plugin'
1931 # mock return values
1932 m_get
.return_value
.status_code
= 200
1933 expected_return
= '1ef15663-9739-49fe-8c41-022bcc9f690c'
1934 m_get
.return_value
.content
= '{"notificationPluginInstances":\
1935 [{"pluginTypeId":"RestPlugin",\
1936 "pluginId":"1ef15663-9739-49fe-8c41-022bcc9f690c",\
1937 "name":"MON_module_REST_Plugin","version":1518694966987,\
1938 "enabled":true,"configValues":[{"name":"Url",\
1939 "value":"https://MON.lxd:8080/notify/"},\
1940 {"name":"Content-type","value":"application/json"},\
1941 {"name":"Certificate",\
1942 "value":"AA:E7:3E:A5:34:E0:25:FB:28:84:3B:74:B2:18:74:C0"},\
1943 {"name":"ConnectionCount","value":"20"}]}]}'
1945 # call check if plugin configured method under test
1946 actual_return
= self
.mon_plugin
.check_if_plugin_configured(plugin_name
)
1948 # verify that mocked method is called
1949 m_get
.assert_called()
1951 # verify return value with expected value
1952 self
.assertEqual(expected_return
, actual_return
)
1955 @mock.patch
.object(monPlugin
.requests
, 'get')
1956 def test_check_if_plugin_configured_invalid_req_response(self
, m_get
):
1957 """Test check if plugin configured method-invalid request response"""
1959 plugin_name
= 'MON_module_REST_Plugin'
1960 # mock return values
1961 m_get
.return_value
.status_code
= 201
1962 expected_return
= None
1963 m_get
.return_value
.content
= '{"notificationPluginInstances":\
1964 [{"pluginTypeId":"RestPlugin",\
1965 "pluginId":"1ef15663-9739-49fe-8c41-022bcc9f690c",\
1966 "name":"MON_module_REST_Plugin","version":1518694966987,\
1967 "enabled":true,"configValues":[{"name":"Url",\
1968 "value":"https://MON.lxd:8080/notify/"},\
1969 {"name":"Content-type","value":"application/json"},\
1970 {"name":"Certificate",\
1971 "value":"AA:E7:3E:A5:34:E0:25:FB:28:84:3B:74:B2:18:74:C0"},\
1972 {"name":"ConnectionCount","value":"20"}]}]}'
1974 # call check if plugin configured method under test
1975 actual_return
= self
.mon_plugin
.check_if_plugin_configured(plugin_name
)
1977 # verify that mocked method is called
1978 m_get
.assert_called()
1980 # verify return value with expected value
1981 self
.assertEqual(expected_return
, actual_return
)
1984 @mock.patch
.object(monPlugin
.requests
, 'put')
1985 def test_enable_rest_plugin_valid_req_response(self
, m_put
):
1986 """Test enable rest plugin method-valid request response"""
1988 plugin_name
= 'MON_module_REST_Plugin'
1989 plugin_id
= '1ef15663-9739-49fe-8c41-022bcc9f690c'
1990 # mock return values
1991 m_put
.return_value
.status_code
= 204
1992 expected_return
= True
1993 m_put
.return_value
.content
= ''
1995 # call enable rest plugin configured method under test
1996 actual_return
= self
.mon_plugin
.enable_rest_plugin(plugin_id
, plugin_name
)
1998 # verify that mocked method is called
1999 m_put
.assert_called()
2001 # verify return value with expected value
2002 self
.assertEqual(expected_return
, actual_return
)
2005 @mock.patch
.object(monPlugin
.requests
, 'put')
2006 def test_enable_rest_plugin_invalid_req_response(self
, m_put
):
2007 """Test enable rest plugin method-invalid request response"""
2009 plugin_name
= 'MON_module_REST_Plugin'
2010 plugin_id
= '08018c0f-8879-4ca1-9b92-00e22d2ff81b' #invalid plugin id
2011 # mock return values
2012 m_put
.return_value
.status_code
= 404 # api Error code
2013 expected_return
= False
2014 m_put
.return_value
.content
= '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ops:\
2015 error xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \
2016 xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ops=\
2017 "http://webservice.vmware.com/vRealizeOpsMgr/1.0/" \
2018 httpStatusCode="404" apiErrorCode="404"><ops:message>\
2019 No such Notification Plugin - 08018c0f-8879-4ca1-9b92-\
2020 00e22d2ff81b.</ops:message></ops:error>'
2022 # call enable rest plugin configured method under test
2023 actual_return
= self
.mon_plugin
.enable_rest_plugin(plugin_id
, plugin_name
)
2025 # verify that mocked method is called
2026 m_put
.assert_called()
2028 # verify return value with expected value
2029 self
.assertEqual(expected_return
, actual_return
)
2032 @mock.patch
.object(monPlugin
.requests
, 'post')
2033 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
2034 def test_create_alarm_notification_rule_valid_req(self
, m_check_if_plugin_configured
, m_post
):
2035 """Test create alarm notification rule method valid request response"""
2037 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2038 alarm_id
= 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14'
2039 res_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2040 expected_return
= "8db86441-71d8-4830-9e1a-a90be3776d12"
2042 # mock return values
2043 m_check_if_plugin_configured
.return_value
= '03053f51-f829-438d-993d-cc33a435d76a'
2044 m_post
.return_value
.status_code
= 201
2045 m_post
.return_value
.content
= '{"id":"8db86441-71d8-4830-9e1a-a90be3776d12",\
2046 "name":"notify_CPU_Utilization_Above_Thr-e14b203c",\
2047 "pluginId":"03053f51-f829-438d-993d-cc33a435d76a",\
2048 "alertControlStates":[],"alertStatuses":[],\
2049 "resourceFilter":{"matchResourceIdOnly":true,\
2050 "childrenResourceKindFilters":[],\
2051 "resourceId":"ac87622f-b761-40a0-b151-00872a2a456e"},\
2052 "alertTypeFilters":[],"alertDefinitionIdFilters":{"values":[\
2053 "AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14"]}}'
2055 # call enable rest plugin configured method under test
2056 actual_return
= self
.mon_plugin
.create_alarm_notification_rule(alarm_name
, alarm_id
, res_id
)
2058 # verify that mocked method is called
2059 m_check_if_plugin_configured
.assert_called_with('MON_module_REST_Plugin')
2060 m_post
.assert_called()
2062 # verify return value with expected value
2063 self
.assertEqual(expected_return
, actual_return
)
2066 @mock.patch
.object(monPlugin
.requests
, 'post')
2067 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
2068 def test_create_alarm_notification_rule_invalid_req(self
, m_check_if_plugin_configured
, m_post
):
2069 """Test create alarm notification rule method invalid request response"""
2071 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2072 alarm_id
= 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14'
2073 res_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2074 expected_return
= None # invalid req should retrun none
2076 # mock return values
2077 m_check_if_plugin_configured
.return_value
= '03053f51-f829-438d-993d-cc33a435d76a'
2078 m_post
.return_value
.status_code
= 500
2079 m_post
.return_value
.content
= '{"message":"Internal Server error, cause unknown.",\
2080 "moreInformation":[{"name":"errorMessage","value":\
2081 "there is already a rule with the same rule name"},\
2082 {"name":"localizedMessage","value":"there is already \
2083 a rule with the same rule name;"}],"httpStatusCode":500,\
2084 "apiErrorCode":500}'
2086 # call enable rest plugin configured method under test
2087 actual_return
= self
.mon_plugin
.create_alarm_notification_rule(alarm_name
, alarm_id
, res_id
)
2089 # verify that mocked method is called
2090 m_check_if_plugin_configured
.assert_called_with('MON_module_REST_Plugin')
2091 m_post
.assert_called()
2093 # verify return value with expected value
2094 self
.assertEqual(expected_return
, actual_return
)
2097 @mock.patch
.object(monPlugin
.requests
, 'post')
2098 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
2099 def test_create_alarm_notification_rule_failed_to_get_plugin_id(self
, \
2100 m_check_if_plugin_configured
, m_post
):
2101 """Test create alarm notification rule method invalid plugin id"""
2103 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2104 alarm_id
= 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14'
2105 res_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2106 expected_return
= None # invalid req should retrun none
2108 # mock return values
2109 m_check_if_plugin_configured
.return_value
= None
2111 # call enable rest plugin configured method under test
2112 actual_return
= self
.mon_plugin
.create_alarm_notification_rule(alarm_name
, alarm_id
, res_id
)
2114 # verify that mocked method is called
2115 m_check_if_plugin_configured
.assert_called_with('MON_module_REST_Plugin')
2116 m_post
.assert_not_called()
2118 # verify return value with expected value
2119 self
.assertEqual(expected_return
, actual_return
)
2122 @mock.patch
.object(monPlugin
.requests
, 'get')
2123 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2124 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2125 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2126 def test_get_metrics_data_valid_rest_req_response(self
, m_get_default_Params
, \
2127 m_get_vm_moref_id
, \
2128 m_get_vm_resource_id
, \
2130 """Test get metrics data of resource method valid request response"""
2132 metrics
= {'collection_period': 1, 'metric_name': 'CPU_UTILIZATION', 'metric_uuid': None, \
2133 'schema_version': 1.0, 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',\
2134 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef', \
2135 'schema_type': 'read_metric_data_request', 'vim_type': 'VMware', \
2136 'collection_unit': 'HR'}
2139 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2140 vm_moref_id
= m_get_vm_moref_id
.return_value
= 'vm-6626'
2141 m_get_vm_resource_id
.return_value
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2142 m_get
.return_value
.status_code
= 200
2143 m_get
.return_value
.content
= '{"values":[{"resourceId":"ac87622f-b761-40a0-b151-\
2144 00872a2a456e","stat-list":{"stat":[{"timestamps":\
2145 [1519716874297,1519717174294,1519717474295,1519717774298,\
2146 1519718074300,1519718374299,1519718674314,1519718974325,\
2147 1519719274304,1519719574298,1519719874298,1519720174301],\
2148 "statKey":{"key":"cpu|usage_average"},"intervalUnit":\
2149 {"quantifier":1},"data":[0.1120000034570694,\
2150 0.11866666376590729,0.11599999666213989,0.11400000005960464,\
2151 0.12066666781902313,0.11533333361148834,0.11800000071525574,\
2152 0.11533333361148834,0.12200000137090683,0.11400000005960464,\
2153 0.1459999978542328,0.12133333086967468]}]}}]}'
2155 # call get matrics data method under test
2156 actual_return
= self
.mon_plugin
.get_metrics_data(metrics
)
2158 # verify that mocked method is called
2159 m_get_default_Params
.assert_called_with(metrics
['metric_name'])
2160 m_get_vm_moref_id
.assert_called_with(metrics
['resource_uuid'])
2161 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
2162 m_get
.assert_called()
2164 # verify return value with expected value
2165 #self.assertEqual(expected_return, actual_return)
2168 @mock.patch
.object(monPlugin
.requests
, 'get')
2169 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2170 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2171 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2172 def test_get_metrics_data_invalid_rest_req_response(self
, m_get_default_Params
, \
2173 m_get_vm_moref_id
, \
2174 m_get_vm_resource_id
, \
2176 """Test get metrics data of resource method invalid request response"""
2178 metrics
= {'collection_period': 1, 'metric_name': 'CPU_UTILIZATION', 'metric_uuid': None, \
2179 'schema_version': 1.0, 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',\
2180 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef', \
2181 'schema_type': 'read_metric_data_request', 'vim_type': 'VMware', \
2182 'collection_unit': 'HR'}
2185 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2186 vm_moref_id
= m_get_vm_moref_id
.return_value
= 'vm-6626'
2187 m_get_vm_resource_id
.return_value
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2188 m_get
.return_value
.status_code
= 400
2189 expected_return
= {'metric_name': 'CPU_UTILIZATION', 'metric_uuid': '0',
2190 'schema_version': '1.0',
2191 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2192 'correlation_id': u
'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2193 'metrics_data': {'time_series': [], 'metrics_series': []},
2194 'schema_type': 'read_metric_data_response', 'tenant_uuid': None,
2197 # call get matrics data method under test
2198 actual_return
= self
.mon_plugin
.get_metrics_data(metrics
)
2200 # verify that mocked method is called
2201 m_get_default_Params
.assert_called_with(metrics
['metric_name'])
2202 m_get_vm_moref_id
.assert_called_with(metrics
['resource_uuid'])
2203 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
2204 m_get
.assert_called()
2206 m_get
.return_value
.content
= '{"message":"Invalid request... #1 violations found.",\
2207 "validationFailures":[{"failureMessage":"Invalid Parameter",\
2208 "violationPath":"end"}],"httpStatusCode":400,\
2209 "apiErrorCode":400}'
2211 # verify return value with expected value
2212 self
.assertEqual(expected_return
, actual_return
)
2215 @mock.patch
.object(monPlugin
.requests
, 'get')
2216 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2217 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2218 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2219 def test_get_metrics_data_metric_not_supported(self
, m_get_default_Params
, \
2220 m_get_vm_moref_id
, \
2221 m_get_vm_resource_id
, \
2223 """Test get metrics data of resource method invalid metric name"""
2225 metrics
= {'collection_period': 1, 'metric_name': 'INVALID_METRIC', 'metric_uuid': None, \
2226 'schema_version': 1.0, 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',\
2227 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef', \
2228 'schema_type': 'read_metric_data_request', 'vim_type': 'VMware', \
2229 'collection_unit': 'HR'}
2232 m_get_default_Params
.return_value
= {} # returns empty dict
2234 expected_return
= {'metric_name': 'INVALID_METRIC', 'metric_uuid': '0',
2235 'schema_version': '1.0',
2236 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2237 'correlation_id': u
'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2238 'metrics_data': {'time_series': [], 'metrics_series': []},
2239 'schema_type': 'read_metric_data_response', 'tenant_uuid': None,
2242 # call get matrics data method under test
2243 actual_return
= self
.mon_plugin
.get_metrics_data(metrics
)
2245 # verify that mocked method is called/not called
2246 m_get_default_Params
.assert_called_with(metrics
['metric_name'])
2247 m_get_vm_moref_id
.assert_not_called()
2248 m_get_vm_resource_id
.assert_not_called()
2249 m_get
.assert_not_called()
2251 # verify return value with expected value
2252 self
.assertEqual(expected_return
, actual_return
)
2255 @mock.patch
.object(monPlugin
.requests
, 'get')
2256 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2257 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2258 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2259 def test_get_metrics_data_failed_to_get_vm_moref_id(self
, m_get_default_Params
, \
2260 m_get_vm_moref_id
, \
2261 m_get_vm_resource_id
, \
2263 """Test get metrics data method negative scenario- invalid resource id"""
2265 metrics
= {'collection_period': 1, 'metric_name': 'CPU_UTILIZATION', 'metric_uuid': None, \
2266 'schema_version': 1.0, 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',\
2267 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef', \
2268 'schema_type': 'read_metric_data_request', 'vim_type': 'VMware', \
2269 'collection_unit': 'HR'}
2272 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2273 m_get_vm_moref_id
.return_value
= None
2274 expected_return
= {'metric_name': 'CPU_UTILIZATION', 'metric_uuid': '0',
2275 'schema_version': '1.0',
2276 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2277 'correlation_id': u
'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2278 'metrics_data': {'time_series': [], 'metrics_series': []},
2279 'schema_type': 'read_metric_data_response', 'tenant_uuid': None,
2282 # call get matrics data method under test
2283 actual_return
= self
.mon_plugin
.get_metrics_data(metrics
)
2285 # verify that mocked method is called/not called
2286 m_get_default_Params
.assert_called_with(metrics
['metric_name'])
2287 m_get_vm_moref_id
.assert_called_with(metrics
['resource_uuid'])
2288 m_get_vm_resource_id
.assert_not_called()
2289 m_get
.assert_not_called()
2291 # verify return value with expected value
2292 self
.assertEqual(expected_return
, actual_return
)
2295 @mock.patch
.object(monPlugin
.requests
, 'get')
2296 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2297 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2298 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2299 def test_get_metrics_data_failed_to_get_vm_resource_id(self
, m_get_default_Params
, \
2300 m_get_vm_moref_id
, \
2301 m_get_vm_resource_id
, \
2303 """Test get metrics data method negative scenario- invalid moref id"""
2305 metrics
= {'collection_period': 1, 'metric_name': 'CPU_UTILIZATION', 'metric_uuid': None, \
2306 'schema_version': 1.0, 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',\
2307 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef', \
2308 'schema_type': 'read_metric_data_request', 'vim_type': 'VMware', \
2309 'collection_unit': 'HR'}
2312 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2313 m_get_vm_moref_id
.return_value
= 'Invalid-vm-6626'
2314 m_get_vm_resource_id
.return_value
= None
2315 expected_return
= {'metric_name': 'CPU_UTILIZATION', 'metric_uuid': '0',
2316 'schema_version': '1.0',
2317 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2318 'correlation_id': u
'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2319 'metrics_data': {'time_series': [], 'metrics_series': []},
2320 'schema_type': 'read_metric_data_response', 'tenant_uuid': None,
2323 # call get matrics data method under test
2324 actual_return
= self
.mon_plugin
.get_metrics_data(metrics
)
2326 # verify that mocked method is called/not called
2327 m_get_default_Params
.assert_called_with(metrics
['metric_name'])
2328 m_get_vm_moref_id
.assert_called_with(metrics
['resource_uuid'])
2329 m_get_vm_resource_id
.assert_called()
2330 m_get
.assert_not_called()
2332 # verify return value with expected value
2333 self
.assertEqual(expected_return
, actual_return
)
2336 @mock.patch
.object(monPlugin
.MonPlugin
, 'reconfigure_alarm')
2337 @mock.patch
.object(monPlugin
.MonPlugin
, 'update_symptom_defination')
2338 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
2339 def test_update_alarm_configuration_successful_updation(self
, m_get_alarm_defination_details
, \
2340 m_update_symptom_defination
, \
2341 m_reconfigure_alarm
):
2342 """Test update alarm configuration method"""
2344 alarm_config
= {'alarm_uuid': 'f1163767-6eac-438f-8e60-a7a867257e14',
2345 'correlation_id': 14203,
2346 'description': 'CPU_Utilization_Above_Threshold_L', 'operation': 'GT'}
2349 alarm_details_json
= {'states': [{'impact': {'impactType': 'BADGE', 'detail': 'risk'},
2350 'severity': 'CRITICAL', 'base-symptom-set': {'symptomDefinitionIds':
2351 ['SymptomDefinition-47c88675-bea8-436a-bb41-8d2231428f44'],
2352 'relation': 'SELF', 'type': 'SYMPTOM_SET', 'aggregation': 'ALL'}}],
2353 'description': 'CPU_Utilization_Above_Threshold', 'type': 16,
2354 'id': 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14',
2355 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d2'}
2356 alarm_details
= {'symptom_definition_id': 'SymptomDefinition-47c88675-bea8-436a-bb41-\
2357 8d2231428f44', 'alarm_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-\
2358 a91c-8c19d2', 'alarm_id': 'AlertDefinition-f1163767-6eac-438f-8e60-\
2359 a7a867257e14', 'resource_kind': u
'VirtualMachine', 'type': 16}
2360 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
2361 m_update_symptom_defination
.return_value
= 'SymptomDefinition-47c88675-bea8-436a-bb41-\
2363 expected_return
= m_reconfigure_alarm
.return_value
= 'f1163767-6eac-438f-8e60-a7a867257e14'
2365 # call update alarm configuration method under test
2366 actual_return
= self
.mon_plugin
.update_alarm_configuration(alarm_config
)
2368 # verify that mocked method is called
2369 m_get_alarm_defination_details
.assert_called_with(alarm_config
['alarm_uuid'])
2370 m_update_symptom_defination
.assert_called_with(alarm_details
['symptom_definition_id'],\
2372 m_reconfigure_alarm
.assert_called_with(alarm_details_json
, alarm_config
)
2374 # verify return value with expected value
2375 self
.assertEqual(expected_return
, actual_return
)
2378 @mock.patch
.object(monPlugin
.MonPlugin
, 'reconfigure_alarm')
2379 @mock.patch
.object(monPlugin
.MonPlugin
, 'update_symptom_defination')
2380 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
2381 def test_update_alarm_configuration_failed_to_reconfigure_alarm(self
, \
2382 m_get_alarm_defination_details
, \
2383 m_update_symptom_defination
, \
2384 m_reconfigure_alarm
):
2385 """Test update alarm configuration method- failed to reconfigure alarm"""
2387 alarm_config
= {'alarm_uuid': 'f1163767-6eac-438f-8e60-a7a867257e14',
2388 'correlation_id': 14203,
2389 'description': 'CPU_Utilization_Above_Threshold_L', 'operation': 'GT'}
2392 alarm_details_json
= {'states': [{'impact': {'impactType': 'BADGE', 'detail': 'risk'},
2393 'severity': 'CRITICAL', 'base-symptom-set': {'symptomDefinitionIds':
2394 ['SymptomDefinition-47c88675-bea8-436a-bb41-8d2231428f44'],
2395 'relation': 'SELF', 'type': 'SYMPTOM_SET', 'aggregation': 'ALL'}}],
2396 'description': 'CPU_Utilization_Above_Threshold', 'type': 16,
2397 'id': 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14',
2398 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d2'}
2399 alarm_details
= {'symptom_definition_id': 'SymptomDefinition-47c88675-bea8-436a-bb41-\
2400 8d2231428f44', 'alarm_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-\
2401 a91c-8c19d2', 'alarm_id': 'AlertDefinition-f1163767-6eac-438f-8e60-\
2402 a7a867257e14', 'resource_kind': u
'VirtualMachine', 'type': 16}
2403 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
2404 m_update_symptom_defination
.return_value
= 'SymptomDefinition-47c88675-bea8-436a-bb41-\
2406 expected_return
= m_reconfigure_alarm
.return_value
= None # failed to reconfigure
2408 # call update alarm configuration method under test
2409 actual_return
= self
.mon_plugin
.update_alarm_configuration(alarm_config
)
2411 # verify that mocked method is called
2412 m_get_alarm_defination_details
.assert_called_with(alarm_config
['alarm_uuid'])
2413 m_update_symptom_defination
.assert_called_with(alarm_details
['symptom_definition_id'],\
2415 m_reconfigure_alarm
.assert_called_with(alarm_details_json
, alarm_config
)
2417 # verify return value with expected value
2418 self
.assertEqual(expected_return
, actual_return
)
2421 @mock.patch
.object(monPlugin
.MonPlugin
, 'reconfigure_alarm')
2422 @mock.patch
.object(monPlugin
.MonPlugin
, 'update_symptom_defination')
2423 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
2424 def test_update_alarm_configuration_failed_to_update_symptom(self
, \
2425 m_get_alarm_defination_details
, \
2426 m_update_symptom_defination
, \
2427 m_reconfigure_alarm
):
2428 """Test update alarm configuration method- failed to update alarm"""
2430 alarm_config
= {'alarm_uuid': 'f1163767-6eac-438f-8e60-a7a867257e14',
2431 'correlation_id': 14203,
2432 'description': 'CPU_Utilization_Above_Threshold_L', 'operation': 'GT'}
2435 alarm_details_json
= {'states': [{'impact': {'impactType': 'BADGE', 'detail': 'risk'},
2436 'severity': 'CRITICAL', 'base-symptom-set': {'symptomDefinitionIds':
2437 ['SymptomDefinition-47c88675-bea8-436a-bb41-8d2231428f44'],
2438 'relation': 'SELF', 'type': 'SYMPTOM_SET', 'aggregation': 'ALL'}}],
2439 'description': 'CPU_Utilization_Above_Threshold', 'type': 16,
2440 'id': 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14',
2441 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d2'}
2442 alarm_details
= {'symptom_definition_id': 'Invalid-47c88675-bea8-436a-bb41-\
2443 8d2231428f44', 'alarm_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-\
2444 a91c-8c19d2', 'alarm_id': 'AlertDefinition-f1163767-6eac-438f-8e60-\
2445 a7a867257e14', 'resource_kind': u
'VirtualMachine', 'type': 16}
2446 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
2447 expected_return
= m_update_symptom_defination
.return_value
= None
2449 # call update alarm configuration method under test
2450 actual_return
= self
.mon_plugin
.update_alarm_configuration(alarm_config
)
2452 # verify that mocked method is called
2453 m_get_alarm_defination_details
.assert_called_with(alarm_config
['alarm_uuid'])
2454 m_update_symptom_defination
.assert_called_with(alarm_details
['symptom_definition_id'],\
2456 m_reconfigure_alarm
.assert_not_called()
2458 # verify return value with expected value
2459 self
.assertEqual(expected_return
, actual_return
)
2462 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2463 def test_verify_metric_support_metric_supported_with_unit(self
,m_get_default_Params
):
2464 """Test verify metric support method for supported metric"""
2467 metric_info
= {'metric_unit': '%', 'metric_name': 'CPU_UTILIZATION',
2468 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'}
2469 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2470 expected_return
= True #supported metric returns True
2472 # call verify metric support method under test
2473 actual_return
= self
.mon_plugin
.verify_metric_support(metric_info
)
2475 # verify that mocked method is called
2476 m_get_default_Params
.assert_called_with(metric_info
['metric_name'])
2478 # verify return value with expected value
2479 self
.assertEqual(expected_return
, actual_return
)
2482 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2483 def test_verify_metric_support_metric_not_supported(self
,m_get_default_Params
):
2484 """Test verify metric support method for un-supported metric"""
2487 metric_info
= {'metric_unit': '%', 'metric_name': 'INVALID_METRIC',
2488 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'}
2489 m_get_default_Params
.return_value
= {}
2490 expected_return
= False #supported metric returns True
2492 # call verify metric support method under test
2493 actual_return
= self
.mon_plugin
.verify_metric_support(metric_info
)
2495 # verify that mocked method is called
2496 m_get_default_Params
.assert_called_with(metric_info
['metric_name'])
2498 # verify return value with expected value
2499 self
.assertEqual(expected_return
, actual_return
)
2502 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2503 def test_verify_metric_support_metric_supported_with_mismatched_unit(self
, \
2504 m_get_default_Params
):
2505 """Test verify metric support method for supported metric with mismatched unit"""
2508 metric_info
= {'metric_unit': '', 'metric_name': 'INVALID_METRIC',
2509 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'}
2510 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2511 expected_return
= True #supported metric returns True
2513 # call verify metric support method under test
2514 actual_return
= self
.mon_plugin
.verify_metric_support(metric_info
)
2516 # verify that mocked method is called
2517 m_get_default_Params
.assert_called_with(metric_info
['metric_name'])
2519 # verify return value with expected value
2520 self
.assertEqual(expected_return
, actual_return
)
2523 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_triggered_alarms_on_resource')
2524 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vrops_resourceid_from_ro_uuid')
2525 def test_get_triggered_alarms_list_returns_triggered_alarms(self
, \
2526 m_get_vrops_resourceid
, \
2527 m_triggered_alarms
):
2528 """Test get triggered alarm list method valid input"""
2530 # Mock list alarm input
2531 list_alarm_input
= {'severity': 'CRITICAL',
2532 'correlation_id': 'e14b203c',
2533 'alarm_name': 'CPU_Utilization_Above_Threshold',
2534 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'}
2536 resource_id
= m_get_vrops_resourceid
.return_value
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2537 expected_return
= m_triggered_alarms
.return_value
= [{'status': 'ACTIVE',
2538 'update_date': '2018-01-12T08:34:05',
2539 'severity': 'CRITICAL', 'resource_uuid': 'e14b203c',
2540 'cancel_date': '0000-00-00T00:00:00',
2541 'alarm_instance_uuid': 'd9e3bc84',
2542 'alarm_uuid': '5714977d', 'vim_type': 'VMware',
2543 'start_date': '2018-01-12T08:34:05'},
2544 {'status': 'CANCELED','update_date':'2017-12-20T09:37:57',
2545 'severity': 'CRITICAL', 'resource_uuid': 'e14b203c',
2546 'cancel_date': '2018-01-12T06:49:19',
2547 'alarm_instance_uuid': 'd3bbeef6',
2548 'alarm_uuid': '7ba1bf3e', 'vim_type': 'VMware',
2549 'start_date': '2017-12-20T09:37:57'}]
2551 # call get triggered alarms list method under test
2552 actual_return
= self
.mon_plugin
.get_triggered_alarms_list(list_alarm_input
)
2554 # verify that mocked method is called
2555 m_get_vrops_resourceid
.assert_called_with(list_alarm_input
['resource_uuid'])
2556 m_triggered_alarms
.assert_called_with(list_alarm_input
['resource_uuid'] , resource_id
)
2558 # verify return value with expected value
2559 self
.assertEqual(expected_return
, actual_return
)
2562 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_triggered_alarms_on_resource')
2563 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vrops_resourceid_from_ro_uuid')
2564 def test_get_triggered_alarms_list_invalid_resource_uuid(self
, \
2565 m_get_vrops_resourceid
, \
2566 m_triggered_alarms
):
2567 """Test get triggered alarm list method invalid resource uuid"""
2569 # Mock list alarm input
2570 list_alarm_input
= {'severity': 'CRITICAL',
2571 'correlation_id': 'e14b203c',
2572 'alarm_name': 'CPU_Utilization_Above_Threshold',
2573 'resource_uuid': '12345'} #invalid resource uuid
2575 expected_return
= m_get_vrops_resourceid
.return_value
= None #returns empty list
2577 # call get triggered alarms list method under test
2578 actual_return
= self
.mon_plugin
.get_triggered_alarms_list(list_alarm_input
)
2580 # verify that mocked method is called
2581 m_get_vrops_resourceid
.assert_called_with(list_alarm_input
['resource_uuid'])
2582 m_triggered_alarms
.assert_not_called()
2584 # verify return value with expected value
2585 self
.assertEqual([], actual_return
)
2588 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_triggered_alarms_on_resource')
2589 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vrops_resourceid_from_ro_uuid')
2590 def test_get_triggered_alarms_list_resource_uuid_not_present(self
, \
2591 m_get_vrops_resourceid
, \
2592 m_triggered_alarms
):
2593 """Test get triggered alarm list method resource not present"""
2595 # Mock list alarm input
2596 list_alarm_input
= {'severity': 'CRITICAL',
2597 'correlation_id': 'e14b203c',
2598 'alarm_name': 'CPU_Utilization_Above_Threshold'}
2600 # call get triggered alarms list method under test
2601 actual_return
= self
.mon_plugin
.get_triggered_alarms_list(list_alarm_input
)
2603 # verify that mocked method is called
2604 m_get_vrops_resourceid
.assert_not_called()
2605 m_triggered_alarms
.assert_not_called()
2607 # verify return value with expected value
2608 self
.assertEqual([], actual_return
)
2611 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2612 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2613 def test_get_vrops_resourceid_from_ro_uuid(self
, m_get_vm_moref_id
, m_get_vm_resource_id
):
2614 """Test get vrops resourceid from ro uuid method"""
2617 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2618 vm_moref_id
= m_get_vm_moref_id
.return_value
= 'vm-6626'
2619 expected_return
= m_get_vm_resource_id
.return_value
='ac87622f-b761-40a0-b151-00872a2a456e'
2621 # call get_vrops_resourceid_from_ro_uuid method under test
2622 actual_return
= self
.mon_plugin
.get_vrops_resourceid_from_ro_uuid(ro_resource_uuid
)
2624 # verify that mocked method is called
2625 m_get_vm_moref_id
.assert_called_with(ro_resource_uuid
)
2626 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
2628 # verify return value with expected value
2629 self
.assertEqual(expected_return
, actual_return
)
2632 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2633 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2634 def test_get_vrops_resourceid_from_ro_uuid_failed_to_get_vm_resource_id(self
, \
2635 m_get_vm_moref_id
, \
2636 m_get_vm_resource_id
):
2637 """Test get vrops resourceid from ro uuid method negative scenario"""
2640 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2641 vm_moref_id
= m_get_vm_moref_id
.return_value
= 'vm-6626'
2642 expected_return
= m_get_vm_resource_id
.return_value
= None
2644 # call get_vrops_resourceid_from_ro_uuid method under test
2645 actual_return
= self
.mon_plugin
.get_vrops_resourceid_from_ro_uuid(ro_resource_uuid
)
2647 # verify that mocked method is called
2648 m_get_vm_moref_id
.assert_called_with(ro_resource_uuid
)
2649 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
2651 # verify return value with expected value
2652 self
.assertEqual(expected_return
, actual_return
)
2655 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2656 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2657 def test_get_vrops_resourceid_from_ro_uuid_failed_to_get_vm_moref_id(self
, \
2658 m_get_vm_moref_id
, \
2659 m_get_vm_resource_id
):
2660 """Test get vrops resourceid from ro uuid method negative scenario"""
2663 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2664 expected_return
= vm_moref_id
= m_get_vm_moref_id
.return_value
= None
2666 # call get_vrops_resourceid_from_ro_uuid method under test
2667 actual_return
= self
.mon_plugin
.get_vrops_resourceid_from_ro_uuid(ro_resource_uuid
)
2669 # verify that mocked method is called
2670 m_get_vm_moref_id
.assert_called_with(ro_resource_uuid
)
2671 m_get_vm_resource_id
.assert_not_called()
2673 # verify return value with expected value
2674 self
.assertEqual(expected_return
, actual_return
)
2677 @mock.patch
.object(monPlugin
.requests
, 'get')
2678 def test_get_triggered_alarms_on_resource_valid_req_response(self
, m_get
):
2679 """Test get triggered alarms on resource method for valid request"""
2682 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2683 vrops_resource_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2684 m_get
.return_value
.status_code
= 200
2685 expected_return
= [{'status': 'ACTIVE', 'update_date': '2018-01-12T08:34:05',
2686 'severity': 'CRITICAL', 'start_date': '2018-01-12T08:34:05',
2687 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2688 'cancel_date': '2018-02-12T08:24:48', 'vim_type': 'VMware',
2689 'alarm_instance_uuid': 'd9e3bc84-dcb4-4905-b592-00a55f4cdaf1',
2690 'alarm_uuid': '5714977d-56f6-4222-adc7-43fa6c6e7e39'}]
2692 m_get
.return_value
.content
= '{"alerts": [\
2694 "alertId": "d9e3bc84-dcb4-4905-b592-00a55f4cdaf1",\
2695 "resourceId": "ac87622f-b761-40a0-b151-00872a2a456e",\
2696 "alertLevel": "CRITICAL",\
2697 "status": "ACTIVE",\
2698 "startTimeUTC": 1515746045278,\
2699 "cancelTimeUTC": 1518423888708,\
2700 "updateTimeUTC": 1515746045278,\
2701 "alertDefinitionId": "AlertDefinition-5714977d-56f6-4222-adc7-43fa6c6e7e39",\
2702 "alertDefinitionName": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4"\
2705 "alertId": "5fb5e940-e161-4253-a729-7255c6d6b1f5",\
2706 "resourceId": "ac87622f-b761-40a0-b151-00872a2a456e",\
2707 "alertLevel": "WARNING",\
2708 "status": "CANCELED",\
2709 "startTimeUTC": 1506684979154,\
2710 "cancelTimeUTC": 0,\
2711 "updateTimeUTC": 1520471975507,\
2712 "alertDefinitionId": "AlertDefinition-9ec5a921-1a54-411d-85ec-4c1c9b26dd02",\
2713 "alertDefinitionName": "VM_CPU_Usage_Alarm"\
2716 # call get_vrops_resourceid_from_ro_uuid method under test
2717 actual_return
= self
.mon_plugin
.get_triggered_alarms_on_resource(ro_resource_uuid
, \
2720 # verify that mocked method is called
2721 m_get
.assert_called()
2723 # verify return value with expected value
2724 self
.assertEqual(expected_return
, actual_return
)
2727 @mock.patch
.object(monPlugin
.requests
, 'get')
2728 def test_get_triggered_alarms_on_resource_invalid_req_response(self
, m_get
):
2729 """Test get triggered alarms on resource method for invalid request"""
2732 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2733 vrops_resource_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2734 m_get
.return_value
.status_code
= 204
2735 expected_return
= None
2737 # call get_vrops_resourceid_from_ro_uuid method under test
2738 actual_return
= self
.mon_plugin
.get_triggered_alarms_on_resource(ro_resource_uuid
, \
2741 # verify that mocked method is called
2742 m_get
.assert_called()
2744 # verify return value with expected value
2745 self
.assertEqual(expected_return
, actual_return
)
2748 @mock.patch
.object(monPlugin
.requests
, 'get')
2749 def test_get_triggered_alarms_on_resource_no_alarms_present(self
, m_get
):
2750 """Test get triggered alarms on resource method for no alarms present"""
2753 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2754 vrops_resource_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2755 m_get
.return_value
.status_code
= 200
2756 expected_return
= []
2757 m_get
.return_value
.content
= '{"alerts": []}'
2759 # call get_vrops_resourceid_from_ro_uuid method under test
2760 actual_return
= self
.mon_plugin
.get_triggered_alarms_on_resource(ro_resource_uuid
, \
2763 # verify that mocked method is called
2764 m_get
.assert_called()
2766 # verify return value with expected value
2767 self
.assertEqual(expected_return
, actual_return
)
2770 def test_convert_date_time_valid_date_time(self
):
2771 """Test convert date time method valid input"""
2774 date_time
= 1515746045278
2775 expected_return
= '2018-01-12T08:34:05'
2777 # call convert_date_time method under test
2778 actual_return
= self
.mon_plugin
.convert_date_time(date_time
)
2780 # verify return value with expected value
2781 self
.assertEqual(expected_return
, actual_return
)
2783 def test_convert_date_time_invalid_date_time(self
):
2784 """Test convert date time method invalid input"""
2788 expected_return
= '0000-00-00T00:00:00'
2790 # call convert_date_time method under test
2791 actual_return
= self
.mon_plugin
.convert_date_time(date_time
)
2793 # verify return value with expected value
2794 self
.assertEqual(expected_return
, actual_return
)
2797 @mock.patch
.object(monPlugin
.requests
, 'get')
2798 def test_get_vm_resource_id_rest_valid_req_response(self
, m_get
):
2799 """Test get vms resource id valid request"""
2802 vm_moref_id
= 'vm-6626'
2803 m_get
.return_value
.status_code
= 200
2804 expected_return
= "ac87622f-b761-40a0-b151-00872a2a456e"
2805 m_get
.return_value
.content
= \
2809 "creationTime": 1497770174130,\
2811 "name": "OCInst2.ubuntu(4337d51f-1e65-4ab0-9c08-4897778d4fda)",\
2812 "adapterKindKey": "VMWARE",\
2813 "resourceKindKey": "VirtualMachine",\
2814 "resourceIdentifiers": [\
2816 "identifierType": {\
2817 "name": "VMEntityObjectID",\
2818 "dataType": "STRING",\
2819 "isPartOfUniqueness": true\
2825 "identifier": "ac87622f-b761-40a0-b151-00872a2a456e"\
2830 # call get_vm_resource_id method under test
2831 actual_return
= self
.mon_plugin
.get_vm_resource_id(vm_moref_id
)
2833 # verify that mocked method is called
2834 m_get
.assert_called()
2836 # verify return value with expected value
2837 self
.assertEqual(expected_return
, actual_return
)
2840 @mock.patch
.object(monPlugin
.requests
, 'get')
2841 def test_get_vm_resource_id_rest_invalid_req_response(self
, m_get
):
2842 """Test get vms resource id invalid request"""
2845 vm_moref_id
= 'vm-6626'
2846 m_get
.return_value
.status_code
= 406
2847 expected_return
= None
2848 m_get
.return_value
.content
= '406 Not Acceptable'
2850 # call get_vm_resource_id method under test
2851 actual_return
= self
.mon_plugin
.get_vm_resource_id(vm_moref_id
)
2853 # verify that mocked method is called
2854 m_get
.assert_called()
2856 # verify return value with expected value
2857 self
.assertEqual(expected_return
, actual_return
)
2860 @mock.patch
.object(monPlugin
.requests
, 'get')
2861 def test_get_vm_resource_id_rest_invalid_response(self
, m_get
):
2862 """Test get vms resource id invalid response"""
2865 vm_moref_id
= 'vm-6626'
2866 m_get
.return_value
.status_code
= 200
2867 expected_return
= None
2868 m_get
.return_value
.content
= \
2872 "creationTime": 1497770174130,\
2874 "name": "OCInst2.ubuntu(4337d51f-1e65-4ab0-9c08-4897778d4fda)",\
2875 "adapterKindKey": "VMWARE",\
2876 "resourceKindKey": "VirtualMachine",\
2877 "resourceIdentifiers": [\
2879 "identifierType": {\
2880 "name": "VMEntityObjectID",\
2881 "dataType": "STRING",\
2882 "isPartOfUniqueness": true\
2888 "identifier": "ac87622f-b761-40a0-b151-00872a2a456e"\
2893 # call get_vm_resource_id method under test
2894 actual_return
= self
.mon_plugin
.get_vm_resource_id(vm_moref_id
)
2896 # verify that mocked method is called
2897 m_get
.assert_called()
2899 # verify return value with expected value
2900 self
.assertEqual(expected_return
, actual_return
)
2903 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vapp_details_rest')
2904 def test_get_vm_moref_id_valid_id_found (self
, m_get_vapp_details_rest
):
2905 """Test get vm moref id valid scenario"""
2908 vapp_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2909 m_get_vapp_details_rest
.return_value
= {'vm_vcenter_info': {'vm_moref_id': 'vm-6626'}}
2910 expected_return
= 'vm-6626'
2912 # call get_vm_resource_id method under test
2913 actual_return
= self
.mon_plugin
.get_vm_moref_id(vapp_uuid
)
2915 # verify that mocked method is called
2916 m_get_vapp_details_rest
.assert_called_with(vapp_uuid
)
2918 # verify return value with expected value
2919 self
.assertEqual(expected_return
, actual_return
)
2922 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vapp_details_rest')
2923 def test_get_vm_moref_id_valid_id_not_found(self
, m_get_vapp_details_rest
):
2924 """Test get vm moref id invalid scenario"""
2927 vapp_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda'#invalid uuid
2928 m_get_vapp_details_rest
.return_value
= {}
2929 expected_return
= None
2931 # call get_vm_resource_id method under test
2932 actual_return
= self
.mon_plugin
.get_vm_moref_id(vapp_uuid
)
2934 # verify that mocked method is called
2935 m_get_vapp_details_rest
.assert_called_with(vapp_uuid
)
2937 # verify return value with expected value
2938 self
.assertEqual(expected_return
, actual_return
)
2941 @mock.patch
.object(monPlugin
.requests
, 'get')
2942 @mock.patch
.object(monPlugin
.MonPlugin
, 'connect_as_admin')
2943 def test_get_vapp_details_rest_valid_req_response(self
, m_connect_as_admin
, m_get
):
2944 """Test get vapp details rest method for valid request response"""
2947 vapp_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2948 m_connect_as_admin
.return_value
= self
.vca
2949 self
.vca
._session
= self
.session
2950 self
.vca
._session
.headers
['x-vcloud-authorization'] = '2ec69b2cc6264ad0a47aaf4e3e280d16'
2951 m_get
.return_value
.status_code
= 200
2952 expected_return
= {'vm_vcenter_info': {'vm_moref_id': 'vm-6626'}}
2953 m_get
.return_value
.content
= '<?xml version="1.0" encoding="UTF-8"?>\
2954 <VApp xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5" >\
2956 <Vm needsCustomization="false" type="application/vnd.vmware.vcloud.vm+xml">\
2957 <VCloudExtension required="false">\
2959 <vmext:VmVimObjectRef>\
2960 <vmext:VimServerRef type="application/vnd.vmware.admin.vmwvirtualcenter+xml"/>\
2961 <vmext:MoRef>vm-6626</vmext:MoRef>\
2962 <vmext:VimObjectType>VIRTUAL_MACHINE</vmext:VimObjectType>\
2963 </vmext:VmVimObjectRef>\
2970 # call get_vapp_details_rest method under test
2971 actual_return
= self
.mon_plugin
.get_vapp_details_rest(vapp_uuid
)
2973 # verify that mocked method is called
2974 m_connect_as_admin
.assert_called_with()
2975 m_get
.assert_called()
2977 # verify return value with expected value
2978 self
.assertEqual(expected_return
, actual_return
)
2981 @mock.patch
.object(monPlugin
.requests
, 'get')
2982 @mock.patch
.object(monPlugin
.MonPlugin
, 'connect_as_admin')
2983 def test_get_vapp_details_rest_invalid_req_response(self
, m_connect_as_admin
, m_get
):
2984 """Test get vapp details rest method for invalid request response"""
2987 vapp_uuid
= 'Invalid-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2988 m_connect_as_admin
.return_value
= self
.vca
2989 self
.vca
._session
= self
.session
2990 self
.vca
._session
.headers
['x-vcloud-authorization'] = '2ec69b2cc6264ad0a47aaf4e3e280d16'
2991 m_get
.return_value
.status_code
= 400
2992 expected_return
= {}
2993 m_get
.return_value
.content
= 'Bad Request'
2995 # call get_vapp_details_rest method under test
2996 actual_return
= self
.mon_plugin
.get_vapp_details_rest(vapp_uuid
)
2998 # verify that mocked method is called
2999 m_connect_as_admin
.assert_called_with()
3000 m_get
.assert_called()
3002 # verify return value with expected value
3003 self
.assertEqual(expected_return
, actual_return
)
3006 @mock.patch
.object(monPlugin
.requests
, 'get')
3007 @mock.patch
.object(monPlugin
.MonPlugin
, 'connect_as_admin')
3008 def test_get_vapp_details_rest_failed_to_connect_vcd(self
, m_connect_as_admin
, m_get
):
3009 """Test get vapp details rest method for failed to connect to vcd"""
3012 vapp_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
3013 m_connect_as_admin
.return_value
= None
3014 expected_return
= {}
3016 # call get_vapp_details_rest method under test
3017 actual_return
= self
.mon_plugin
.get_vapp_details_rest(vapp_uuid
)
3019 # verify that mocked method is called
3020 m_connect_as_admin
.assert_called_with()
3021 m_get
.assert_not_called()
3023 # verify return value with expected value
3024 self
.assertEqual(expected_return
, actual_return
)
3027 @mock.patch
.object(monPlugin
.requests
, 'get')
3028 @mock.patch
.object(monPlugin
.MonPlugin
, 'connect_as_admin')
3029 def test_get_vapp_details_rest_invalid_response(self
, m_connect_as_admin
, m_get
):
3030 """Test get vapp details rest method for invalid response"""
3033 vapp_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
3034 m_connect_as_admin
.return_value
= self
.vca
3035 self
.vca
._session
= self
.session
3036 self
.vca
._session
.headers
['x-vcloud-authorization'] = '2ec69b2cc6264ad0a47aaf4e3e280d16'
3037 m_get
.return_value
.status_code
= 200
3038 expected_return
= {}
3039 m_get
.return_value
.content
= '<?xml version="1.0" encoding="UTF-8"?>\
3040 <VApp xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5" >\
3042 <Vm needsCustomization="false" type="application/vnd.vmware.vcloud.vm+xml">\
3043 <VCloudExtension required="false">\
3045 <vmext:VmVimObjectRef>\
3046 <vmext:VimServerRef type="application/vnd.vmware.admin.vmwvirtualcenter+xml"/>\
3047 <vmext:MoRef>vm-!!6626</vmext:MoRef>\
3048 <vmext:VimObjectType>VIRTUAL_MACHINE</vmext:VimObjectType>\
3056 # call get_vapp_details_rest method under test
3057 actual_return
= self
.mon_plugin
.get_vapp_details_rest(vapp_uuid
)
3059 # verify that mocked method is called
3060 m_connect_as_admin
.assert_called_with()
3061 m_get
.assert_called()
3063 # verify return value with expected value
3064 self
.assertEqual(expected_return
, actual_return
)
3067 @mock.patch
.object(monPlugin
.Client
, 'set_credentials')
3068 @mock.patch
.object(monPlugin
, 'Client')
3069 def test_connect_as_admin(self
, m_client
, m_set_credentials
):
3070 """Test connect as admin to vCD method"""
3072 #mock the inputs and mocked returns
3073 expected_return
= m_client
.return_value
= self
.vca
3074 m_set_credentials
.retrun_value
= True
3076 # call connect_as_admin method under test
3077 actual_return
= self
.mon_plugin
.connect_as_admin()
3079 # verify that mocked method is called
3080 m_client
.assert_called()
3081 m_set_credentials
.assert_called()
3083 # verify return value with expected value
3084 self
.assertEqual(expected_return
, actual_return
)
3087 # For testing purpose
3088 #if __name__ == '__main__':