Fix StrategyContext to use the strategy_id in the Audit Template

This patch fixes the StrategyContext to use the optional
attribute strategy_id.

Change-Id: Ib78581f564282de6cfc7f07495c846615ec1866a
Closed-bug: #1590357
This commit is contained in:
Jean-Emile DARTOIS
2016-06-08 15:44:00 +02:00
parent 6f2c82316c
commit 7b403c0d3b
2 changed files with 88 additions and 17 deletions

View File

@@ -25,7 +25,6 @@ LOG = log.getLogger(__name__)
class DefaultStrategyContext(base.BaseStrategyContext):
def __init__(self):
super(DefaultStrategyContext, self).__init__()
LOG.debug("Initializing Strategy Context")
@@ -41,10 +40,20 @@ class DefaultStrategyContext(base.BaseStrategyContext):
# todo(jed) retrieve in audit_template parameters (threshold,...)
# todo(jed) create ActionPlan
goal = objects.Goal.get_by_id(request_context, audit_template.goal_id)
# NOTE(jed56) In the audit_template object, the 'strategy_id' attribute
# is optional. If the admin wants to force the trigger of a Strategy
# it could specify the Strategy uuid in the Audit Template.
strategy_name = None
if audit_template.strategy_id:
strategy = objects.Strategy.get_by_id(request_context,
audit_template.strategy_id)
strategy_name = strategy.name
strategy_selector = default.DefaultStrategySelector(
goal_name=objects.Goal.get_by_id(
request_context, audit_template.goal_id).name,
strategy_name=None,
goal_name=goal.name,
strategy_name=strategy_name,
osc=osc)
selected_strategy = strategy_selector.select()

View File

@@ -13,15 +13,14 @@
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
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 import dummy_strategy
from watcher.common import utils
from watcher.decision_engine.solution import default
from watcher.decision_engine.strategy.context import default as d_strategy_ctx
from watcher.decision_engine.strategy.selection import default as d_selector
from watcher.decision_engine.strategy import strategies
from watcher.metrics_engine.cluster_model_collector import manager
from watcher.tests.db import base
from watcher.tests.objects import utils as obj_utils
@@ -30,19 +29,82 @@ class TestStrategyContext(base.DbTestCase):
def setUp(self):
super(TestStrategyContext, self).setUp()
obj_utils.create_test_goal(self.context, id=1, name="DUMMY")
audit_template = obj_utils.create_test_audit_template(self.context)
audit_template = obj_utils.create_test_audit_template(
self.context, uuid=utils.generate_uuid())
self.audit = obj_utils.create_test_audit(
self.context, audit_template_id=audit_template.id)
strategy_context = DefaultStrategyContext()
strategy_context = d_strategy_ctx.DefaultStrategyContext()
@mock.patch.object(dummy_strategy.DummyStrategy, 'model',
@mock.patch.object(strategies.DummyStrategy, 'model',
new_callable=mock.PropertyMock)
@mock.patch.object(DefaultStrategySelector, 'select')
@mock.patch.object(d_selector.DefaultStrategySelector, 'select')
def test_execute_strategy(self, mock_call, m_model):
m_model.return_value = mock.Mock()
mock_call.return_value = dummy_strategy.DummyStrategy(
mock_call.return_value = strategies.DummyStrategy(
config=mock.Mock())
solution = self.strategy_context.execute_strategy(
self.audit.uuid, self.context)
self.assertIsInstance(solution, DefaultSolution)
self.assertIsInstance(solution, default.DefaultSolution)
@mock.patch.object(manager.CollectorManager, "get_cluster_model_collector",
mock.Mock())
def test_execute_force_dummy(self):
obj_utils.create_test_goal(self.context, id=50,
uuid=utils.generate_uuid(),
name="my_goal")
strategy = obj_utils.create_test_strategy(self.context,
id=42,
uuid=utils.generate_uuid(),
name="dummy")
audit_template = obj_utils.create_test_audit_template(
self.context,
uuid=utils.generate_uuid(),
strategy_id=strategy.id,
name="my_template")
audit = obj_utils.create_test_audit(
self.context,
audit_template_id=audit_template.id,
uuid=utils.generate_uuid(),
)
solution = self.strategy_context.execute_strategy(
audit.uuid, self.context)
self.assertEqual(len(solution.actions), 3)
@mock.patch.object(strategies.BasicConsolidation, "execute")
@mock.patch.object(manager.CollectorManager, "get_cluster_model_collector",
mock.Mock())
def test_execute_force_basic(self, mock_call):
expected_strategy = "basic"
mock_call.return_value = expected_strategy
obj_utils.create_test_goal(self.context, id=50,
uuid=utils.generate_uuid(),
name="my_goal")
strategy = obj_utils.create_test_strategy(self.context,
id=42,
uuid=utils.generate_uuid(),
name=expected_strategy)
audit_template = obj_utils.create_test_audit_template(
self.context,
uuid=utils.generate_uuid(),
strategy_id=strategy.id,
name="my_template")
audit = obj_utils.create_test_audit(
self.context,
audit_template_id=audit_template.id,
uuid=utils.generate_uuid(),
)
solution = self.strategy_context.execute_strategy(
audit.uuid, self.context)
self.assertEqual(solution, expected_strategy)