1 # -*- coding: utf-8 -*-
4 # Copyright 2017-2018 VMware Inc.
5 # This file is part of ETSI OSM
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: osslegalrouting@vmware.com
24 """ Mock tests for VMware vROPs Mon plugin """
40 sys
.path
.append(os
.path
.join(os
.path
.dirname(os
.path
.abspath(__file__
)),"..","..",".."))
42 from osm_mon
.plugins
.vRealiseOps
import mon_plugin_vrops
as monPlugin
44 from pyvcloud
.vcd
.client
import Client
47 class TestMonPlugin(unittest
.TestCase
):
48 """Test class for vROPs Mon Plugin class methods"""
51 """Setup the tests for Mon Plugin class methods"""
52 super(TestMonPlugin
, self
).setUp()
53 self
.mon_plugin
= monPlugin
.MonPlugin()
54 # create client object
55 self
.vca
= Client('test', verify_ssl_certs
=False)
57 self
.session
= requests
.Session()
59 def test_get_default_Params_valid_metric_alarm_name(self
):
60 """Test get default params method"""
62 # Mock valid metric_alarm_name and response
63 metric_alarm_name
= "Average_Memory_Usage_Above_Threshold"
64 expected_return
= {'impact': 'risk', 'cancel_cycles': 2, 'adapter_kind': 'VMWARE',
65 'repeat': False,'cancel_period': 300, 'alarm_type': 16,
66 'vrops_alarm': 'Avg_Mem_Usage_Above_Thr','enabled': True, 'period': 300,
67 'resource_kind': 'VirtualMachine', 'alarm_subType': 19,
68 'action': 'acknowledge', 'evaluation': 2, 'unit': '%'}
70 # call get default param function under test
71 actual_return
= self
.mon_plugin
.get_default_Params(metric_alarm_name
)
73 # verify return value with expected value
74 self
.assertEqual(expected_return
, actual_return
)
77 def test_get_default_Params_invalid_metric_alarm_name(self
):
78 """Test get default params method invalid metric alarm"""
80 # Mock valid metric_alarm_name and response
81 metric_alarm_name
= "Invalid_Alarm"
84 # call get default param function under test
85 actual_return
= self
.mon_plugin
.get_default_Params(metric_alarm_name
)
87 # verify return value with expected value
88 self
.assertEqual(expected_return
, actual_return
)
91 @mock.patch
.object(monPlugin
.requests
, 'post')
92 def test_create_symptom_valid_req_response(self
, m_post
):
93 """Test create symptom method-valid request"""
95 # Mock valid symptom params and mock responses
96 symptom_param
= {'threshold_value': 0, 'cancel_cycles': 1, 'adapter_kind_key': 'VMWARE',
97 'resource_kind_key': 'VirtualMachine', 'severity': 'CRITICAL',
98 'symptom_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
99 'operation': 'GT', 'wait_cycles': 1, 'metric_key': 'cpu|usage_average'}
101 m_post
.return_value
.status_code
= 201
102 m_post
.return_value
.content
= \
103 '{"id":"SymptomDefinition-351c23b4-bc3c-4c7b-b4af-1ad90a673c5d",\
104 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
105 "adapterKindKey":"VMWARE","resourceKindKey":"VirtualMachine","waitCycles":1,\
106 "cancelCycles":1,"state":{"severity":"CRITICAL","condition":{"type":"CONDITION_HT",\
107 "key":"cpu|usage_average","operator":"GT","value":"0.0","valueType":"NUMERIC",\
108 "instanced":false,"thresholdType":"STATIC"}}}'
110 expected_return
= "SymptomDefinition-351c23b4-bc3c-4c7b-b4af-1ad90a673c5d"
112 # call create symptom method under test
113 actual_return
= self
.mon_plugin
.create_symptom(symptom_param
)
115 # verify that mocked method is called
116 m_post
.assert_called()
118 # verify return value with expected value
119 self
.assertEqual(expected_return
, actual_return
)
122 @mock.patch
.object(monPlugin
.requests
, 'post')
123 def test_create_symptom_invalid_req_response(self
, m_post
):
124 """Test create symptom method-invalid response"""
126 # Mock valid symptom params and invalid mock responses
127 symptom_param
= {'threshold_value': 0, 'cancel_cycles': 1, 'adapter_kind_key': 'VMWARE',
128 'resource_kind_key': 'VirtualMachine', 'severity': 'CRITICAL',
129 'symptom_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
130 'operation': 'GT', 'wait_cycles': 1, 'metric_key': 'cpu|usage_average'}
132 m_post
.return_value
.status_code
= 404
133 m_post
.return_value
.content
= '404 Not Found'
135 expected_return
= None
137 # call create symptom method under test
138 actual_return
= self
.mon_plugin
.create_symptom(symptom_param
)
140 # verify that mocked method is called
141 m_post
.assert_called()
143 # verify return value with expected value
144 self
.assertEqual(expected_return
, actual_return
)
147 @mock.patch
.object(monPlugin
.requests
, 'post')
148 def test_create_symptom_incorrect_data(self
, m_post
):
149 """Test create symptom method-incorrect data"""
151 # Mock valid symptom params and invalid mock responses
152 symptom_param
= {'threshold_value': 0, 'cancel_cycles': 1, 'adapter_kind_key': 'VMWARE',
153 'resource_kind_key': 'VirtualMachine', 'severity': 'CRITICAL',
154 'symptom_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
155 'operation': 'GT', 'metric_key': 'cpu|usage_average'}
157 expected_return
= None
159 # call create symptom method under test
160 actual_return
= self
.mon_plugin
.create_symptom(symptom_param
)
162 # verify that mocked method is not called
163 m_post
.assert_not_called()
165 # verify return value with expected value
166 self
.assertEqual(expected_return
, actual_return
)
169 @mock.patch
.object(monPlugin
.requests
, 'post')
170 def test_create_alarm_definition_valid_req_response(self
, m_post
):
171 """Test create alarm definition method-valid response"""
173 # Mock valid alarm params and mock responses
174 alarm_param
= {'description': 'CPU_Utilization_Above_Threshold', 'cancelCycles': 1, 'subType': 19,
175 'waitCycles': 1, 'severity': 'CRITICAL', 'impact': 'risk', 'adapterKindKey': 'VMWARE',
176 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
177 'resourceKindKey': 'VirtualMachine', 'type': 16,
178 'symptomDefinitionId': 'SymptomDefinition-25278b06-bff8-4409-a141-9b4e064235df'}
180 m_post
.return_value
.status_code
= 201
181 m_post
.return_value
.content
= \
182 '{"id":"AlertDefinition-d4f21e4b-770a-45d6-b298-022eaf489115",\
183 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
184 "description":"CPU_Utilization_Above_Threshold","adapterKindKey":"VMWARE",\
185 "resourceKindKey":"VirtualMachine","waitCycles":1,"cancelCycles":1,"type":16,\
186 "subType":19,"states":[{"severity":"CRITICAL","base-symptom-set":{"type":"SYMPTOM_SET",\
187 "relation":"SELF","aggregation":"ALL","symptomSetOperator":"AND",\
188 "symptomDefinitionIds":["SymptomDefinition-25278b06-bff8-4409-a141-9b4e064235df"]},\
189 "impact":{"impactType":"BADGE","detail":"risk"}}]}'
191 expected_return
= "AlertDefinition-d4f21e4b-770a-45d6-b298-022eaf489115"
193 # call create alarm definition method under test
194 actual_return
= self
.mon_plugin
.create_alarm_definition(alarm_param
)
196 # verify that mocked method is called
197 m_post
.assert_called()
199 # verify return value with expected value
200 self
.assertEqual(expected_return
, actual_return
)
203 @mock.patch
.object(monPlugin
.requests
, 'post')
204 def test_create_alarm_definition_invalid_req_response(self
, m_post
):
205 """Test create alarm definition method-invalid response"""
207 # Mock valid alarm params and mock responses
208 alarm_param
= {'description': 'CPU_Utilization_Above_Threshold', 'cancelCycles': 1, 'subType': 19,
209 'waitCycles': 1, 'severity': 'CRITICAL', 'impact': 'risk', 'adapterKindKey': 'VMWARE',
210 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
211 'resourceKindKey': 'VirtualMachine', 'type': 16,
212 'symptomDefinitionId': 'SymptomDefinition-25278b06-bff8-4409-a141-9b4e064235df'}
214 m_post
.return_value
.status_code
= 404
215 m_post
.return_value
.content
= '404 Not Found'
217 expected_return
= None
219 # call create alarm definition method under test
220 actual_return
= self
.mon_plugin
.create_alarm_definition(alarm_param
)
222 # verify that mocked method is called
223 m_post
.assert_called()
225 # verify return value with expected value
226 self
.assertEqual(expected_return
, actual_return
)
229 @mock.patch
.object(monPlugin
.requests
, 'post')
230 def test_create_alarm_definition_incorrect_data(self
, m_post
):
231 """Test create alarm definition method-incorrect data"""
233 # Mock incorrect alarm param
234 alarm_param
= {'description': 'CPU_Utilization_Above_Threshold', 'cancelCycles': 1, 'subType': 19,
235 'waitCycles': 1, 'severity': 'CRITICAL', 'impact': 'risk', 'adapterKindKey': 'VMWARE',
236 'symptomDefinitionId': 'SymptomDefinition-25278b06-bff8-4409-a141-9b4e064235df', 'type': 16}
237 expected_return
= None
239 # call create symptom method under test
240 actual_return
= self
.mon_plugin
.create_alarm_definition(alarm_param
)
242 # verify that mocked method is not called
243 m_post
.assert_not_called()
245 # verify return value with expected value
246 self
.assertEqual(expected_return
, actual_return
)
249 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
250 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
251 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
252 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
253 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
254 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
255 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
256 def test_configure_alarm_valid_req(self
, m_get_default_Params
,\
257 m_get_alarm_defination_by_name
,\
259 m_create_alarm_definition
,\
261 m_get_vm_resource_id
,\
262 m_create_alarm_notification_rule
):
263 """Test configure alarm valid request creating alarm"""
265 #Mock input configuration dictionary
266 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
267 'alarm_name': 'CPU_Utilization_Above_Threshold',
268 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
269 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
270 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
271 'operation': 'GT', 'unit': '%',
272 'description': 'CPU_Utilization_Above_Threshold'}
274 #symptom parameters to be passed for symptom creation
275 symptom_params
= {'threshold_value': 0,
277 'adapter_kind_key': 'VMWARE',
278 'resource_kind_key': 'VirtualMachine',
279 'severity': 'CRITICAL',
280 'symptom_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
283 'metric_key': 'cpu|usage_average'}
285 #alarm parameters to be passed for alarm creation
286 alarm_params
= {'description': 'CPU_Utilization_Above_Threshold',
287 'cancelCycles': 1, 'subType': 19,
288 'waitCycles': 1, 'severity': 'CRITICAL',
289 'impact': 'risk', 'adapterKindKey': 'VMWARE',
290 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
291 'resourceKindKey': 'VirtualMachine',
292 'symptomDefinitionId': 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804',
295 vm_moref_id
= 'vm-6626'
296 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
297 alarm_def
= 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
298 resource_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
299 alarm_def_uuid
= '0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
301 #Mock default Parameters for alarm & metric configuration
302 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
303 'adapter_kind': 'VMWARE', 'repeat': False,
304 'cancel_period': 300, 'alarm_type': 16,
305 'vrops_alarm': 'CPU_Utilization_Above_Thr',
306 'enabled': True, 'period': 300,
307 'resource_kind': 'VirtualMachine',
308 'alarm_subType': 19, 'action': 'acknowledge',
309 'evaluation': 1, 'unit': 'msec'},
310 {'metric_key': 'cpu|usage_average', 'unit': '%'}
313 #set mocked function return values
314 m_get_alarm_defination_by_name
.return_value
= []
315 m_create_symptom
.return_value
= 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804'
316 m_create_alarm_definition
.return_value
= 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
317 m_get_vm_moref_id
.return_value
= vm_moref_id
318 m_get_vm_resource_id
.return_value
= 'ac87622f-b761-40a0-b151-00872a2a456e'
319 m_create_alarm_notification_rule
.return_value
= 'f37900e7-dd01-4383-b84c-08f519530d71'
321 #Call configure_alarm method under test
322 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
324 #Verify that mocked methods are called with correct parameters
325 self
.assertEqual(m_get_default_Params
.call_count
, 2)
326 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
327 m_create_symptom
.assert_called_with(symptom_params
)
328 m_create_alarm_definition
.assert_called_with(alarm_params
)
329 m_get_vm_moref_id
.assert_called_with(config_dict
['resource_uuid'])
330 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
331 m_create_alarm_notification_rule
.assert_called_with(vrops_alarm_name
, alarm_def
, resource_id
)
333 #Verify return value with expected value of alarm_def_uuid
334 self
.assertEqual(return_value
, alarm_def_uuid
)
337 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
338 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
339 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
340 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
341 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
342 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
343 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
344 def test_configure_alarm_invalid_alarm_name_req(self
, m_get_default_Params
,\
345 m_get_alarm_defination_by_name
,\
347 m_create_alarm_definition
,\
349 m_get_vm_resource_id
,\
350 m_create_alarm_notification_rule
):
351 """Test configure alarm invalid test: for invalid alarm name"""
353 #Mock input configuration dictionary
354 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
355 'alarm_name': 'CPU_Utilization_Above_Threshold',
356 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
357 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
358 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
359 'operation': 'GT', 'unit': '%',
360 'description': 'CPU_Utilization_Above_Threshold'}
362 alarm_def_uuid
= None
364 #Mock default Parameters return value to None
365 m_get_default_Params
.return_value
= {}
367 #Call configure_alarm method under test
368 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
370 #Verify that mocked methods are called with correct parameters
371 m_get_default_Params
.assert_called_once()
372 m_get_alarm_defination_by_name
.assert_not_called()
373 m_create_symptom
.assert_not_called()
374 m_create_alarm_definition
.assert_not_called()
375 m_get_vm_moref_id
.assert_not_called()
376 m_get_vm_resource_id
.assert_not_called()
377 m_create_alarm_notification_rule
.assert_not_called()
379 #Verify return value with expected value i.e. None
380 self
.assertEqual(return_value
, alarm_def_uuid
)
383 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
384 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
385 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
386 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
387 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
388 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
389 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
390 def test_configure_alarm_invalid_metric_name_req(self
, m_get_default_Params
,\
391 m_get_alarm_defination_by_name
,\
393 m_create_alarm_definition
,\
395 m_get_vm_resource_id
,\
396 m_create_alarm_notification_rule
):
397 """Test configure alarm invalid test: for invalid metric name"""
399 #Mock input configuration dictionary
400 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
401 'alarm_name': 'CPU_Utilization_Above_Threshold',
402 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
403 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
404 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
405 'operation': 'GT', 'unit': '%',
406 'description': 'CPU_Utilization_Above_Threshold'}
408 alarm_def_uuid
= None
410 #Mock default Parameters return values for metrics to None
411 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
412 'adapter_kind': 'VMWARE', 'repeat': False,
413 'cancel_period': 300, 'alarm_type': 16,
414 'vrops_alarm': 'CPU_Utilization_Above_Thr',
415 'enabled': True, 'period': 300,
416 'resource_kind': 'VirtualMachine',
417 'alarm_subType': 19, 'action': 'acknowledge',
418 'evaluation': 1, 'unit': 'msec'},
422 #Call configure_alarm method under test
423 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
425 #Verify that mocked methods are called with correct parameters
426 self
.assertEqual(m_get_default_Params
.call_count
, 2)
427 m_get_alarm_defination_by_name
.assert_not_called()
428 m_create_symptom
.assert_not_called()
429 m_create_alarm_definition
.assert_not_called()
430 m_get_vm_moref_id
.assert_not_called()
431 m_get_vm_resource_id
.assert_not_called()
432 m_create_alarm_notification_rule
.assert_not_called()
434 #Verify return value with expected value i.e. None
435 self
.assertEqual(return_value
, alarm_def_uuid
)
437 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
438 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
439 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
440 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
441 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
442 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
443 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
444 def test_configure_alarm_invalid_already_exists(self
, m_get_default_Params
,\
445 m_get_alarm_defination_by_name
,\
447 m_create_alarm_definition
,\
449 m_get_vm_resource_id
,\
450 m_create_alarm_notification_rule
):
451 """Test configure alarm invalid test: for alarm that already exists"""
453 #Mock input configuration dictionary
454 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
455 'alarm_name': 'CPU_Utilization_Above_Threshold',
456 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
457 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
458 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
459 'operation': 'GT', 'unit': '%',
460 'description': 'CPU_Utilization_Above_Threshold'}
462 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
463 alarm_def_uuid
= None
465 #Mock default Parameters for alarm & metric configuration
466 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
467 'adapter_kind': 'VMWARE', 'repeat': False,
468 'cancel_period': 300, 'alarm_type': 16,
469 'vrops_alarm': 'CPU_Utilization_Above_Thr',
470 'enabled': True, 'period': 300,
471 'resource_kind': 'VirtualMachine',
472 'alarm_subType': 19, 'action': 'acknowledge',
473 'evaluation': 1, 'unit': 'msec'},
474 {'metric_key': 'cpu|usage_average', 'unit': '%'}
476 #set mocked function return value
477 m_get_alarm_defination_by_name
.return_value
= ['mocked_alarm_CPU_Utilization_Above_Thr']
480 #Call configure_alarm method under test
481 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
483 #Verify that mocked methods are called with correct parameters
484 self
.assertEqual(m_get_default_Params
.call_count
, 2)
485 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
486 m_create_symptom
.assert_not_called()
487 m_create_alarm_definition
.assert_not_called()
488 m_get_vm_moref_id
.assert_not_called()
489 m_get_vm_resource_id
.assert_not_called()
490 m_create_alarm_notification_rule
.assert_not_called()
492 #Verify return value with expected value of alarm_def_uuid
493 self
.assertEqual(return_value
, alarm_def_uuid
)
496 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
497 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
498 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
499 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
500 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
501 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
502 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
503 def test_configure_alarm_failed_symptom_creation(self
, m_get_default_Params
,\
504 m_get_alarm_defination_by_name
,\
506 m_create_alarm_definition
,\
508 m_get_vm_resource_id
,\
509 m_create_alarm_notification_rule
):
510 """Test configure alarm: failed to create symptom"""
512 #Mock input configuration dictionary
513 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
514 'alarm_name': 'CPU_Utilization_Above_Threshold',
515 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
516 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
517 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
518 'operation': 'GT', 'unit': '%',
519 'description': 'CPU_Utilization_Above_Threshold'}
521 #symptom parameters to be passed for symptom creation
522 symptom_params
= {'threshold_value': 0,
524 'adapter_kind_key': 'VMWARE',
525 'resource_kind_key': 'VirtualMachine',
526 'severity': 'CRITICAL',
527 'symptom_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
530 'metric_key': 'cpu|usage_average'}
531 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
532 alarm_def_uuid
= None
534 #Mock default Parameters for alarm & metric configuration
535 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
536 'adapter_kind': 'VMWARE', 'repeat': False,
537 'cancel_period': 300, 'alarm_type': 16,
538 'vrops_alarm': 'CPU_Utilization_Above_Thr',
539 'enabled': True, 'period': 300,
540 'resource_kind': 'VirtualMachine',
541 'alarm_subType': 19, 'action': 'acknowledge',
542 'evaluation': 1, 'unit': 'msec'},
543 {'metric_key': 'cpu|usage_average', 'unit': '%'}
545 #set mocked function return values
546 m_get_alarm_defination_by_name
.return_value
= []
547 m_create_symptom
.return_value
= None
549 #Call configure_alarm method under test
550 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
552 #Verify that mocked methods are called with correct parameters
553 self
.assertEqual(m_get_default_Params
.call_count
, 2)
554 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
555 m_create_symptom
.assert_called_with(symptom_params
)
556 m_create_alarm_definition
.assert_not_called()
557 m_get_vm_moref_id
.assert_not_called()
558 m_get_vm_resource_id
.assert_not_called()
559 m_create_alarm_notification_rule
.assert_not_called()
561 #Verify return value with expected value of alarm_def_uuid
562 self
.assertEqual(return_value
, alarm_def_uuid
)
565 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
566 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
567 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
568 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
569 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
570 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
571 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
572 def test_configure_alarm_failed_alert_creation(self
, m_get_default_Params
,\
573 m_get_alarm_defination_by_name
,\
575 m_create_alarm_definition
,\
577 m_get_vm_resource_id
,\
578 m_create_alarm_notification_rule
):
579 """Test configure alarm: failed to create alert in vROPs"""
581 #Mock input configuration dictionary
582 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
583 'alarm_name': 'CPU_Utilization_Above_Threshold',
584 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
585 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
586 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
587 'operation': 'GT', 'unit': '%',
588 'description': 'CPU_Utilization_Above_Threshold'}
590 #symptom parameters to be passed for symptom creation
591 symptom_params
= {'threshold_value': 0,
593 'adapter_kind_key': 'VMWARE',
594 'resource_kind_key': 'VirtualMachine',
595 'severity': 'CRITICAL',
596 'symptom_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
599 'metric_key': 'cpu|usage_average'}
601 #alarm parameters to be passed for alarm creation
602 alarm_params
= {'description': 'CPU_Utilization_Above_Threshold',
603 'cancelCycles': 1, 'subType': 19,
604 'waitCycles': 1, 'severity': 'CRITICAL',
605 'impact': 'risk', 'adapterKindKey': 'VMWARE',
606 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
607 'resourceKindKey': 'VirtualMachine',
608 'symptomDefinitionId': 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804',
611 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
612 alarm_def_uuid
= None
614 #Mock default Parameters for alarm & metric configuration
615 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
616 'adapter_kind': 'VMWARE', 'repeat': False,
617 'cancel_period': 300, 'alarm_type': 16,
618 'vrops_alarm': 'CPU_Utilization_Above_Thr',
619 'enabled': True, 'period': 300,
620 'resource_kind': 'VirtualMachine',
621 'alarm_subType': 19, 'action': 'acknowledge',
622 'evaluation': 1, 'unit': 'msec'},
623 {'metric_key': 'cpu|usage_average', 'unit': '%'}
625 #set mocked function return values
626 m_get_alarm_defination_by_name
.return_value
= []
627 m_create_symptom
.return_value
= 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804'
628 m_create_alarm_definition
.return_value
= None
630 #Call configure_alarm method under test
631 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
633 #Verify that mocked methods are called with correct parameters
634 self
.assertEqual(m_get_default_Params
.call_count
, 2)
635 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
636 m_create_symptom
.assert_called_with(symptom_params
)
637 m_create_alarm_definition
.assert_called_with(alarm_params
)
638 m_get_vm_moref_id
.assert_not_called()
639 m_get_vm_resource_id
.assert_not_called()
640 m_create_alarm_notification_rule
.assert_not_called()
642 #Verify return value with expected value of alarm_def_uuid
643 self
.assertEqual(return_value
, alarm_def_uuid
)
646 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
647 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
648 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
649 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
650 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
651 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
652 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
653 def test_configure_alarm_failed_to_get_vm_moref_id(self
, m_get_default_Params
,\
654 m_get_alarm_defination_by_name
,\
656 m_create_alarm_definition
,\
658 m_get_vm_resource_id
,\
659 m_create_alarm_notification_rule
):
660 """Test configure alarm: failed to get vm_moref_id"""
662 #Mock input configuration dictionary
663 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
664 'alarm_name': 'CPU_Utilization_Above_Threshold',
665 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
666 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
667 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
668 'operation': 'GT', 'unit': '%',
669 'description': 'CPU_Utilization_Above_Threshold'}
671 #symptom parameters to be passed for symptom creation
672 symptom_params
= {'threshold_value': 0,
674 'adapter_kind_key': 'VMWARE',
675 'resource_kind_key': 'VirtualMachine',
676 'severity': 'CRITICAL',
677 'symptom_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
680 'metric_key': 'cpu|usage_average'}
682 #alarm parameters to be passed for alarm creation
683 alarm_params
= {'description': 'CPU_Utilization_Above_Threshold',
684 'cancelCycles': 1, 'subType': 19,
685 'waitCycles': 1, 'severity': 'CRITICAL',
686 'impact': 'risk', 'adapterKindKey': 'VMWARE',
687 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
688 'resourceKindKey': 'VirtualMachine',
689 'symptomDefinitionId': 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804',
692 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
693 alarm_def_uuid
= None
695 #Mock default Parameters for alarm & metric configuration
696 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
697 'adapter_kind': 'VMWARE', 'repeat': False,
698 'cancel_period': 300, 'alarm_type': 16,
699 'vrops_alarm': 'CPU_Utilization_Above_Thr',
700 'enabled': True, 'period': 300,
701 'resource_kind': 'VirtualMachine',
702 'alarm_subType': 19, 'action': 'acknowledge',
703 'evaluation': 1, 'unit': 'msec'},
704 {'metric_key': 'cpu|usage_average', 'unit': '%'}
706 #set mocked function return values
707 m_get_alarm_defination_by_name
.return_value
= []
708 m_create_symptom
.return_value
= 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804'
709 m_create_alarm_definition
.return_value
= 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
710 m_get_vm_moref_id
.return_value
= None
712 #Call configure_alarm method under test
713 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
715 #Verify that mocked methods are called with correct parameters
716 self
.assertEqual(m_get_default_Params
.call_count
, 2)
717 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
718 m_create_symptom
.assert_called_with(symptom_params
)
719 m_create_alarm_definition
.assert_called_with(alarm_params
)
720 m_get_vm_moref_id
.assert_called_with(config_dict
['resource_uuid'])
721 m_get_vm_resource_id
.assert_not_called()
722 m_create_alarm_notification_rule
.assert_not_called()
724 #Verify return value with expected value of alarm_def_uuid
725 self
.assertEqual(return_value
, alarm_def_uuid
)
728 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
729 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
730 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
731 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
732 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
733 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
734 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
735 def test_configure_alarm_failed_to_get_vm_resource_id(self
, m_get_default_Params
,\
736 m_get_alarm_defination_by_name
,\
738 m_create_alarm_definition
,\
740 m_get_vm_resource_id
,\
741 m_create_alarm_notification_rule
):
742 """Test configure alarm: failed to get vm resource_id"""
744 #Mock input configuration dictionary
745 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
746 'alarm_name': 'CPU_Utilization_Above_Threshold',
747 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
748 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
749 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
750 'operation': 'GT', 'unit': '%',
751 'description': 'CPU_Utilization_Above_Threshold'}
753 #symptom parameters to be passed for symptom creation
754 symptom_params
= {'threshold_value': 0,
756 'adapter_kind_key': 'VMWARE',
757 'resource_kind_key': 'VirtualMachine',
758 'severity': 'CRITICAL',
759 'symptom_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
762 'metric_key': 'cpu|usage_average'}
764 #alarm parameters to be passed for alarm creation
765 alarm_params
= {'description': 'CPU_Utilization_Above_Threshold',
766 'cancelCycles': 1, 'subType': 19,
767 'waitCycles': 1, 'severity': 'CRITICAL',
768 'impact': 'risk', 'adapterKindKey': 'VMWARE',
769 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
770 'resourceKindKey': 'VirtualMachine',
771 'symptomDefinitionId': 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804',
774 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
775 vm_moref_id
= 'vm-6626'
776 alarm_def_uuid
= None
778 #Mock default Parameters for alarm & metric configuration
779 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
780 'adapter_kind': 'VMWARE', 'repeat': False,
781 'cancel_period': 300, 'alarm_type': 16,
782 'vrops_alarm': 'CPU_Utilization_Above_Thr',
783 'enabled': True, 'period': 300,
784 'resource_kind': 'VirtualMachine',
785 'alarm_subType': 19, 'action': 'acknowledge',
786 'evaluation': 1, 'unit': 'msec'},
787 {'metric_key': 'cpu|usage_average', 'unit': '%'}
789 #set mocked function return values
790 m_get_alarm_defination_by_name
.return_value
= []
791 m_create_symptom
.return_value
= 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804'
792 m_create_alarm_definition
.return_value
= 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
793 m_get_vm_moref_id
.return_value
= vm_moref_id
794 m_get_vm_resource_id
.return_value
= None
796 #Call configure_alarm method under test
797 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
799 #Verify that mocked methods are called with correct parameters
800 self
.assertEqual(m_get_default_Params
.call_count
, 2)
801 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
802 m_create_symptom
.assert_called_with(symptom_params
)
803 m_create_alarm_definition
.assert_called_with(alarm_params
)
804 m_get_vm_moref_id
.assert_called_with(config_dict
['resource_uuid'])
805 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
806 m_create_alarm_notification_rule
.assert_not_called()
808 #Verify return value with expected value of alarm_def_uuid
809 self
.assertEqual(return_value
, alarm_def_uuid
)
812 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_notification_rule')
813 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
814 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
815 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_alarm_definition')
816 @mock.patch
.object(monPlugin
.MonPlugin
, 'create_symptom')
817 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_by_name')
818 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
819 def test_configure_alarm_failed_to_create_alarm_notification_rule(self
, m_get_default_Params
,\
820 m_get_alarm_defination_by_name
,\
822 m_create_alarm_definition
,\
824 m_get_vm_resource_id
,\
825 m_create_alarm_notification_rule
):
826 """Test configure alarm: failed to create alarm notification rule"""
828 #Mock input configuration dictionary
829 config_dict
= {'threshold_value': 0, 'severity': 'CRITICAL',
830 'alarm_name': 'CPU_Utilization_Above_Threshold',
831 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
832 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
833 'statistic': 'AVERAGE', 'metric_name': 'CPU_UTILIZATION',
834 'operation': 'GT', 'unit': '%',
835 'description': 'CPU_Utilization_Above_Threshold'}
837 #symptom parameters to be passed for symptom creation
838 symptom_params
= {'threshold_value': 0,
840 'adapter_kind_key': 'VMWARE',
841 'resource_kind_key': 'VirtualMachine',
842 'severity': 'CRITICAL',
843 'symptom_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
846 'metric_key': 'cpu|usage_average'}
848 #alarm parameters to be passed for alarm creation
849 alarm_params
= {'description': 'CPU_Utilization_Above_Threshold',
850 'cancelCycles': 1, 'subType': 19,
851 'waitCycles': 1, 'severity': 'CRITICAL',
852 'impact': 'risk', 'adapterKindKey': 'VMWARE',
853 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
854 'resourceKindKey': 'VirtualMachine',
855 'symptomDefinitionId': 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804',
858 vrops_alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
859 vm_moref_id
= 'vm-6626'
860 alarm_def
= 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
861 resource_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
862 alarm_def_uuid
= None
864 #Mock default Parameters for alarm & metric configuration
865 m_get_default_Params
.side_effect
= [{'impact': 'risk', 'cancel_cycles': 1,
866 'adapter_kind': 'VMWARE', 'repeat': False,
867 'cancel_period': 300, 'alarm_type': 16,
868 'vrops_alarm': 'CPU_Utilization_Above_Thr',
869 'enabled': True, 'period': 300,
870 'resource_kind': 'VirtualMachine',
871 'alarm_subType': 19, 'action': 'acknowledge',
872 'evaluation': 1, 'unit': 'msec'},
873 {'metric_key': 'cpu|usage_average', 'unit': '%'}
875 #set mocked function return values
876 m_get_alarm_defination_by_name
.return_value
= []
877 m_create_symptom
.return_value
= 'SymptomDefinition-2e8f9ddc-9f7b-4cd6-b85d-7d7fe3a8a804'
878 m_create_alarm_definition
.return_value
= 'AlertDefinition-0f3cdcb3-4e1b-4a0b-86d0-66d4b3f65220'
879 m_get_vm_moref_id
.return_value
= vm_moref_id
880 m_get_vm_resource_id
.return_value
= 'ac87622f-b761-40a0-b151-00872a2a456e'
881 m_create_alarm_notification_rule
.return_value
= None
883 #Call configure_alarm method under test
884 return_value
= self
.mon_plugin
.configure_alarm(config_dict
)
886 #Verify that mocked methods are called with correct parameters
887 self
.assertEqual(m_get_default_Params
.call_count
, 2)
888 m_get_alarm_defination_by_name
.assert_called_with(vrops_alarm_name
)
889 m_create_symptom
.assert_called_with(symptom_params
)
890 m_create_alarm_definition
.assert_called_with(alarm_params
)
891 m_get_vm_moref_id
.assert_called_with(config_dict
['resource_uuid'])
892 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
893 m_create_alarm_notification_rule
.assert_called_with(vrops_alarm_name
, alarm_def
, resource_id
)
895 #Verify return value with expected value of alarm_def_uuid
896 self
.assertEqual(return_value
, alarm_def_uuid
)
899 @mock.patch
.object(monPlugin
.requests
, 'get')
900 def test_get_alarm_defination_details_valid_rest_req_response(self
, m_get
):
901 """Test get_alarm_defination_details: For a valid REST request response"""
903 alarm_uuid
= '9a6d8a14-9f25-4d81-bf91-4d773497444d'
905 #Set mocked function's return values
906 m_get
.return_value
.status_code
= 200
907 m_get
.return_value
.content
= '{"id":"AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d",\
908 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
909 "description":"CPU_Utilization_Above_Threshold",\
910 "adapterKindKey":"VMWARE","resourceKindKey":"VirtualMachine",\
911 "waitCycles":1,"cancelCycles":1,"type":16,"subType":19,\
912 "states":[{"severity":"CRITICAL","base-symptom-set":{"type":"SYMPTOM_SET",\
913 "relation":"SELF","aggregation":"ALL","symptomSetOperator":"AND",\
914 "symptomDefinitionIds":["SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278"]},\
915 "impact":{"impactType":"BADGE","detail":"risk"}}]}'
917 expected_alarm_details
= {'adapter_kind': 'VMWARE',
918 'symptom_definition_id': 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278',
919 'alarm_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
920 'alarm_id': 'AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d',
921 'resource_kind': 'VirtualMachine', 'type': 16, 'sub_type': 19}
923 expected_alarm_details_json
= {'states':
925 {'impactType':'BADGE', 'detail':'risk'},'severity':'CRITICAL',
926 'base-symptom-set': {'symptomDefinitionIds':\
927 ['SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'],
928 'relation': 'SELF', 'type': 'SYMPTOM_SET',
929 'aggregation':'ALL', 'symptomSetOperator': 'AND'}}],
930 'adapterKindKey': 'VMWARE',
931 'description': 'CPU_Utilization_Above_Threshold',
932 'type': 16, 'cancelCycles': 1, 'resourceKindKey': 'VirtualMachine',
933 'subType': 19, 'waitCycles': 1,
934 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d',
935 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'}
937 #Call get_alarm_defination_details method under test
938 alarm_details_json
, alarm_details
= self
.mon_plugin
.get_alarm_defination_details(alarm_uuid
)
940 #Verify that mocked method is called
941 m_get
.assert_called()
943 #Verify return value with expected value
944 self
.assertEqual(expected_alarm_details
, alarm_details
)
945 self
.assertEqual(expected_alarm_details_json
, alarm_details_json
)
948 @mock.patch
.object(monPlugin
.requests
, 'get')
949 def test_get_alarm_defination_details_invalid_rest_req_response(self
, m_get
):
950 """Test get_alarm_defination_details: For an invalid REST request response"""
952 alarm_uuid
= '9a6d8a14-9f25-4d81-bf91-4d773497444d'
954 #Set mocked function's return values
955 m_get
.return_value
.status_code
= 404
956 m_get
.return_value
.content
= '{"message": "No such AlertDefinition - \
957 AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444.",\
958 "httpStatusCode": 404,"apiErrorCode": 404}'
960 expected_alarm_details
= None
961 expected_alarm_details_json
= None
963 #Call get_alarm_defination_details method under test
964 alarm_details_json
, alarm_details
= self
.mon_plugin
.get_alarm_defination_details(alarm_uuid
)
966 #verify that mocked method is called
967 m_get
.assert_called()
969 #Verify return value with expected value
970 self
.assertEqual(expected_alarm_details
, alarm_details
)
971 self
.assertEqual(expected_alarm_details_json
, alarm_details_json
)
974 @mock.patch
.object(monPlugin
.requests
, 'get')
975 def test_get_alarm_defination_by_name_valid_rest_req_response(self
, m_get
):
976 """Test get_alarm_defination_by_name: For a valid REST request response"""
978 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
980 #Set mocked function's return values
981 m_get
.return_value
.status_code
= 200
982 m_get
.return_value
.content
= '{"pageInfo": {"totalCount": 1,"page": 0,"pageSize": 1000},\
984 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
985 "rel": "SELF","name": "current"},\
986 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
987 "rel": "RELATED","name": "first"},\
988 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
989 "rel": "RELATED","name": "last"}],\
990 "alertDefinitions": [{\
991 "id": "AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d",\
992 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
993 "description": "CPU_Utilization_Above_Threshold",\
994 "adapterKindKey": "VMWARE","resourceKindKey": "VirtualMachine",\
995 "waitCycles": 1,"cancelCycles": 1,"type": 16,"subType": 19,\
996 "states": [{"impact": {"impactType": "BADGE","detail": "risk"},\
997 "severity": "CRITICAL",\
998 "base-symptom-set": {"type": "SYMPTOM_SET",\
999 "relation": "SELF","aggregation": "ALL",\
1000 "symptomSetOperator": "AND",\
1001 "symptomDefinitionIds": [\
1002 "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278"]}}]\
1005 #Expected return match list
1006 Exp_alert_match_list
= [{'states':
1007 [{'impact': {'impactType': 'BADGE', 'detail': 'risk'},
1008 'severity': 'CRITICAL',
1009 'base-symptom-set': {
1010 'symptomDefinitionIds': \
1011 ['SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'],
1013 'type': 'SYMPTOM_SET',
1014 'aggregation': 'ALL',
1015 'symptomSetOperator': 'AND'}
1017 'adapterKindKey': 'VMWARE',
1018 'description': 'CPU_Utilization_Above_Threshold',
1021 'resourceKindKey': 'VirtualMachine',
1022 'subType': 19, 'waitCycles': 1,
1023 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d',
1025 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
1028 #Call get_alarm_defination_by_name method under test
1029 alert_match_list
= self
.mon_plugin
.get_alarm_defination_by_name(alarm_name
)
1031 #Verify that mocked method is called
1032 m_get
.assert_called()
1034 #Verify return value with expected value
1035 self
.assertEqual(Exp_alert_match_list
, alert_match_list
)
1038 @mock.patch
.object(monPlugin
.requests
, 'get')
1039 # @unittest.skip("NEEDS FIX")
1040 def test_get_alarm_defination_by_name_no_valid_alarm_found(self
, m_get
):
1041 """Test get_alarm_defination_by_name: With no valid alarm found in returned list"""
1043 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda5'
1045 #Set mocked function's return values
1046 m_get
.return_value
.status_code
= 200
1047 m_get
.return_value
.content
= '{"pageInfo": {"totalCount": 1,"page": 0,"pageSize": 1000},\
1049 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
1050 "rel": "SELF","name": "current"},\
1051 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
1052 "rel": "RELATED","name": "first"},\
1053 {"href": "/suite-api/api/alertdefinitions?page=0&pageSize=1000",\
1054 "rel": "RELATED","name": "last"}],\
1055 "alertDefinitions": [{\
1056 "id": "AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d",\
1057 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1058 "description": "CPU_Utilization_Above_Threshold",\
1059 "adapterKindKey": "VMWARE","resourceKindKey": "VirtualMachine",\
1060 "waitCycles": 1,"cancelCycles": 1,"type": 16,"subType": 19,\
1061 "states": [{"impact": {"impactType": "BADGE","detail": "risk"},\
1062 "severity": "CRITICAL",\
1063 "base-symptom-set": {"type": "SYMPTOM_SET",\
1064 "relation": "SELF","aggregation": "ALL",\
1065 "symptomSetOperator": "AND",\
1066 "symptomDefinitionIds": [\
1067 "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278"]}}]\
1070 #Expected return match list
1071 Exp_alert_match_list
= []
1073 #Call get_alarm_defination_by_name method under test
1074 alert_match_list
= self
.mon_plugin
.get_alarm_defination_by_name(alarm_name
)
1076 #Verify that mocked method is called
1077 m_get
.assert_called()
1079 #Verify return value with expected value
1080 self
.assertEqual(Exp_alert_match_list
, alert_match_list
)
1083 @mock.patch
.object(monPlugin
.requests
, 'put')
1084 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_symptom_defination_details')
1085 def test_update_symptom_defination_valid_symptom_req_response(self
,\
1086 m_get_symptom_defination_details
,\
1088 """Test update_symptom_defination: With valid REST response, update symptom"""
1090 #Expected symptom to be updated
1091 symptom_defination_id
= 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'
1092 new_alarm_config
= {'severity':"CRITICAL",
1094 'threshold_value':5,
1095 'alarm_uuid':'9a6d8a14-9f25-4d81-bf91-4d773497444d'
1098 #Set mocked function's return values
1099 m_get_symptom_defination_details
.return_value
= {
1100 "id": "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278",
1101 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",
1102 "adapterKindKey": "VMWARE",
1103 "resourceKindKey": "VirtualMachine",
1106 "state": {"severity": "CRITICAL",
1108 "type": "CONDITION_HT",
1109 "key": "cpu|usage_average","operator": "GT","value": "0.0",
1110 "valueType": "NUMERIC","instanced": False,
1111 "thresholdType": "STATIC"}
1115 m_put
.return_value
.status_code
= 200
1116 m_put
.return_value
.content
= '{\
1117 "id":"SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278",\
1118 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1119 "adapterKindKey":"VMWARE","resourceKindKey":"VirtualMachine","waitCycles":1,\
1122 "severity":"CRITICAL",\
1124 "type":"CONDITION_HT","key":"cpu|usage_average","operator":"GT","value":"5.0",\
1125 "valueType":"NUMERIC","instanced":False,"thresholdType":"STATIC"}}}'
1127 #Call update_symptom_defination method under test
1128 symptom_uuid
= self
.mon_plugin
.update_symptom_defination(symptom_defination_id
,\
1131 #Verify that mocked method is called with required parameters
1132 m_get_symptom_defination_details
.assert_called_with(symptom_defination_id
)
1133 m_put
.assert_called()
1135 #Verify return value with expected value
1136 self
.assertEqual(symptom_defination_id
, symptom_uuid
)
1139 @mock.patch
.object(monPlugin
.requests
, 'put')
1140 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_symptom_defination_details')
1141 def test_update_symptom_defination_invalid_symptom_req_response(self
,\
1142 m_get_symptom_defination_details
,\
1144 """Test update_symptom_defination: If invalid REST response received, return None"""
1146 #Expected symptom to be updated
1147 symptom_defination_id
= 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'
1148 new_alarm_config
= {'severity':"CRITICAL",
1150 'threshold_value':5,
1151 'alarm_uuid':'9a6d8a14-9f25-4d81-bf91-4d773497444d'
1154 #Set mocked function's return values
1155 m_get_symptom_defination_details
.return_value
= {
1156 "id": "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278",
1157 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",
1158 "adapterKindKey": "VMWARE",
1159 "resourceKindKey": "VirtualMachine",
1162 "state": {"severity": "CRITICAL",
1164 "type": "CONDITION_HT",
1165 "key": "cpu|usage_average","operator": "GT","value": "0.0",
1166 "valueType": "NUMERIC","instanced": False,
1167 "thresholdType": "STATIC"}
1171 m_put
.return_value
.status_code
= 500
1172 m_put
.return_value
.content
= '{\
1173 "message": "Internal Server error, cause unknown.",\
1174 "moreInformation": [\
1175 {"name": "errorMessage",\
1176 "value": "Symptom Definition CPU_Utilization_Above_Thr-e14b203c-\
1177 6bf2-4e2f-a91c-8c19d240eda4 does not exist and hence cannot be updated."},\
1178 {"name": "localizedMessage",\
1179 "value": "Symptom Definition CPU_Utilization_Above_Thr-e14b203c-\
1180 6bf2-4e2f-a91c-8c19d240eda4 does not exist and hence cannot be updated.;"}],\
1181 "httpStatusCode": 500,"apiErrorCode": 500}'
1183 #Call update_symptom_defination method under test
1184 symptom_uuid
= self
.mon_plugin
.update_symptom_defination(symptom_defination_id
,\
1187 #Verify that mocked method is called with required parameters
1188 m_get_symptom_defination_details
.assert_called_with(symptom_defination_id
)
1189 m_put
.assert_called()
1191 #Verify return value with expected value
1192 self
.assertEqual(symptom_uuid
, None)
1195 @mock.patch
.object(monPlugin
.requests
, 'put')
1196 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_symptom_defination_details')
1197 def test_update_symptom_defination_failed_to_get_symptom_defination(self
,\
1198 m_get_symptom_defination_details
,\
1200 """Test update_symptom_defination: if fails to get symptom_defination returns None"""
1202 #Expected symptom to be updated
1203 symptom_defination_id
= 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'
1204 new_alarm_config
= {'severity':"CRITICAL",
1206 'threshold_value':5,
1207 'alarm_uuid':'9a6d8a14-9f25-4d81-bf91-4d773497444d'
1210 #Set mocked function's return values
1211 m_get_symptom_defination_details
.return_value
= None
1213 #Call update_symptom_defination method under test
1214 symptom_uuid
= self
.mon_plugin
.update_symptom_defination(symptom_defination_id
,\
1217 #Verify that mocked method is called with required parameters
1218 m_get_symptom_defination_details
.assert_called_with(symptom_defination_id
)
1219 m_put
.assert_not_called()
1221 #Verify return value with expected value
1222 self
.assertEqual(symptom_uuid
, None)
1225 @mock.patch
.object(monPlugin
.requests
, 'get')
1226 def test_get_symptom_defination_details_valid_req_response(self
,m_get
):
1227 """Test update_symptom_defination: With valid REST response symptom is created"""
1229 #Expected symptom to be updated
1230 symptom_uuid
= 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'
1232 #Set mocked function's return values
1233 m_get
.return_value
.status_code
= 200
1234 m_get
.return_value
.content
= '{\
1235 "id": "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278",\
1236 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1237 "adapterKindKey": "VMWARE","resourceKindKey": "VirtualMachine","waitCycles": 1,\
1238 "cancelCycles": 1,"state": {"severity": "CRITICAL","condition": {"type": "CONDITION_HT",\
1239 "key": "cpu|usage_average","operator": "GT","value": "6.0","valueType": "NUMERIC",\
1240 "instanced": false,"thresholdType": "STATIC"}}}'
1241 expected_symptom_details
= {\
1242 "id": "SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278",\
1243 "name": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1244 "adapterKindKey": "VMWARE","resourceKindKey": "VirtualMachine","waitCycles": 1,\
1245 "cancelCycles": 1,"state": {"severity": "CRITICAL","condition": {"type": "CONDITION_HT",\
1246 "key": "cpu|usage_average","operator": "GT","value": "6.0","valueType": "NUMERIC",\
1247 "instanced": False,"thresholdType": "STATIC"}}}
1249 #Call update_symptom_defination method under test
1250 symptom_details
= self
.mon_plugin
.get_symptom_defination_details(symptom_uuid
)
1252 #Verify that mocked method is called with required parameters
1253 m_get
.assert_called()
1255 #Verify return value with expected value
1256 self
.assertEqual(expected_symptom_details
, symptom_details
)
1259 @mock.patch
.object(monPlugin
.requests
, 'get')
1260 def test_get_symptom_defination_details_invalid_req_response(self
,m_get
):
1261 """Test update_symptom_defination: if invalid REST response received return None"""
1263 #Expected symptom to be updated
1264 symptom_uuid
= 'SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'
1266 #Set mocked function's return values
1267 m_get
.return_value
.status_code
= 404
1268 m_get
.return_value
.content
= '{"message": "No such SymptomDefinition\
1269 - SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278.",\
1270 "httpStatusCode": 404,"apiErrorCode": 404}'
1272 expected_symptom_details
= None
1274 #Call update_symptom_defination method under test
1275 symptom_details
= self
.mon_plugin
.get_symptom_defination_details(symptom_uuid
)
1277 #Verify that mocked method is called with required parameters
1278 m_get
.assert_called()
1280 #Verify return value with expected value
1281 self
.assertEqual(expected_symptom_details
, symptom_details
)
1284 @mock.patch
.object(monPlugin
.requests
, 'get')
1285 def test_get_symptom_defination_details_symptom_uuid_not_provided(self
,m_get
):
1286 """Test update_symptom_defination: if required symptom uuid is not provided"""
1288 #Expected symptom to be updated
1290 expected_symptom_details
= None
1292 #Call update_symptom_defination method under test
1293 symptom_details
= self
.mon_plugin
.get_symptom_defination_details(symptom_uuid
)
1295 #Verify that mocked method is called with required parameters
1296 m_get
.assert_not_called()
1298 #Verify return value with expected value
1299 self
.assertEqual(expected_symptom_details
, symptom_details
)
1302 @mock.patch
.object(monPlugin
.requests
, 'put')
1303 def test_reconfigure_alarm_valid_req_response(self
, m_put
):
1304 """Test reconfigure_alarm: for valid REST response"""
1306 #Set input parameters to reconfigure_alarm
1307 alarm_details_json
= {
1308 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d',
1309 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
1310 'description': 'CPU_Utilization_Above_Threshold', 'adapterKindKey': 'VMWARE',
1311 'states':[{'impact':{'impactType':'BADGE', 'detail':'risk'}, 'severity':'CRITICAL',
1312 'base-symptom-set':{
1313 'symptomDefinitionIds':['SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'],
1314 'relation': 'SELF','type': 'SYMPTOM_SET', 'aggregation':'ALL',
1315 'symptomSetOperator': 'AND'}}],
1316 'type': 16, 'cancelCycles': 1, 'resourceKindKey': 'VirtualMachine','subType': 19,
1319 new_alarm_config
= {'severity':'WARNING',
1320 'description': 'CPU_Utilization_Above_Threshold_Warning'}
1322 #Set mocked function's return values
1323 m_put
.return_value
.status_code
= 200
1324 m_put
.return_value
.content
= '{"id":"AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d",\
1325 "name":"CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1326 "description":"CPU_Utilization_Above_Threshold_Warning","adapterKindKey":"VMWARE",\
1327 "resourceKindKey":"VirtualMachine","waitCycles":1,"cancelCycles":1,"type":16,\
1328 "subType":19,"states":[{"severity":"WARNING","base-symptom-set":{"type":"SYMPTOM_SET",\
1329 "relation":"SELF","aggregation":"ALL","symptomSetOperator":"AND",\
1330 "symptomDefinitionIds":["SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278"]},\
1331 "impact":{"impactType":"BADGE","detail":"risk"}}]}'
1333 #Expected alarm_def_uuid to be returned
1334 expected_alarm_def_uuid
= '9a6d8a14-9f25-4d81-bf91-4d773497444d'
1336 #Call reconfigure_alarm method under test
1337 alarm_def_uuid
= self
.mon_plugin
.reconfigure_alarm(alarm_details_json
, new_alarm_config
)
1339 #Verify that mocked method is called with required parameters
1340 m_put
.assert_called()
1342 #Verify return value with expected value
1343 self
.assertEqual(expected_alarm_def_uuid
, alarm_def_uuid
)
1346 @mock.patch
.object(monPlugin
.requests
, 'put')
1347 def test_reconfigure_alarm_invalid_req_response(self
, m_put
):
1348 """Test reconfigure_alarm: for invalid REST response, return None"""
1350 #Set input parameters to reconfigure_alarm
1351 alarm_details_json
= {
1352 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-bf91-4d773497444d',
1353 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
1354 'description': 'CPU_Utilization_Above_Threshold', 'adapterKindKey': 'VMWARE',
1355 'states':[{'impact':{'impactType':'BADGE', 'detail':'risk'}, 'severity':'CRITICAL',
1356 'base-symptom-set':{
1357 'symptomDefinitionIds':['SymptomDefinition-bcc2cb36-a67b-4deb-bcd3-9b5884973278'],
1358 'relation': 'SELF','type': 'SYMPTOM_SET', 'aggregation':'ALL',
1359 'symptomSetOperator': 'AND'}}],
1360 'type': 16, 'cancelCycles': 1, 'resourceKindKey': 'VirtualMachine','subType': 19,
1363 new_alarm_config
= {'severity':'WARNING',
1364 'description': 'CPU_Utilization_Above_Threshold_Warning'}
1366 #Set mocked function's return values
1367 m_put
.return_value
.status_code
= 500
1368 m_put
.return_value
.content
= '{"message": "Internal Server error, cause unknown.",\
1369 "moreInformation": [{"name": "errorMessage",\
1370 "value": "Cannot update Alert Definition CPU_Utilization_Above_Thr-\
1371 e14b203c-6bf2-4e2f-a91c-8c19d240eda4 since it does not exist"},\
1372 {"name": "localizedMessage",\
1373 "value": "Cannot update Alert Definition CPU_Utilization_Above_Thr-\
1374 e14b203c-6bf2-4e2f-a91c-8c19d240eda4 since it does not exist;"}],\
1375 "httpStatusCode": 500,"apiErrorCode": 500}'
1377 #Expected alarm_def_uuid to be returned
1378 expected_alarm_def_uuid
= None
1380 #Call reconfigure_alarm method under test
1381 alarm_def_uuid
= self
.mon_plugin
.reconfigure_alarm(alarm_details_json
, new_alarm_config
)
1383 #Verify that mocked method is called with required parameters
1384 m_put
.assert_called()
1386 #Verify return value with expected value
1387 self
.assertEqual(expected_alarm_def_uuid
, alarm_def_uuid
)
1390 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_symptom_definition')
1391 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_alarm_defination')
1392 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_notification_rule')
1393 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
1394 def test_delete_alarm_configuration_successful_alarm_deletion(self
,\
1395 m_get_alarm_defination_details
,\
1396 m_delete_notification_rule
,\
1397 m_delete_alarm_defination
,\
1398 m_delete_symptom_definition
):
1399 """Test delete_alarm_configuration: for successful alarm deletion, return alarm uuid"""
1401 #Set input parameters to delete_alarm_configuration
1402 delete_alarm_req_dict
= {'alarm_uuid':'9a6d8a14-9f25-4d81-bf91-4d773497444d'}
1404 #Set mocked function's return values
1405 alarm_details_json
= {
1406 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1407 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1408 'symptomDefinitionIds':['SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278']}
1410 'alarm_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1411 'alarm_id':'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1412 'symptom_definition_id':'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'}
1414 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
1415 m_delete_notification_rule
.return_value
= '989e7293-d78d-4405-92e30ec4f247'
1416 m_delete_alarm_defination
.return_value
= alarm_details
['alarm_id']
1417 m_delete_symptom_definition
.return_value
= alarm_details
['symptom_definition_id']
1419 #Call reconfigure_alarm method under test
1420 alarm_uuid
= self
.mon_plugin
.delete_alarm_configuration(delete_alarm_req_dict
)
1422 #Verify that mocked method is called with required parameters
1423 m_get_alarm_defination_details
.assert_called_with(delete_alarm_req_dict
['alarm_uuid'])
1424 m_delete_notification_rule
.assert_called_with(alarm_details
['alarm_name'])
1425 m_delete_alarm_defination
.assert_called_with(alarm_details
['alarm_id'])
1426 m_delete_symptom_definition
.assert_called_with(alarm_details
['symptom_definition_id'])
1428 #Verify return value with expected value
1429 self
.assertEqual(alarm_uuid
, delete_alarm_req_dict
['alarm_uuid'])
1432 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_symptom_definition')
1433 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_alarm_defination')
1434 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_notification_rule')
1435 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
1436 def test_delete_alarm_configuration_failed_to_get_alarm_defination(self
,\
1437 m_get_alarm_defination_details
,\
1438 m_delete_notification_rule
,\
1439 m_delete_alarm_defination
,\
1440 m_delete_symptom_definition
):
1441 """Test delete_alarm_configuration: if failed to get alarm definition, return None"""
1443 #Set input parameters to delete_alarm_configuration
1444 delete_alarm_req_dict
= {'alarm_uuid':'9a6d8a14-9f25-4d81-bf91-4d773497444d'}
1446 #Set mocked function's return values
1447 alarm_details_json
= None
1448 alarm_details
= None
1450 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
1452 #Call reconfigure_alarm method under test
1453 alarm_uuid
= self
.mon_plugin
.delete_alarm_configuration(delete_alarm_req_dict
)
1455 #Verify that mocked method is called with required parameters
1456 m_get_alarm_defination_details
.assert_called_with(delete_alarm_req_dict
['alarm_uuid'])
1457 m_delete_notification_rule
.assert_not_called()
1458 m_delete_alarm_defination
.assert_not_called()
1459 m_delete_symptom_definition
.assert_not_called()
1461 #Verify return value with expected value
1462 self
.assertEqual(alarm_uuid
, None)
1465 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_symptom_definition')
1466 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_alarm_defination')
1467 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_notification_rule')
1468 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
1469 def test_delete_alarm_configuration_failed_to_delete_notification_rule(self
,\
1470 m_get_alarm_defination_details
,\
1471 m_delete_notification_rule
,\
1472 m_delete_alarm_defination
,\
1473 m_delete_symptom_definition
):
1474 """Test delete_alarm_configuration: if failed to delete notification rule, return None"""
1476 #Set input parameters to delete_alarm_configuration
1477 delete_alarm_req_dict
= {'alarm_uuid':'9a6d8a14-9f25-4d81-bf91-4d773497444d'}
1479 #Set mocked function's return values
1480 alarm_details_json
= {
1481 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1482 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1483 'symptomDefinitionIds':['SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278']}
1485 'alarm_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1486 'alarm_id':'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1487 'symptom_definition_id':'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'}
1489 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
1490 m_delete_notification_rule
.return_value
= None
1492 #Call reconfigure_alarm method under test
1493 alarm_uuid
= self
.mon_plugin
.delete_alarm_configuration(delete_alarm_req_dict
)
1495 #Verify that mocked method is called with required parameters
1496 m_get_alarm_defination_details
.assert_called_with(delete_alarm_req_dict
['alarm_uuid'])
1497 m_delete_notification_rule
.assert_called_with(alarm_details
['alarm_name'])
1498 m_delete_alarm_defination
.assert_not_called()
1499 m_delete_symptom_definition
.assert_not_called()
1501 #Verify return value with expected value
1502 self
.assertEqual(alarm_uuid
, None)
1505 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_symptom_definition')
1506 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_alarm_defination')
1507 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_notification_rule')
1508 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
1509 def test_delete_alarm_configuration_failed_to_delete_alarm_defination(self
,\
1510 m_get_alarm_defination_details
,\
1511 m_delete_notification_rule
,\
1512 m_delete_alarm_defination
,\
1513 m_delete_symptom_definition
):
1514 """Test delete_alarm_configuration: if failed to delete alarm definition, return None"""
1516 #Set input parameters to delete_alarm_configuration
1517 delete_alarm_req_dict
= {'alarm_uuid':'9a6d8a14-9f25-4d81-bf91-4d773497444d'}
1519 #Set mocked function's return values
1520 alarm_details_json
= {
1521 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1522 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1523 'symptomDefinitionIds':['SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278']}
1525 'alarm_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1526 'alarm_id':'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1527 'symptom_definition_id':'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'}
1529 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
1530 m_delete_notification_rule
.return_value
= '989e7293-d78d-4405-92e30ec4f247'
1531 m_delete_alarm_defination
.return_value
= None
1533 #Call reconfigure_alarm method under test
1534 alarm_uuid
= self
.mon_plugin
.delete_alarm_configuration(delete_alarm_req_dict
)
1536 #Verify that mocked method is called with required parameters
1537 m_get_alarm_defination_details
.assert_called_with(delete_alarm_req_dict
['alarm_uuid'])
1538 m_delete_notification_rule
.assert_called_with(alarm_details
['alarm_name'])
1539 m_delete_alarm_defination
.assert_called_with(alarm_details
['alarm_id'])
1540 m_delete_symptom_definition
.assert_not_called()
1542 #Verify return value with expected value
1543 self
.assertEqual(alarm_uuid
, None)
1546 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_symptom_definition')
1547 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_alarm_defination')
1548 @mock.patch
.object(monPlugin
.MonPlugin
, 'delete_notification_rule')
1549 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
1550 def test_delete_alarm_configuration_failed_to_delete_symptom_definition(self
,\
1551 m_get_alarm_defination_details
,\
1552 m_delete_notification_rule
,\
1553 m_delete_alarm_defination
,\
1554 m_delete_symptom_definition
):
1555 """Test delete_alarm_configuration: if failed to delete symptom definition, return None"""
1557 #Set input parameters to delete_alarm_configuration
1558 delete_alarm_req_dict
= {'alarm_uuid':'9a6d8a14-9f25-4d81-bf91-4d773497444d'}
1560 #Set mocked function's return values
1561 alarm_details_json
= {
1562 'id': 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1563 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1564 'symptomDefinitionIds':['SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278']}
1566 'alarm_name':'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4',
1567 'alarm_id':'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d',
1568 'symptom_definition_id':'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'}
1570 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
1571 m_delete_notification_rule
.return_value
= '989e7293-d78d-4405-92e30ec4f247'
1572 m_delete_alarm_defination
.return_value
= alarm_details
['alarm_id']
1573 m_delete_symptom_definition
.return_value
= None
1575 #Call reconfigure_alarm method under test
1576 alarm_uuid
= self
.mon_plugin
.delete_alarm_configuration(delete_alarm_req_dict
)
1578 #Verify that mocked method is called with required parameters
1579 m_get_alarm_defination_details
.assert_called_with(delete_alarm_req_dict
['alarm_uuid'])
1580 m_delete_notification_rule
.assert_called_with(alarm_details
['alarm_name'])
1581 m_delete_alarm_defination
.assert_called_with(alarm_details
['alarm_id'])
1582 m_delete_symptom_definition
.assert_called_with(alarm_details
['symptom_definition_id'])
1584 #Verify return value with expected value
1585 self
.assertEqual(alarm_uuid
, None)
1588 @mock.patch
.object(monPlugin
.requests
, 'delete')
1589 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_notification_rule_id_by_alarm_name')
1590 def test_delete_notification_rule_successful_deletion_req_response(self
,\
1591 m_get_notification_rule_id_by_alarm_name
,\
1593 """Test delete_notification_rule: Valid notification rule is deleted & returns rule_id"""
1595 #Set input parameters to delete_notification_rule
1596 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4'
1598 #Set mocked function's return values
1599 m_get_notification_rule_id_by_alarm_name
.return_value
= '8db86441-71d8-4830-9e1a-a90be3776d12'
1600 m_delete
.return_value
.status_code
= 204
1602 #Call delete_notification_rule method under test
1603 rule_id
= self
.mon_plugin
.delete_notification_rule(alarm_name
)
1605 #Verify that mocked method is called with required parameters
1606 m_get_notification_rule_id_by_alarm_name
.assert_called_with(alarm_name
)
1607 m_delete
.assert_called()
1609 #Verify return value with expected value
1610 self
.assertEqual(rule_id
, '8db86441-71d8-4830-9e1a-a90be3776d12')
1613 @mock.patch
.object(monPlugin
.requests
, 'delete')
1614 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_notification_rule_id_by_alarm_name')
1615 def test_delete_notification_rule_failed_to_get_notification_rule_id(self
,\
1616 m_get_notification_rule_id_by_alarm_name
,\
1618 """Test delete_notification_rule: if notification rule is not found, returns None"""
1620 #Set input parameters to delete_notification_rule
1621 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4'
1623 #Set mocked function's return values
1624 m_get_notification_rule_id_by_alarm_name
.return_value
= None
1626 #Call delete_notification_rule method under test
1627 rule_id
= self
.mon_plugin
.delete_notification_rule(alarm_name
)
1629 #Verify that mocked method is called with required parameters
1630 m_get_notification_rule_id_by_alarm_name
.assert_called_with(alarm_name
)
1631 m_delete
.assert_not_called()
1633 # verify return value with expected value
1634 self
.assertEqual(rule_id
, None)
1637 @mock.patch
.object(monPlugin
.requests
, 'delete')
1638 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_notification_rule_id_by_alarm_name')
1639 def test_delete_notification_rule_invalid_deletion_req_response(self
,\
1640 m_get_notification_rule_id_by_alarm_name
,\
1642 """Test delete_notification_rule: If an invalid response is received, returns None"""
1644 #Set input parameters to delete_notification_rule
1645 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-8c19d240eda4'
1647 #Set mocked function's return values
1648 m_get_notification_rule_id_by_alarm_name
.return_value
= '8db86441-71d8-4830-9e1a-a90be3776d12'
1649 m_delete
.return_value
.status_code
= 404
1651 #Call delete_notification_rule method under test
1652 rule_id
= self
.mon_plugin
.delete_notification_rule(alarm_name
)
1654 #Verify that mocked method is called with required parameters
1655 m_get_notification_rule_id_by_alarm_name
.assert_called_with(alarm_name
)
1656 m_delete
.assert_called()
1658 #Verify return value with expected value
1659 self
.assertEqual(rule_id
, None)
1662 @mock.patch
.object(monPlugin
.requests
, 'get')
1663 def test_get_notification_rule_id_by_alarm_name_valid_req_response(self
,m_get
):
1664 """Test get_notification_rule_id_by_alarm_name: A valid request response received,
1665 returns notification_id
1668 #Set input parameters to get_notification_rule_id_by_alarm_name
1669 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
1671 #Set mocked function's return values
1672 m_get
.return_value
.status_code
= 200
1673 m_get
.return_value
.content
= '{\
1674 "pageInfo": {"totalCount": 0,"page": 0,"pageSize": 1000},\
1676 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1677 "rel": "SELF","name": "current"},\
1678 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1679 "rel": "RELATED","name": "first"},\
1680 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1681 "rel": "RELATED","name": "last"}],\
1682 "notification-rule": [{\
1683 "id": "2b86fa23-0c15-445c-a2b1-7bd725c46f59",\
1684 "name": "notify_CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1685 "pluginId": "03053f51-f829-438d-993d-cc33a435d76a",\
1686 "links": [{"href": "/suite-api/api/notifications/rules/2b86fa23-0c15-445c-a2b1-7bd725c46f59",\
1687 "rel": "SELF","name": "linkToSelf"}]}]}'
1689 #Call get_notification_rule_id_by_alarm_name method under test
1690 notification_id
= self
.mon_plugin
.get_notification_rule_id_by_alarm_name(alarm_name
)
1692 #Verify that mocked method is called with required parameters
1693 m_get
.assert_called()
1695 #Verify return value with expected value
1696 self
.assertEqual(notification_id
, '2b86fa23-0c15-445c-a2b1-7bd725c46f59')
1699 @mock.patch
.object(monPlugin
.requests
, 'get')
1700 def test_get_notification_rule_id_by_alarm_name_invalid_req_response(self
,m_get
):
1701 """Test get_notification_rule_id_by_alarm_name: If an invalid response received,\
1705 #Set input parameters to delete_alarm_configuration
1706 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
1708 #Set mocked function's return values
1709 m_get
.return_value
.status_code
= 404
1711 #Call get_notification_rule_id_by_alarm_name method under test
1712 notification_id
= self
.mon_plugin
.get_notification_rule_id_by_alarm_name(alarm_name
)
1714 #Verify that mocked method is called with required parameters
1715 m_get
.assert_called()
1717 #Verify return value with expected value
1718 self
.assertEqual(notification_id
, None)
1721 @mock.patch
.object(monPlugin
.requests
, 'get')
1722 def test_get_notification_rule_id_by_alarm_name_rule_not_found(self
,m_get
):
1723 """Test get_notification_rule_id_by_alarm_name: If a notification rule is not found,
1727 #Set input parameters to delete_alarm_configuration
1728 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda'
1730 #Set mocked function's return values
1731 m_get
.return_value
.status_code
= 200
1732 m_get
.return_value
.content
= '{\
1733 "pageInfo": {"totalCount": 0,"page": 0,"pageSize": 1000},\
1735 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1736 "rel": "SELF","name": "current"},\
1737 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1738 "rel": "RELATED","name": "first"},\
1739 {"href": "/suite-api/api/notifications/rules?page=0&pageSize=1000",\
1740 "rel": "RELATED","name": "last"}],\
1741 "notification-rule": [{\
1742 "id": "2b86fa23-0c15-445c-a2b1-7bd725c46f59",\
1743 "name": "notify_CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4",\
1744 "pluginId": "03053f51-f829-438d-993d-cc33a435d76a",\
1745 "links": [{"href": "/suite-api/api/notifications/rules/2b86fa23-0c15-445c-a2b1-7bd725c46f59",\
1746 "rel": "SELF","name": "linkToSelf"}]}]}'
1748 #Call get_notification_rule_id_by_alarm_name method under test
1749 notification_id
= self
.mon_plugin
.get_notification_rule_id_by_alarm_name(alarm_name
)
1751 #Verify that mocked method is called with required parameters
1752 m_get
.assert_called()
1754 #Verify return value with expected value
1755 self
.assertEqual(notification_id
, None)
1758 @mock.patch
.object(monPlugin
.requests
, 'delete')
1759 def test_delete_alarm_defination_valid_req_response(self
,m_delete
):
1760 """Test delete_alarm_defination: A valid request response received,
1764 #Set input parameters to delete_alarm_definition
1765 alarm_definition_id
= 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d'
1767 #Set mocked function's return values
1768 m_delete
.return_value
.status_code
= 204
1770 #Call delete_alarm_defination method under test
1771 actual_alarm_id
= self
.mon_plugin
.delete_alarm_defination(alarm_definition_id
)
1773 #Verify that mocked method is called with required parameters
1774 m_delete
.assert_called()
1776 #Verify return value with expected value
1777 self
.assertEqual(actual_alarm_id
, alarm_definition_id
)
1780 @mock.patch
.object(monPlugin
.requests
, 'delete')
1781 def test_delete_alarm_defination_invalid_req_response(self
,m_delete
):
1782 """Test delete_alarm_defination: If an invalid request response received,
1786 #Set input parameters to delete_alarm_definition
1787 alarm_definition_id
= 'AlertDefinition-9a6d8a14-9f25-4d81-4d773497444d'
1789 #Set mocked function's return values
1790 m_delete
.return_value
.status_code
= 404
1792 #Call delete_alarm_defination method under test
1793 actual_alarm_id
= self
.mon_plugin
.delete_alarm_defination(alarm_definition_id
)
1795 #Verify that mocked method is called with required parameters
1796 m_delete
.assert_called()
1798 #Verify return value with expected value
1799 self
.assertEqual(actual_alarm_id
, None)
1802 @mock.patch
.object(monPlugin
.requests
, 'delete')
1803 def test_delete_symptom_definition_valid_req_response(self
,m_delete
):
1804 """Test delete_symptom_definition: A valid request response received,
1808 #Set input parameters to delete_symptom_definition
1809 symptom_definition_id
= 'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'
1811 #Set mocked function's return values
1812 m_delete
.return_value
.status_code
= 204
1814 #Call delete_symptom_definition method under test
1815 actual_symptom_id
= self
.mon_plugin
.delete_symptom_definition(symptom_definition_id
)
1817 #Verify that mocked method is called with required parameters
1818 m_delete
.assert_called()
1820 #Verify return value with expected value
1821 self
.assertEqual(actual_symptom_id
, symptom_definition_id
)
1824 @mock.patch
.object(monPlugin
.requests
, 'delete')
1825 def test_delete_symptom_definition_invalid_req_response(self
,m_delete
):
1826 """Test delete_symptom_definition: If an invalid request response received,
1830 #Set input parameters to delete_symptom_definition
1831 symptom_definition_id
= 'SymptomDefinition-bcc2cb36-a67b-4deb-9b5884973278'
1833 #Set mocked function's return values
1834 m_delete
.return_value
.status_code
= 404
1836 #Call delete_symptom_definition method under test
1837 actual_symptom_id
= self
.mon_plugin
.delete_symptom_definition(symptom_definition_id
)
1839 #Verify that mocked method is called with required parameters
1840 m_delete
.assert_called()
1842 #Verify return value with expected value
1843 self
.assertEqual(actual_symptom_id
, None)
1846 @mock.patch
.object(monPlugin
.requests
, 'post')
1847 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
1848 def test_configure_rest_plugin_valid_plugin_id(self
, m_check_if_plugin_configured
, m_post
):
1849 """Test configure rest plugin method-valid plugin id"""
1851 # mock return values
1852 expected_return
= m_check_if_plugin_configured
.return_value
= "mock_pluginid"
1854 # call configure rest plugin method under test
1855 actual_return
= self
.mon_plugin
.configure_rest_plugin()
1857 # verify that mocked method is called
1858 m_check_if_plugin_configured
.assert_called()
1859 m_post
.assert_not_called()
1861 # verify return value with expected value
1862 self
.assertEqual(expected_return
, actual_return
)
1865 @mock.patch
.object(monPlugin
.MonPlugin
,'enable_rest_plugin')
1866 @mock.patch
.object(monPlugin
.requests
, 'post')
1867 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
1868 def est_configure_rest_plugin_invalid_plugin_id(self
, m_check_if_plugin_configured
, m_post
, m_enable_rest_plugin
):
1869 """Test configure rest plugin method-invalid plugin id"""
1871 # mock return values
1872 m_check_if_plugin_configured
.return_value
= None # not configured
1873 m_post
.return_value
.status_code
= 201 #success
1874 m_post
.return_value
.content
= '{"pluginTypeId":"RestPlugin","pluginId":"1ef15663-9739-49fe-8c41-022bcc9f690c",\
1875 "name":"MON_module_REST_Plugin","version":1518693747871,"enabled":false,\
1876 "configValues":[{"name":"Url","value":"https://MON.lxd:8080/notify/"},\
1877 {"name":"Content-type","value":"application/json"},{"name":"Certificate",\
1878 "value":"AA:E7:3E:A5:34:E0:25:FB:28:84:3B:74:B2:18:74:C0:C3:E8:26:50"},\
1879 {"name":"ConnectionCount","value":"20"}]}'
1881 m_enable_rest_plugin
.return_value
= True #success
1882 expected_return
= '1ef15663-9739-49fe-8c41-022bcc9f690c'
1884 # call configure rest plugin method under test
1885 actual_return
= self
.mon_plugin
.configure_rest_plugin()
1887 # verify that mocked method is called
1888 m_check_if_plugin_configured
.assert_called()
1889 m_post
.assert_called()
1890 m_enable_rest_plugin
.assert_called_with('1ef15663-9739-49fe-8c41-022bcc9f690c','MON_module_REST_Plugin')
1892 # verify return value with expected value
1893 self
.assertEqual(expected_return
, actual_return
)
1896 @mock.patch
.object(monPlugin
.MonPlugin
,'enable_rest_plugin')
1897 @mock.patch
.object(monPlugin
.requests
, 'post')
1898 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
1899 def est_configure_rest_plugin_failed_to_enable_plugin(self
, m_check_if_plugin_configured
, m_post
, m_enable_rest_plugin
):
1900 """Test configure rest plugin method-failed to enable plugin case"""
1902 # mock return values
1903 m_check_if_plugin_configured
.return_value
= None # not configured
1904 m_post
.return_value
.status_code
= 201 #success
1905 m_post
.return_value
.content
= '{"pluginTypeId":"RestPlugin","pluginId":"1ef15663-9739-49fe-8c41-022bcc9f690c",\
1906 "name":"MON_module_REST_Plugin","version":1518693747871,"enabled":false,\
1907 "configValues":[{"name":"Url","value":"https://MON.lxd:8080/notify/"},\
1908 {"name":"Content-type","value":"application/json"},{"name":"Certificate",\
1909 "value":"AA:E7:3E:A5:34:E0:25:FB:28:84:3B:74:B2:18:74:C0:C3:E8:26:50"},\
1910 {"name":"ConnectionCount","value":"20"}]}'
1912 m_enable_rest_plugin
.return_value
= False #return failure
1913 expected_return
= None
1915 # call configure rest plugin method under test
1916 actual_return
= self
.mon_plugin
.configure_rest_plugin()
1918 # verify that mocked method is called
1919 m_check_if_plugin_configured
.assert_called()
1920 m_post
.assert_called()
1921 m_enable_rest_plugin
.assert_called_with('1ef15663-9739-49fe-8c41-022bcc9f690c','MON_module_REST_Plugin')
1923 # verify return value with expected value
1924 self
.assertEqual(expected_return
, actual_return
)
1927 @mock.patch
.object(monPlugin
.requests
, 'get')
1928 def test_check_if_plugin_configured_valid_req_response(self
, m_get
):
1929 """Test check if plugin configured method-valid request response"""
1931 plugin_name
= 'MON_module_REST_Plugin'
1932 # mock return values
1933 m_get
.return_value
.status_code
= 200
1934 expected_return
= '1ef15663-9739-49fe-8c41-022bcc9f690c'
1935 m_get
.return_value
.content
= '{"notificationPluginInstances":\
1936 [{"pluginTypeId":"RestPlugin",\
1937 "pluginId":"1ef15663-9739-49fe-8c41-022bcc9f690c",\
1938 "name":"MON_module_REST_Plugin","version":1518694966987,\
1939 "enabled":true,"configValues":[{"name":"Url",\
1940 "value":"https://MON.lxd:8080/notify/"},\
1941 {"name":"Content-type","value":"application/json"},\
1942 {"name":"Certificate",\
1943 "value":"AA:E7:3E:A5:34:E0:25:FB:28:84:3B:74:B2:18:74:C0"},\
1944 {"name":"ConnectionCount","value":"20"}]}]}'
1946 # call check if plugin configured method under test
1947 actual_return
= self
.mon_plugin
.check_if_plugin_configured(plugin_name
)
1949 # verify that mocked method is called
1950 m_get
.assert_called()
1952 # verify return value with expected value
1953 self
.assertEqual(expected_return
, actual_return
)
1956 @mock.patch
.object(monPlugin
.requests
, 'get')
1957 def test_check_if_plugin_configured_invalid_req_response(self
, m_get
):
1958 """Test check if plugin configured method-invalid request response"""
1960 plugin_name
= 'MON_module_REST_Plugin'
1961 # mock return values
1962 m_get
.return_value
.status_code
= 201
1963 expected_return
= None
1964 m_get
.return_value
.content
= '{"notificationPluginInstances":\
1965 [{"pluginTypeId":"RestPlugin",\
1966 "pluginId":"1ef15663-9739-49fe-8c41-022bcc9f690c",\
1967 "name":"MON_module_REST_Plugin","version":1518694966987,\
1968 "enabled":true,"configValues":[{"name":"Url",\
1969 "value":"https://MON.lxd:8080/notify/"},\
1970 {"name":"Content-type","value":"application/json"},\
1971 {"name":"Certificate",\
1972 "value":"AA:E7:3E:A5:34:E0:25:FB:28:84:3B:74:B2:18:74:C0"},\
1973 {"name":"ConnectionCount","value":"20"}]}]}'
1975 # call check if plugin configured method under test
1976 actual_return
= self
.mon_plugin
.check_if_plugin_configured(plugin_name
)
1978 # verify that mocked method is called
1979 m_get
.assert_called()
1981 # verify return value with expected value
1982 self
.assertEqual(expected_return
, actual_return
)
1985 @mock.patch
.object(monPlugin
.requests
, 'put')
1986 def test_enable_rest_plugin_valid_req_response(self
, m_put
):
1987 """Test enable rest plugin method-valid request response"""
1989 plugin_name
= 'MON_module_REST_Plugin'
1990 plugin_id
= '1ef15663-9739-49fe-8c41-022bcc9f690c'
1991 # mock return values
1992 m_put
.return_value
.status_code
= 204
1993 expected_return
= True
1994 m_put
.return_value
.content
= ''
1996 # call enable rest plugin configured method under test
1997 actual_return
= self
.mon_plugin
.enable_rest_plugin(plugin_id
, plugin_name
)
1999 # verify that mocked method is called
2000 m_put
.assert_called()
2002 # verify return value with expected value
2003 self
.assertEqual(expected_return
, actual_return
)
2006 @mock.patch
.object(monPlugin
.requests
, 'put')
2007 def test_enable_rest_plugin_invalid_req_response(self
, m_put
):
2008 """Test enable rest plugin method-invalid request response"""
2010 plugin_name
= 'MON_module_REST_Plugin'
2011 plugin_id
= '08018c0f-8879-4ca1-9b92-00e22d2ff81b' #invalid plugin id
2012 # mock return values
2013 m_put
.return_value
.status_code
= 404 # api Error code
2014 expected_return
= False
2015 m_put
.return_value
.content
= '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ops:\
2016 error xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \
2017 xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ops=\
2018 "http://webservice.vmware.com/vRealizeOpsMgr/1.0/" \
2019 httpStatusCode="404" apiErrorCode="404"><ops:message>\
2020 No such Notification Plugin - 08018c0f-8879-4ca1-9b92-\
2021 00e22d2ff81b.</ops:message></ops:error>'
2023 # call enable rest plugin configured method under test
2024 actual_return
= self
.mon_plugin
.enable_rest_plugin(plugin_id
, plugin_name
)
2026 # verify that mocked method is called
2027 m_put
.assert_called()
2029 # verify return value with expected value
2030 self
.assertEqual(expected_return
, actual_return
)
2033 @mock.patch
.object(monPlugin
.requests
, 'post')
2034 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
2035 def test_create_alarm_notification_rule_valid_req(self
, m_check_if_plugin_configured
, m_post
):
2036 """Test create alarm notification rule method valid request response"""
2038 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2039 alarm_id
= 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14'
2040 res_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2041 expected_return
= "8db86441-71d8-4830-9e1a-a90be3776d12"
2043 # mock return values
2044 m_check_if_plugin_configured
.return_value
= '03053f51-f829-438d-993d-cc33a435d76a'
2045 m_post
.return_value
.status_code
= 201
2046 m_post
.return_value
.content
= '{"id":"8db86441-71d8-4830-9e1a-a90be3776d12",\
2047 "name":"notify_CPU_Utilization_Above_Thr-e14b203c",\
2048 "pluginId":"03053f51-f829-438d-993d-cc33a435d76a",\
2049 "alertControlStates":[],"alertStatuses":[],\
2050 "resourceFilter":{"matchResourceIdOnly":true,\
2051 "childrenResourceKindFilters":[],\
2052 "resourceId":"ac87622f-b761-40a0-b151-00872a2a456e"},\
2053 "alertTypeFilters":[],"alertDefinitionIdFilters":{"values":[\
2054 "AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14"]}}'
2056 # call enable rest plugin configured method under test
2057 actual_return
= self
.mon_plugin
.create_alarm_notification_rule(alarm_name
, alarm_id
, res_id
)
2059 # verify that mocked method is called
2060 m_check_if_plugin_configured
.assert_called_with('MON_module_REST_Plugin')
2061 m_post
.assert_called()
2063 # verify return value with expected value
2064 self
.assertEqual(expected_return
, actual_return
)
2067 @mock.patch
.object(monPlugin
.requests
, 'post')
2068 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
2069 def test_create_alarm_notification_rule_invalid_req(self
, m_check_if_plugin_configured
, m_post
):
2070 """Test create alarm notification rule method invalid request response"""
2072 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2073 alarm_id
= 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14'
2074 res_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2075 expected_return
= None # invalid req should retrun none
2077 # mock return values
2078 m_check_if_plugin_configured
.return_value
= '03053f51-f829-438d-993d-cc33a435d76a'
2079 m_post
.return_value
.status_code
= 500
2080 m_post
.return_value
.content
= '{"message":"Internal Server error, cause unknown.",\
2081 "moreInformation":[{"name":"errorMessage","value":\
2082 "there is already a rule with the same rule name"},\
2083 {"name":"localizedMessage","value":"there is already \
2084 a rule with the same rule name;"}],"httpStatusCode":500,\
2085 "apiErrorCode":500}'
2087 # call enable rest plugin configured method under test
2088 actual_return
= self
.mon_plugin
.create_alarm_notification_rule(alarm_name
, alarm_id
, res_id
)
2090 # verify that mocked method is called
2091 m_check_if_plugin_configured
.assert_called_with('MON_module_REST_Plugin')
2092 m_post
.assert_called()
2094 # verify return value with expected value
2095 self
.assertEqual(expected_return
, actual_return
)
2098 @mock.patch
.object(monPlugin
.requests
, 'post')
2099 @mock.patch
.object(monPlugin
.MonPlugin
, 'check_if_plugin_configured')
2100 def test_create_alarm_notification_rule_failed_to_get_plugin_id(self
, \
2101 m_check_if_plugin_configured
, m_post
):
2102 """Test create alarm notification rule method invalid plugin id"""
2104 alarm_name
= 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2105 alarm_id
= 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14'
2106 res_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2107 expected_return
= None # invalid req should retrun none
2109 # mock return values
2110 m_check_if_plugin_configured
.return_value
= None
2112 # call enable rest plugin configured method under test
2113 actual_return
= self
.mon_plugin
.create_alarm_notification_rule(alarm_name
, alarm_id
, res_id
)
2115 # verify that mocked method is called
2116 m_check_if_plugin_configured
.assert_called_with('MON_module_REST_Plugin')
2117 m_post
.assert_not_called()
2119 # verify return value with expected value
2120 self
.assertEqual(expected_return
, actual_return
)
2123 @mock.patch
.object(monPlugin
.requests
, 'get')
2124 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2125 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2126 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2127 def test_get_metrics_data_valid_rest_req_response(self
, m_get_default_Params
, \
2128 m_get_vm_moref_id
, \
2129 m_get_vm_resource_id
, \
2131 """Test get metrics data of resource method valid request response"""
2133 metrics
= {'collection_period': 1, 'metric_name': 'CPU_UTILIZATION', 'metric_uuid': None, \
2134 'schema_version': 1.0, 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',\
2135 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef', \
2136 'schema_type': 'read_metric_data_request', 'vim_type': 'VMware', \
2137 'collection_unit': 'HR'}
2140 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2141 vm_moref_id
= m_get_vm_moref_id
.return_value
= 'vm-6626'
2142 m_get_vm_resource_id
.return_value
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2143 m_get
.return_value
.status_code
= 200
2144 m_get
.return_value
.content
= '{"values":[{"resourceId":"ac87622f-b761-40a0-b151-\
2145 00872a2a456e","stat-list":{"stat":[{"timestamps":\
2146 [1519716874297,1519717174294,1519717474295,1519717774298,\
2147 1519718074300,1519718374299,1519718674314,1519718974325,\
2148 1519719274304,1519719574298,1519719874298,1519720174301],\
2149 "statKey":{"key":"cpu|usage_average"},"intervalUnit":\
2150 {"quantifier":1},"data":[0.1120000034570694,\
2151 0.11866666376590729,0.11599999666213989,0.11400000005960464,\
2152 0.12066666781902313,0.11533333361148834,0.11800000071525574,\
2153 0.11533333361148834,0.12200000137090683,0.11400000005960464,\
2154 0.1459999978542328,0.12133333086967468]}]}}]}'
2156 # call get matrics data method under test
2157 actual_return
= self
.mon_plugin
.get_metrics_data(metrics
)
2159 # verify that mocked method is called
2160 m_get_default_Params
.assert_called_with(metrics
['metric_name'])
2161 m_get_vm_moref_id
.assert_called_with(metrics
['resource_uuid'])
2162 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
2163 m_get
.assert_called()
2165 # verify return value with expected value
2166 #self.assertEqual(expected_return, actual_return)
2169 @mock.patch
.object(monPlugin
.requests
, 'get')
2170 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2171 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2172 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2173 def test_get_metrics_data_invalid_rest_req_response(self
, m_get_default_Params
, \
2174 m_get_vm_moref_id
, \
2175 m_get_vm_resource_id
, \
2177 """Test get metrics data of resource method invalid request response"""
2179 metrics
= {'collection_period': 1, 'metric_name': 'CPU_UTILIZATION', 'metric_uuid': None, \
2180 'schema_version': 1.0, 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',\
2181 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef', \
2182 'schema_type': 'read_metric_data_request', 'vim_type': 'VMware', \
2183 'collection_unit': 'HR'}
2186 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2187 vm_moref_id
= m_get_vm_moref_id
.return_value
= 'vm-6626'
2188 m_get_vm_resource_id
.return_value
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2189 m_get
.return_value
.status_code
= 400
2190 expected_return
= {'metric_name': 'CPU_UTILIZATION', 'metric_uuid': '0',
2191 'schema_version': '1.0',
2192 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2193 'correlation_id': u
'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2194 'metrics_data': {'time_series': [], 'metrics_series': []},
2195 'schema_type': 'read_metric_data_response', 'tenant_uuid': None,
2198 # call get matrics data method under test
2199 actual_return
= self
.mon_plugin
.get_metrics_data(metrics
)
2201 # verify that mocked method is called
2202 m_get_default_Params
.assert_called_with(metrics
['metric_name'])
2203 m_get_vm_moref_id
.assert_called_with(metrics
['resource_uuid'])
2204 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
2205 m_get
.assert_called()
2207 m_get
.return_value
.content
= '{"message":"Invalid request... #1 violations found.",\
2208 "validationFailures":[{"failureMessage":"Invalid Parameter",\
2209 "violationPath":"end"}],"httpStatusCode":400,\
2210 "apiErrorCode":400}'
2212 # verify return value with expected value
2213 self
.assertEqual(expected_return
, actual_return
)
2216 @mock.patch
.object(monPlugin
.requests
, 'get')
2217 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2218 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2219 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2220 def test_get_metrics_data_metric_not_supported(self
, m_get_default_Params
, \
2221 m_get_vm_moref_id
, \
2222 m_get_vm_resource_id
, \
2224 """Test get metrics data of resource method invalid metric name"""
2226 metrics
= {'collection_period': 1, 'metric_name': 'INVALID_METRIC', 'metric_uuid': None, \
2227 'schema_version': 1.0, 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',\
2228 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef', \
2229 'schema_type': 'read_metric_data_request', 'vim_type': 'VMware', \
2230 'collection_unit': 'HR'}
2233 m_get_default_Params
.return_value
= {} # returns empty dict
2235 expected_return
= {'metric_name': 'INVALID_METRIC', 'metric_uuid': '0',
2236 'schema_version': '1.0',
2237 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2238 'correlation_id': u
'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2239 'metrics_data': {'time_series': [], 'metrics_series': []},
2240 'schema_type': 'read_metric_data_response', 'tenant_uuid': None,
2243 # call get matrics data method under test
2244 actual_return
= self
.mon_plugin
.get_metrics_data(metrics
)
2246 # verify that mocked method is called/not called
2247 m_get_default_Params
.assert_called_with(metrics
['metric_name'])
2248 m_get_vm_moref_id
.assert_not_called()
2249 m_get_vm_resource_id
.assert_not_called()
2250 m_get
.assert_not_called()
2252 # verify return value with expected value
2253 self
.assertEqual(expected_return
, actual_return
)
2256 @mock.patch
.object(monPlugin
.requests
, 'get')
2257 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2258 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2259 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2260 def test_get_metrics_data_failed_to_get_vm_moref_id(self
, m_get_default_Params
, \
2261 m_get_vm_moref_id
, \
2262 m_get_vm_resource_id
, \
2264 """Test get metrics data method negative scenario- invalid resource id"""
2266 metrics
= {'collection_period': 1, 'metric_name': 'CPU_UTILIZATION', 'metric_uuid': None, \
2267 'schema_version': 1.0, 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',\
2268 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef', \
2269 'schema_type': 'read_metric_data_request', 'vim_type': 'VMware', \
2270 'collection_unit': 'HR'}
2273 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2274 m_get_vm_moref_id
.return_value
= None
2275 expected_return
= {'metric_name': 'CPU_UTILIZATION', 'metric_uuid': '0',
2276 'schema_version': '1.0',
2277 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2278 'correlation_id': u
'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2279 'metrics_data': {'time_series': [], 'metrics_series': []},
2280 'schema_type': 'read_metric_data_response', 'tenant_uuid': None,
2283 # call get matrics data method under test
2284 actual_return
= self
.mon_plugin
.get_metrics_data(metrics
)
2286 # verify that mocked method is called/not called
2287 m_get_default_Params
.assert_called_with(metrics
['metric_name'])
2288 m_get_vm_moref_id
.assert_called_with(metrics
['resource_uuid'])
2289 m_get_vm_resource_id
.assert_not_called()
2290 m_get
.assert_not_called()
2292 # verify return value with expected value
2293 self
.assertEqual(expected_return
, actual_return
)
2296 @mock.patch
.object(monPlugin
.requests
, 'get')
2297 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2298 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2299 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2300 def test_get_metrics_data_failed_to_get_vm_resource_id(self
, m_get_default_Params
, \
2301 m_get_vm_moref_id
, \
2302 m_get_vm_resource_id
, \
2304 """Test get metrics data method negative scenario- invalid moref id"""
2306 metrics
= {'collection_period': 1, 'metric_name': 'CPU_UTILIZATION', 'metric_uuid': None, \
2307 'schema_version': 1.0, 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',\
2308 'correlation_id': 'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef', \
2309 'schema_type': 'read_metric_data_request', 'vim_type': 'VMware', \
2310 'collection_unit': 'HR'}
2313 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2314 m_get_vm_moref_id
.return_value
= 'Invalid-vm-6626'
2315 m_get_vm_resource_id
.return_value
= None
2316 expected_return
= {'metric_name': 'CPU_UTILIZATION', 'metric_uuid': '0',
2317 'schema_version': '1.0',
2318 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2319 'correlation_id': u
'e14b203c-6bf2-4e2f-a91c-8c19d2abcdef',
2320 'metrics_data': {'time_series': [], 'metrics_series': []},
2321 'schema_type': 'read_metric_data_response', 'tenant_uuid': None,
2324 # call get matrics data method under test
2325 actual_return
= self
.mon_plugin
.get_metrics_data(metrics
)
2327 # verify that mocked method is called/not called
2328 m_get_default_Params
.assert_called_with(metrics
['metric_name'])
2329 m_get_vm_moref_id
.assert_called_with(metrics
['resource_uuid'])
2330 m_get_vm_resource_id
.assert_called()
2331 m_get
.assert_not_called()
2333 # verify return value with expected value
2334 self
.assertEqual(expected_return
, actual_return
)
2337 @mock.patch
.object(monPlugin
.MonPlugin
, 'reconfigure_alarm')
2338 @mock.patch
.object(monPlugin
.MonPlugin
, 'update_symptom_defination')
2339 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
2340 def test_update_alarm_configuration_successful_updation(self
, m_get_alarm_defination_details
, \
2341 m_update_symptom_defination
, \
2342 m_reconfigure_alarm
):
2343 """Test update alarm configuration method"""
2345 alarm_config
= {'alarm_uuid': 'f1163767-6eac-438f-8e60-a7a867257e14',
2346 'correlation_id': 14203,
2347 'description': 'CPU_Utilization_Above_Threshold_L', 'operation': 'GT'}
2350 alarm_details_json
= {'states': [{'impact': {'impactType': 'BADGE', 'detail': 'risk'},
2351 'severity': 'CRITICAL', 'base-symptom-set': {'symptomDefinitionIds':
2352 ['SymptomDefinition-47c88675-bea8-436a-bb41-8d2231428f44'],
2353 'relation': 'SELF', 'type': 'SYMPTOM_SET', 'aggregation': 'ALL'}}],
2354 'description': 'CPU_Utilization_Above_Threshold', 'type': 16,
2355 'id': 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14',
2356 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d2'}
2357 alarm_details
= {'symptom_definition_id': 'SymptomDefinition-47c88675-bea8-436a-bb41-\
2358 8d2231428f44', 'alarm_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-\
2359 a91c-8c19d2', 'alarm_id': 'AlertDefinition-f1163767-6eac-438f-8e60-\
2360 a7a867257e14', 'resource_kind': u
'VirtualMachine', 'type': 16}
2361 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
2362 m_update_symptom_defination
.return_value
= 'SymptomDefinition-47c88675-bea8-436a-bb41-\
2364 expected_return
= m_reconfigure_alarm
.return_value
= 'f1163767-6eac-438f-8e60-a7a867257e14'
2366 # call update alarm configuration method under test
2367 actual_return
= self
.mon_plugin
.update_alarm_configuration(alarm_config
)
2369 # verify that mocked method is called
2370 m_get_alarm_defination_details
.assert_called_with(alarm_config
['alarm_uuid'])
2371 m_update_symptom_defination
.assert_called_with(alarm_details
['symptom_definition_id'],\
2373 m_reconfigure_alarm
.assert_called_with(alarm_details_json
, alarm_config
)
2375 # verify return value with expected value
2376 self
.assertEqual(expected_return
, actual_return
)
2379 @mock.patch
.object(monPlugin
.MonPlugin
, 'reconfigure_alarm')
2380 @mock.patch
.object(monPlugin
.MonPlugin
, 'update_symptom_defination')
2381 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
2382 def test_update_alarm_configuration_failed_to_reconfigure_alarm(self
, \
2383 m_get_alarm_defination_details
, \
2384 m_update_symptom_defination
, \
2385 m_reconfigure_alarm
):
2386 """Test update alarm configuration method- failed to reconfigure alarm"""
2388 alarm_config
= {'alarm_uuid': 'f1163767-6eac-438f-8e60-a7a867257e14',
2389 'correlation_id': 14203,
2390 'description': 'CPU_Utilization_Above_Threshold_L', 'operation': 'GT'}
2393 alarm_details_json
= {'states': [{'impact': {'impactType': 'BADGE', 'detail': 'risk'},
2394 'severity': 'CRITICAL', 'base-symptom-set': {'symptomDefinitionIds':
2395 ['SymptomDefinition-47c88675-bea8-436a-bb41-8d2231428f44'],
2396 'relation': 'SELF', 'type': 'SYMPTOM_SET', 'aggregation': 'ALL'}}],
2397 'description': 'CPU_Utilization_Above_Threshold', 'type': 16,
2398 'id': 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14',
2399 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d2'}
2400 alarm_details
= {'symptom_definition_id': 'SymptomDefinition-47c88675-bea8-436a-bb41-\
2401 8d2231428f44', 'alarm_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-\
2402 a91c-8c19d2', 'alarm_id': 'AlertDefinition-f1163767-6eac-438f-8e60-\
2403 a7a867257e14', 'resource_kind': u
'VirtualMachine', 'type': 16}
2404 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
2405 m_update_symptom_defination
.return_value
= 'SymptomDefinition-47c88675-bea8-436a-bb41-\
2407 expected_return
= m_reconfigure_alarm
.return_value
= None # failed to reconfigure
2409 # call update alarm configuration method under test
2410 actual_return
= self
.mon_plugin
.update_alarm_configuration(alarm_config
)
2412 # verify that mocked method is called
2413 m_get_alarm_defination_details
.assert_called_with(alarm_config
['alarm_uuid'])
2414 m_update_symptom_defination
.assert_called_with(alarm_details
['symptom_definition_id'],\
2416 m_reconfigure_alarm
.assert_called_with(alarm_details_json
, alarm_config
)
2418 # verify return value with expected value
2419 self
.assertEqual(expected_return
, actual_return
)
2422 @mock.patch
.object(monPlugin
.MonPlugin
, 'reconfigure_alarm')
2423 @mock.patch
.object(monPlugin
.MonPlugin
, 'update_symptom_defination')
2424 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_alarm_defination_details')
2425 def test_update_alarm_configuration_failed_to_update_symptom(self
, \
2426 m_get_alarm_defination_details
, \
2427 m_update_symptom_defination
, \
2428 m_reconfigure_alarm
):
2429 """Test update alarm configuration method- failed to update alarm"""
2431 alarm_config
= {'alarm_uuid': 'f1163767-6eac-438f-8e60-a7a867257e14',
2432 'correlation_id': 14203,
2433 'description': 'CPU_Utilization_Above_Threshold_L', 'operation': 'GT'}
2436 alarm_details_json
= {'states': [{'impact': {'impactType': 'BADGE', 'detail': 'risk'},
2437 'severity': 'CRITICAL', 'base-symptom-set': {'symptomDefinitionIds':
2438 ['SymptomDefinition-47c88675-bea8-436a-bb41-8d2231428f44'],
2439 'relation': 'SELF', 'type': 'SYMPTOM_SET', 'aggregation': 'ALL'}}],
2440 'description': 'CPU_Utilization_Above_Threshold', 'type': 16,
2441 'id': 'AlertDefinition-f1163767-6eac-438f-8e60-a7a867257e14',
2442 'name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d2'}
2443 alarm_details
= {'symptom_definition_id': 'Invalid-47c88675-bea8-436a-bb41-\
2444 8d2231428f44', 'alarm_name': 'CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-\
2445 a91c-8c19d2', 'alarm_id': 'AlertDefinition-f1163767-6eac-438f-8e60-\
2446 a7a867257e14', 'resource_kind': u
'VirtualMachine', 'type': 16}
2447 m_get_alarm_defination_details
.return_value
= (alarm_details_json
, alarm_details
)
2448 expected_return
= m_update_symptom_defination
.return_value
= None
2450 # call update alarm configuration method under test
2451 actual_return
= self
.mon_plugin
.update_alarm_configuration(alarm_config
)
2453 # verify that mocked method is called
2454 m_get_alarm_defination_details
.assert_called_with(alarm_config
['alarm_uuid'])
2455 m_update_symptom_defination
.assert_called_with(alarm_details
['symptom_definition_id'],\
2457 m_reconfigure_alarm
.assert_not_called()
2459 # verify return value with expected value
2460 self
.assertEqual(expected_return
, actual_return
)
2463 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2464 def test_verify_metric_support_metric_supported_with_unit(self
,m_get_default_Params
):
2465 """Test verify metric support method for supported metric"""
2468 metric_info
= {'metric_unit': '%', 'metric_name': 'CPU_UTILIZATION',
2469 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'}
2470 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2471 expected_return
= True #supported metric returns True
2473 # call verify metric support method under test
2474 actual_return
= self
.mon_plugin
.verify_metric_support(metric_info
)
2476 # verify that mocked method is called
2477 m_get_default_Params
.assert_called_with(metric_info
['metric_name'])
2479 # verify return value with expected value
2480 self
.assertEqual(expected_return
, actual_return
)
2483 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2484 def test_verify_metric_support_metric_not_supported(self
,m_get_default_Params
):
2485 """Test verify metric support method for un-supported metric"""
2488 metric_info
= {'metric_unit': '%', 'metric_name': 'INVALID_METRIC',
2489 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'}
2490 m_get_default_Params
.return_value
= {}
2491 expected_return
= False #supported metric returns True
2493 # call verify metric support method under test
2494 actual_return
= self
.mon_plugin
.verify_metric_support(metric_info
)
2496 # verify that mocked method is called
2497 m_get_default_Params
.assert_called_with(metric_info
['metric_name'])
2499 # verify return value with expected value
2500 self
.assertEqual(expected_return
, actual_return
)
2503 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_default_Params')
2504 def test_verify_metric_support_metric_supported_with_mismatched_unit(self
, \
2505 m_get_default_Params
):
2506 """Test verify metric support method for supported metric with mismatched unit"""
2509 metric_info
= {'metric_unit': '', 'metric_name': 'INVALID_METRIC',
2510 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'}
2511 m_get_default_Params
.return_value
= {'metric_key': 'cpu|usage_average', 'unit': '%'}
2512 expected_return
= True #supported metric returns True
2514 # call verify metric support method under test
2515 actual_return
= self
.mon_plugin
.verify_metric_support(metric_info
)
2517 # verify that mocked method is called
2518 m_get_default_Params
.assert_called_with(metric_info
['metric_name'])
2520 # verify return value with expected value
2521 self
.assertEqual(expected_return
, actual_return
)
2524 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_triggered_alarms_on_resource')
2525 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vrops_resourceid_from_ro_uuid')
2526 def test_get_triggered_alarms_list_returns_triggered_alarms(self
, \
2527 m_get_vrops_resourceid
, \
2528 m_triggered_alarms
):
2529 """Test get triggered alarm list method valid input"""
2531 # Mock list alarm input
2532 list_alarm_input
= {'severity': 'CRITICAL',
2533 'correlation_id': 'e14b203c',
2534 'alarm_name': 'CPU_Utilization_Above_Threshold',
2535 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'}
2537 resource_id
= m_get_vrops_resourceid
.return_value
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2538 expected_return
= m_triggered_alarms
.return_value
= [{'status': 'ACTIVE',
2539 'update_date': '2018-01-12T08:34:05',
2540 'severity': 'CRITICAL', 'resource_uuid': 'e14b203c',
2541 'cancel_date': '0000-00-00T00:00:00',
2542 'alarm_instance_uuid': 'd9e3bc84',
2543 'alarm_uuid': '5714977d', 'vim_type': 'VMware',
2544 'start_date': '2018-01-12T08:34:05'},
2545 {'status': 'CANCELED','update_date':'2017-12-20T09:37:57',
2546 'severity': 'CRITICAL', 'resource_uuid': 'e14b203c',
2547 'cancel_date': '2018-01-12T06:49:19',
2548 'alarm_instance_uuid': 'd3bbeef6',
2549 'alarm_uuid': '7ba1bf3e', 'vim_type': 'VMware',
2550 'start_date': '2017-12-20T09:37:57'}]
2552 # call get triggered alarms list method under test
2553 actual_return
= self
.mon_plugin
.get_triggered_alarms_list(list_alarm_input
)
2555 # verify that mocked method is called
2556 m_get_vrops_resourceid
.assert_called_with(list_alarm_input
['resource_uuid'])
2557 m_triggered_alarms
.assert_called_with(list_alarm_input
['resource_uuid'] , resource_id
)
2559 # verify return value with expected value
2560 self
.assertEqual(expected_return
, actual_return
)
2563 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_triggered_alarms_on_resource')
2564 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vrops_resourceid_from_ro_uuid')
2565 def test_get_triggered_alarms_list_invalid_resource_uuid(self
, \
2566 m_get_vrops_resourceid
, \
2567 m_triggered_alarms
):
2568 """Test get triggered alarm list method invalid resource uuid"""
2570 # Mock list alarm input
2571 list_alarm_input
= {'severity': 'CRITICAL',
2572 'correlation_id': 'e14b203c',
2573 'alarm_name': 'CPU_Utilization_Above_Threshold',
2574 'resource_uuid': '12345'} #invalid resource uuid
2576 expected_return
= m_get_vrops_resourceid
.return_value
= None #returns empty list
2578 # call get triggered alarms list method under test
2579 actual_return
= self
.mon_plugin
.get_triggered_alarms_list(list_alarm_input
)
2581 # verify that mocked method is called
2582 m_get_vrops_resourceid
.assert_called_with(list_alarm_input
['resource_uuid'])
2583 m_triggered_alarms
.assert_not_called()
2585 # verify return value with expected value
2586 self
.assertEqual([], actual_return
)
2589 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_triggered_alarms_on_resource')
2590 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vrops_resourceid_from_ro_uuid')
2591 def test_get_triggered_alarms_list_resource_uuid_not_present(self
, \
2592 m_get_vrops_resourceid
, \
2593 m_triggered_alarms
):
2594 """Test get triggered alarm list method resource not present"""
2596 # Mock list alarm input
2597 list_alarm_input
= {'severity': 'CRITICAL',
2598 'correlation_id': 'e14b203c',
2599 'alarm_name': 'CPU_Utilization_Above_Threshold'}
2601 # call get triggered alarms list method under test
2602 actual_return
= self
.mon_plugin
.get_triggered_alarms_list(list_alarm_input
)
2604 # verify that mocked method is called
2605 m_get_vrops_resourceid
.assert_not_called()
2606 m_triggered_alarms
.assert_not_called()
2608 # verify return value with expected value
2609 self
.assertEqual([], actual_return
)
2612 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2613 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2614 def test_get_vrops_resourceid_from_ro_uuid(self
, m_get_vm_moref_id
, m_get_vm_resource_id
):
2615 """Test get vrops resourceid from ro uuid method"""
2618 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2619 vm_moref_id
= m_get_vm_moref_id
.return_value
= 'vm-6626'
2620 expected_return
= m_get_vm_resource_id
.return_value
='ac87622f-b761-40a0-b151-00872a2a456e'
2622 # call get_vrops_resourceid_from_ro_uuid method under test
2623 actual_return
= self
.mon_plugin
.get_vrops_resourceid_from_ro_uuid(ro_resource_uuid
)
2625 # verify that mocked method is called
2626 m_get_vm_moref_id
.assert_called_with(ro_resource_uuid
)
2627 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
2629 # verify return value with expected value
2630 self
.assertEqual(expected_return
, actual_return
)
2633 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2634 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2635 def test_get_vrops_resourceid_from_ro_uuid_failed_to_get_vm_resource_id(self
, \
2636 m_get_vm_moref_id
, \
2637 m_get_vm_resource_id
):
2638 """Test get vrops resourceid from ro uuid method negative scenario"""
2641 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2642 vm_moref_id
= m_get_vm_moref_id
.return_value
= 'vm-6626'
2643 expected_return
= m_get_vm_resource_id
.return_value
= None
2645 # call get_vrops_resourceid_from_ro_uuid method under test
2646 actual_return
= self
.mon_plugin
.get_vrops_resourceid_from_ro_uuid(ro_resource_uuid
)
2648 # verify that mocked method is called
2649 m_get_vm_moref_id
.assert_called_with(ro_resource_uuid
)
2650 m_get_vm_resource_id
.assert_called_with(vm_moref_id
)
2652 # verify return value with expected value
2653 self
.assertEqual(expected_return
, actual_return
)
2656 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_resource_id')
2657 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vm_moref_id')
2658 def test_get_vrops_resourceid_from_ro_uuid_failed_to_get_vm_moref_id(self
, \
2659 m_get_vm_moref_id
, \
2660 m_get_vm_resource_id
):
2661 """Test get vrops resourceid from ro uuid method negative scenario"""
2664 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2665 expected_return
= vm_moref_id
= m_get_vm_moref_id
.return_value
= None
2667 # call get_vrops_resourceid_from_ro_uuid method under test
2668 actual_return
= self
.mon_plugin
.get_vrops_resourceid_from_ro_uuid(ro_resource_uuid
)
2670 # verify that mocked method is called
2671 m_get_vm_moref_id
.assert_called_with(ro_resource_uuid
)
2672 m_get_vm_resource_id
.assert_not_called()
2674 # verify return value with expected value
2675 self
.assertEqual(expected_return
, actual_return
)
2678 @mock.patch
.object(monPlugin
.requests
, 'get')
2679 def test_get_triggered_alarms_on_resource_valid_req_response(self
, m_get
):
2680 """Test get triggered alarms on resource method for valid request"""
2683 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2684 vrops_resource_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2685 m_get
.return_value
.status_code
= 200
2686 expected_return
= [{'status': 'ACTIVE', 'update_date': '2018-01-12T08:34:05',
2687 'severity': 'CRITICAL', 'start_date': '2018-01-12T08:34:05',
2688 'resource_uuid': 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4',
2689 'cancel_date': '2018-02-12T08:24:48', 'vim_type': 'VMware',
2690 'alarm_instance_uuid': 'd9e3bc84-dcb4-4905-b592-00a55f4cdaf1',
2691 'alarm_uuid': '5714977d-56f6-4222-adc7-43fa6c6e7e39'}]
2693 m_get
.return_value
.content
= '{"alerts": [\
2695 "alertId": "d9e3bc84-dcb4-4905-b592-00a55f4cdaf1",\
2696 "resourceId": "ac87622f-b761-40a0-b151-00872a2a456e",\
2697 "alertLevel": "CRITICAL",\
2698 "status": "ACTIVE",\
2699 "startTimeUTC": 1515746045278,\
2700 "cancelTimeUTC": 1518423888708,\
2701 "updateTimeUTC": 1515746045278,\
2702 "alertDefinitionId": "AlertDefinition-5714977d-56f6-4222-adc7-43fa6c6e7e39",\
2703 "alertDefinitionName": "CPU_Utilization_Above_Thr-e14b203c-6bf2-4e2f-a91c-8c19d240eda4"\
2706 "alertId": "5fb5e940-e161-4253-a729-7255c6d6b1f5",\
2707 "resourceId": "ac87622f-b761-40a0-b151-00872a2a456e",\
2708 "alertLevel": "WARNING",\
2709 "status": "CANCELED",\
2710 "startTimeUTC": 1506684979154,\
2711 "cancelTimeUTC": 0,\
2712 "updateTimeUTC": 1520471975507,\
2713 "alertDefinitionId": "AlertDefinition-9ec5a921-1a54-411d-85ec-4c1c9b26dd02",\
2714 "alertDefinitionName": "VM_CPU_Usage_Alarm"\
2717 # call get_vrops_resourceid_from_ro_uuid method under test
2718 actual_return
= self
.mon_plugin
.get_triggered_alarms_on_resource(ro_resource_uuid
, \
2721 # verify that mocked method is called
2722 m_get
.assert_called()
2724 # verify return value with expected value
2725 self
.assertEqual(expected_return
, actual_return
)
2728 @mock.patch
.object(monPlugin
.requests
, 'get')
2729 def test_get_triggered_alarms_on_resource_invalid_req_response(self
, m_get
):
2730 """Test get triggered alarms on resource method for invalid request"""
2733 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2734 vrops_resource_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2735 m_get
.return_value
.status_code
= 204
2736 expected_return
= None
2738 # call get_vrops_resourceid_from_ro_uuid method under test
2739 actual_return
= self
.mon_plugin
.get_triggered_alarms_on_resource(ro_resource_uuid
, \
2742 # verify that mocked method is called
2743 m_get
.assert_called()
2745 # verify return value with expected value
2746 self
.assertEqual(expected_return
, actual_return
)
2749 @mock.patch
.object(monPlugin
.requests
, 'get')
2750 def test_get_triggered_alarms_on_resource_no_alarms_present(self
, m_get
):
2751 """Test get triggered alarms on resource method for no alarms present"""
2754 ro_resource_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2755 vrops_resource_id
= 'ac87622f-b761-40a0-b151-00872a2a456e'
2756 m_get
.return_value
.status_code
= 200
2757 expected_return
= []
2758 m_get
.return_value
.content
= '{"alerts": []}'
2760 # call get_vrops_resourceid_from_ro_uuid method under test
2761 actual_return
= self
.mon_plugin
.get_triggered_alarms_on_resource(ro_resource_uuid
, \
2764 # verify that mocked method is called
2765 m_get
.assert_called()
2767 # verify return value with expected value
2768 self
.assertEqual(expected_return
, actual_return
)
2771 def test_convert_date_time_valid_date_time(self
):
2772 """Test convert date time method valid input"""
2775 date_time
= 1515746045278
2776 expected_return
= '2018-01-12T08:34:05'
2778 # call convert_date_time method under test
2779 actual_return
= self
.mon_plugin
.convert_date_time(date_time
)
2781 # verify return value with expected value
2782 self
.assertEqual(expected_return
, actual_return
)
2784 def test_convert_date_time_invalid_date_time(self
):
2785 """Test convert date time method invalid input"""
2789 expected_return
= '0000-00-00T00:00:00'
2791 # call convert_date_time method under test
2792 actual_return
= self
.mon_plugin
.convert_date_time(date_time
)
2794 # verify return value with expected value
2795 self
.assertEqual(expected_return
, actual_return
)
2798 @mock.patch
.object(monPlugin
.requests
, 'get')
2799 def test_get_vm_resource_id_rest_valid_req_response(self
, m_get
):
2800 """Test get vms resource id valid request"""
2803 vm_moref_id
= 'vm-6626'
2804 m_get
.return_value
.status_code
= 200
2805 expected_return
= "ac87622f-b761-40a0-b151-00872a2a456e"
2806 m_get
.return_value
.content
= \
2810 "creationTime": 1497770174130,\
2812 "name": "OCInst2.ubuntu(4337d51f-1e65-4ab0-9c08-4897778d4fda)",\
2813 "adapterKindKey": "VMWARE",\
2814 "resourceKindKey": "VirtualMachine",\
2815 "resourceIdentifiers": [\
2817 "identifierType": {\
2818 "name": "VMEntityObjectID",\
2819 "dataType": "STRING",\
2820 "isPartOfUniqueness": true\
2826 "identifier": "ac87622f-b761-40a0-b151-00872a2a456e"\
2831 # call get_vm_resource_id method under test
2832 actual_return
= self
.mon_plugin
.get_vm_resource_id(vm_moref_id
)
2834 # verify that mocked method is called
2835 m_get
.assert_called()
2837 # verify return value with expected value
2838 self
.assertEqual(expected_return
, actual_return
)
2841 @mock.patch
.object(monPlugin
.requests
, 'get')
2842 def test_get_vm_resource_id_rest_invalid_req_response(self
, m_get
):
2843 """Test get vms resource id invalid request"""
2846 vm_moref_id
= 'vm-6626'
2847 m_get
.return_value
.status_code
= 406
2848 expected_return
= None
2849 m_get
.return_value
.content
= '406 Not Acceptable'
2851 # call get_vm_resource_id method under test
2852 actual_return
= self
.mon_plugin
.get_vm_resource_id(vm_moref_id
)
2854 # verify that mocked method is called
2855 m_get
.assert_called()
2857 # verify return value with expected value
2858 self
.assertEqual(expected_return
, actual_return
)
2861 @mock.patch
.object(monPlugin
.requests
, 'get')
2862 def test_get_vm_resource_id_rest_invalid_response(self
, m_get
):
2863 """Test get vms resource id invalid response"""
2866 vm_moref_id
= 'vm-6626'
2867 m_get
.return_value
.status_code
= 200
2868 expected_return
= None
2869 m_get
.return_value
.content
= \
2873 "creationTime": 1497770174130,\
2875 "name": "OCInst2.ubuntu(4337d51f-1e65-4ab0-9c08-4897778d4fda)",\
2876 "adapterKindKey": "VMWARE",\
2877 "resourceKindKey": "VirtualMachine",\
2878 "resourceIdentifiers": [\
2880 "identifierType": {\
2881 "name": "VMEntityObjectID",\
2882 "dataType": "STRING",\
2883 "isPartOfUniqueness": true\
2889 "identifier": "ac87622f-b761-40a0-b151-00872a2a456e"\
2894 # call get_vm_resource_id method under test
2895 actual_return
= self
.mon_plugin
.get_vm_resource_id(vm_moref_id
)
2897 # verify that mocked method is called
2898 m_get
.assert_called()
2900 # verify return value with expected value
2901 self
.assertEqual(expected_return
, actual_return
)
2904 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vapp_details_rest')
2905 def test_get_vm_moref_id_valid_id_found (self
, m_get_vapp_details_rest
):
2906 """Test get vm moref id valid scenario"""
2909 vapp_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2910 m_get_vapp_details_rest
.return_value
= {'vm_vcenter_info': {'vm_moref_id': 'vm-6626'}}
2911 expected_return
= 'vm-6626'
2913 # call get_vm_resource_id method under test
2914 actual_return
= self
.mon_plugin
.get_vm_moref_id(vapp_uuid
)
2916 # verify that mocked method is called
2917 m_get_vapp_details_rest
.assert_called_with(vapp_uuid
)
2919 # verify return value with expected value
2920 self
.assertEqual(expected_return
, actual_return
)
2923 @mock.patch
.object(monPlugin
.MonPlugin
, 'get_vapp_details_rest')
2924 def test_get_vm_moref_id_valid_id_not_found(self
, m_get_vapp_details_rest
):
2925 """Test get vm moref id invalid scenario"""
2928 vapp_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda'#invalid uuid
2929 m_get_vapp_details_rest
.return_value
= {}
2930 expected_return
= None
2932 # call get_vm_resource_id method under test
2933 actual_return
= self
.mon_plugin
.get_vm_moref_id(vapp_uuid
)
2935 # verify that mocked method is called
2936 m_get_vapp_details_rest
.assert_called_with(vapp_uuid
)
2938 # verify return value with expected value
2939 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
)
2982 @mock.patch
.object(monPlugin
.requests
, 'get')
2983 @mock.patch
.object(monPlugin
.MonPlugin
, 'connect_as_admin')
2984 def test_get_vapp_details_rest_invalid_req_response(self
, m_connect_as_admin
, m_get
):
2985 """Test get vapp details rest method for invalid request response"""
2988 vapp_uuid
= 'Invalid-e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
2989 m_connect_as_admin
.return_value
= self
.vca
2990 self
.vca
._session
= self
.session
2991 self
.vca
._session
.headers
['x-vcloud-authorization'] = '2ec69b2cc6264ad0a47aaf4e3e280d16'
2992 m_get
.return_value
.status_code
= 400
2993 expected_return
= {}
2994 m_get
.return_value
.content
= 'Bad Request'
2996 # call get_vapp_details_rest method under test
2997 actual_return
= self
.mon_plugin
.get_vapp_details_rest(vapp_uuid
)
2999 # verify that mocked method is called
3000 m_connect_as_admin
.assert_called_with()
3001 m_get
.assert_called()
3003 # verify return value with expected value
3004 self
.assertEqual(expected_return
, actual_return
)
3007 @mock.patch
.object(monPlugin
.requests
, 'get')
3008 @mock.patch
.object(monPlugin
.MonPlugin
, 'connect_as_admin')
3009 def test_get_vapp_details_rest_failed_to_connect_vcd(self
, m_connect_as_admin
, m_get
):
3010 """Test get vapp details rest method for failed to connect to vcd"""
3013 vapp_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
3014 m_connect_as_admin
.return_value
= None
3015 expected_return
= {}
3017 # call get_vapp_details_rest method under test
3018 actual_return
= self
.mon_plugin
.get_vapp_details_rest(vapp_uuid
)
3020 # verify that mocked method is called
3021 m_connect_as_admin
.assert_called_with()
3022 m_get
.assert_not_called()
3024 # verify return value with expected value
3025 self
.assertEqual(expected_return
, actual_return
)
3028 @mock.patch
.object(monPlugin
.requests
, 'get')
3029 @mock.patch
.object(monPlugin
.MonPlugin
, 'connect_as_admin')
3030 def test_get_vapp_details_rest_invalid_response(self
, m_connect_as_admin
, m_get
):
3031 """Test get vapp details rest method for invalid response"""
3034 vapp_uuid
= 'e14b203c-6bf2-4e2f-a91c-8c19d240eda4'
3035 m_connect_as_admin
.return_value
= self
.vca
3036 self
.vca
._session
= self
.session
3037 self
.vca
._session
.headers
['x-vcloud-authorization'] = '2ec69b2cc6264ad0a47aaf4e3e280d16'
3038 m_get
.return_value
.status_code
= 200
3039 expected_return
= {}
3040 m_get
.return_value
.content
= '<?xml version="1.0" encoding="UTF-8"?>\
3041 <VApp xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5" >\
3043 <Vm needsCustomization="false" type="application/vnd.vmware.vcloud.vm+xml">\
3044 <VCloudExtension required="false">\
3046 <vmext:VmVimObjectRef>\
3047 <vmext:VimServerRef type="application/vnd.vmware.admin.vmwvirtualcenter+xml"/>\
3048 <vmext:MoRef>vm-!!6626</vmext:MoRef>\
3049 <vmext:VimObjectType>VIRTUAL_MACHINE</vmext:VimObjectType>\
3057 # call get_vapp_details_rest method under test
3058 actual_return
= self
.mon_plugin
.get_vapp_details_rest(vapp_uuid
)
3060 # verify that mocked method is called
3061 m_connect_as_admin
.assert_called_with()
3062 m_get
.assert_called()
3064 # verify return value with expected value
3065 self
.assertEqual(expected_return
, actual_return
)
3068 @mock.patch
.object(monPlugin
.Client
, 'set_credentials')
3069 @mock.patch
.object(monPlugin
, 'Client')
3070 def test_connect_as_admin(self
, m_client
, m_set_credentials
):
3071 """Test connect as admin to vCD method"""
3073 #mock the inputs and mocked returns
3074 expected_return
= m_client
.return_value
= self
.vca
3075 m_set_credentials
.retrun_value
= True
3077 # call connect_as_admin method under test
3078 actual_return
= self
.mon_plugin
.connect_as_admin()
3080 # verify that mocked method is called
3081 m_client
.assert_called()
3082 m_set_credentials
.assert_called()
3084 # verify return value with expected value
3085 self
.assertEqual(expected_return
, actual_return
)
3088 # For testing purpose
3089 #if __name__ == '__main__':