Generalize exceptions & structure of strategies

Many strategies execute very similar statements especially in
pre_execute and some might raise errors that others might not. This
same pattern of many similar statements can also be observed in
strategies their tests.

This patch addresses these issues, firstly; the BaseStrategy class gets
1 additional method _pre_execute which allows for general logic that
most strategies perform at that stage. This method can be executed
before the similarly named method of the superclass. A notable change
is that _pre_execute now handles common exception handling for
ClusterStateStale & ClusterStateNotDefined exceptions.

A similar pattern is applied to the test classes of the strategies
each of these classes now inherits from the TestBaseStrategy class.
This class provides the common attributes almost every test class for
the strategies requires such as: The mocked compute_model, mocked
audit_scope and an instance of FakerModelCollector.

Finally, some minor changes were required in test_strategy_context
& test_audit_handlers and exceptions around 0 nodes in cluster or
storage are removed.

Change-Id: Ia7154376b2448aac65cf17999cc8c3e1c8309b5b
This commit is contained in:
Dantali0n
2019-02-26 17:41:57 +01:00
parent c7fe13e9e3
commit b24bd7a3bb
35 changed files with 413 additions and 794 deletions

View File

@@ -21,7 +21,6 @@ from oslo_config import cfg
from oslo_log import log
from watcher._i18n import _
from watcher.common import exception
from watcher.decision_engine.model import element
from watcher.decision_engine.strategy.strategies import base
@@ -429,18 +428,7 @@ class BasicConsolidation(base.ServerConsolidationBaseStrategy):
return unsuccessful_migration + 1
def pre_execute(self):
LOG.info("Initializing Server Consolidation")
if not self.compute_model:
raise exception.ClusterStateNotDefined()
if len(self.get_available_compute_nodes()) == 0:
raise exception.ClusterEmpty()
if self.compute_model.stale:
raise exception.ClusterStateStale()
LOG.debug(self.compute_model.to_string())
self._pre_execute()
def do_execute(self):
unsuccessful_migration = 0