1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
13 # http://www.apache.org/licenses/LICENSE-2.0
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
26 from unittest
import mock
28 from osm_policy_module
.alarming
.service
import AlarmingService
29 from osm_policy_module
.autoscaling
.service
import AutoscalingService
30 from osm_policy_module
.common
.common_db_client
import CommonDbClient
31 from osm_policy_module
.core
.agent
import PolicyModuleAgent
32 from osm_policy_module
.core
.config
import Config
35 class PolicyAgentTest(unittest
.TestCase
):
37 self
.loop
= asyncio
.new_event_loop()
39 @mock.patch
.object(CommonDbClient
, "__init__", lambda *args
, **kwargs
: None)
40 @mock.patch('osm_policy_module.alarming.service.MonClient')
41 @mock.patch('osm_policy_module.alarming.service.LcmClient')
42 @mock.patch('osm_policy_module.autoscaling.service.MonClient')
43 @mock.patch('osm_policy_module.autoscaling.service.LcmClient')
44 @mock.patch
.object(AutoscalingService
, 'configure_scaling_groups')
45 @mock.patch
.object(AlarmingService
, 'configure_vnf_alarms')
46 @mock.patch
.object(AutoscalingService
, 'delete_orphaned_alarms')
47 @mock.patch
.object(CommonDbClient
, 'get_nslcmop')
48 def test_handle_instantiated(self
,
50 delete_orphaned_alarms
,
52 configure_scaling_groups
,
53 autoscaling_lcm_client
,
54 autoscaling_mon_client
,
57 async def mock_configure_scaling_groups(nsr_id
):
60 async def mock_configure_vnf_alarms(nsr_id
):
63 async def mock_delete_orphaned_alarms(nsr_id
):
67 agent
= PolicyModuleAgent(config
, self
.loop
)
68 assert autoscaling_lcm_client
.called
69 assert autoscaling_mon_client
.called
70 assert alarming_lcm_client
.called
71 assert alarming_mon_client
.called
73 'nslcmop_id': 'test_id',
76 'operationState': 'COMPLETED',
77 'nsInstanceId': 'test_nsr_id'
80 'operationState': 'FAILED',
81 'nsInstanceId': 'test_nsr_id'
83 configure_scaling_groups
.side_effect
= mock_configure_scaling_groups
84 configure_vnf_alarms
.side_effect
= mock_configure_vnf_alarms
85 delete_orphaned_alarms
.side_effect
= mock_delete_orphaned_alarms
87 get_nslcmop
.return_value
= nslcmop_completed
88 self
.loop
.run_until_complete(agent
._handle
_instantiated
(content
))
89 configure_scaling_groups
.assert_called_with('test_nsr_id')
90 configure_scaling_groups
.reset_mock()
92 get_nslcmop
.return_value
= nslcmop_failed
93 self
.loop
.run_until_complete(agent
._handle
_instantiated
(content
))
94 configure_scaling_groups
.assert_not_called()
96 @mock.patch
.object(CommonDbClient
, "__init__", lambda *args
, **kwargs
: None)
97 @mock.patch('osm_policy_module.autoscaling.service.MonClient')
98 @mock.patch('osm_policy_module.autoscaling.service.LcmClient')
99 @mock.patch('osm_policy_module.alarming.service.MonClient')
100 @mock.patch('osm_policy_module.alarming.service.LcmClient')
101 @mock.patch
.object(AutoscalingService
, 'handle_alarm')
102 @mock.patch
.object(AlarmingService
, 'handle_alarm')
103 def test_handle_alarm_notification(self
,
104 alarming_handle_alarm
,
105 autoscaling_handle_alarm
,
106 autoscaling_lcm_client
,
107 autoscaling_mon_client
,
109 alarming_mon_client
):
110 async def mock_handle_alarm(alarm_uuid
, status
, payload
=None):
114 agent
= PolicyModuleAgent(config
, self
.loop
)
115 assert autoscaling_lcm_client
.called
116 assert autoscaling_mon_client
.called
117 assert alarming_lcm_client
.called
118 assert alarming_mon_client
.called
121 'alarm_uuid': 'test_alarm_uuid',
122 'metric_name': 'test_metric_name',
123 'operation': 'test_operation',
124 'threshold_value': 'test_threshold_value',
125 'vdu_name': 'test_vdu_name',
126 'vnf_member_index': 'test_vnf_member_index',
127 'ns_id': 'test_nsr_id',
131 autoscaling_handle_alarm
.side_effect
= mock_handle_alarm
132 alarming_handle_alarm
.side_effect
= mock_handle_alarm
134 self
.loop
.run_until_complete(agent
._handle
_alarm
_notification
(content
))
135 autoscaling_handle_alarm
.assert_called_with('test_alarm_uuid', 'alarm')
136 alarming_handle_alarm
.assert_called_with('test_alarm_uuid', 'alarm', content
)
139 if __name__
== '__main__':