Feature 10986: Autoheal switch and Autoscale switch
[osm/POL.git] / osm_policy_module / tests / unit / healing / test_healing_service.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
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
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
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
19 # under the License.
20
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
23 ##
24 import asyncio
25 import datetime
26 from unittest import TestCase, mock
27
28 from osm_policy_module.autoscaling.service import HealingService
29 from osm_policy_module.common.common_db_client import CommonDbClient
30 from osm_policy_module.common.lcm_client import LcmClient
31 from osm_policy_module.common.mon_client import MonClient
32 from osm_policy_module.core.config import Config
33 from osm_policy_module.core.database import HealingActionRepository
34
35
36 @mock.patch.object(LcmClient, "__init__", lambda *args, **kwargs: None)
37 @mock.patch.object(MonClient, "__init__", lambda *args, **kwargs: None)
38 @mock.patch.object(CommonDbClient, "__init__", lambda *args, **kwargs: None)
39 class TestHealscalingService(TestCase):
40 def setUp(self):
41 self.config = Config()
42 self.loop = asyncio.new_event_loop()
43
44 @mock.patch.object(HealingActionRepository, "get")
45 @mock.patch("osm_policy_module.core.database.db")
46 def test_update_alarm_status(self, database, get_alarm):
47 mock_alarm = mock.Mock()
48 mock_alarm.last_status = "insufficient_data"
49 get_alarm.return_value = mock_alarm
50
51 service = HealingService(self.config)
52 self.loop.run_until_complete(service.update_alarm_status("test_uuid", "alarm"))
53 self.assertEqual(mock_alarm.last_status, "alarm")
54 mock_alarm.save.assert_called_with()
55 service = HealingService(self.config)
56 self.loop.run_until_complete(service.update_alarm_status("test_uuid", "ok"))
57 self.assertEqual(mock_alarm.last_status, "ok")
58 mock_alarm.save.assert_called_with()
59 self.loop.run_until_complete(
60 service.update_alarm_status("test_uuid", "insufficient_data")
61 )
62 self.assertEqual(mock_alarm.last_status, "insufficient_data")
63 mock_alarm.save.assert_called_with()
64 """
65 if self.config.get("autoheal", "enabled") == "True":
66 self.loop.run_until_complete(
67 service.update_alarm_status("test_uuid", "alarm")
68 )
69 self.assertEqual(mock_alarm.last_status, "alarm")
70 mock_alarm.save.assert_called_with()
71
72 service = HealingService(self.config)
73 if self.config.get("autoheal", "enabled") == "True":
74 self.loop.run_until_complete(service.update_alarm_status("test_uuid", "ok"))
75 self.assertEqual(mock_alarm.last_status, "ok")
76 mock_alarm.save.assert_called_with()
77
78 service = HealingService(self.config)
79 if self.config.get("autoheal", "enabled") == "True":
80 self.loop.run_until_complete(
81 service.update_alarm_status("test_uuid", "insufficient_data")
82 )
83 self.assertEqual(mock_alarm.last_status, "insufficient_data")
84 mock_alarm.save.assert_called_with()
85 """
86
87 @mock.patch.object(HealingActionRepository, "list")
88 @mock.patch.object(HealingActionRepository, "get")
89 @mock.patch(LcmClient, "heal")
90 @mock.patch("osm_policy_module.core.database.db")
91 def test_handle_alarm(self, database, heal, get_alarm, list_alarms):
92 mock_alarm = self._build_mock_alarm("test_id", status="alarm")
93 get_alarm.return_value = mock_alarm
94 service = HealingService(self.config)
95 self.loop.run_until_complete(service.handle_alarm("test_id", "alarm"))
96 heal.assert_called_with(
97 "test_nsr_id",
98 "test_vnfinstance_id",
99 "test_vdur_name",
100 "test_vdu_id",
101 "test_vnf_member_index",
102 "test_heal_type",
103 "test_day1",
104 "test_count_index",
105 )
106 """
107 if self.config.get("autoheal", "enabled") == "True":
108 self.loop.run_until_complete(service.handle_alarm("test_id", "alarm"))
109 heal.assert_called_with(
110 "test_nsr_id",
111 "test_vnfinstance_id",
112 "test_vdur_name",
113 "test_vdu_id",
114 "test_vnf_member_index",
115 "test_heal_type",
116 "test_day1",
117 "test_count_index",
118 )
119 """
120
121 def _build_mock_alarm(
122 self,
123 last_status="alarm",
124 last_heal=datetime.datetime.min,
125 cooldown_time=10,
126 ):
127 mock_alarm = mock.Mock()
128 mock_alarm.last_status = last_status
129 mock_alarm.vnf_member_index = "1"
130 mock_alarm.last_heal = last_heal
131 mock_alarm.cooldown_time = cooldown_time
132 mock_alarm.nsr_id = "test_nsr_id"
133 mock_alarm.vnfinstance_id = "test_vnfinstance_id"
134 mock_alarm.vdur_name = "test_vdur_name"
135 mock_alarm.vdu_id = "test_vdu_id"
136 mock_alarm.vnf_member_index = "test_vnf_member_index"
137 mock_alarm.heal_type = "test_heal_type"
138 mock_alarm.day1 = "test_day1"
139 mock_alarm.count_index = "test_count_index"
140 return mock_alarm