From ddba3573273bc0ccc39f15ab98a4451246fd2ec9 Mon Sep 17 00:00:00 2001 From: chenke Date: Thu, 22 Nov 2018 16:21:06 +0800 Subject: [PATCH] Increase the unit test coverage of vm_workload_consolidation.py Increase the test of the execute method which contains the pre_execute(), do_execute(), post_execute() methods. Increase coverage from 82% to 87%. Change-Id: Ibde67d7b7d7945657ad0b674e06b1edc9eb24a9f --- .../test_vm_workload_consolidation.py | 41 ++++++++++++++++--- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/watcher/tests/decision_engine/strategy/strategies/test_vm_workload_consolidation.py b/watcher/tests/decision_engine/strategy/strategies/test_vm_workload_consolidation.py index fffa31069..dd75fb3d9 100644 --- a/watcher/tests/decision_engine/strategy/strategies/test_vm_workload_consolidation.py +++ b/watcher/tests/decision_engine/strategy/strategies/test_vm_workload_consolidation.py @@ -23,6 +23,7 @@ import mock from watcher.common import exception from watcher.decision_engine.model import element from watcher.decision_engine.model import model_root +from watcher.decision_engine.solution.base import BaseSolution from watcher.decision_engine.strategy import strategies from watcher.tests import base from watcher.tests.decision_engine.model import faker_cluster_and_metrics @@ -277,13 +278,19 @@ class TestVMWorkloadConsolidation(base.TestCase): model = self.fake_cluster.generate_scenario_2() self.m_model.return_value = model self.fake_metrics.model = model + + result = self.strategy.pre_execute() + self.assertIsNone(result) + n1 = model.get_node_by_uuid('Node_0') - cc = {'cpu': 1.0, 'ram': 1.0, 'disk': 1.0} - self.strategy.offload_phase(cc) - self.strategy.consolidation_phase(cc) - self.strategy.optimize_solution() + self.strategy.get_relative_cluster_utilization = mock.MagicMock() + self.strategy.do_execute() n2 = self.strategy.solution.actions[0][ 'input_parameters']['destination_node'] + n3 = self.strategy.solution.actions[2][ + 'input_parameters']['resource_id'] + n4 = self.strategy.solution.actions[3][ + 'input_parameters']['resource_id'] expected = [{'action_type': 'migrate', 'input_parameters': {'destination_node': n2, 'source_node': n1.uuid, @@ -293,10 +300,32 @@ class TestVMWorkloadConsolidation(base.TestCase): 'input_parameters': {'destination_node': n2, 'source_node': n1.uuid, 'migration_type': 'live', - 'resource_id': 'INSTANCE_1'}}] - + 'resource_id': 'INSTANCE_1'}}, + {'action_type': 'change_nova_service_state', + 'input_parameters': {'state': 'disabled', + 'disabled_reason': + 'watcher_disabled', + 'resource_id': n3}}, + {'action_type': 'change_nova_service_state', + 'input_parameters': {'state': 'disabled', + 'disabled_reason': + 'watcher_disabled', + 'resource_id': n4}}] self.assertEqual(expected, self.strategy.solution.actions) + compute_nodes_count = len(self.strategy.get_available_compute_nodes()) + number_of_released_nodes = self.strategy.number_of_released_nodes + number_of_migrations = self.strategy.number_of_migrations + with mock.patch.object( + BaseSolution, 'set_efficacy_indicators' + ) as mock_set_efficacy_indicators: + result = self.strategy.post_execute() + mock_set_efficacy_indicators.assert_called_once_with( + compute_nodes_count=compute_nodes_count, + released_compute_nodes_count=number_of_released_nodes, + instance_migrations_count=number_of_migrations + ) + def test_strategy2(self): model = self.fake_cluster.generate_scenario_3() self.m_model.return_value = model