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 """
33 sys
.path
.append(os
.path
.join(os
.path
.dirname(os
.path
.abspath(__file__
)), "..", "..", ".."))
35 from osm_mon
.plugins
.vRealiseOps
import mon_plugin_vrops
as monPlugin
37 from pyvcloud
.vcd
.client
import Client
40 class TestMonPlugin(unittest
.TestCase
):
41 """Test class for vROPs Mon Plugin class methods"""
44 """Setup the tests for Mon Plugin class methods"""
45 super(TestMonPlugin
, self
).setUp()
47 self
.m_vim_access_config
= {'vrops_site': 'abc',
49 'vrops_password': 'passwd',
51 'admin_username': 'admin',
52 'admin_password': 'admin_passwd',
54 'tenant_id': 'org_vdc_1'}
55 self
.mon_plugin
= monPlugin
.MonPlugin(self
.m_vim_access_config
)
56 # create client object
57 self
.vca
= Client('test', verify_ssl_certs
=False)
59 self
.session
= requests
.Session()
61 def test_get_default_Params_valid_metric_alarm_name(self
):
62 """Test get default params method"""
64 # Mock valid metric_alarm_name and response
65 metric_alarm_name
= "Average_Memory_Usage_Above_Threshold"
66 expected_return
= {'impact': 'risk', 'cancel_cycles': 2, 'adapter_kind': 'VMWARE',
67 'repeat': False, 'cancel_period': 300, 'alarm_type': 16,
68 'vrops_alarm': 'Avg_Mem_Usage_Above_Thr', 'enabled': True, 'period': 300,
69 'resource_kind': 'VirtualMachine', 'alarm_subType': 19,
70 'action': 'acknowledge', 'evaluation': 2, 'unit': '%'}
72 # call get default param function under test
73 actual_return
= self
.mon_plugin
.get_default_Params(metric_alarm_name
)
75 # verify return value with expected value
76 self
.assertEqual(expected_return
, actual_return
)
78 def test_get_default_Params_invalid_metric_alarm_name(self
):
79 """Test get default params method invalid metric alarm"""
81 # Mock valid metric_alarm_name and response
82 metric_alarm_name
= "Invalid_Alarm"
85 # call get default param function under test
86 actual_return
= self
.mon_plugin
.get_default_Params(metric_alarm_name
)
88 # verify return value with expected value
89 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',
99 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
100 'operation': 'GT', 'wait_cycles': 1, 'metric_key': 'cpu|usage_average'}
102 m_post
.return_value
.status_code
= 201
103 m_post
.return_value
.content
= \
104 '{"id":"SymptomDefinition-351c23b4-bc3c-4c7b-b4af-1ad90a673c5d",\
105 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
106 "adapterKindKey":"VMWARE","resourceKindKey":"VirtualMachine",\
107 "waitCycles":1,"cancelCycles":1,\
108 "state":{"severity":"CRITICAL","condition":{"type":"CONDITION_HT",\
109 "key":"cpu|usage_average","operator":"GT","value":"0.0",\
110 "valueType":"NUMERIC",\
111 "instanced":false,"thresholdType":"STATIC"}}}'
113 expected_return
= "SymptomDefinition-351c23b4-bc3c-4c7b-b4af-1ad90a673c5d"
115 # call create symptom method under test
116 actual_return
= self
.mon_plugin
.create_symptom(symptom_param
)
118 # verify that mocked method is called
119 m_post
.assert_called()
121 # verify return value with expected value
122 self
.assertEqual(expected_return
, actual_return
)
124 @mock.patch
.object(monPlugin
.requests
, 'post')
125 def test_create_symptom_invalid_req_response(self
, m_post
):
126 """Test create symptom method-invalid response"""
128 # Mock valid symptom params and invalid mock responses
129 symptom_param
= {'threshold_value': 0, 'cancel_cycles': 1, 'adapter_kind_key': 'VMWARE',
130 'resource_kind_key': 'VirtualMachine', 'severity': 'CRITICAL',
132 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
133 'operation': 'GT', 'wait_cycles': 1, 'metric_key': 'cpu|usage_average'}
135 m_post
.return_value
.status_code
= 404
136 m_post
.return_value
.content
= '404 Not Found'
138 expected_return
= None
140 # call create symptom method under test
141 actual_return
= self
.mon_plugin
.create_symptom(symptom_param
)
143 # verify that mocked method is called
144 m_post
.assert_called()
146 # verify return value with expected value
147 self
.assertEqual(expected_return
, actual_return
)
149 @mock.patch
.object(monPlugin
.requests
, 'post')
150 def test_create_symptom_incorrect_data(self
, m_post
):
151 """Test create symptom method-incorrect data"""
153 # Mock valid symptom params and invalid mock responses
154 symptom_param
= {'threshold_value': 0, 'cancel_cycles': 1, 'adapter_kind_key': 'VMWARE',
155 'resource_kind_key': 'VirtualMachine', 'severity': 'CRITICAL',
157 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
158 'operation': 'GT', 'metric_key': 'cpu|usage_average'}
160 expected_return
= None
162 # call create symptom method under test
163 actual_return
= self
.mon_plugin
.create_symptom(symptom_param
)
165 # verify that mocked method is not called
166 m_post
.assert_not_called()
168 # verify return value with expected value
169 self
.assertEqual(expected_return
, actual_return
)
171 @mock.patch
.object(monPlugin
.requests
, 'post')
172 def test_create_alarm_definition_valid_req_response(self
, m_post
):
173 """Test create alarm definition method-valid response"""
175 # Mock valid alarm params and mock responses
176 alarm_param
= {'description': 'CPU_Utilization_Above_Threshold', 'cancelCycles': 1,
177 'subType': 19, 'waitCycles': 1,
178 'severity': 'CRITICAL', 'impact': 'risk', 'adapterKindKey': 'VMWARE',
179 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
180 'resourceKindKey': 'VirtualMachine', 'type': 16,
181 'symptomDefinitionId': \
182 'SymptomDefinition-25278b06-bff8-4409-a141-9b4e064235df'}
184 m_post
.return_value
.status_code
= 201
185 m_post
.return_value
.content
= \
186 '{"id":"AlertDefinition-d4f21e4b-770a-45d6-b298-022eaf489115",\
187 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
188 "description":"CPU_Utilization_Above_Threshold","adapterKindKey":"VMWARE",\
189 "resourceKindKey":"VirtualMachine","waitCycles":1,"cancelCycles":1,\
190 "type":16,"subType":19,\
191 "states":[{"severity":"CRITICAL","base-symptom-set":{"type":"SYMPTOM_SET",\
192 "relation":"SELF","aggregation":"ALL","symptomSetOperator":"AND",\
193 "symptomDefinitionIds":\
194 ["SymptomDefinition-25278b06-bff8-4409-a141-9b4e064235df"]},\
195 "impact":{"impactType":"BADGE","detail":"risk"}}]}'
197 expected_return
= "AlertDefinition-d4f21e4b-770a-45d6-b298-022eaf489115"
199 # call create alarm definition method under test
200 actual_return
= self
.mon_plugin
.create_alarm_definition(alarm_param
)
202 # verify that mocked method is called
203 m_post
.assert_called()
205 # verify return value with expected value
206 self
.assertEqual(expected_return
, actual_return
)
208 @mock.patch
.object(monPlugin
.requests
, 'post')
209 def test_create_alarm_definition_invalid_req_response(self
, m_post
):
210 """Test create alarm definition method-invalid response"""
212 # Mock valid alarm params and mock responses
213 alarm_param
= {'description': 'CPU_Utilization_Above_Threshold', 'cancelCycles': 1,
214 'subType': 19, 'waitCycles': 1,
215 'severity': 'CRITICAL', 'impact': 'risk', 'adapterKindKey': 'VMWARE',
216 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
217 'resourceKindKey': 'VirtualMachine', 'type': 16,
218 'symptomDefinitionId': \
219 'SymptomDefinition-25278b06-bff8-4409-a141-9b4e064235df'}
221 m_post
.return_value
.status_code
= 404
222 m_post
.return_value
.content
= '404 Not Found'
224 expected_return
= None
226 # call create alarm definition method under test
227 actual_return
= self
.mon_plugin
.create_alarm_definition(alarm_param
)
229 # verify that mocked method is called
230 m_post
.assert_called()
232 # verify return value with expected value
233 self
.assertEqual(expected_return
, actual_return
)
235 @mock.patch
.object(monPlugin
.requests
, 'post')
236 def test_create_alarm_definition_incorrect_data(self
, m_post
):
237 """Test create alarm definition method-incorrect data"""
239 # Mock incorrect alarm param
240 alarm_param
= {'description': 'CPU_Utilization_Above_Threshold', 'cancelCycles': 1,
241 'subType': 19, 'waitCycles': 1, 'type': 16,
242 'severity': 'CRITICAL', 'impact': 'risk', 'adapterKindKey': 'VMWARE',
243 'symptomDefinitionId': \
244 'SymptomDefinition-25278b06-bff8-4409-a141-9b4e064235df'}
245 expected_return
= None
247 # call create symptom method under test
248 actual_return
= self
.mon_plugin
.create_alarm_definition(alarm_param
)
250 # verify that mocked method is not called
251 m_post
.assert_not_called()
253 # verify return value with expected value
254 self
.assertEqual(expected_return
, actual_return
)
256 @mock.patch
.object(monPlugin
.DatabaseManager
, 'save_alarm')
257 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
258 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
259 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
260 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
261 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
262 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
263 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
264 def test_configure_alarm_valid_req(self
, m_get_default_Params
,
265 m_get_alarm_defination_by_name
,
267 m_create_alarm_definition
,
269 m_get_vm_resource_id
,
270 m_create_alarm_notification_rule
,
272 """Test configure alarm valid request creating alarm"""
274 # Mock input configuration dictionary
275 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
276 'alarm_name': 'CPU_Utilization_Above_Threshold',
277 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
278 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
279 'statistic': 'AVERAGE', 'metric_name': 'cpu_utilization',
280 'vdu_name': 'vdu1', 'vnf_member_index': 'index1', 'ns_id': 'nsd1',
281 'operation': 'GT', 'unit': '%',
282 'description': 'CPU_Utilization_Above_Threshold'}
284 # symptom parameters to be passed for symptom creation
285 symptom_params
= {'threshold_value': 0,
287 'adapter_kind_key': 'VMWARE',
288 'resource_kind_key': 'VirtualMachine',
289 'severity': 'CRITICAL',
291 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
294 'metric_key': 'cpu|usage_average'}
296 # alarm parameters to be passed for alarm creation
297 alarm_params
= {'description': 'CPU_Utilization_Above_Threshold',
298 'cancelCycles': 1, 'subType': 19,
299 'waitCycles': 1, 'severity': 'CRITICAL',
300 'impact': 'risk', 'adapterKindKey': 'VMWARE',
301 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
302 'resourceKindKey': 'VirtualMachine',
303 'symptomDefinitionId': \
304 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804',
307 vm_moref_id
= 'vm-6626'
308 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
309 alarm_def
= 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
310 resource_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
311 alarm_def_uuid
= '0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
313 # Mock default Parameters for alarm & metric configuration
314 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
315 'adapter_kind': 'VMWARE', 'repeat': False,
316 'cancel_period': 300, 'alarm_type': 16,
317 'vrops_alarm': 'CPU_Utilization_Above_Thr',
318 'enabled': True, 'period': 300,
319 'resource_kind': 'VirtualMachine',
320 'alarm_subType': 19, 'action': 'acknowledge',
321 'evaluation': 1, 'unit': 'msec'},
322 {'metric_key': 'cpu|usage_average', 'unit': '%'}
325 # set mocked function return values
326 m_get_alarm_defination_by_name
.return_value
= []
327 m_create_symptom
.return_value
= 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804'
328 m_create_alarm_definition
.return_value
= \
329 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
330 m_get_vm_moref_id
.return_value
= vm_moref_id
331 m_get_vm_resource_id
.return_value
= 'ac87622f-b761-40a0-b151-00872a2a456e'
332 m_create_alarm_notification_rule
.return_value
= 'f37900e7-dd01-4383-b84c-08f519530d71'
334 # Call configure_alarm method under test
335 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
337 # Verify that mocked methods are called with correct parameters
338 self
.assertEqual(m_get_default_Params
.call_count
, 2)
339 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
340 m_create_symptom
.assert_called_with(symptom_params
)
341 m_create_alarm_definition
.assert_called_with(alarm_params
)
342 m_get_vm_moref_id
.assert_called_with(config_dict
['resource_uuid'])
343 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
344 m_create_alarm_notification_rule
.assert_called_with(vrops_alarm_name
,
347 m_save_alarm
.assert_called_with(vrops_alarm_name
, '1',
348 config_dict
['threshold_value'],
349 config_dict
['operation'],
350 config_dict
['metric_name'],
351 config_dict
['vdu_name'],
352 config_dict
['vnf_member_index'],
353 config_dict
['ns_id'])
355 # Verify return value with expected value of alarm_def_uuid
356 self
.assertEqual(return_value
, alarm_def_uuid
)
358 @mock.patch
.object(monPlugin
.DatabaseManager
, 'save_alarm')
359 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
360 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
361 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
362 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
363 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
364 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
365 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
366 def test_configure_alarm_invalid_alarm_name_req(self
, m_get_default_Params
,
367 m_get_alarm_defination_by_name
,
369 m_create_alarm_definition
,
371 m_get_vm_resource_id
,
372 m_create_alarm_notification_rule
,
374 """Test configure alarm invalid test: for invalid alarm name"""
376 # Mock input configuration dictionary
377 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
378 'alarm_name': 'CPU_Utilization_Above_Threshold',
379 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
380 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
381 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
382 'operation': 'GT', 'unit': '%',
383 'description': 'CPU_Utilization_Above_Threshold'}
385 alarm_def_uuid
= None
387 # Mock default Parameters return value to None
388 m_get_default_Params
.return_value
= {}
390 # Call configure_alarm method under test
391 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
393 # Verify that mocked methods are called with correct parameters
394 m_get_default_Params
.assert_called_with(config_dict
['alarm_name'])
395 m_get_alarm_defination_by_name
.assert_not_called()
396 m_create_symptom
.assert_not_called()
397 m_create_alarm_definition
.assert_not_called()
398 m_get_vm_moref_id
.assert_not_called()
399 m_get_vm_resource_id
.assert_not_called()
400 m_create_alarm_notification_rule
.assert_not_called()
401 m_save_alarm
.assert_not_called()
403 # Verify return value with expected value i.e. None
404 self
.assertEqual(return_value
, alarm_def_uuid
)
406 @mock.patch
.object(monPlugin
.DatabaseManager
, 'save_alarm')
407 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
408 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
409 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
410 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
411 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
412 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
413 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
414 def test_configure_alarm_invalid_metric_name_req(self
, m_get_default_Params
,
415 m_get_alarm_defination_by_name
,
417 m_create_alarm_definition
,
419 m_get_vm_resource_id
,
420 m_create_alarm_notification_rule
,
422 """Test configure alarm invalid test: for invalid metric name"""
424 # Mock input configuration dictionary
425 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
426 'alarm_name': 'CPU_Utilization_Above_Threshold',
427 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
428 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
429 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
430 'operation': 'GT', 'unit': '%',
431 'description': 'CPU_Utilization_Above_Threshold'}
433 alarm_def_uuid
= None
435 # Mock default Parameters return values for metrics to None
436 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
437 'adapter_kind': 'VMWARE', 'repeat': False,
438 'cancel_period': 300, 'alarm_type': 16,
439 'vrops_alarm': 'CPU_Utilization_Above_Thr',
440 'enabled': True, 'period': 300,
441 'resource_kind': 'VirtualMachine',
442 'alarm_subType': 19, 'action': 'acknowledge',
443 'evaluation': 1, 'unit': 'msec'},
447 # Call configure_alarm method under test
448 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
450 # Verify that mocked methods are called with correct parameters
451 self
.assertEqual(m_get_default_Params
.call_count
, 2)
452 m_get_alarm_defination_by_name
.assert_not_called()
453 m_create_symptom
.assert_not_called()
454 m_create_alarm_definition
.assert_not_called()
455 m_get_vm_moref_id
.assert_not_called()
456 m_get_vm_resource_id
.assert_not_called()
457 m_create_alarm_notification_rule
.assert_not_called()
458 m_save_alarm
.assert_not_called()
460 # Verify return value with expected value i.e. None
461 self
.assertEqual(return_value
, alarm_def_uuid
)
463 @mock.patch
.object(monPlugin
.DatabaseManager
, 'save_alarm')
464 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
465 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
466 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
467 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
468 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
469 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
470 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
471 def test_configure_alarm_invalid_already_exists(self
, m_get_default_Params
,
472 m_get_alarm_defination_by_name
,
474 m_create_alarm_definition
,
476 m_get_vm_resource_id
,
477 m_create_alarm_notification_rule
,
479 """Test configure alarm invalid test: for alarm that already exists"""
481 # Mock input configuration dictionary
482 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
483 'alarm_name': 'CPU_Utilization_Above_Threshold',
484 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
485 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
486 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
487 'operation': 'GT', 'unit': '%',
488 'description': 'CPU_Utilization_Above_Threshold'}
490 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
491 alarm_def_uuid
= None
493 # Mock default Parameters for alarm & metric configuration
494 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
495 'adapter_kind': 'VMWARE', 'repeat': False,
496 'cancel_period': 300, 'alarm_type': 16,
497 'vrops_alarm': 'CPU_Utilization_Above_Thr',
498 'enabled': True, 'period': 300,
499 'resource_kind': 'VirtualMachine',
500 'alarm_subType': 19, 'action': 'acknowledge',
501 'evaluation': 1, 'unit': 'msec'},
502 {'metric_key': 'cpu|usage_average', 'unit': '%'}
504 # set mocked function return value
505 m_get_alarm_defination_by_name
.return_value
= ['mocked_alarm_CPU_Utilization_Above_Thr']
507 # Call configure_alarm method under test
508 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
510 # Verify that mocked methods are called with correct parameters
511 self
.assertEqual(m_get_default_Params
.call_count
, 2)
512 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
513 m_create_symptom
.assert_not_called()
514 m_create_alarm_definition
.assert_not_called()
515 m_get_vm_moref_id
.assert_not_called()
516 m_get_vm_resource_id
.assert_not_called()
517 m_create_alarm_notification_rule
.assert_not_called()
518 m_save_alarm
.assert_not_called()
519 # Verify return value with expected value of alarm_def_uuid
520 self
.assertEqual(return_value
, alarm_def_uuid
)
522 @mock.patch
.object(monPlugin
.DatabaseManager
, 'save_alarm')
523 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
524 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
525 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
526 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
527 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
528 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
529 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
530 def test_configure_alarm_failed_symptom_creation(self
, m_get_default_Params
,
531 m_get_alarm_defination_by_name
,
533 m_create_alarm_definition
,
535 m_get_vm_resource_id
,
536 m_create_alarm_notification_rule
,
538 """Test configure alarm: failed to create symptom"""
540 # Mock input configuration dictionary
541 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
542 'alarm_name': 'CPU_Utilization_Above_Threshold',
543 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
544 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
545 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
546 'operation': 'GT', 'unit': '%',
547 'description': 'CPU_Utilization_Above_Threshold'}
549 # symptom parameters to be passed for symptom creation
550 symptom_params
= {'threshold_value': 0,
552 'adapter_kind_key': 'VMWARE',
553 'resource_kind_key': 'VirtualMachine',
554 'severity': 'CRITICAL',
555 'symptom_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
558 'metric_key': 'cpu|usage_average'}
559 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
560 alarm_def_uuid
= None
562 # Mock default Parameters for alarm & metric configuration
563 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
564 'adapter_kind': 'VMWARE', 'repeat': False,
565 'cancel_period': 300, 'alarm_type': 16,
566 'vrops_alarm': 'CPU_Utilization_Above_Thr',
567 'enabled': True, 'period': 300,
568 'resource_kind': 'VirtualMachine',
569 'alarm_subType': 19, 'action': 'acknowledge',
570 'evaluation': 1, 'unit': 'msec'},
571 {'metric_key': 'cpu|usage_average', 'unit': '%'}
573 # set mocked function return values
574 m_get_alarm_defination_by_name
.return_value
= []
575 m_create_symptom
.return_value
= None
577 # Call configure_alarm method under test
578 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
580 # Verify that mocked methods are called with correct parameters
581 self
.assertEqual(m_get_default_Params
.call_count
, 2)
582 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
583 m_create_symptom
.assert_called_with(symptom_params
)
584 m_create_alarm_definition
.assert_not_called()
585 m_get_vm_moref_id
.assert_not_called()
586 m_get_vm_resource_id
.assert_not_called()
587 m_create_alarm_notification_rule
.assert_not_called()
588 m_save_alarm
.assert_not_called()
590 # Verify return value with expected value of alarm_def_uuid
591 self
.assertEqual(return_value
, alarm_def_uuid
)
593 @mock.patch
.object(monPlugin
.DatabaseManager
, 'save_alarm')
594 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
595 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
596 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
597 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
598 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
599 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
600 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
601 def test_configure_alarm_failed_alert_creation(self
, m_get_default_Params
,
602 m_get_alarm_defination_by_name
,
604 m_create_alarm_definition
,
606 m_get_vm_resource_id
,
607 m_create_alarm_notification_rule
,
609 """Test configure alarm: failed to create alert in vROPs"""
611 # Mock input configuration dictionary
612 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
613 'alarm_name': 'CPU_Utilization_Above_Threshold',
614 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
615 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
616 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
617 'operation': 'GT', 'unit': '%',
618 'description': 'CPU_Utilization_Above_Threshold'}
620 # symptom parameters to be passed for symptom creation
621 symptom_params
= {'threshold_value': 0,
623 'adapter_kind_key': 'VMWARE',
624 'resource_kind_key': 'VirtualMachine',
625 'severity': 'CRITICAL',
626 'symptom_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
629 'metric_key': 'cpu|usage_average'}
631 # alarm parameters to be passed for alarm creation
632 alarm_params
= {'description': 'CPU_Utilization_Above_Threshold',
633 'cancelCycles': 1, 'subType': 19,
634 'waitCycles': 1, 'severity': 'CRITICAL',
635 'impact': 'risk', 'adapterKindKey': 'VMWARE',
636 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
637 'resourceKindKey': 'VirtualMachine',
638 'symptomDefinitionId': 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804',
641 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
642 alarm_def_uuid
= None
644 # Mock default Parameters for alarm & metric configuration
645 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
646 'adapter_kind': 'VMWARE', 'repeat': False,
647 'cancel_period': 300, 'alarm_type': 16,
648 'vrops_alarm': 'CPU_Utilization_Above_Thr',
649 'enabled': True, 'period': 300,
650 'resource_kind': 'VirtualMachine',
651 'alarm_subType': 19, 'action': 'acknowledge',
652 'evaluation': 1, 'unit': 'msec'},
653 {'metric_key': 'cpu|usage_average', 'unit': '%'}
655 # set mocked function return values
656 m_get_alarm_defination_by_name
.return_value
= []
657 m_create_symptom
.return_value
= 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804'
658 m_create_alarm_definition
.return_value
= None
660 # Call configure_alarm method under test
661 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
663 # Verify that mocked methods are called with correct parameters
664 self
.assertEqual(m_get_default_Params
.call_count
, 2)
665 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
666 m_create_symptom
.assert_called_with(symptom_params
)
667 m_create_alarm_definition
.assert_called_with(alarm_params
)
668 m_get_vm_moref_id
.assert_not_called()
669 m_get_vm_resource_id
.assert_not_called()
670 m_create_alarm_notification_rule
.assert_not_called()
671 m_save_alarm
.assert_not_called()
673 # Verify return value with expected value of alarm_def_uuid
674 self
.assertEqual(return_value
, alarm_def_uuid
)
676 @mock.patch
.object(monPlugin
.DatabaseManager
, 'save_alarm')
677 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
678 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
679 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
680 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
681 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
682 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
683 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
684 def test_configure_alarm_failed_to_get_vm_moref_id(self
, m_get_default_Params
,
685 m_get_alarm_defination_by_name
,
687 m_create_alarm_definition
,
689 m_get_vm_resource_id
,
690 m_create_alarm_notification_rule
,
692 """Test configure alarm: failed to get vm_moref_id"""
694 # Mock input configuration dictionary
695 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
696 'alarm_name': 'CPU_Utilization_Above_Threshold',
697 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
698 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
699 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
700 'operation': 'GT', 'unit': '%',
701 'description': 'CPU_Utilization_Above_Threshold'}
703 # symptom parameters to be passed for symptom creation
704 symptom_params
= {'threshold_value': 0,
706 'adapter_kind_key': 'VMWARE',
707 'resource_kind_key': 'VirtualMachine',
708 'severity': 'CRITICAL',
710 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
713 'metric_key': 'cpu|usage_average'}
715 # alarm parameters to be passed for alarm creation
716 alarm_params
= {'description': 'CPU_Utilization_Above_Threshold',
717 'cancelCycles': 1, 'subType': 19,
718 'waitCycles': 1, 'severity': 'CRITICAL',
719 'impact': 'risk', 'adapterKindKey': 'VMWARE',
720 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
721 'resourceKindKey': 'VirtualMachine',
722 'symptomDefinitionId': \
723 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804',
726 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
727 alarm_def_uuid
= None
729 # Mock default Parameters for alarm & metric configuration
730 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
731 'adapter_kind': 'VMWARE', 'repeat': False,
732 'cancel_period': 300, 'alarm_type': 16,
733 'vrops_alarm': 'CPU_Utilization_Above_Thr',
734 'enabled': True, 'period': 300,
735 'resource_kind': 'VirtualMachine',
736 'alarm_subType': 19, 'action': 'acknowledge',
737 'evaluation': 1, 'unit': 'msec'},
738 {'metric_key': 'cpu|usage_average', 'unit': '%'}
740 # set mocked function return values
741 m_get_alarm_defination_by_name
.return_value
= []
742 m_create_symptom
.return_value
= 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804'
743 m_create_alarm_definition
.return_value
= \
744 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
745 m_get_vm_moref_id
.return_value
= None
747 # Call configure_alarm method under test
748 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
750 # Verify that mocked methods are called with correct parameters
751 self
.assertEqual(m_get_default_Params
.call_count
, 2)
752 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
753 m_create_symptom
.assert_called_with(symptom_params
)
754 m_create_alarm_definition
.assert_called_with(alarm_params
)
755 m_get_vm_moref_id
.assert_called_with(config_dict
['resource_uuid'])
756 m_get_vm_resource_id
.assert_not_called()
757 m_create_alarm_notification_rule
.assert_not_called()
758 m_save_alarm
.assert_not_called()
760 # Verify return value with expected value of alarm_def_uuid
761 self
.assertEqual(return_value
, alarm_def_uuid
)
763 @mock.patch
.object(monPlugin
.DatabaseManager
, 'save_alarm')
764 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
765 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
766 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
767 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
768 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
769 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
770 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
771 def test_configure_alarm_failed_to_get_vm_resource_id(self
, m_get_default_Params
,
772 m_get_alarm_defination_by_name
,
774 m_create_alarm_definition
,
776 m_get_vm_resource_id
,
777 m_create_alarm_notification_rule
,
779 """Test configure alarm: failed to get vm resource_id"""
781 # Mock input configuration dictionary
782 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
783 'alarm_name': 'CPU_Utilization_Above_Threshold',
784 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
785 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
786 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
787 'operation': 'GT', 'unit': '%',
788 'description': 'CPU_Utilization_Above_Threshold'}
790 # symptom parameters to be passed for symptom creation
791 symptom_params
= {'threshold_value': 0,
793 'adapter_kind_key': 'VMWARE',
794 'resource_kind_key': 'VirtualMachine',
795 'severity': 'CRITICAL',
797 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
800 'metric_key': 'cpu|usage_average'}
802 # alarm parameters to be passed for alarm creation
803 alarm_params
= {'description': 'CPU_Utilization_Above_Threshold',
804 'cancelCycles': 1, 'subType': 19,
805 'waitCycles': 1, 'severity': 'CRITICAL',
806 'impact': 'risk', 'adapterKindKey': 'VMWARE',
807 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
808 'resourceKindKey': 'VirtualMachine',
809 'symptomDefinitionId': \
810 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804',
813 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
814 vm_moref_id
= 'vm-6626'
815 alarm_def_uuid
= None
817 # Mock default Parameters for alarm & metric configuration
818 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
819 'adapter_kind': 'VMWARE', 'repeat': False,
820 'cancel_period': 300, 'alarm_type': 16,
821 'vrops_alarm': 'CPU_Utilization_Above_Thr',
822 'enabled': True, 'period': 300,
823 'resource_kind': 'VirtualMachine',
824 'alarm_subType': 19, 'action': 'acknowledge',
825 'evaluation': 1, 'unit': 'msec'},
826 {'metric_key': 'cpu|usage_average', 'unit': '%'}
828 # set mocked function return values
829 m_get_alarm_defination_by_name
.return_value
= []
830 m_create_symptom
.return_value
= 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804'
831 m_create_alarm_definition
.return_value
= \
832 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
833 m_get_vm_moref_id
.return_value
= vm_moref_id
834 m_get_vm_resource_id
.return_value
= None
835 m_save_alarm
.assert_not_called()
837 # Call configure_alarm method under test
838 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
840 # Verify that mocked methods are called with correct parameters
841 self
.assertEqual(m_get_default_Params
.call_count
, 2)
842 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
843 m_create_symptom
.assert_called_with(symptom_params
)
844 m_create_alarm_definition
.assert_called_with(alarm_params
)
845 m_get_vm_moref_id
.assert_called_with(config_dict
['resource_uuid'])
846 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
847 m_create_alarm_notification_rule
.assert_not_called()
849 # Verify return value with expected value of alarm_def_uuid
850 self
.assertEqual(return_value
, alarm_def_uuid
)
852 @mock.patch
.object(monPlugin
.DatabaseManager
, 'save_alarm')
853 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
854 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
855 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
856 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
857 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
858 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
859 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
860 def test_configure_alarm_failed_to_create_alarm_notification_rule(self
, m_get_default_Params
,
861 m_get_alarm_defination_by_name
,
863 m_create_alarm_definition
,
865 m_get_vm_resource_id
,
866 m_create_alarm_notification_rule
,
868 """Test configure alarm: failed to create alarm notification rule"""
870 # Mock input configuration dictionary
871 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
872 'alarm_name': 'CPU_Utilization_Above_Threshold',
873 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
874 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
875 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
876 'operation': 'GT', 'unit': '%',
877 'description': 'CPU_Utilization_Above_Threshold'}
879 # symptom parameters to be passed for symptom creation
880 symptom_params
= {'threshold_value': 0,
882 'adapter_kind_key': 'VMWARE',
883 'resource_kind_key': 'VirtualMachine',
884 'severity': 'CRITICAL',
886 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
889 'metric_key': 'cpu|usage_average'}
891 # alarm parameters to be passed for alarm creation
892 alarm_params
= {'description': 'CPU_Utilization_Above_Threshold',
893 'cancelCycles': 1, 'subType': 19,
894 'waitCycles': 1, 'severity': 'CRITICAL',
895 'impact': 'risk', 'adapterKindKey': 'VMWARE',
896 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
897 'resourceKindKey': 'VirtualMachine',
898 'symptomDefinitionId': \
899 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804',
902 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
903 vm_moref_id
= 'vm-6626'
904 alarm_def
= 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
905 resource_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
906 alarm_def_uuid
= None
908 # Mock default Parameters for alarm & metric configuration
909 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
910 'adapter_kind': 'VMWARE', 'repeat': False,
911 'cancel_period': 300, 'alarm_type': 16,
912 'vrops_alarm': 'CPU_Utilization_Above_Thr',
913 'enabled': True, 'period': 300,
914 'resource_kind': 'VirtualMachine',
915 'alarm_subType': 19, 'action': 'acknowledge',
916 'evaluation': 1, 'unit': 'msec'},
917 {'metric_key': 'cpu|usage_average', 'unit': '%'}
919 # set mocked function return values
920 m_get_alarm_defination_by_name
.return_value
= []
921 m_create_symptom
.return_value
= 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804'
922 m_create_alarm_definition
.return_value
= \
923 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
924 m_get_vm_moref_id
.return_value
= vm_moref_id
925 m_get_vm_resource_id
.return_value
= 'ac87622f-b761-40a0-b151-00872a2a456e'
926 m_create_alarm_notification_rule
.return_value
= None
928 # Call configure_alarm method under test
929 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
931 # Verify that mocked methods are called with correct parameters
932 self
.assertEqual(m_get_default_Params
.call_count
, 2)
933 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
934 m_create_symptom
.assert_called_with(symptom_params
)
935 m_create_alarm_definition
.assert_called_with(alarm_params
)
936 m_get_vm_moref_id
.assert_called_with(config_dict
['resource_uuid'])
937 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
938 m_create_alarm_notification_rule
.assert_called_with(vrops_alarm_name
, alarm_def
, resource_id
)
939 m_save_alarm
.assert_not_called()
941 # Verify return value with expected value of alarm_def_uuid
942 self
.assertEqual(return_value
, alarm_def_uuid
)
944 @mock.patch
.object(monPlugin
.requests
, 'get')
945 def test_get_alarm_defination_details_valid_rest_req_response(self
, m_get
):
946 """Test get_alarm_defination_details: For a valid REST request response"""
948 alarm_uuid
= '9a6d8a14-9f25-4d81-bf91-4d773497444d'
950 # Set mocked function's return values
951 m_get
.return_value
.status_code
= 200
952 m_get
.return_value
.content
= '{"id":"AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d",\
953 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
954 "description":"CPU_Utilization_Above_Threshold",\
955 "adapterKindKey":"VMWARE","resourceKindKey":"VirtualMachine",\
956 "waitCycles":1,"cancelCycles":1,"type":16,"subType":19,\
957 "states":[{"severity":"CRITICAL","base-symptom-set":\
958 {"type":"SYMPTOM_SET","relation":"SELF",\
959 "aggregation":"ALL","symptomSetOperator":"AND","symptomDefinitionIds":\
960 ["SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278"]},\
961 "impact":{"impactType":"BADGE","detail":"risk"}}]}'
963 expected_alarm_details
= {'adapter_kind': 'VMWARE', 'symptom_definition_id': \
964 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278',
966 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
967 'alarm_id': 'AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d',
968 'resource_kind': 'VirtualMachine', 'type': 16, 'sub_type': 19}
970 expected_alarm_details_json
= {'states':
972 {'impactType': 'BADGE', 'detail': 'risk'}, 'severity': 'CRITICAL',
973 'base-symptom-set': {'symptomDefinitionIds': \
975 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'],
976 'relation': 'SELF', 'type': 'SYMPTOM_SET',
977 'aggregation': 'ALL', 'symptomSetOperator': 'AND'}}],
978 'adapterKindKey': 'VMWARE',
979 'description': 'CPU_Utilization_Above_Threshold',
980 'type': 16, 'cancelCycles': 1,
981 'resourceKindKey': 'VirtualMachine',
982 'subType': 19, 'waitCycles': 1,
983 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d',
985 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'}
987 # Call get_alarm_defination_details method under test
988 alarm_details_json
, alarm_details
= self
.mon_plugin
.get_alarm_defination_details(alarm_uuid
)
990 # Verify that mocked method is called
991 m_get
.assert_called()
993 # Verify return value with expected value
994 self
.assertEqual(expected_alarm_details
, alarm_details
)
995 self
.assertEqual(expected_alarm_details_json
, alarm_details_json
)
997 @mock.patch
.object(monPlugin
.requests
, 'get')
998 def test_get_alarm_defination_details_invalid_rest_req_response(self
, m_get
):
999 """Test get_alarm_defination_details: For an invalid REST request response"""
1001 alarm_uuid
= '9a6d8a14-9f25-4d81-bf91-4d773497444d'
1003 # Set mocked function's return values
1004 m_get
.return_value
.status_code
= 404
1005 m_get
.return_value
.content
= '{"message": "No such AlertDefinition - \
1006 AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444.",\
1007 "httpStatusCode": 404,"apiErrorCode": 404}'
1009 expected_alarm_details
= None
1010 expected_alarm_details_json
= None
1012 # Call get_alarm_defination_details method under test
1013 alarm_details_json
, alarm_details
= self
.mon_plugin
.get_alarm_defination_details(alarm_uuid
)
1015 # verify that mocked method is called
1016 m_get
.assert_called()
1018 # Verify return value with expected value
1019 self
.assertEqual(expected_alarm_details
, alarm_details
)
1020 self
.assertEqual(expected_alarm_details_json
, alarm_details_json
)
1022 @mock.patch
.object(monPlugin
.requests
, 'get')
1023 def test_get_alarm_defination_by_name_valid_rest_req_response(self
, m_get
):
1024 """Test get_alarm_defination_by_name: For a valid REST request response"""
1026 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
1028 # Set mocked function's return values
1029 m_get
.return_value
.status_code
= 200
1030 m_get
.return_value
.content
= '{"pageInfo": {"totalCount": 1,"page": 0,"pageSize": 1000},\
1032 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
1033 "rel": "SELF","name": "current"},\
1034 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
1035 "rel": "RELATED","name": "first"},\
1036 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
1037 "rel": "RELATED","name": "last"}],\
1038 "alertDefinitions": [{\
1039 "id": "AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d",\
1040 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1041 "description": "CPU_Utilization_Above_Threshold",\
1042 "adapterKindKey": "VMWARE","resourceKindKey": "VirtualMachine",\
1043 "waitCycles": 1,"cancelCycles": 1,"type": 16,"subType": 19,\
1044 "states": [{"impact": {"impactType": "BADGE","detail": "risk"},\
1045 "severity": "CRITICAL",\
1046 "base-symptom-set": {"type": "SYMPTOM_SET",\
1047 "relation": "SELF","aggregation": "ALL",\
1048 "symptomSetOperator": "AND",\
1049 "symptomDefinitionIds": [\
1050 "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278"]}}]\
1053 # Expected return match list
1054 Exp_alert_match_list
= [{'states':
1055 [{'impact': {'impactType': 'BADGE', 'detail': 'risk'},
1056 'severity': 'CRITICAL',
1057 'base-symptom-set': {
1058 'symptomDefinitionIds': \
1059 ['SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'],
1061 'type': 'SYMPTOM_SET',
1062 'aggregation': 'ALL',
1063 'symptomSetOperator': 'AND'}
1065 'adapterKindKey': 'VMWARE',
1066 'description': 'CPU_Utilization_Above_Threshold',
1069 'resourceKindKey': 'VirtualMachine',
1070 'subType': 19, 'waitCycles': 1,
1071 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d',
1073 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
1076 # Call get_alarm_defination_by_name method under test
1077 alert_match_list
= self
.mon_plugin
.get_alarm_defination_by_name(alarm_name
)
1079 # Verify that mocked method is called
1080 m_get
.assert_called()
1082 # Verify return value with expected value
1083 self
.assertEqual(Exp_alert_match_list
, alert_match_list
)
1085 @mock.patch
.object(monPlugin
.requests
, 'get')
1086 def test_get_alarm_defination_by_name_no_valid_alarm_found(self
, m_get
):
1087 """Test get_alarm_defination_by_name: With no valid alarm found in returned list"""
1089 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda5'
1091 # Set mocked function's return values
1092 m_get
.return_value
.status_code
= 200
1093 m_get
.return_value
.content
= '{"pageInfo": {"totalCount": 1,"page": 0,"pageSize": 1000},\
1095 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
1096 "rel": "SELF","name": "current"},\
1097 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
1098 "rel": "RELATED","name": "first"},\
1099 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
1100 "rel": "RELATED","name": "last"}],\
1101 "alertDefinitions": [{\
1102 "id": "AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d",\
1103 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1104 "description": "CPU_Utilization_Above_Threshold",\
1105 "adapterKindKey": "VMWARE","resourceKindKey": "VirtualMachine",\
1106 "waitCycles": 1,"cancelCycles": 1,"type": 16,"subType": 19,\
1107 "states": [{"impact": {"impactType": "BADGE","detail": "risk"},\
1108 "severity": "CRITICAL",\
1109 "base-symptom-set": {"type": "SYMPTOM_SET",\
1110 "relation": "SELF","aggregation": "ALL",\
1111 "symptomSetOperator": "AND",\
1112 "symptomDefinitionIds": [\
1113 "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278"]}}]\
1116 # Expected return match list
1117 Exp_alert_match_list
= []
1119 # Call get_alarm_defination_by_name method under test
1120 alert_match_list
= self
.mon_plugin
.get_alarm_defination_by_name(alarm_name
)
1122 # Verify that mocked method is called
1123 m_get
.assert_called()
1125 # Verify return value with expected value
1126 self
.assertEqual(Exp_alert_match_list
, alert_match_list
)
1128 @mock.patch
.object(monPlugin
.requests
, 'put')
1129 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_symptom_defination_details')
1130 def test_update_symptom_defination_valid_symptom_req_response(self
,
1131 m_get_symptom_defination_details
,
1133 """Test update_symptom_defination: With valid REST response, update symptom"""
1135 # Expected symptom to be updated
1136 symptom_defination_id
= 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'
1137 new_alarm_config
= {'severity': "CRITICAL",
1139 'threshold_value': 5,
1140 'alarm_uuid': '9a6d8a14-9f25-4d81-bf91-4d773497444d'
1143 # Set mocked function's return values
1144 m_get_symptom_defination_details
.return_value
= {
1145 "id": "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278",
1146 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",
1147 "adapterKindKey": "VMWARE",
1148 "resourceKindKey": "VirtualMachine",
1151 "state": {"severity": "CRITICAL",
1153 "type": "CONDITION_HT",
1154 "key": "cpu|usage_average", "operator": "GT", "value": "0.0",
1155 "valueType": "NUMERIC", "instanced": False,
1156 "thresholdType": "STATIC"}
1160 m_put
.return_value
.status_code
= 200
1161 m_put
.return_value
.content
= '{\
1162 "id":"SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278",\
1163 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1164 "adapterKindKey":"VMWARE","resourceKindKey":"VirtualMachine","waitCycles":1,\
1167 "severity":"CRITICAL",\
1169 "type":"CONDITION_HT","key":"cpu|usage_average","operator":"GT","value":"5.0",\
1170 "valueType":"NUMERIC","instanced":False,"thresholdType":"STATIC"}}}'
1172 # Call update_symptom_defination method under test
1173 symptom_uuid
= self
.mon_plugin
.update_symptom_defination(symptom_defination_id
,
1176 # Verify that mocked method is called with required parameters
1177 m_get_symptom_defination_details
.assert_called_with(symptom_defination_id
)
1178 # m_put.assert_called_with(symptom_defination_id,new_alarm_config)
1180 # Verify return value with expected value
1181 self
.assertEqual(symptom_defination_id
, symptom_uuid
)
1183 @mock.patch
.object(monPlugin
.requests
, 'put')
1184 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_symptom_defination_details')
1185 def test_update_symptom_defination_invalid_symptom_req_response(self
,
1186 m_get_symptom_defination_details
,
1188 """Test update_symptom_defination: If invalid REST response received, return None"""
1190 # Expected symptom to be updated
1191 symptom_defination_id
= 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'
1192 new_alarm_config
= {'severity': "CRITICAL",
1194 'threshold_value': 5,
1195 'alarm_uuid': '9a6d8a14-9f25-4d81-bf91-4d773497444d'
1198 # Set mocked function's return values
1199 m_get_symptom_defination_details
.return_value
= {
1200 "id": "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278",
1201 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",
1202 "adapterKindKey": "VMWARE",
1203 "resourceKindKey": "VirtualMachine",
1206 "state": {"severity": "CRITICAL",
1208 "type": "CONDITION_HT",
1209 "key": "cpu|usage_average", "operator": "GT", "value": "0.0",
1210 "valueType": "NUMERIC", "instanced": False,
1211 "thresholdType": "STATIC"}
1215 m_put
.return_value
.status_code
= 500
1216 m_put
.return_value
.content
= '{\
1217 "message": "Internal Server error, cause unknown.",\
1218 "moreInformation": [\
1219 {"name": "errorMessage",\
1220 "value": "Symptom Definition CPU_Utilization_Above_Thr-e14b203c-\
1221 6bf2-4e2f-a91c-8c19d240eda4 does not exist and hence cannot be updated."},\
1222 {"name": "localizedMessage",\
1223 "value": "Symptom Definition CPU_Utilization_Above_Thr-e14b203c-\
1224 6bf2-4e2f-a91c-8c19d240eda4 does not exist and hence cannot be updated.;"}],\
1225 "httpStatusCode": 500,"apiErrorCode": 500}'
1227 # Call update_symptom_defination method under test
1228 symptom_uuid
= self
.mon_plugin
.update_symptom_defination(symptom_defination_id
,
1231 # Verify that mocked method is called with required parameters
1232 m_get_symptom_defination_details
.assert_called_with(symptom_defination_id
)
1233 m_put
.assert_called()
1235 # Verify return value with expected value
1236 self
.assertEqual(symptom_uuid
, None)
1238 @mock.patch
.object(monPlugin
.requests
, 'put')
1239 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_symptom_defination_details')
1240 def test_update_symptom_defination_failed_to_get_symptom_defination(self
,
1241 m_get_symptom_defination_details
,
1243 """Test update_symptom_defination: if fails to get symptom_defination returns None"""
1245 # Expected symptom to be updated
1246 symptom_defination_id
= 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'
1247 new_alarm_config
= {'severity': "CRITICAL",
1249 'threshold_value': 5,
1250 'alarm_uuid': '9a6d8a14-9f25-4d81-bf91-4d773497444d'
1253 # Set mocked function's return values
1254 m_get_symptom_defination_details
.return_value
= None
1256 # Call update_symptom_defination method under test
1257 symptom_uuid
= self
.mon_plugin
.update_symptom_defination(symptom_defination_id
,
1260 # Verify that mocked method is called with required parameters
1261 m_get_symptom_defination_details
.assert_called_with(symptom_defination_id
)
1262 m_put
.assert_not_called()
1264 # Verify return value with expected value
1265 self
.assertEqual(symptom_uuid
, None)
1267 @mock.patch
.object(monPlugin
.requests
, 'get')
1268 def test_get_symptom_defination_details_valid_req_response(self
, m_get
):
1269 """Test update_symptom_defination: With valid REST response symptom is created"""
1271 # Expected symptom to be updated
1272 symptom_uuid
= 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'
1274 # Set mocked function's return values
1275 m_get
.return_value
.status_code
= 200
1276 m_get
.return_value
.content
= '{\
1277 "id": "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278",\
1278 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1279 "adapterKindKey": "VMWARE","resourceKindKey": "VirtualMachine","waitCycles": 1,\
1280 "cancelCycles": 1,"state": {"severity": "CRITICAL","condition": {"type": "CONDITION_HT",\
1281 "key": "cpu|usage_average","operator": "GT","value": "6.0","valueType": "NUMERIC",\
1282 "instanced": false,"thresholdType": "STATIC"}}}'
1283 expected_symptom_details
= { \
1284 "id": "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278",
1285 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",
1286 "adapterKindKey": "VMWARE", "resourceKindKey": "VirtualMachine", "waitCycles": 1,
1287 "cancelCycles": 1, "state": {"severity": "CRITICAL", "condition": {"type": "CONDITION_HT",
1288 "key": "cpu|usage_average",
1289 "operator": "GT", "value": "6.0",
1290 "valueType": "NUMERIC",
1292 "thresholdType": "STATIC"}}}
1294 # Call update_symptom_defination method under test
1295 symptom_details
= self
.mon_plugin
.get_symptom_defination_details(symptom_uuid
)
1297 # Verify that mocked method is called with required parameters
1298 m_get
.assert_called()
1300 # Verify return value with expected value
1301 self
.assertEqual(expected_symptom_details
, symptom_details
)
1303 @mock.patch
.object(monPlugin
.requests
, 'get')
1304 def test_get_symptom_defination_details_invalid_req_response(self
, m_get
):
1305 """Test update_symptom_defination: if invalid REST response received return None"""
1307 # Expected symptom to be updated
1308 symptom_uuid
= 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'
1310 # Set mocked function's return values
1311 m_get
.return_value
.status_code
= 404
1312 m_get
.return_value
.content
= '{"message": "No such SymptomDefinition\
1313 - SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278.",\
1314 "httpStatusCode": 404,"apiErrorCode": 404}'
1316 expected_symptom_details
= None
1318 # Call update_symptom_defination method under test
1319 symptom_details
= self
.mon_plugin
.get_symptom_defination_details(symptom_uuid
)
1321 # Verify that mocked method is called with required parameters
1322 m_get
.assert_called()
1324 # Verify return value with expected value
1325 self
.assertEqual(expected_symptom_details
, symptom_details
)
1327 @mock.patch
.object(monPlugin
.requests
, 'get')
1328 def test_get_symptom_defination_details_symptom_uuid_not_provided(self
, m_get
):
1329 """Test update_symptom_defination: if required symptom uuid is not provided"""
1331 # Expected symptom to be updated
1333 expected_symptom_details
= None
1335 # Call update_symptom_defination method under test
1336 symptom_details
= self
.mon_plugin
.get_symptom_defination_details(symptom_uuid
)
1338 # Verify that mocked method is called with required parameters
1339 m_get
.assert_not_called()
1341 # Verify return value with expected value
1342 self
.assertEqual(expected_symptom_details
, symptom_details
)
1344 @mock.patch
.object(monPlugin
.requests
, 'put')
1345 def test_reconfigure_alarm_valid_req_response(self
, m_put
):
1346 """Test reconfigure_alarm: for valid REST response"""
1348 # Set input parameters to reconfigure_alarm
1349 alarm_details_json
= {
1350 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d',
1351 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
1352 'description': 'CPU_Utilization_Above_Threshold', 'adapterKindKey': 'VMWARE',
1353 'states': [{'impact': {'impactType': 'BADGE', 'detail': 'risk'}, 'severity': 'CRITICAL',
1354 'base-symptom-set': {
1355 'symptomDefinitionIds': ['SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'],
1356 'relation': 'SELF', 'type': 'SYMPTOM_SET', 'aggregation': 'ALL',
1357 'symptomSetOperator': 'AND'}}],
1358 'type': 16, 'cancelCycles': 1, 'resourceKindKey': 'VirtualMachine', 'subType': 19,
1361 new_alarm_config
= {'severity': 'WARNING',
1362 'description': 'CPU_Utilization_Above_Threshold_Warning'}
1364 # Set mocked function's return values
1365 m_put
.return_value
.status_code
= 200
1366 m_put
.return_value
.content
= '{"id":"AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d",\
1367 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1368 "description":"CPU_Utilization_Above_Threshold_Warning","adapterKindKey":"VMWARE",\
1369 "resourceKindKey":"VirtualMachine","waitCycles":1,"cancelCycles":1,"type":16,\
1370 "subType":19,"states":[{"severity":"WARNING","base-symptom-set":{"type":"SYMPTOM_SET",\
1371 "relation":"SELF","aggregation":"ALL","symptomSetOperator":"AND",\
1372 "symptomDefinitionIds":["SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278"]},\
1373 "impact":{"impactType":"BADGE","detail":"risk"}}]}'
1375 # Expected alarm_def_uuid to be returned
1376 expected_alarm_def_uuid
= '9a6d8a14-9f25-4d81-bf91-4d773497444d'
1378 # Call reconfigure_alarm method under test
1379 alarm_def_uuid
= self
.mon_plugin
.reconfigure_alarm(alarm_details_json
, new_alarm_config
)
1381 # Verify that mocked method is called with required parameters
1382 m_put
.assert_called()
1384 # Verify return value with expected value
1385 self
.assertEqual(expected_alarm_def_uuid
, alarm_def_uuid
)
1387 @mock.patch
.object(monPlugin
.requests
, 'put')
1388 def test_reconfigure_alarm_invalid_req_response(self
, m_put
):
1389 """Test reconfigure_alarm: for invalid REST response, return None"""
1391 # Set input parameters to reconfigure_alarm
1392 alarm_details_json
= {
1393 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d',
1394 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
1395 'description': 'CPU_Utilization_Above_Threshold', 'adapterKindKey': 'VMWARE',
1396 'states': [{'impact': {'impactType': 'BADGE', 'detail': 'risk'}, 'severity': 'CRITICAL',
1397 'base-symptom-set': {
1398 'symptomDefinitionIds': ['SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'],
1399 'relation': 'SELF', 'type': 'SYMPTOM_SET', 'aggregation': 'ALL',
1400 'symptomSetOperator': 'AND'}}],
1401 'type': 16, 'cancelCycles': 1, 'resourceKindKey': 'VirtualMachine', 'subType': 19,
1404 new_alarm_config
= {'severity': 'WARNING',
1405 'description': 'CPU_Utilization_Above_Threshold_Warning'}
1407 # Set mocked function's return values
1408 m_put
.return_value
.status_code
= 500
1409 m_put
.return_value
.content
= '{"message": "Internal Server error, cause unknown.",\
1410 "moreInformation": [{"name": "errorMessage",\
1411 "value": "Cannot update Alert Definition CPU_Utilization_Above_Thr-\
1412 e14b203c-6bf2-4e2f-a91c-8c19d240eda4 since it does not exist"},\
1413 {"name": "localizedMessage",\
1414 "value": "Cannot update Alert Definition CPU_Utilization_Above_Thr-\
1415 e14b203c-6bf2-4e2f-a91c-8c19d240eda4 since it does not exist;"}],\
1416 "httpStatusCode": 500,"apiErrorCode": 500}'
1418 # Expected alarm_def_uuid to be returned
1419 expected_alarm_def_uuid
= None
1421 # Call reconfigure_alarm method under test
1422 alarm_def_uuid
= self
.mon_plugin
.reconfigure_alarm(alarm_details_json
, new_alarm_config
)
1424 # Verify that mocked method is called with required parameters
1425 m_put
.assert_called()
1427 # Verify return value with expected value
1428 self
.assertEqual(expected_alarm_def_uuid
, alarm_def_uuid
)
1430 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_symptom_definition')
1431 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_alarm_defination')
1432 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_notification_rule')
1433 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
1434 def test_delete_alarm_configuration_successful_alarm_deletion(self
,
1435 m_get_alarm_defination_details
,
1436 m_delete_notification_rule
,
1437 m_delete_alarm_defination
,
1438 m_delete_symptom_definition
):
1439 """Test delete_alarm_configuration: for successful alarm deletion, return alarm uuid"""
1441 # Set input parameters to delete_alarm_configuration
1442 delete_alarm_req_dict
= {'alarm_uuid': '9a6d8a14-9f25-4d81-bf91-4d773497444d'}
1444 # Set mocked function's return values
1445 alarm_details_json
= {
1446 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1447 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1448 'symptomDefinitionIds': ['SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278']}
1450 'alarm_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1451 'alarm_id': 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1452 'symptom_definition_id': 'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'}
1454 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
1455 m_delete_notification_rule
.return_value
= '989e7293-d78d-4405-92e30ec4f247'
1456 m_delete_alarm_defination
.return_value
= alarm_details
['alarm_id']
1457 m_delete_symptom_definition
.return_value
= alarm_details
['symptom_definition_id']
1459 # Call reconfigure_alarm method under test
1460 alarm_uuid
= self
.mon_plugin
.delete_alarm_configuration(delete_alarm_req_dict
)
1462 # Verify that mocked method is called with required parameters
1463 m_get_alarm_defination_details
.assert_called_with(delete_alarm_req_dict
['alarm_uuid'])
1464 m_delete_notification_rule
.assert_called_with(alarm_details
['alarm_name'])
1465 m_delete_alarm_defination
.assert_called_with(alarm_details
['alarm_id'])
1466 m_delete_symptom_definition
.assert_called_with(alarm_details
['symptom_definition_id'])
1468 # Verify return value with expected value
1469 self
.assertEqual(alarm_uuid
, delete_alarm_req_dict
['alarm_uuid'])
1471 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_symptom_definition')
1472 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_alarm_defination')
1473 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_notification_rule')
1474 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
1475 def test_delete_alarm_configuration_failed_to_get_alarm_defination(self
,
1476 m_get_alarm_defination_details
,
1477 m_delete_notification_rule
,
1478 m_delete_alarm_defination
,
1479 m_delete_symptom_definition
):
1480 """Test delete_alarm_configuration: if failed to get alarm definition, return None"""
1482 # Set input parameters to delete_alarm_configuration
1483 delete_alarm_req_dict
= {'alarm_uuid': '9a6d8a14-9f25-4d81-bf91-4d773497444d'}
1485 # Set mocked function's return values
1486 alarm_details_json
= None
1487 alarm_details
= None
1489 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
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_not_called()
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)
1503 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_symptom_definition')
1504 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_alarm_defination')
1505 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_notification_rule')
1506 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
1507 def test_delete_alarm_configuration_failed_to_delete_notification_rule(self
,
1508 m_get_alarm_defination_details
,
1509 m_delete_notification_rule
,
1510 m_delete_alarm_defination
,
1511 m_delete_symptom_definition
):
1512 """Test delete_alarm_configuration: if failed to delete notification rule, return None"""
1514 # Set input parameters to delete_alarm_configuration
1515 delete_alarm_req_dict
= {'alarm_uuid': '9a6d8a14-9f25-4d81-bf91-4d773497444d'}
1517 # Set mocked function's return values
1518 alarm_details_json
= {
1519 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1520 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1521 'symptomDefinitionIds': ['SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278']}
1523 'alarm_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1524 'alarm_id': 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1525 'symptom_definition_id': 'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'}
1527 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
1528 m_delete_notification_rule
.return_value
= None
1530 # Call reconfigure_alarm method under test
1531 alarm_uuid
= self
.mon_plugin
.delete_alarm_configuration(delete_alarm_req_dict
)
1533 # Verify that mocked method is called with required parameters
1534 m_get_alarm_defination_details
.assert_called_with(delete_alarm_req_dict
['alarm_uuid'])
1535 m_delete_notification_rule
.assert_called_with(alarm_details
['alarm_name'])
1536 m_delete_alarm_defination
.assert_not_called()
1537 m_delete_symptom_definition
.assert_not_called()
1539 # Verify return value with expected value
1540 self
.assertEqual(alarm_uuid
, None)
1542 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_symptom_definition')
1543 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_alarm_defination')
1544 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_notification_rule')
1545 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
1546 def test_delete_alarm_configuration_failed_to_delete_alarm_defination(self
,
1547 m_get_alarm_defination_details
,
1548 m_delete_notification_rule
,
1549 m_delete_alarm_defination
,
1550 m_delete_symptom_definition
):
1551 """Test delete_alarm_configuration: if failed to delete alarm definition, return None"""
1553 # Set input parameters to delete_alarm_configuration
1554 delete_alarm_req_dict
= {'alarm_uuid': '9a6d8a14-9f25-4d81-bf91-4d773497444d'}
1556 # Set mocked function's return values
1557 alarm_details_json
= {
1558 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1559 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1560 'symptomDefinitionIds': ['SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278']}
1562 'alarm_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1563 'alarm_id': 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1564 'symptom_definition_id': 'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'}
1566 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
1567 m_delete_notification_rule
.return_value
= '989e7293-d78d-4405-92e30ec4f247'
1568 m_delete_alarm_defination
.return_value
= None
1570 # Call reconfigure_alarm method under test
1571 alarm_uuid
= self
.mon_plugin
.delete_alarm_configuration(delete_alarm_req_dict
)
1573 # Verify that mocked method is called with required parameters
1574 m_get_alarm_defination_details
.assert_called_with(delete_alarm_req_dict
['alarm_uuid'])
1575 m_delete_notification_rule
.assert_called_with(alarm_details
['alarm_name'])
1576 m_delete_alarm_defination
.assert_called_with(alarm_details
['alarm_id'])
1577 m_delete_symptom_definition
.assert_not_called()
1579 # Verify return value with expected value
1580 self
.assertEqual(alarm_uuid
, None)
1582 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_symptom_definition')
1583 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_alarm_defination')
1584 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_notification_rule')
1585 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
1586 def test_delete_alarm_configuration_failed_to_delete_symptom_definition(self
,
1587 m_get_alarm_defination_details
,
1588 m_delete_notification_rule
,
1589 m_delete_alarm_defination
,
1590 m_delete_symptom_definition
):
1591 """Test delete_alarm_configuration: if failed to delete symptom definition, return None"""
1593 # Set input parameters to delete_alarm_configuration
1594 delete_alarm_req_dict
= {'alarm_uuid': '9a6d8a14-9f25-4d81-bf91-4d773497444d'}
1596 # Set mocked function's return values
1597 alarm_details_json
= {
1598 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1599 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1600 'symptomDefinitionIds': ['SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278']}
1602 'alarm_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1603 'alarm_id': 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1604 'symptom_definition_id': 'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'}
1606 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
1607 m_delete_notification_rule
.return_value
= '989e7293-d78d-4405-92e30ec4f247'
1608 m_delete_alarm_defination
.return_value
= alarm_details
['alarm_id']
1609 m_delete_symptom_definition
.return_value
= None
1611 # Call reconfigure_alarm method under test
1612 alarm_uuid
= self
.mon_plugin
.delete_alarm_configuration(delete_alarm_req_dict
)
1614 # Verify that mocked method is called with required parameters
1615 m_get_alarm_defination_details
.assert_called_with(delete_alarm_req_dict
['alarm_uuid'])
1616 m_delete_notification_rule
.assert_called_with(alarm_details
['alarm_name'])
1617 m_delete_alarm_defination
.assert_called_with(alarm_details
['alarm_id'])
1618 m_delete_symptom_definition
.assert_called_with(alarm_details
['symptom_definition_id'])
1620 # Verify return value with expected value
1621 self
.assertEqual(alarm_uuid
, None)
1623 @mock.patch
.object(monPlugin
.requests
, 'delete')
1624 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_notification_rule_id_by_alarm_name')
1625 def test_delete_notification_rule_successful_deletion_req_response(self
,
1626 m_get_notification_rule_id_by_alarm_name
,
1628 """Test delete_notification_rule: Valid notification rule is deleted & returns rule_id"""
1630 # Set input parameters to delete_notification_rule
1631 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4'
1633 # Set mocked function's return values
1634 m_get_notification_rule_id_by_alarm_name
.return_value
= '8db86441-71d8-4830-9e1a-a90be3776d12'
1635 m_delete
.return_value
.status_code
= 204
1637 # Call delete_notification_rule method under test
1638 rule_id
= self
.mon_plugin
.delete_notification_rule(alarm_name
)
1640 # Verify that mocked method is called with required parameters
1641 m_get_notification_rule_id_by_alarm_name
.assert_called_with(alarm_name
)
1642 m_delete
.assert_called()
1644 # Verify return value with expected value
1645 self
.assertEqual(rule_id
, '8db86441-71d8-4830-9e1a-a90be3776d12')
1647 @mock.patch
.object(monPlugin
.requests
, 'delete')
1648 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_notification_rule_id_by_alarm_name')
1649 def test_delete_notification_rule_failed_to_get_notification_rule_id(self
,
1650 m_get_notification_rule_id_by_alarm_name
,
1652 """Test delete_notification_rule: if notification rule is not found, returns None"""
1654 # Set input parameters to delete_notification_rule
1655 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4'
1657 # Set mocked function's return values
1658 m_get_notification_rule_id_by_alarm_name
.return_value
= None
1660 # Call delete_notification_rule method under test
1661 rule_id
= self
.mon_plugin
.delete_notification_rule(alarm_name
)
1663 # Verify that mocked method is called with required parameters
1664 m_get_notification_rule_id_by_alarm_name
.assert_called_with(alarm_name
)
1665 m_delete
.assert_not_called()
1667 # verify return value with expected value
1668 self
.assertEqual(rule_id
, None)
1670 @mock.patch
.object(monPlugin
.requests
, 'delete')
1671 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_notification_rule_id_by_alarm_name')
1672 def test_delete_notification_rule_invalid_deletion_req_response(self
,
1673 m_get_notification_rule_id_by_alarm_name
,
1675 """Test delete_notification_rule: If an invalid response is received, returns None"""
1677 # Set input parameters to delete_notification_rule
1678 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4'
1680 # Set mocked function's return values
1681 m_get_notification_rule_id_by_alarm_name
.return_value
= '8db86441-71d8-4830-9e1a-a90be3776d12'
1682 m_delete
.return_value
.status_code
= 404
1684 # Call delete_notification_rule method under test
1685 rule_id
= self
.mon_plugin
.delete_notification_rule(alarm_name
)
1687 # Verify that mocked method is called with required parameters
1688 m_get_notification_rule_id_by_alarm_name
.assert_called_with(alarm_name
)
1689 m_delete
.assert_called()
1691 # Verify return value with expected value
1692 self
.assertEqual(rule_id
, None)
1694 @mock.patch
.object(monPlugin
.requests
, 'get')
1695 def test_get_notification_rule_id_by_alarm_name_valid_req_response(self
, m_get
):
1696 """Test get_notification_rule_id_by_alarm_name: A valid request response received,
1697 returns notification_id
1700 # Set input parameters to get_notification_rule_id_by_alarm_name
1701 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
1703 # Set mocked function's return values
1704 m_get
.return_value
.status_code
= 200
1705 m_get
.return_value
.content
= '{\
1706 "pageInfo": {"totalCount": 0,"page": 0,"pageSize": 1000},\
1708 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1709 "rel": "SELF","name": "current"},\
1710 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1711 "rel": "RELATED","name": "first"},\
1712 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1713 "rel": "RELATED","name": "last"}],\
1714 "notification-rule": [{\
1715 "id": "2b86fa23-0c15-445c-a2b1-7bd725c46f59",\
1716 "name": "notify_CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1717 "pluginId": "03053f51-f829-438d-993d-cc33a435d76a",\
1718 "links": [{"href": "/suite-api/api/notifications/rules/2b86fa23-0c15-445c-a2b1-7bd725c46f59",\
1719 "rel": "SELF","name": "linkToSelf"}]}]}'
1721 # Call get_notification_rule_id_by_alarm_name method under test
1722 notification_id
= self
.mon_plugin
.get_notification_rule_id_by_alarm_name(alarm_name
)
1724 # Verify that mocked method is called with required parameters
1725 m_get
.assert_called()
1727 # Verify return value with expected value
1728 self
.assertEqual(notification_id
, '2b86fa23-0c15-445c-a2b1-7bd725c46f59')
1730 @mock.patch
.object(monPlugin
.requests
, 'get')
1731 def test_get_notification_rule_id_by_alarm_name_invalid_req_response(self
, m_get
):
1732 """Test get_notification_rule_id_by_alarm_name: If an invalid response received,\
1736 # Set input parameters to delete_alarm_configuration
1737 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
1739 # Set mocked function's return values
1740 m_get
.return_value
.status_code
= 404
1742 # Call get_notification_rule_id_by_alarm_name method under test
1743 notification_id
= self
.mon_plugin
.get_notification_rule_id_by_alarm_name(alarm_name
)
1745 # Verify that mocked method is called with required parameters
1746 m_get
.assert_called()
1748 # Verify return value with expected value
1749 self
.assertEqual(notification_id
, None)
1751 @mock.patch
.object(monPlugin
.requests
, 'get')
1752 def test_get_notification_rule_id_by_alarm_name_rule_not_found(self
, m_get
):
1753 """Test get_notification_rule_id_by_alarm_name: If a notification rule is not found,
1757 # Set input parameters to delete_alarm_configuration
1758 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda'
1760 # Set mocked function's return values
1761 m_get
.return_value
.status_code
= 200
1762 m_get
.return_value
.content
= '{\
1763 "pageInfo": {"totalCount": 0,"page": 0,"pageSize": 1000},\
1765 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1766 "rel": "SELF","name": "current"},\
1767 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1768 "rel": "RELATED","name": "first"},\
1769 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1770 "rel": "RELATED","name": "last"}],\
1771 "notification-rule": [{\
1772 "id": "2b86fa23-0c15-445c-a2b1-7bd725c46f59",\
1773 "name": "notify_CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1774 "pluginId": "03053f51-f829-438d-993d-cc33a435d76a",\
1775 "links": [{"href": "/suite-api/api/notifications/rules/2b86fa23-0c15-445c-a2b1-7bd725c46f59",\
1776 "rel": "SELF","name": "linkToSelf"}]}]}'
1778 # Call get_notification_rule_id_by_alarm_name method under test
1779 notification_id
= self
.mon_plugin
.get_notification_rule_id_by_alarm_name(alarm_name
)
1781 # Verify that mocked method is called with required parameters
1782 m_get
.assert_called()
1784 # Verify return value with expected value
1785 self
.assertEqual(notification_id
, None)
1787 @mock.patch
.object(monPlugin
.requests
, 'delete')
1788 def test_delete_alarm_defination_valid_req_response(self
, m_delete
):
1789 """Test delete_alarm_defination: A valid request response received,
1793 # Set input parameters to delete_alarm_definition
1794 alarm_definition_id
= 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d'
1796 # Set mocked function's return values
1797 m_delete
.return_value
.status_code
= 204
1799 # Call delete_alarm_defination method under test
1800 actual_alarm_id
= self
.mon_plugin
.delete_alarm_defination(alarm_definition_id
)
1802 # Verify that mocked method is called with required parameters
1803 m_delete
.assert_called()
1805 # Verify return value with expected value
1806 self
.assertEqual(actual_alarm_id
, alarm_definition_id
)
1808 @mock.patch
.object(monPlugin
.requests
, 'delete')
1809 def test_delete_alarm_defination_invalid_req_response(self
, m_delete
):
1810 """Test delete_alarm_defination: If an invalid request response received,
1814 # Set input parameters to delete_alarm_definition
1815 alarm_definition_id
= 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d'
1817 # Set mocked function's return values
1818 m_delete
.return_value
.status_code
= 404
1820 # Call delete_alarm_defination method under test
1821 actual_alarm_id
= self
.mon_plugin
.delete_alarm_defination(alarm_definition_id
)
1823 # Verify that mocked method is called with required parameters
1824 m_delete
.assert_called()
1826 # Verify return value with expected value
1827 self
.assertEqual(actual_alarm_id
, None)
1829 @mock.patch
.object(monPlugin
.requests
, 'delete')
1830 def test_delete_symptom_definition_valid_req_response(self
, m_delete
):
1831 """Test delete_symptom_definition: A valid request response received,
1835 # Set input parameters to delete_symptom_definition
1836 symptom_definition_id
= 'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'
1838 # Set mocked function's return values
1839 m_delete
.return_value
.status_code
= 204
1841 # Call delete_symptom_definition method under test
1842 actual_symptom_id
= self
.mon_plugin
.delete_symptom_definition(symptom_definition_id
)
1844 # Verify that mocked method is called with required parameters
1845 m_delete
.assert_called()
1847 # Verify return value with expected value
1848 self
.assertEqual(actual_symptom_id
, symptom_definition_id
)
1850 @mock.patch
.object(monPlugin
.requests
, 'delete')
1851 def test_delete_symptom_definition_invalid_req_response(self
, m_delete
):
1852 """Test delete_symptom_definition: If an invalid request response received,
1856 # Set input parameters to delete_symptom_definition
1857 symptom_definition_id
= 'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'
1859 # Set mocked function's return values
1860 m_delete
.return_value
.status_code
= 404
1862 # Call delete_symptom_definition method under test
1863 actual_symptom_id
= self
.mon_plugin
.delete_symptom_definition(symptom_definition_id
)
1865 # Verify that mocked method is called with required parameters
1866 m_delete
.assert_called()
1868 # Verify return value with expected value
1869 self
.assertEqual(actual_symptom_id
, None)
1871 @mock.patch
.object(monPlugin
.requests
, 'post')
1872 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
1873 def test_configure_rest_plugin_valid_plugin_id(self
, m_check_if_plugin_configured
, m_post
):
1874 """Test configure rest plugin method-valid plugin id"""
1876 # mock return values
1877 expected_return
= m_check_if_plugin_configured
.return_value
= "mock_pluginid"
1879 # call configure rest plugin method under test
1880 actual_return
= self
.mon_plugin
.configure_rest_plugin()
1882 # verify that mocked method is called
1883 m_check_if_plugin_configured
.assert_called()
1884 m_post
.assert_not_called()
1886 # verify return value with expected value
1887 self
.assertEqual(expected_return
, actual_return
)
1889 @mock.patch
.object(monPlugin
.MonPlugin
, 'enable_rest_plugin')
1890 @mock.patch
.object(monPlugin
.requests
, 'post')
1891 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
1892 def est_configure_rest_plugin_invalid_plugin_id(self
, m_check_if_plugin_configured
, m_post
, m_enable_rest_plugin
):
1893 """Test configure rest plugin method-invalid plugin id"""
1895 # mock return values
1896 m_check_if_plugin_configured
.return_value
= None # not configured
1897 m_post
.return_value
.status_code
= 201 # success
1898 m_post
.return_value
.content
= '{"pluginTypeId":"RestPlugin","pluginId":"1ef15663-9739-49fe-8c41-022bcc9f690c",\
1899 "name":"MON_module_REST_Plugin","version":1518693747871,"enabled":false,\
1900 "configValues":[{"name":"Url","value":"https://MON.lxd:8080/notify/"},\
1901 {"name":"Content-type","value":"application/json"},{"name":"Certificate",\
1902 "value":"AA:E7:3E:A5:34:E0:25:FB:28:84:3B:74:B2:18:74:C0:C3:E8:26:50"},\
1903 {"name":"ConnectionCount","value":"20"}]}'
1905 m_enable_rest_plugin
.return_value
= True # success
1906 expected_return
= '1ef15663-9739-49fe-8c41-022bcc9f690c'
1908 # call configure rest plugin method under test
1909 actual_return
= self
.mon_plugin
.configure_rest_plugin()
1911 # verify that mocked method is called
1912 m_check_if_plugin_configured
.assert_called()
1913 m_post
.assert_called()
1914 m_enable_rest_plugin
.assert_called_with('1ef15663-9739-49fe-8c41-022bcc9f690c', 'MON_module_REST_Plugin')
1916 # verify return value with expected value
1917 self
.assertEqual(expected_return
, actual_return
)
1919 @mock.patch
.object(monPlugin
.MonPlugin
, 'enable_rest_plugin')
1920 @mock.patch
.object(monPlugin
.requests
, 'post')
1921 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
1922 def est_configure_rest_plugin_failed_to_enable_plugin(self
, m_check_if_plugin_configured
, m_post
,
1923 m_enable_rest_plugin
):
1924 """Test configure rest plugin method-failed to enable plugin case"""
1926 # mock return values
1927 m_check_if_plugin_configured
.return_value
= None # not configured
1928 m_post
.return_value
.status_code
= 201 # success
1929 m_post
.return_value
.content
= '{"pluginTypeId":"RestPlugin","pluginId":"1ef15663-9739-49fe-8c41-022bcc9f690c",\
1930 "name":"MON_module_REST_Plugin","version":1518693747871,"enabled":false,\
1931 "configValues":[{"name":"Url","value":"https://MON.lxd:8080/notify/"},\
1932 {"name":"Content-type","value":"application/json"},{"name":"Certificate",\
1933 "value":"AA:E7:3E:A5:34:E0:25:FB:28:84:3B:74:B2:18:74:C0:C3:E8:26:50"},\
1934 {"name":"ConnectionCount","value":"20"}]}'
1936 m_enable_rest_plugin
.return_value
= False # return failure
1937 expected_return
= None
1939 # call configure rest plugin method under test
1940 actual_return
= self
.mon_plugin
.configure_rest_plugin()
1942 # verify that mocked method is called
1943 m_check_if_plugin_configured
.assert_called()
1944 m_post
.assert_called()
1945 m_enable_rest_plugin
.assert_called_with('1ef15663-9739-49fe-8c41-022bcc9f690c', 'MON_module_REST_Plugin')
1947 # verify return value with expected value
1948 self
.assertEqual(expected_return
, actual_return
)
1950 @mock.patch
.object(monPlugin
.requests
, 'get')
1951 def test_check_if_plugin_configured_valid_req_response(self
, m_get
):
1952 """Test check if plugin configured method-valid request response"""
1954 plugin_name
= 'MON_module_REST_Plugin'
1955 # mock return values
1956 m_get
.return_value
.status_code
= 200
1957 expected_return
= '1ef15663-9739-49fe-8c41-022bcc9f690c'
1958 m_get
.return_value
.content
= '{"notificationPluginInstances":\
1959 [{"pluginTypeId":"RestPlugin",\
1960 "pluginId":"1ef15663-9739-49fe-8c41-022bcc9f690c",\
1961 "name":"MON_module_REST_Plugin","version":1518694966987,\
1962 "enabled":true,"configValues":[{"name":"Url",\
1963 "value":"https://MON.lxd:8080/notify/"},\
1964 {"name":"Content-type","value":"application/json"},\
1965 {"name":"Certificate",\
1966 "value":"AA:E7:3E:A5:34:E0:25:FB:28:84:3B:74:B2:18:74:C0"},\
1967 {"name":"ConnectionCount","value":"20"}]}]}'
1969 # call check if plugin configured method under test
1970 actual_return
= self
.mon_plugin
.check_if_plugin_configured(plugin_name
)
1972 # verify that mocked method is called
1973 m_get
.assert_called()
1975 # verify return value with expected value
1976 self
.assertEqual(expected_return
, actual_return
)
1978 @mock.patch
.object(monPlugin
.requests
, 'get')
1979 def test_check_if_plugin_configured_invalid_req_response(self
, m_get
):
1980 """Test check if plugin configured method-invalid request response"""
1982 plugin_name
= 'MON_module_REST_Plugin'
1983 # mock return values
1984 m_get
.return_value
.status_code
= 201
1985 expected_return
= None
1986 m_get
.return_value
.content
= '{"notificationPluginInstances":\
1987 [{"pluginTypeId":"RestPlugin",\
1988 "pluginId":"1ef15663-9739-49fe-8c41-022bcc9f690c",\
1989 "name":"MON_module_REST_Plugin","version":1518694966987,\
1990 "enabled":true,"configValues":[{"name":"Url",\
1991 "value":"https://MON.lxd:8080/notify/"},\
1992 {"name":"Content-type","value":"application/json"},\
1993 {"name":"Certificate",\
1994 "value":"AA:E7:3E:A5:34:E0:25:FB:28:84:3B:74:B2:18:74:C0"},\
1995 {"name":"ConnectionCount","value":"20"}]}]}'
1997 # call check if plugin configured method under test
1998 actual_return
= self
.mon_plugin
.check_if_plugin_configured(plugin_name
)
2000 # verify that mocked method is called
2001 m_get
.assert_called()
2003 # verify return value with expected value
2004 self
.assertEqual(expected_return
, actual_return
)
2006 @mock.patch
.object(monPlugin
.requests
, 'put')
2007 def test_enable_rest_plugin_valid_req_response(self
, m_put
):
2008 """Test enable rest plugin method-valid request response"""
2010 plugin_name
= 'MON_module_REST_Plugin'
2011 plugin_id
= '1ef15663-9739-49fe-8c41-022bcc9f690c'
2012 # mock return values
2013 m_put
.return_value
.status_code
= 204
2014 expected_return
= True
2015 m_put
.return_value
.content
= ''
2017 # call enable rest plugin configured method under test
2018 actual_return
= self
.mon_plugin
.enable_rest_plugin(plugin_id
, plugin_name
)
2020 # verify that mocked method is called
2021 m_put
.assert_called()
2023 # verify return value with expected value
2024 self
.assertEqual(expected_return
, actual_return
)
2026 @mock.patch
.object(monPlugin
.requests
, 'put')
2027 def test_enable_rest_plugin_invalid_req_response(self
, m_put
):
2028 """Test enable rest plugin method-invalid request response"""
2030 plugin_name
= 'MON_module_REST_Plugin'
2031 plugin_id
= '08018c0f-8879-4ca1-9b92-00e22d2ff81b' # invalid plugin id
2032 # mock return values
2033 m_put
.return_value
.status_code
= 404 # api Error code
2034 expected_return
= False
2035 m_put
.return_value
.content
= '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ops:\
2036 error xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \
2037 xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ops=\
2038 "http://webservice.vmware.com/vRealizeOpsMgr/1.0/" \
2039 httpStatusCode="404" apiErrorCode="404"><ops:message>\
2040 No such Notification Plugin - 08018c0f-8879-4ca1-9b92-\
2041 00e22d2ff81b.</ops:message></ops:error>'
2043 # call enable rest plugin configured method under test
2044 actual_return
= self
.mon_plugin
.enable_rest_plugin(plugin_id
, plugin_name
)
2046 # verify that mocked method is called
2047 m_put
.assert_called()
2049 # verify return value with expected value
2050 self
.assertEqual(expected_return
, actual_return
)
2052 @mock.patch
.object(monPlugin
.requests
, 'post')
2053 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
2054 def test_create_alarm_notification_rule_valid_req(self
, m_check_if_plugin_configured
, m_post
):
2055 """Test create alarm notification rule method valid request response"""
2057 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2058 alarm_id
= 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14'
2059 res_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2060 expected_return
= "8db86441-71d8-4830-9e1a-a90be3776d12"
2062 # mock return values
2063 m_check_if_plugin_configured
.return_value
= '03053f51-f829-438d-993d-cc33a435d76a'
2064 m_post
.return_value
.status_code
= 201
2065 m_post
.return_value
.content
= '{"id":"8db86441-71d8-4830-9e1a-a90be3776d12",\
2066 "name":"notify_CPU_Utilization_Above_Thr-e14b203c",\
2067 "pluginId":"03053f51-f829-438d-993d-cc33a435d76a",\
2068 "alertControlStates":[],"alertStatuses":[],\
2069 "resourceFilter":{"matchResourceIdOnly":true,\
2070 "childrenResourceKindFilters":[],\
2071 "resourceId":"ac87622f-b761-40a0-b151-00872a2a456e"},\
2072 "alertTypeFilters":[],"alertDefinitionIdFilters":{"values":[\
2073 "AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14"]}}'
2075 # call enable rest plugin configured method under test
2076 actual_return
= self
.mon_plugin
.create_alarm_notification_rule(alarm_name
, alarm_id
, res_id
)
2078 # verify that mocked method is called
2079 m_check_if_plugin_configured
.assert_called_with('MON_module_REST_Plugin')
2080 m_post
.assert_called()
2082 # verify return value with expected value
2083 self
.assertEqual(expected_return
, actual_return
)
2085 @mock.patch
.object(monPlugin
.requests
, 'post')
2086 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
2087 def test_create_alarm_notification_rule_invalid_req(self
, m_check_if_plugin_configured
, m_post
):
2088 """Test create alarm notification rule method invalid request response"""
2090 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2091 alarm_id
= 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14'
2092 res_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2093 expected_return
= None # invalid req should retrun none
2095 # mock return values
2096 m_check_if_plugin_configured
.return_value
= '03053f51-f829-438d-993d-cc33a435d76a'
2097 m_post
.return_value
.status_code
= 500
2098 m_post
.return_value
.content
= '{"message":"Internal Server error, cause unknown.",\
2099 "moreInformation":[{"name":"errorMessage","value":\
2100 "there is already a rule with the same rule name"},\
2101 {"name":"localizedMessage","value":"there is already \
2102 a rule with the same rule name;"}],"httpStatusCode":500,\
2103 "apiErrorCode":500}'
2105 # call enable rest plugin configured method under test
2106 actual_return
= self
.mon_plugin
.create_alarm_notification_rule(alarm_name
, alarm_id
, res_id
)
2108 # verify that mocked method is called
2109 m_check_if_plugin_configured
.assert_called_with('MON_module_REST_Plugin')
2110 m_post
.assert_called()
2112 # verify return value with expected value
2113 self
.assertEqual(expected_return
, actual_return
)
2115 @mock.patch
.object(monPlugin
.requests
, 'post')
2116 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
2117 def test_create_alarm_notification_rule_failed_to_get_plugin_id(self
,
2118 m_check_if_plugin_configured
, m_post
):
2119 """Test create alarm notification rule method invalid plugin id"""
2121 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2122 alarm_id
= 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14'
2123 res_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2124 expected_return
= None # invalid req should retrun none
2126 # mock return values
2127 m_check_if_plugin_configured
.return_value
= None
2129 # call enable rest plugin configured method under test
2130 actual_return
= self
.mon_plugin
.create_alarm_notification_rule(alarm_name
, alarm_id
, res_id
)
2132 # verify that mocked method is called
2133 m_check_if_plugin_configured
.assert_called_with('MON_module_REST_Plugin')
2134 m_post
.assert_not_called()
2136 # verify return value with expected value
2137 self
.assertEqual(expected_return
, actual_return
)
2139 @mock.patch
.object(monPlugin
.requests
, 'get')
2140 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2141 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2142 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2143 def test_get_metrics_data_valid_rest_req_response(self
, m_get_default_Params
,
2145 m_get_vm_resource_id
,
2147 """Test get metrics data of resource method valid request response"""
2149 metrics
= {'collection_period': 1, 'metric_name': 'CPU_UTILIZATION', 'metric_uuid': None,
2150 'schema_version': 1.0, 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2151 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2152 'schema_type': 'read_metric_data_request', 'vim_type': 'VMware',
2153 'collection_unit': 'HR', 'vim_uuid': '1'}
2156 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2157 vm_moref_id
= m_get_vm_moref_id
.return_value
= 'vm-6626'
2158 m_get_vm_resource_id
.return_value
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2159 m_get
.return_value
.status_code
= 200
2160 m_get
.return_value
.content
= '{"values":[{"resourceId":"ac87622f-b761-40a0-b151-\
2161 00872a2a456e","stat-list":{"stat":[{"timestamps":\
2162 [1519716874297,1519717174294,1519717474295,1519717774298,\
2163 1519718074300,1519718374299,1519718674314,1519718974325,\
2164 1519719274304,1519719574298,1519719874298,1519720174301],\
2165 "statKey":{"key":"cpu|usage_average"},"intervalUnit":\
2166 {"quantifier":1},"data":[0.1120000034570694,\
2167 0.11866666376590729,0.11599999666213989,0.11400000005960464,\
2168 0.12066666781902313,0.11533333361148834,0.11800000071525574,\
2169 0.11533333361148834,0.12200000137090683,0.11400000005960464,\
2170 0.1459999978542328,0.12133333086967468]}]}}]}'
2172 # call get matrics data method under test
2173 actual_return
= self
.mon_plugin
.get_metrics_data(metrics
)
2175 # verify that mocked method is called
2176 m_get_default_Params
.assert_called_with(metrics
['metric_name'])
2177 m_get_vm_moref_id
.assert_called_with(metrics
['resource_uuid'])
2178 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
2179 m_get
.assert_called()
2181 # verify return value with expected value
2182 # self.assertEqual(expected_return, actual_return)
2184 @mock.patch
.object(monPlugin
.requests
, 'get')
2185 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2186 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2187 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2188 def test_get_metrics_data_invalid_rest_req_response(self
, m_get_default_Params
,
2190 m_get_vm_resource_id
,
2192 """Test get metrics data of resource method invalid request response"""
2194 metrics
= {'collection_period': 1, 'metric_name': 'CPU_UTILIZATION', 'metric_uuid': None,
2195 'schema_version': 1.0, 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2196 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2197 'schema_type': 'read_metric_data_request', 'vim_type': 'VMware',
2198 'collection_unit': 'HR', 'vim_uuid': '1'}
2201 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2202 vm_moref_id
= m_get_vm_moref_id
.return_value
= 'vm-6626'
2203 m_get_vm_resource_id
.return_value
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2204 m_get
.return_value
.status_code
= 400
2205 expected_return
= {'metric_name': 'CPU_UTILIZATION', 'metric_uuid': '0',
2206 'schema_version': '1.0',
2207 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2208 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2209 'metrics_data': {'time_series': [], 'metrics_series': []},
2210 'schema_type': 'read_metric_data_response',
2211 'unit': '%', 'vim_uuid': '1'}
2213 # call get metrics data method under test
2214 actual_return
= self
.mon_plugin
.get_metrics_data(metrics
)
2216 # verify that mocked method is called
2217 m_get_default_Params
.assert_called_with(metrics
['metric_name'])
2218 m_get_vm_moref_id
.assert_called_with(metrics
['resource_uuid'])
2219 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
2220 m_get
.assert_called()
2222 m_get
.return_value
.content
= '{"message":"Invalid request... #1 violations found.",\
2223 "validationFailures":[{"failureMessage":"Invalid Parameter",\
2224 "violationPath":"end"}],"httpStatusCode":400,\
2225 "apiErrorCode":400}'
2227 # verify return value with expected value
2228 self
.assertEqual(expected_return
, actual_return
)
2230 @mock.patch
.object(monPlugin
.requests
, 'get')
2231 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2232 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2233 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2234 def test_get_metrics_data_metric_not_supported(self
, m_get_default_Params
,
2236 m_get_vm_resource_id
,
2238 """Test get metrics data of resource method invalid metric name"""
2240 metrics
= {'collection_period': 1, 'metric_name': 'invalid_metric', 'metric_uuid': None,
2241 'schema_version': 1.0,
2242 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2243 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2244 'schema_type': 'read_metric_data_request', 'vim_type': 'VMware',
2245 'collection_unit': 'HR', 'vim_uuid': '1'}
2248 m_get_default_Params
.return_value
= {} # returns empty dict
2250 expected_return
= {'metric_name': 'invalid_metric', 'metric_uuid': '0', 'vim_uuid': '1',
2251 'schema_version': '1.0', 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2252 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2253 'metrics_data': {'time_series': [], 'metrics_series': []},
2254 'schema_type': 'read_metric_data_response', 'unit': None}
2256 # call get matrics data method under test
2257 actual_return
= self
.mon_plugin
.get_metrics_data(metrics
)
2259 # verify that mocked method is called/not called
2260 m_get_default_Params
.assert_called_with(metrics
['metric_name'])
2261 m_get_vm_moref_id
.assert_not_called()
2262 m_get_vm_resource_id
.assert_not_called()
2263 m_get
.assert_not_called()
2265 # verify return value with expected value
2266 self
.assertEqual(expected_return
, actual_return
)
2268 @mock.patch
.object(monPlugin
.requests
, 'get')
2269 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2270 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2271 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2272 def test_get_metrics_data_failed_to_get_vm_moref_id(self
, m_get_default_Params
,
2274 m_get_vm_resource_id
,
2276 """Test get metrics data method negative scenario- invalid resource id"""
2278 metrics
= {'collection_period': 1, 'metric_name': 'cpu_utilization', 'metric_uuid': None,
2279 'schema_version': 1.0, 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2280 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2281 'schema_type': 'read_metric_data_request', 'vim_type': 'VMware',
2282 'collection_unit': 'HR', 'vim_uuid': '1'}
2285 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2286 m_get_vm_moref_id
.return_value
= None
2287 expected_return
= {'metric_name': 'cpu_utilization', 'metric_uuid': '0',
2288 'schema_version': '1.0',
2289 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2290 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2291 'metrics_data': {'time_series': [], 'metrics_series': []},
2292 'schema_type': 'read_metric_data_response',
2293 'unit': '%', 'vim_uuid': '1'}
2295 # call get matrics data method under test
2296 actual_return
= self
.mon_plugin
.get_metrics_data(metrics
)
2298 # verify that mocked method is called/not called
2299 m_get_default_Params
.assert_called_with(metrics
['metric_name'])
2300 m_get_vm_moref_id
.assert_called_with(metrics
['resource_uuid'])
2301 m_get_vm_resource_id
.assert_not_called()
2302 m_get
.assert_not_called()
2304 # verify return value with expected value
2305 self
.assertEqual(expected_return
, actual_return
)
2307 @mock.patch
.object(monPlugin
.requests
, 'get')
2308 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2309 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2310 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2311 def test_get_metrics_data_failed_to_get_vm_resource_id(self
, m_get_default_Params
,
2313 m_get_vm_resource_id
,
2315 """Test get metrics data method negative scenario- invalid moref id"""
2317 metrics
= {'collection_period': 1, 'metric_name': 'CPU_UTILIZATION', 'metric_uuid': None,
2318 'schema_version': 1.0, 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2319 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2320 'schema_type': 'read_metric_data_request', 'vim_type': 'VMware',
2321 'collection_unit': 'HR', 'vim_uuid': '1'}
2324 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2325 m_get_vm_moref_id
.return_value
= 'Invalid-vm-6626'
2326 m_get_vm_resource_id
.return_value
= None
2327 expected_return
= {'metric_name': 'CPU_UTILIZATION', 'metric_uuid': '0',
2328 'schema_version': '1.0',
2329 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2330 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2331 'metrics_data': {'time_series': [], 'metrics_series': []},
2332 'schema_type': 'read_metric_data_response',
2333 'unit': '%', 'vim_uuid': '1'}
2335 # call get matrics data method under test
2336 actual_return
= self
.mon_plugin
.get_metrics_data(metrics
)
2338 # verify that mocked method is called/not called
2339 m_get_default_Params
.assert_called_with(metrics
['metric_name'])
2340 m_get_vm_moref_id
.assert_called_with(metrics
['resource_uuid'])
2341 m_get_vm_resource_id
.assert_called()
2342 m_get_vm_resource_id
.assert_called_with('Invalid-vm-6626')
2343 m_get
.assert_not_called()
2345 # verify return value with expected value
2346 self
.assertEqual(expected_return
, actual_return
)
2348 @mock.patch
.object(monPlugin
.MonPlugin
, 'reconfigure_alarm')
2349 @mock.patch
.object(monPlugin
.MonPlugin
, 'update_symptom_defination')
2350 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
2351 def test_update_alarm_configuration_successful_updation(self
, m_get_alarm_defination_details
,
2352 m_update_symptom_defination
,
2353 m_reconfigure_alarm
):
2354 """Test update alarm configuration method"""
2356 alarm_config
= {'alarm_uuid': 'f1163767-6eac-438f-8e60-a7a867257e14',
2357 'correlation_id': 14203,
2358 'description': 'CPU_Utilization_Above_Threshold_L', 'operation': 'GT'}
2361 alarm_details_json
= {'states': [{'impact': {'impactType': 'BADGE', 'detail': 'risk'},
2362 'severity': 'CRITICAL', 'base-symptom-set': {'symptomDefinitionIds':
2364 'SymptomDefinition-47c88675-bea8-436a-bb41-8d2231428f44'],
2366 'type': 'SYMPTOM_SET',
2367 'aggregation': 'ALL'}}],
2368 'description': 'CPU_Utilization_Above_Threshold', 'type': 16,
2369 'id': 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14',
2370 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d2'}
2371 alarm_details
= {'symptom_definition_id': 'SymptomDefinition-47c88675-bea8-436a-bb41-\
2372 8d2231428f44', 'alarm_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-\
2373 a91c-8c19d2', 'alarm_id': 'AlertDefinition-f1163767-6eac-438f-8e60-\
2374 a7a867257e14', 'resource_kind': 'VirtualMachine', 'type': 16}
2375 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
2376 m_update_symptom_defination
.return_value
= 'SymptomDefinition-47c88675-bea8-436a-bb41-\
2378 expected_return
= m_reconfigure_alarm
.return_value
= 'f1163767-6eac-438f-8e60-a7a867257e14'
2380 # call update alarm configuration method under test
2381 actual_return
= self
.mon_plugin
.update_alarm_configuration(alarm_config
)
2383 # verify that mocked method is called
2384 m_get_alarm_defination_details
.assert_called_with(alarm_config
['alarm_uuid'])
2385 m_update_symptom_defination
.assert_called_with(alarm_details
['symptom_definition_id'],
2387 m_reconfigure_alarm
.assert_called_with(alarm_details_json
, alarm_config
)
2389 # verify return value with expected value
2390 self
.assertEqual(expected_return
, actual_return
)
2392 @mock.patch
.object(monPlugin
.MonPlugin
, 'reconfigure_alarm')
2393 @mock.patch
.object(monPlugin
.MonPlugin
, 'update_symptom_defination')
2394 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
2395 def test_update_alarm_configuration_failed_to_reconfigure_alarm(self
,
2396 m_get_alarm_defination_details
,
2397 m_update_symptom_defination
,
2398 m_reconfigure_alarm
):
2399 """Test update alarm configuration method- failed to reconfigure alarm"""
2401 alarm_config
= {'alarm_uuid': 'f1163767-6eac-438f-8e60-a7a867257e14',
2402 'correlation_id': 14203,
2403 'description': 'CPU_Utilization_Above_Threshold_L', 'operation': 'GT'}
2406 alarm_details_json
= {'states': [{'impact': {'impactType': 'BADGE', 'detail': 'risk'},
2407 'severity': 'CRITICAL', 'base-symptom-set': {'symptomDefinitionIds':
2409 'SymptomDefinition-47c88675-bea8-436a-bb41-8d2231428f44'],
2411 'type': 'SYMPTOM_SET',
2412 'aggregation': 'ALL'}}],
2413 'description': 'CPU_Utilization_Above_Threshold', 'type': 16,
2414 'id': 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14',
2415 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d2'}
2416 alarm_details
= {'symptom_definition_id': 'SymptomDefinition-47c88675-bea8-436a-bb41-\
2417 8d2231428f44', 'alarm_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-\
2418 a91c-8c19d2', 'alarm_id': 'AlertDefinition-f1163767-6eac-438f-8e60-\
2419 a7a867257e14', 'resource_kind': 'VirtualMachine', 'type': 16}
2420 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
2421 m_update_symptom_defination
.return_value
= 'SymptomDefinition-47c88675-bea8-436a-bb41-\
2423 expected_return
= m_reconfigure_alarm
.return_value
= None # failed to reconfigure
2425 # call update alarm configuration method under test
2426 actual_return
= self
.mon_plugin
.update_alarm_configuration(alarm_config
)
2428 # verify that mocked method is called
2429 m_get_alarm_defination_details
.assert_called_with(alarm_config
['alarm_uuid'])
2430 m_update_symptom_defination
.assert_called_with(alarm_details
['symptom_definition_id'],
2432 m_reconfigure_alarm
.assert_called_with(alarm_details_json
, alarm_config
)
2434 # verify return value with expected value
2435 self
.assertEqual(expected_return
, actual_return
)
2437 @mock.patch
.object(monPlugin
.MonPlugin
, 'reconfigure_alarm')
2438 @mock.patch
.object(monPlugin
.MonPlugin
, 'update_symptom_defination')
2439 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
2440 def test_update_alarm_configuration_failed_to_update_symptom(self
,
2441 m_get_alarm_defination_details
,
2442 m_update_symptom_defination
,
2443 m_reconfigure_alarm
):
2444 """Test update alarm configuration method- failed to update alarm"""
2446 alarm_config
= {'alarm_uuid': 'f1163767-6eac-438f-8e60-a7a867257e14',
2447 'correlation_id': 14203,
2448 'description': 'CPU_Utilization_Above_Threshold_L', 'operation': 'GT'}
2451 alarm_details_json
= {'states': [{'impact': {'impactType': 'BADGE', 'detail': 'risk'},
2452 'severity': 'CRITICAL', 'base-symptom-set': {'symptomDefinitionIds':
2454 'SymptomDefinition-47c88675-bea8-436a-bb41-8d2231428f44'],
2456 'type': 'SYMPTOM_SET',
2457 'aggregation': 'ALL'}}],
2458 'description': 'CPU_Utilization_Above_Threshold', 'type': 16,
2459 'id': 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14',
2460 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d2'}
2461 alarm_details
= {'symptom_definition_id': 'Invalid-47c88675-bea8-436a-bb41-\
2462 8d2231428f44', 'alarm_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-\
2463 a91c-8c19d2', 'alarm_id': 'AlertDefinition-f1163767-6eac-438f-8e60-\
2464 a7a867257e14', 'resource_kind': 'VirtualMachine', 'type': 16}
2465 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
2466 expected_return
= m_update_symptom_defination
.return_value
= None
2468 # call update alarm configuration method under test
2469 actual_return
= self
.mon_plugin
.update_alarm_configuration(alarm_config
)
2471 # verify that mocked method is called
2472 m_get_alarm_defination_details
.assert_called_with(alarm_config
['alarm_uuid'])
2473 m_update_symptom_defination
.assert_called_with(alarm_details
['symptom_definition_id'],
2475 m_reconfigure_alarm
.assert_not_called()
2477 # verify return value with expected value
2478 self
.assertEqual(expected_return
, actual_return
)
2480 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2481 def test_verify_metric_support_metric_supported_with_unit(self
, m_get_default_Params
):
2482 """Test verify metric support method for supported metric"""
2485 metric_info
= {'metric_unit': '%', 'metric_name': 'cpu_utilization',
2486 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'}
2487 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2488 expected_return
= True # supported metric returns True
2490 # call verify metric support method under test
2491 actual_return
= self
.mon_plugin
.verify_metric_support(metric_info
)
2493 # verify that mocked method is called
2494 m_get_default_Params
.assert_called_with(metric_info
['metric_name'])
2495 # m_get_default_Params.assert_called_with(metric_info)
2497 # verify return value with expected value
2498 self
.assertEqual(expected_return
, actual_return
)
2500 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2501 def test_verify_metric_support_metric_not_supported(self
, m_get_default_Params
):
2502 """Test verify metric support method for un-supported metric"""
2505 metric_info
= {'metric_unit': '%', 'metric_name': 'invalid_metric',
2506 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'}
2507 m_get_default_Params
.return_value
= {}
2508 expected_return
= False # supported metric returns True
2510 # call verify metric support method under test
2511 actual_return
= self
.mon_plugin
.verify_metric_support(metric_info
)
2513 # verify that mocked method is called
2514 m_get_default_Params
.assert_called_with(metric_info
['metric_name'])
2516 # verify return value with expected value
2517 self
.assertEqual(expected_return
, actual_return
)
2519 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2520 def test_verify_metric_support_metric_supported_with_mismatched_unit(self
,
2521 m_get_default_Params
):
2522 """Test verify metric support method for supported metric with mismatched unit"""
2525 metric_info
= {'metric_unit': '', 'metric_name': 'invalid_metric',
2526 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'}
2527 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2528 expected_return
= True # supported metric returns True
2530 # call verify metric support method under test
2531 actual_return
= self
.mon_plugin
.verify_metric_support(metric_info
)
2533 # verify that mocked method is called
2534 m_get_default_Params
.assert_called_with(metric_info
['metric_name'])
2536 # verify return value with expected value
2537 self
.assertEqual(expected_return
, actual_return
)
2539 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_triggered_alarms_on_resource')
2540 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vrops_resourceid_from_ro_uuid')
2541 def test_get_triggered_alarms_list_returns_triggered_alarms(self
,
2542 m_get_vrops_resourceid
,
2543 m_triggered_alarms
):
2544 """Test get triggered alarm list method valid input"""
2546 # Mock list alarm input
2547 list_alarm_input
= {'severity': 'CRITICAL',
2548 'correlation_id': 'e14b203c',
2549 'alarm_name': 'CPU_Utilization_Above_Threshold',
2550 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'}
2552 resource_id
= m_get_vrops_resourceid
.return_value
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2553 expected_return
= m_triggered_alarms
.return_value
= [{'status': 'ACTIVE',
2554 'update_date': '2018-01-12T08:34:05',
2555 'severity': 'CRITICAL', 'resource_uuid': 'e14b203c',
2556 'cancel_date': '0000-00-00T00:00:00',
2557 'alarm_instance_uuid': 'd9e3bc84',
2558 'alarm_uuid': '5714977d', 'vim_type': 'VMware',
2559 'start_date': '2018-01-12T08:34:05'},
2560 {'status': 'CANCELED',
2561 'update_date': '2017-12-20T09:37:57',
2562 'severity': 'CRITICAL', 'resource_uuid': 'e14b203c',
2563 'cancel_date': '2018-01-12T06:49:19',
2564 'alarm_instance_uuid': 'd3bbeef6',
2565 'alarm_uuid': '7ba1bf3e', 'vim_type': 'VMware',
2566 'start_date': '2017-12-20T09:37:57'}]
2568 # call get triggered alarms list method under test
2569 actual_return
= self
.mon_plugin
.get_triggered_alarms_list(list_alarm_input
)
2571 # verify that mocked method is called
2572 m_get_vrops_resourceid
.assert_called_with(list_alarm_input
['resource_uuid'])
2573 m_triggered_alarms
.assert_called_with(list_alarm_input
['resource_uuid'], resource_id
)
2575 # verify return value with expected value
2576 self
.assertEqual(expected_return
, actual_return
)
2578 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_triggered_alarms_on_resource')
2579 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vrops_resourceid_from_ro_uuid')
2580 def test_get_triggered_alarms_list_invalid_resource_uuid(self
,
2581 m_get_vrops_resourceid
,
2582 m_triggered_alarms
):
2583 """Test get triggered alarm list method invalid resource uuid"""
2585 # Mock list alarm input
2586 list_alarm_input
= {'severity': 'CRITICAL',
2587 'correlation_id': 'e14b203c',
2588 'alarm_name': 'CPU_Utilization_Above_Threshold',
2589 'resource_uuid': '12345'} # invalid resource uuid
2591 expected_return
= m_get_vrops_resourceid
.return_value
= None # returns empty list
2593 # call get triggered alarms list method under test
2594 actual_return
= self
.mon_plugin
.get_triggered_alarms_list(list_alarm_input
)
2596 # verify that mocked method is called
2597 m_get_vrops_resourceid
.assert_called_with(list_alarm_input
['resource_uuid'])
2598 m_triggered_alarms
.assert_not_called()
2600 # verify return value with expected value
2601 self
.assertEqual([], actual_return
)
2603 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_triggered_alarms_on_resource')
2604 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vrops_resourceid_from_ro_uuid')
2605 def test_get_triggered_alarms_list_resource_uuid_not_present(self
,
2606 m_get_vrops_resourceid
,
2607 m_triggered_alarms
):
2608 """Test get triggered alarm list method resource not present"""
2610 # Mock list alarm input
2611 list_alarm_input
= {'severity': 'CRITICAL',
2612 'correlation_id': 'e14b203c',
2613 'alarm_name': 'CPU_Utilization_Above_Threshold'}
2615 # call get triggered alarms list method under test
2616 actual_return
= self
.mon_plugin
.get_triggered_alarms_list(list_alarm_input
)
2618 # verify that mocked method is called
2619 m_get_vrops_resourceid
.assert_not_called()
2620 m_triggered_alarms
.assert_not_called()
2622 # verify return value with expected value
2623 self
.assertEqual([], actual_return
)
2625 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2626 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2627 def test_get_vrops_resourceid_from_ro_uuid(self
, m_get_vm_moref_id
, m_get_vm_resource_id
):
2628 """Test get vrops resourceid from ro uuid method"""
2631 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2632 vm_moref_id
= m_get_vm_moref_id
.return_value
= 'vm-6626'
2633 expected_return
= m_get_vm_resource_id
.return_value
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2635 # call get_vrops_resourceid_from_ro_uuid method under test
2636 actual_return
= self
.mon_plugin
.get_vrops_resourceid_from_ro_uuid(ro_resource_uuid
)
2638 # verify that mocked method is called
2639 m_get_vm_moref_id
.assert_called_with(ro_resource_uuid
)
2640 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
2642 # verify return value with expected value
2643 self
.assertEqual(expected_return
, actual_return
)
2645 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2646 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2647 def test_get_vrops_resourceid_from_ro_uuid_failed_to_get_vm_resource_id(self
,
2649 m_get_vm_resource_id
):
2650 """Test get vrops resourceid from ro uuid method negative scenario"""
2653 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2654 vm_moref_id
= m_get_vm_moref_id
.return_value
= 'vm-6626'
2655 expected_return
= m_get_vm_resource_id
.return_value
= None
2657 # call get_vrops_resourceid_from_ro_uuid method under test
2658 actual_return
= self
.mon_plugin
.get_vrops_resourceid_from_ro_uuid(ro_resource_uuid
)
2660 # verify that mocked method is called
2661 m_get_vm_moref_id
.assert_called_with(ro_resource_uuid
)
2662 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
2664 # verify return value with expected value
2665 self
.assertEqual(expected_return
, actual_return
)
2667 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2668 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2669 def test_get_vrops_resourceid_from_ro_uuid_failed_to_get_vm_moref_id(self
,
2671 m_get_vm_resource_id
):
2672 """Test get vrops resourceid from ro uuid method negative scenario"""
2675 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2676 expected_return
= vm_moref_id
= m_get_vm_moref_id
.return_value
= None
2678 # call get_vrops_resourceid_from_ro_uuid method under test
2679 actual_return
= self
.mon_plugin
.get_vrops_resourceid_from_ro_uuid(ro_resource_uuid
)
2681 # verify that mocked method is called
2682 m_get_vm_moref_id
.assert_called_with(ro_resource_uuid
)
2683 m_get_vm_resource_id
.assert_not_called()
2685 # verify return value with expected value
2686 self
.assertEqual(expected_return
, actual_return
)
2688 @mock.patch
.object(monPlugin
.requests
, 'get')
2689 def test_get_triggered_alarms_on_resource_valid_req_response(self
, m_get
):
2690 """Test get triggered alarms on resource method for valid request"""
2693 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2694 vrops_resource_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2695 m_get
.return_value
.status_code
= 200
2696 expected_return
= [{'status': 'ACTIVE', 'update_date': '2018-01-12T08:34:05',
2697 'severity': 'CRITICAL', 'start_date': '2018-01-12T08:34:05',
2698 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2699 'cancel_date': '2018-02-12T08:24:48', 'vim_type': 'VMware',
2700 'alarm_instance_uuid': 'd9e3bc84-dcb4-4905-b592-00a55f4cdaf1',
2701 'alarm_uuid': '5714977d-56f6-4222-adc7-43fa6c6e7e39'}]
2703 m_get
.return_value
.content
= '{"alerts": [\
2705 "alertId": "d9e3bc84-dcb4-4905-b592-00a55f4cdaf1",\
2706 "resourceId": "ac87622f-b761-40a0-b151-00872a2a456e",\
2707 "alertLevel": "CRITICAL",\
2708 "status": "ACTIVE",\
2709 "startTimeUTC": 1515746045278,\
2710 "cancelTimeUTC": 1518423888708,\
2711 "updateTimeUTC": 1515746045278,\
2712 "alertDefinitionId": "AlertDefinition-5714977d-56f6-4222-adc7-43fa6c6e7e39",\
2713 "alertDefinitionName": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4"\
2716 "alertId": "5fb5e940-e161-4253-a729-7255c6d6b1f5",\
2717 "resourceId": "ac87622f-b761-40a0-b151-00872a2a456e",\
2718 "alertLevel": "WARNING",\
2719 "status": "CANCELED",\
2720 "startTimeUTC": 1506684979154,\
2721 "cancelTimeUTC": 0,\
2722 "updateTimeUTC": 1520471975507,\
2723 "alertDefinitionId": "AlertDefinition-9ec5a921-1a54-411d-85ec-4c1c9b26dd02",\
2724 "alertDefinitionName": "VM_CPU_Usage_Alarm"\
2727 # call get_vrops_resourceid_from_ro_uuid method under test
2728 actual_return
= self
.mon_plugin
.get_triggered_alarms_on_resource(ro_resource_uuid
,
2731 # verify that mocked method is called
2732 m_get
.assert_called()
2734 # verify return value with expected value
2735 self
.assertEqual(expected_return
, actual_return
)
2737 @mock.patch
.object(monPlugin
.requests
, 'get')
2738 def test_get_triggered_alarms_on_resource_invalid_req_response(self
, m_get
):
2739 """Test get triggered alarms on resource method for invalid request"""
2742 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2743 vrops_resource_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2744 m_get
.return_value
.status_code
= 204
2745 expected_return
= None
2747 # call get_vrops_resourceid_from_ro_uuid method under test
2748 actual_return
= self
.mon_plugin
.get_triggered_alarms_on_resource(ro_resource_uuid
,
2751 # verify that mocked method is called
2752 m_get
.assert_called()
2754 # verify return value with expected value
2755 self
.assertEqual(expected_return
, actual_return
)
2757 @mock.patch
.object(monPlugin
.requests
, 'get')
2758 def test_get_triggered_alarms_on_resource_no_alarms_present(self
, m_get
):
2759 """Test get triggered alarms on resource method for no alarms present"""
2762 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2763 vrops_resource_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2764 m_get
.return_value
.status_code
= 200
2765 expected_return
= []
2766 m_get
.return_value
.content
= '{"alerts": []}'
2768 # call get_vrops_resourceid_from_ro_uuid method under test
2769 actual_return
= self
.mon_plugin
.get_triggered_alarms_on_resource(ro_resource_uuid
,
2772 # verify that mocked method is called
2773 m_get
.assert_called()
2775 # verify return value with expected value
2776 self
.assertEqual(expected_return
, actual_return
)
2778 def test_convert_date_time_valid_date_time(self
):
2779 """Test convert date time method valid input"""
2782 date_time
= 1515746045278
2783 expected_return
= '2018-01-12T08:34:05'
2785 # call convert_date_time method under test
2786 actual_return
= self
.mon_plugin
.convert_date_time(date_time
)
2788 # verify return value with expected value
2789 self
.assertEqual(expected_return
, actual_return
)
2791 def test_convert_date_time_invalid_date_time(self
):
2792 """Test convert date time method invalid input"""
2796 expected_return
= '0000-00-00T00:00:00'
2798 # call convert_date_time method under test
2799 actual_return
= self
.mon_plugin
.convert_date_time(date_time
)
2801 # verify return value with expected value
2802 self
.assertEqual(expected_return
, actual_return
)
2804 @mock.patch
.object(monPlugin
.requests
, 'get')
2805 def test_get_vm_resource_id_rest_valid_req_response(self
, m_get
):
2806 """Test get vms resource id valid request"""
2809 vm_moref_id
= 'vm-6626'
2810 m_get
.return_value
.status_code
= 200
2811 expected_return
= "ac87622f-b761-40a0-b151-00872a2a456e"
2812 m_get
.return_value
.content
= \
2816 "creationTime": 1497770174130,\
2818 "name": "OCInst2.ubuntu(4337d51f-1e65-4ab0-9c08-4897778d4fda)",\
2819 "adapterKindKey": "VMWARE",\
2820 "resourceKindKey": "VirtualMachine",\
2821 "resourceIdentifiers": [\
2823 "identifierType": {\
2824 "name": "VMEntityObjectID",\
2825 "dataType": "STRING",\
2826 "isPartOfUniqueness": true\
2832 "identifier": "ac87622f-b761-40a0-b151-00872a2a456e"\
2837 # call get_vm_resource_id method under test
2838 actual_return
= self
.mon_plugin
.get_vm_resource_id(vm_moref_id
)
2840 # verify that mocked method is called
2841 m_get
.assert_called()
2843 # verify return value with expected value
2844 self
.assertEqual(expected_return
, actual_return
)
2846 @mock.patch
.object(monPlugin
.requests
, 'get')
2847 def test_get_vm_resource_id_rest_invalid_req_response(self
, m_get
):
2848 """Test get vms resource id invalid request"""
2851 vm_moref_id
= 'vm-6626'
2852 m_get
.return_value
.status_code
= 406
2853 expected_return
= None
2854 m_get
.return_value
.content
= '406 Not Acceptable'
2856 # call get_vm_resource_id method under test
2857 actual_return
= self
.mon_plugin
.get_vm_resource_id(vm_moref_id
)
2859 # verify that mocked method is called
2860 m_get
.assert_called()
2862 # verify return value with expected value
2863 self
.assertEqual(expected_return
, actual_return
)
2865 @mock.patch
.object(monPlugin
.requests
, 'get')
2866 def test_get_vm_resource_id_rest_invalid_response(self
, m_get
):
2867 """Test get vms resource id invalid response"""
2870 vm_moref_id
= 'vm-6626'
2871 m_get
.return_value
.status_code
= 200
2872 expected_return
= None
2873 m_get
.return_value
.content
= \
2877 "creationTime": 1497770174130,\
2879 "name": "OCInst2.ubuntu(4337d51f-1e65-4ab0-9c08-4897778d4fda)",\
2880 "adapterKindKey": "VMWARE",\
2881 "resourceKindKey": "VirtualMachine",\
2882 "resourceIdentifiers": [\
2884 "identifierType": {\
2885 "name": "VMEntityObjectID",\
2886 "dataType": "STRING",\
2887 "isPartOfUniqueness": true\
2893 "identifier": "ac87622f-b761-40a0-b151-00872a2a456e"\
2897 # call get_vm_resource_id method under test
2898 actual_return
= self
.mon_plugin
.get_vm_resource_id(vm_moref_id
)
2900 # verify that mocked method is called
2901 # m_get.assert_called
2903 # verify return value with expected value
2904 self
.assertEqual(expected_return
, actual_return
)
2906 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vapp_details_rest')
2907 def test_get_vm_moref_id_valid_id_found(self
, m_get_vapp_details_rest
):
2908 """Test get vm moref id valid scenario"""
2911 vapp_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2912 m_get_vapp_details_rest
.return_value
= {'vm_vcenter_info': {'vm_moref_id': 'vm-6626'}}
2913 expected_return
= 'vm-6626'
2915 # call get_vm_resource_id method under test
2916 actual_return
= self
.mon_plugin
.get_vm_moref_id(vapp_uuid
)
2918 # verify that mocked method is called
2919 m_get_vapp_details_rest
.assert_called_with(vapp_uuid
)
2921 # verify return value with expected value
2922 self
.assertEqual(expected_return
, actual_return
)
2924 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vapp_details_rest')
2925 def test_get_vm_moref_id_valid_id_not_found(self
, m_get_vapp_details_rest
):
2926 """Test get vm moref id invalid scenario"""
2929 vapp_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda' # invalid uuid
2930 m_get_vapp_details_rest
.return_value
= {}
2931 expected_return
= None
2933 # call get_vm_resource_id method under test
2934 actual_return
= self
.mon_plugin
.get_vm_moref_id(vapp_uuid
)
2936 # verify that mocked method is called
2937 m_get_vapp_details_rest
.assert_called_with(vapp_uuid
)
2939 # verify return value with expected value
2940 self
.assertEqual(expected_return
, actual_return
)
2942 @mock.patch
.object(monPlugin
.requests
, 'get')
2943 @mock.patch
.object(monPlugin
.MonPlugin
, 'connect_as_admin')
2944 def test_get_vapp_details_rest_valid_req_response(self
, m_connect_as_admin
, m_get
):
2945 """Test get vapp details rest method for valid request response"""
2948 vapp_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2949 m_connect_as_admin
.return_value
= self
.vca
2950 self
.vca
._session
= self
.session
2951 self
.vca
._session
.headers
['x-vcloud-authorization'] = '2ec69b2cc6264ad0a47aaf4e3e280d16'
2952 m_get
.return_value
.status_code
= 200
2953 expected_return
= {'vm_vcenter_info': {'vm_moref_id': 'vm-6626'}}
2954 m_get
.return_value
.content
= '<?xml version="1.0" encoding="UTF-8"?>\
2955 <VApp xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5" >\
2957 <Vm needsCustomization="false" type="application/vnd.vmware.vcloud.vm+xml">\
2958 <VCloudExtension required="false">\
2960 <vmext:VmVimObjectRef>\
2961 <vmext:VimServerRef type="application/vnd.vmware.admin.vmwvirtualcenter+xml"/>\
2962 <vmext:MoRef>vm-6626</vmext:MoRef>\
2963 <vmext:VimObjectType>VIRTUAL_MACHINE</vmext:VimObjectType>\
2964 </vmext:VmVimObjectRef>\
2971 # call get_vapp_details_rest method under test
2972 actual_return
= self
.mon_plugin
.get_vapp_details_rest(vapp_uuid
)
2974 # verify that mocked method is called
2975 m_connect_as_admin
.assert_called_with()
2976 m_get
.assert_called()
2978 # verify return value with expected value
2979 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()
3001 # verify return value with expected value
3002 self
.assertEqual(expected_return
, actual_return
)
3004 @mock.patch
.object(monPlugin
.requests
, 'get')
3005 @mock.patch
.object(monPlugin
.MonPlugin
, 'connect_as_admin')
3006 def test_get_vapp_details_rest_failed_to_connect_vcd(self
, m_connect_as_admin
, m_get
):
3007 """Test get vapp details rest method for failed to connect to vcd"""
3010 vapp_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
3011 m_connect_as_admin
.return_value
= None
3012 expected_return
= {}
3014 # call get_vapp_details_rest method under test
3015 actual_return
= self
.mon_plugin
.get_vapp_details_rest(vapp_uuid
)
3017 # verify that mocked method is called
3018 m_connect_as_admin
.assert_called_with()
3019 m_get
.assert_not_called()
3021 # verify return value with expected value
3022 self
.assertEqual(expected_return
, actual_return
)
3024 @mock.patch
.object(monPlugin
.requests
, 'get')
3025 @mock.patch
.object(monPlugin
.MonPlugin
, 'connect_as_admin')
3026 def test_get_vapp_details_rest_invalid_response(self
, m_connect_as_admin
, m_get
):
3027 """Test get vapp details rest method for invalid response"""
3030 vapp_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
3031 m_connect_as_admin
.return_value
= self
.vca
3032 self
.vca
._session
= self
.session
3033 self
.vca
._session
.headers
['x-vcloud-authorization'] = '2ec69b2cc6264ad0a47aaf4e3e280d16'
3034 m_get
.return_value
.status_code
= 200
3035 expected_return
= {}
3036 m_get
.return_value
.content
= '<?xml version="1.0" encoding="UTF-8"?>\
3037 <VApp xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5" >\
3039 <Vm needsCustomization="false" type="application/vnd.vmware.vcloud.vm+xml">\
3040 <VCloudExtension required="false">\
3042 <vmext:VmVimObjectRef>\
3043 <vmext:VimServerRef type="application/vnd.vmware.admin.vmwvirtualcenter+xml"/>\
3044 <vmext:MoRef>vm-6626</vmext:MoRef>\
3045 <vmext:VimObjectType>VIRTUAL_MACHINE</vmext:VimObjectType>\
3052 # call get_vapp_details_rest method under test
3053 actual_return
= self
.mon_plugin
.get_vapp_details_rest(vapp_uuid
)
3055 # verify that mocked method is called
3056 m_connect_as_admin
.assert_called_with()
3057 m_get
.assert_called()
3059 # verify return value with expected value
3060 self
.assertEqual(expected_return
, actual_return
)
3062 @mock.patch
.object(monPlugin
.Client
, 'set_credentials')
3063 @mock.patch
.object(monPlugin
, 'Client')
3064 def test_connect_as_admin(self
, m_client
, m_set_credentials
):
3065 """Test connect as admin to vCD method"""
3067 # mock the inputs and mocked returns
3068 expected_return
= m_client
.return_value
= self
.vca
3069 m_set_credentials
.retrun_value
= True
3071 # call connect_as_admin method under test
3072 actual_return
= self
.mon_plugin
.connect_as_admin()
3074 # verify that mocked method is called
3075 m_client
.assert_called_with(self
.m_vim_access_config
['vim_url'],
3076 verify_ssl_certs
=False)
3078 # verify return value with expected value
3079 self
.assertEqual(expected_return
, actual_return
)
3081 # For testing purpose
3082 # if __name__ == '__main__':