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
27 from unittest
import mock
28 from unittest
.mock
import Mock
30 from osm_policy_module
.autoscaling
.agent
import PolicyModuleAgent
31 from osm_policy_module
.autoscaling
.service
import Service
32 from osm_policy_module
.core
.config
import Config
35 class PolicyAgentTest(unittest
.TestCase
):
37 self
.loop
= asyncio
.new_event_loop()
39 @mock.patch('osm_policy_module.autoscaling.service.CommonDbClient')
40 @mock.patch('osm_policy_module.autoscaling.service.MonClient')
41 @mock.patch('osm_policy_module.autoscaling.service.LcmClient')
42 @mock.patch
.object(Service
, 'configure_scaling_groups')
43 @mock.patch
.object(Service
, 'delete_orphaned_alarms')
44 def test_handle_instantiated(self
, delete_orphaned_alarms
, configure_scaling_groups
, lcm_client
,
45 mon_client
, db_client
):
46 async def mock_configure_scaling_groups(nsr_id
):
49 async def mock_delete_orphaned_alarms(nsr_id
):
53 agent
= PolicyModuleAgent(config
, self
.loop
)
54 assert lcm_client
.called
55 assert mon_client
.called
56 assert db_client
.called
58 'nslcmop_id': 'test_id',
61 'operationState': 'COMPLETED',
62 'nsInstanceId': 'test_nsr_id'
65 'operationState': 'FAILED',
66 'nsInstanceId': 'test_nsr_id'
68 configure_scaling_groups
.side_effect
= mock_configure_scaling_groups
69 delete_orphaned_alarms
.side_effect
= mock_delete_orphaned_alarms
71 db_client
.return_value
.get_nslcmop
.return_value
= nslcmop_completed
72 self
.loop
.run_until_complete(agent
._handle
_instantiated
(content
))
73 configure_scaling_groups
.assert_called_with('test_nsr_id')
74 configure_scaling_groups
.reset_mock()
76 db_client
.return_value
.get_nslcmop
.return_value
= nslcmop_failed
77 self
.loop
.run_until_complete(agent
._handle
_instantiated
(content
))
78 configure_scaling_groups
.assert_not_called()
80 @mock.patch('osm_policy_module.autoscaling.service.CommonDbClient')
81 @mock.patch('osm_policy_module.autoscaling.service.MonClient')
82 @mock.patch('osm_policy_module.autoscaling.service.LcmClient')
83 @mock.patch('osm_policy_module.core.database.db')
84 @mock.patch
.object(Service
, 'get_alarm')
85 def test_handle_alarm_notification(self
, get_alarm
, db
, lcm_client
, mon_client
, db_client
):
86 async def mock_scale(nsr_id
, scaling_group_name
, vnf_member_index
, action
):
90 agent
= PolicyModuleAgent(config
, self
.loop
)
91 assert lcm_client
.called
92 assert mon_client
.called
93 assert db_client
.called
96 'alarm_uuid': 'test_alarm_uuid',
97 'metric_name': 'test_metric_name',
98 'operation': 'test_operation',
99 'threshold_value': 'test_threshold_value',
100 'vdu_name': 'test_vdu_name',
101 'vnf_member_index': 'test_vnf_member_index',
102 'ns_id': 'test_nsr_id'
106 mock_alarm
.vnf_member_index
= 1
107 mock_alarm
.action
= 'scale_out'
108 mock_scaling_criteria
= Mock()
109 mock_scaling_policy
= Mock()
110 mock_scaling_group
= Mock()
111 mock_scaling_group
.nsr_id
= 'test_nsr_id'
112 mock_scaling_group
.name
= 'test_name'
113 mock_scaling_policy
.cooldown_time
= 60
114 mock_scaling_policy
.scaling_group
= mock_scaling_group
115 mock_scaling_criteria
.scaling_policy
= mock_scaling_policy
116 mock_alarm
.scaling_criteria
= mock_scaling_criteria
117 get_alarm
.return_value
= mock_alarm
118 lcm_client
.return_value
.scale
.side_effect
= mock_scale
120 mock_scaling_policy
.last_scale
= datetime
.datetime
.now() - datetime
.timedelta(minutes
=90)
122 self
.loop
.run_until_complete(agent
._handle
_alarm
_notification
(content
))
123 lcm_client
.return_value
.scale
.assert_called_with('test_nsr_id', 'test_name', 1, 'scale_out')
124 lcm_client
.return_value
.scale
.reset_mock()
126 mock_scaling_policy
.last_scale
= datetime
.datetime
.now()
127 self
.loop
.run_until_complete(agent
._handle
_alarm
_notification
(content
))
128 lcm_client
.return_value
.scale
.assert_not_called()
131 if __name__
== '__main__':