Code refactoring - StrategyContext and Auditendpoint

This patchset aim to remove useless code in StrategyContext
and AuditEndPoint.
This patchset also add a parameter for strategy context to define the
numbers of thread of execute the strategies.

DocImpact
Change-Id: I83e87165b03b42fe6b863921502a300bd94d2982
This commit is contained in:
Jean-Emile DARTOIS
2015-12-17 14:21:18 +01:00
parent 1698eb31f3
commit 4c3073efb4
11 changed files with 116 additions and 95 deletions

View File

@@ -42,7 +42,6 @@ class TestDefaultAuditHandler(DbTestCase):
def test_trigger_audit_state_success(self):
model_collector = FakerModelCollector()
audit_handler = DefaultAuditHandler(MagicMock(), model_collector)
audit_handler.strategy_context.execute_strategy = MagicMock()
audit_handler.execute(self.audit.uuid, self.context)
audit = Audit.get_by_uuid(self.context, self.audit.uuid)
self.assertEqual(AuditStatus.SUCCEEDED, audit.state)
@@ -51,8 +50,6 @@ class TestDefaultAuditHandler(DbTestCase):
messaging = MagicMock()
model_collector = FakerModelCollector()
audit_handler = DefaultAuditHandler(messaging, model_collector)
audit_handler.strategy_context.execute_strategy = MagicMock()
audit_handler.execute(self.audit.uuid, self.context)
call_on_going = call(Events.TRIGGER_AUDIT.name, {

View File

@@ -15,14 +15,15 @@
# limitations under the License.
import mock
from mock import MagicMock
from watcher.common import utils
from watcher.decision_engine.audit.default import DefaultAuditHandler
from watcher.decision_engine.messaging.audit_endpoint import AuditEndpoint
from watcher.metrics_engine.cluster_model_collector.manager import \
CollectorManager
from watcher.tests import base
from watcher.tests.decision_engine.strategy.strategies.faker_cluster_state import \
FakerModelCollector
from watcher.tests.decision_engine.strategy.strategies.faker_cluster_state \
import FakerModelCollector
class DefaultAuditHandlerMock(DefaultAuditHandler):
@@ -36,13 +37,12 @@ class DefaultAuditHandlerMock(DefaultAuditHandler):
class TestAuditEndpoint(base.TestCase):
def setUp(self):
super(TestAuditEndpoint, self).setUp()
self.endpoint = AuditEndpoint(MagicMock())
def test_do_trigger_audit(self):
audit_uuid = utils.generate_uuid()
model_collector = FakerModelCollector()
audit_handler = DefaultAuditHandler(MagicMock(), model_collector)
endpoint = AuditEndpoint(audit_handler)
endpoint = AuditEndpoint(audit_handler, max_workers=2)
with mock.patch.object(CollectorManager, 'get_cluster_model_collector') \
as mock_call2:
@@ -60,7 +60,7 @@ class TestAuditEndpoint(base.TestCase):
model_collector = FakerModelCollector()
audit_handler = DefaultAuditHandlerMock(MagicMock(),
model_collector)
endpoint = AuditEndpoint(audit_handler)
endpoint = AuditEndpoint(audit_handler, max_workers=2)
with mock.patch.object(DefaultAuditHandlerMock, 'executor') \
as mock_call:

View File

@@ -13,19 +13,26 @@
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from mock import MagicMock
from mock import patch
from watcher.decision_engine.strategy.context.default import StrategyContext
from watcher.decision_engine.solution.default import DefaultSolution
from watcher.decision_engine.strategy.context.default import \
DefaultStrategyContext
from watcher.decision_engine.strategy.selection.default import \
DefaultStrategySelector
from watcher.decision_engine.strategy.strategies.dummy_strategy import \
DummyStrategy
from watcher.tests import base
class FakeStrategy(object):
def __init__(self):
self.name = "BALANCE_LOAD"
class TestStrategyContext(base.BaseTestCase):
def test_add_remove_strategy(self):
strategy = FakeStrategy()
strategy_context = StrategyContext()
strategy_context.add_strategy(strategy)
strategy_context.remove_strategy(strategy)
strategy_context = DefaultStrategyContext()
@patch.object(DefaultStrategySelector, 'define_from_goal')
def test_execute_strategy(self, mock_call):
mock_call.return_value = DummyStrategy()
cluster_data_model = MagicMock()
solution = self.strategy_context.execute_strategy("dummy",
cluster_data_model)
self.assertIsInstance(solution, DefaultSolution)

View File

@@ -19,21 +19,20 @@ from oslo_config import cfg
from watcher.common.exception import WatcherException
from watcher.decision_engine.strategy.loading.default import \
DefaultStrategyLoader
from watcher.decision_engine.strategy.selection.default import StrategySelector
from watcher.decision_engine.strategy.selection.default import \
DefaultStrategySelector
from watcher.tests.base import TestCase
CONF = cfg.CONF
class TestStrategySelector(TestCase):
strategy_selector = StrategySelector()
strategy_selector = DefaultStrategySelector()
@patch.object(DefaultStrategyLoader, 'load')
def test_define_from_goal(self, mock_call):
cfg.CONF.set_override(
'goals', {"DUMMY": "fake"}, group='watcher_goals'
)
cfg.CONF.set_override('goals',
{"DUMMY": "fake"}, group='watcher_goals')
expected_goal = 'DUMMY'
expected_strategy = CONF.watcher_goals.goals[expected_goal]
self.strategy_selector.define_from_goal(expected_goal)
@@ -41,12 +40,8 @@ class TestStrategySelector(TestCase):
@patch.object(DefaultStrategyLoader, 'load')
def test_define_from_goal_with_incorrect_mapping(self, mock_call):
cfg.CONF.set_override(
'goals', {}, group='watcher_goals'
)
self.assertRaises(
WatcherException,
self.strategy_selector.define_from_goal,
"DUMMY"
)
cfg.CONF.set_override('goals', {}, group='watcher_goals')
self.assertRaises(WatcherException,
self.strategy_selector.define_from_goal,
"DUMMY")
self.assertEqual(mock_call.call_count, 0)