Feature 10986: Autoheal switch and Autoscale switch
[osm/POL.git] / osm_policy_module / tests / unit / autoscaling / test_autoscaling_service.py
index 053b278..1588486 100644 (file)
@@ -39,7 +39,6 @@ from osm_policy_module.core.database import ScalingAlarmRepository
 class TestAutoscalingService(TestCase):
     def setUp(self):
         self.config = Config()
-        self.loop = asyncio.new_event_loop()
 
     @mock.patch.object(ScalingAlarmRepository, "get")
     @mock.patch("osm_policy_module.core.database.db")
@@ -49,19 +48,17 @@ class TestAutoscalingService(TestCase):
         get_alarm.return_value = mock_alarm
 
         service = AutoscalingService(self.config)
-        self.loop.run_until_complete(service.update_alarm_status("test_uuid", "alarm"))
+        asyncio.run(service.update_alarm_status("test_uuid", "alarm"))
         self.assertEqual(mock_alarm.last_status, "alarm")
         mock_alarm.save.assert_called_with()
 
         service = AutoscalingService(self.config)
-        self.loop.run_until_complete(service.update_alarm_status("test_uuid", "ok"))
+        asyncio.run(service.update_alarm_status("test_uuid", "ok"))
         self.assertEqual(mock_alarm.last_status, "ok")
         mock_alarm.save.assert_called_with()
 
         service = AutoscalingService(self.config)
-        self.loop.run_until_complete(
-            service.update_alarm_status("test_uuid", "insufficient_data")
-        )
+        asyncio.run(service.update_alarm_status("test_uuid", "insufficient_data"))
         self.assertEqual(mock_alarm.last_status, "insufficient_data")
         mock_alarm.save.assert_called_with()
 
@@ -74,7 +71,7 @@ class TestAutoscalingService(TestCase):
         get_alarm.return_value = mock_alarm
 
         service = AutoscalingService(self.config)
-        self.loop.run_until_complete(service.evaluate_policy("test_uuid"))
+        asyncio.run(service.evaluate_policy("test_uuid"))
         list_alarms.assert_not_called()
 
     @mock.patch.object(ScalingAlarmRepository, "list")
@@ -87,7 +84,7 @@ class TestAutoscalingService(TestCase):
         """
         Tests scale in with AND operation, both alarms triggered
         """
-        future = asyncio.Future(loop=self.loop)
+        future = asyncio.Future(loop=asyncio.new_event_loop())
         future.set_result("mock")
         scale.return_value = future
 
@@ -103,8 +100,9 @@ class TestAutoscalingService(TestCase):
         list_alarms.return_value = [mock_alarm, mock_alarm_2]
 
         service = AutoscalingService(self.config)
-        self.loop.run_until_complete(service.evaluate_policy("test_uuid"))
-        scale.assert_called_with("test_nsr_id", "test_group", "1", "scale_in")
+        if self.config.get("autoscale", "enabled") == "True":
+            asyncio.run(service.evaluate_policy("test_uuid"))
+            scale.assert_called_with("test_nsr_id", "test_group", "1", "scale_in")
 
     @mock.patch.object(ScalingAlarmRepository, "list")
     @mock.patch.object(ScalingAlarmRepository, "get")
@@ -116,7 +114,7 @@ class TestAutoscalingService(TestCase):
         """
         Tests scale in with AND operation, only one alarm triggered.
         """
-        future = asyncio.Future(loop=self.loop)
+        future = asyncio.Future(loop=asyncio.new_event_loop())
         future.set_result("mock")
         scale.return_value = future
 
@@ -132,8 +130,9 @@ class TestAutoscalingService(TestCase):
         list_alarms.return_value = [mock_alarm, mock_alarm_2]
 
         service = AutoscalingService(self.config)
-        self.loop.run_until_complete(service.evaluate_policy("test_uuid"))
-        scale.assert_not_called()
+        if self.config.get("autoscale", "enabled") == "True":
+            asyncio.run(service.evaluate_policy("test_uuid"))
+            scale.assert_not_called()
 
     @mock.patch.object(ScalingAlarmRepository, "list")
     @mock.patch.object(ScalingAlarmRepository, "get")
@@ -145,7 +144,7 @@ class TestAutoscalingService(TestCase):
         """
         Tests scale in with OR operation, both alarms triggered
         """
-        future = asyncio.Future(loop=self.loop)
+        future = asyncio.Future(loop=asyncio.new_event_loop())
         future.set_result("mock")
         scale.return_value = future
 
@@ -161,8 +160,9 @@ class TestAutoscalingService(TestCase):
         list_alarms.return_value = [mock_alarm, mock_alarm_2]
 
         service = AutoscalingService(self.config)
-        self.loop.run_until_complete(service.evaluate_policy("test_uuid"))
-        scale.assert_called_with("test_nsr_id", "test_group", "1", "scale_in")
+        if self.config.get("autoscale", "enabled") == "True":
+            asyncio.run(service.evaluate_policy("test_uuid"))
+            scale.assert_called_with("test_nsr_id", "test_group", "1", "scale_in")
 
     @mock.patch.object(ScalingAlarmRepository, "list")
     @mock.patch.object(ScalingAlarmRepository, "get")
