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(
51 delete_orphaned_alarms
,
53 configure_scaling_groups
,
54 autoscaling_lcm_client
,
55 autoscaling_mon_client
,
59 async def mock_configure_scaling_groups(nsr_id
):
62 async def mock_configure_vnf_alarms(nsr_id
):
65 async def mock_delete_orphaned_alarms(nsr_id
):
69 agent
= PolicyModuleAgent(config
, self
.loop
)
70 assert autoscaling_lcm_client
.called
71 assert autoscaling_mon_client
.called
72 assert alarming_lcm_client
.called
73 assert alarming_mon_client
.called
75 "nslcmop_id": "test_id",
78 "operationState": "COMPLETED",
79 "nsInstanceId": "test_nsr_id",
81 nslcmop_failed
= {"operationState": "FAILED", "nsInstanceId": "test_nsr_id"}
82 configure_scaling_groups
.side_effect
= mock_configure_scaling_groups
83 configure_vnf_alarms
.side_effect
= mock_configure_vnf_alarms
84 delete_orphaned_alarms
.side_effect
= mock_delete_orphaned_alarms
86 get_nslcmop
.return_value
= nslcmop_completed
87 self
.loop
.run_until_complete(agent
._handle
_instantiated
(content
))
88 configure_scaling_groups
.assert_called_with("test_nsr_id")
89 configure_scaling_groups
.reset_mock()
91 get_nslcmop
.return_value
= nslcmop_failed
92 self
.loop
.run_until_complete(agent
._handle
_instantiated
(content
))
93 configure_scaling_groups
.assert_not_called()
95 @mock.patch
.object(CommonDbClient
, "__init__", lambda *args
, **kwargs
: None)
96 @mock.patch("osm_policy_module.autoscaling.service.MonClient")
97 @mock.patch("osm_policy_module.autoscaling.service.LcmClient")
98 @mock.patch("osm_policy_module.alarming.service.MonClient")
99 @mock.patch("osm_policy_module.alarming.service.LcmClient")
100 @mock.patch
.object(AutoscalingService
, "handle_alarm")
101 @mock.patch
.object(AlarmingService
, "handle_alarm")
102 def test_handle_alarm_notification(
104 alarming_handle_alarm
,
105 autoscaling_handle_alarm
,
106 autoscaling_lcm_client
,
107 autoscaling_mon_client
,
111 async def mock_handle_alarm(alarm_uuid
, status
, payload
=None):
115 agent
= PolicyModuleAgent(config
, self
.loop
)
116 assert autoscaling_lcm_client
.called
117 assert autoscaling_mon_client
.called
118 assert alarming_lcm_client
.called
119 assert alarming_mon_client
.called
122 "alarm_uuid": "test_alarm_uuid",
123 "metric_name": "test_metric_name",
124 "operation": "test_operation",
125 "threshold_value": "test_threshold_value",
126 "vdu_name": "test_vdu_name",
127 "vnf_member_index": "test_vnf_member_index",
128 "ns_id": "test_nsr_id",
132 autoscaling_handle_alarm
.side_effect
= mock_handle_alarm
133 alarming_handle_alarm
.side_effect
= mock_handle_alarm
135 self
.loop
.run_until_complete(agent
._handle
_alarm
_notification
(content
))
136 autoscaling_handle_alarm
.assert_called_with("test_alarm_uuid", "alarm")
137 alarming_handle_alarm
.assert_called_with("test_alarm_uuid", "alarm", content
)
139 @mock.patch
.object(CommonDbClient
, "__init__", lambda *args
, **kwargs
: None)
140 @mock.patch("osm_policy_module.alarming.service.MonClient")
141 @mock.patch("osm_policy_module.alarming.service.LcmClient")
142 @mock.patch("osm_policy_module.autoscaling.service.MonClient")
143 @mock.patch("osm_policy_module.autoscaling.service.LcmClient")
144 @mock.patch
.object(AutoscalingService
, "delete_scaling_groups")
145 @mock.patch
.object(AlarmingService
, "delete_vnf_alarms")
146 def test_handle_vnf_terminated(
149 delete_scaling_groups
,
150 autoscaling_lcm_client
,
151 autoscaling_mon_client
,
155 async def mock_delete_scaling_groups(nsr_id
, vnf_member_index
):
158 async def mock_delete_vnf_alarms(nsr_id
, vnf_member_index
):
162 agent
= PolicyModuleAgent(config
, self
.loop
)
163 assert autoscaling_lcm_client
.called
164 assert autoscaling_mon_client
.called
165 assert alarming_lcm_client
.called
166 assert alarming_mon_client
.called
168 "nsr_id": "test_nsr_id",
169 "vnf_member_index": "1",
170 "operationState": "COMPLETED"
173 "nsr_id": "test_nsr_id",
174 "vnf_member_index": "1",
175 "operationState": "FAILED"
177 delete_scaling_groups
.side_effect
= mock_delete_scaling_groups
178 delete_vnf_alarms
.side_effect
= mock_delete_vnf_alarms
180 self
.loop
.run_until_complete(agent
._handle
_vnf
_terminated
(content
))
181 delete_scaling_groups
.assert_called_with("test_nsr_id", "1")
182 delete_scaling_groups
.reset_mock()
184 self
.loop
.run_until_complete(agent
._handle
_vnf
_terminated
(failed_content
))
185 delete_scaling_groups
.assert_not_called()
188 if __name__
== "__main__":