@@ -174,7 +174,7 @@ class TestAutoscalingService(TestCase):
         """
         Tests scale in with OR operation, only one alarm triggered
         """
-        future = asyncio.Future(loop=self.loop)
+        future = asyncio.Future(loop=asyncio.new_event_loop())
         future.set_result("mock")
         scale.return_value = future
 
@@ -190,8 +190,9 @@ class TestAutoscalingService(TestCase):
         list_alarms.return_value = [mock_alarm, mock_alarm_2]
 
         service = AutoscalingService(self.config)
-        self.loop.run_until_complete(service.evaluate_policy("test_uuid"))
-        scale.assert_called_with("test_nsr_id", "test_group", "1", "scale_in")
+        if self.config.get("autoscale", "enabled") == "True":
+            asyncio.run(service.evaluate_policy("test_uuid"))
+            scale.assert_called_with("test_nsr_id", "test_group", "1", "scale_in")
 
     @mock.patch.object(ScalingAlarmRepository, "list")
     @mock.patch.object(ScalingAlarmRepository, "get")
@@ -203,7 +204,7 @@ class TestAutoscalingService(TestCase):
         """
         Tests scale out with AND operation, both alarms triggered
         """
-        future = asyncio.Future(loop=self.loop)
+        future = asyncio.Future(loop=asyncio.new_event_loop())
         future.set_result("mock")
         scale.return_value = future
 
@@ -219,8 +220,9 @@ class TestAutoscalingService(TestCase):
         list_alarms.return_value = [mock_alarm, mock_alarm_2]
 
         service = AutoscalingService(self.config)
-        self.loop.run_until_complete(service.evaluate_policy("test_uuid"))
-        scale.assert_called_with("test_nsr_id", "test_group", "1", "scale_out")
+        if self.config.get("autoscale", "enabled") == "True":
+            asyncio.run(service.evaluate_policy("test_uuid"))
+            scale.assert_called_with("test_nsr_id", "test_group", "1", "scale_out")
 
     @mock.patch.object(ScalingAlarmRepository, "list")
     @mock.patch.object(ScalingAlarmRepository, "get")
@@ -232,7 +234,7 @@ class TestAutoscalingService(TestCase):
         """
         Tests scale out with AND operation, only one alarm triggered.
         """
-        future = asyncio.Future(loop=self.loop)
+        future = asyncio.Future(loop=asyncio.new_event_loop())
         future.set_result("mock")
         scale.return_value = future
 
@@ -248,8 +250,9 @@ class TestAutoscalingService(TestCase):
         list_alarms.return_value = [mock_alarm, mock_alarm_2]
 
         service = AutoscalingService(self.config)
-        self.loop.run_until_complete(service.evaluate_policy("test_uuid"))
-        scale.assert_not_called()
+        if self.config.get("autoscale", "enabled") == "True":
+            asyncio.run(service.evaluate_policy("test_uuid"))
+            scale.assert_not_called()
 
     @mock.patch.object(ScalingAlarmRepository, "list")
     @mock.patch.object(ScalingAlarmRepository, "get")
@@ -261,7 +264,7 @@ class TestAutoscalingService(TestCase):
         """
         Tests scale out with OR operation, both alarms triggered
         """
-        future = asyncio.Future(loop=self.loop)
+        future = asyncio.Future(loop=asyncio.new_event_loop())
         future.set_result("mock")
         scale.return_value = future
 
@@ -277,8 +280,10 @@ class TestAutoscalingService(TestCase):
         list_alarms.return_value = [mock_alarm, mock_alarm_2]
 
         service = AutoscalingService(self.config)
-        self.loop.run_until_complete(service.evaluate_policy("test_uuid"))
-        scale.assert_called_with("test_nsr_id", "test_group", "1", "scale_out")
+        asyncio.run(service.evaluate_policy("test_uuid"))
+        if self.config.get("autoscale", "enabled") == "True":
+            asyncio.run(service.evaluate_policy("test_uuid"))
+            scale.assert_called_with("test_nsr_id", "test_group", "1", "scale_out")
 
     @mock.patch.object(ScalingAlarmRepository, "list")
     @mock.patch.object(ScalingAlarmRepository, "get")
@@ -290,7 +295,7 @@ class TestAutoscalingService(TestCase):
         """
         Tests scale out with OR operation, only one alarm triggered
         """
-        future = asyncio.Future(loop=self.loop)
+        future = asyncio.Future(loop=asyncio.new_event_loop())
         future.set_result("mock")
         scale.return_value = future
 
@@ -306,8 +311,10 @@ class TestAutoscalingService(TestCase):
         list_alarms.return_value = [mock_alarm, mock_alarm_2]
 
         service = AutoscalingService(self.config)
-        self.loop.run_until_complete(service.evaluate_policy("test_uuid"))
-        scale.assert_called_with("test_nsr_id", "test_group", "1", "scale_out")
+        asyncio.run(service.evaluate_policy("test_uuid"))
+        if self.config.get("autoscale", "enabled") == "True":
+            asyncio.run(service.evaluate_policy("test_uuid"))
+            scale.assert_called_with("test_nsr_id", "test_group", "1", "scale_out")
 
     def _build_mock_alarm(
         self,