Add Goal in BaseStrategy + Goal API reads from DB
In this changeset, I changed the Strategy base class to add new abstract class methods. I also added an abstract strategy class per Goal type (dummy, server consolidation, thermal optimization). This changeset also includes an update of the /goals Watcher API endpoint to now use the new Goal model (DB entries) instead of reading from the configuration file. Partially Implements: blueprint get-goal-from-strategy Change-Id: Iecfed58c72f3f9df4e9d27e50a3a274a1fc0a75f
This commit is contained in:
80
watcher/tests/decision_engine/fake_strategies.py
Normal file
80
watcher/tests/decision_engine/fake_strategies.py
Normal file
@@ -0,0 +1,80 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2016 b<>com
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from watcher.decision_engine.strategy.strategies import base as base_strategy
|
||||
|
||||
|
||||
class FakeStrategy(base_strategy.BaseStrategy):
|
||||
|
||||
GOAL_NAME = NotImplemented
|
||||
GOAL_DISPLAY_NAME = NotImplemented
|
||||
NAME = NotImplemented
|
||||
DISPLAY_NAME = NotImplemented
|
||||
|
||||
@classmethod
|
||||
def get_name(cls):
|
||||
return cls.NAME
|
||||
|
||||
@classmethod
|
||||
def get_display_name(cls):
|
||||
return cls.DISPLAY_NAME
|
||||
|
||||
@classmethod
|
||||
def get_translatable_display_name(cls):
|
||||
return cls.DISPLAY_NAME
|
||||
|
||||
@classmethod
|
||||
def get_goal_name(cls):
|
||||
return cls.GOAL_NAME
|
||||
|
||||
@classmethod
|
||||
def get_goal_display_name(cls):
|
||||
return cls.GOAL_DISPLAY_NAME
|
||||
|
||||
@classmethod
|
||||
def get_translatable_goal_display_name(cls):
|
||||
return cls.GOAL_DISPLAY_NAME
|
||||
|
||||
def execute(self, original_model):
|
||||
pass
|
||||
|
||||
|
||||
class FakeDummy1Strategy1(FakeStrategy):
|
||||
GOAL_NAME = "DUMMY_1"
|
||||
GOAL_DISPLAY_NAME = "Dummy 1"
|
||||
NAME = "STRATEGY_1"
|
||||
DISPLAY_NAME = "Strategy 1"
|
||||
|
||||
|
||||
class FakeDummy1Strategy2(FakeStrategy):
|
||||
GOAL_NAME = "DUMMY_1"
|
||||
GOAL_DISPLAY_NAME = "Dummy 1"
|
||||
NAME = "STRATEGY_2"
|
||||
DISPLAY_NAME = "Strategy 2"
|
||||
|
||||
|
||||
class FakeDummy2Strategy3(FakeStrategy):
|
||||
GOAL_NAME = "DUMMY_2"
|
||||
GOAL_DISPLAY_NAME = "Dummy 2"
|
||||
NAME = "STRATEGY_3"
|
||||
DISPLAY_NAME = "Strategy 3"
|
||||
|
||||
|
||||
class FakeDummy2Strategy4(FakeStrategy):
|
||||
GOAL_NAME = "DUMMY_2"
|
||||
GOAL_DISPLAY_NAME = "Other Dummy 2"
|
||||
NAME = "STRATEGY_4"
|
||||
DISPLAY_NAME = "Strategy 4"
|
||||
@@ -36,8 +36,7 @@ class SolutionFaker(object):
|
||||
def build():
|
||||
metrics = fake.FakerMetricsCollector()
|
||||
current_state_cluster = faker_cluster_state.FakerModelCollector()
|
||||
sercon = strategies.BasicConsolidation("basic",
|
||||
"Basic offline consolidation")
|
||||
sercon = strategies.BasicConsolidation()
|
||||
sercon.ceilometer = mock.\
|
||||
MagicMock(get_statistics=metrics.mock_get_statistics)
|
||||
return sercon.execute(current_state_cluster.generate_scenario_1())
|
||||
@@ -48,8 +47,7 @@ class SolutionFakerSingleHyp(object):
|
||||
def build():
|
||||
metrics = fake.FakerMetricsCollector()
|
||||
current_state_cluster = faker_cluster_state.FakerModelCollector()
|
||||
sercon = strategies.BasicConsolidation("basic",
|
||||
"Basic offline consolidation")
|
||||
sercon = strategies.BasicConsolidation()
|
||||
sercon.ceilometer = \
|
||||
mock.MagicMock(get_statistics=metrics.mock_get_statistics)
|
||||
|
||||
|
||||
@@ -32,11 +32,11 @@ class TestDefaultStrategyLoader(base.TestCase):
|
||||
exception.LoadingError, self.strategy_loader.load, None)
|
||||
|
||||
def test_load_strategy_is_basic(self):
|
||||
exptected_strategy = 'basic'
|
||||
selected_strategy = self.strategy_loader.load(exptected_strategy)
|
||||
expected_strategy = 'basic'
|
||||
selected_strategy = self.strategy_loader.load(expected_strategy)
|
||||
self.assertEqual(
|
||||
selected_strategy.name,
|
||||
exptected_strategy,
|
||||
selected_strategy.id,
|
||||
expected_strategy,
|
||||
'The default strategy should be basic')
|
||||
|
||||
@patch("watcher.common.loader.default.ExtensionManager")
|
||||
@@ -58,8 +58,8 @@ class TestDefaultStrategyLoader(base.TestCase):
|
||||
strategy_loader = default_loading.DefaultStrategyLoader()
|
||||
loaded_strategy = strategy_loader.load("dummy")
|
||||
|
||||
self.assertEqual("dummy", loaded_strategy.name)
|
||||
self.assertEqual("Dummy Strategy", loaded_strategy.description)
|
||||
self.assertEqual("dummy", loaded_strategy.id)
|
||||
self.assertEqual("Dummy strategy", loaded_strategy.display_name)
|
||||
|
||||
def test_load_dummy_strategy(self):
|
||||
strategy_loader = default_loading.DefaultStrategyLoader()
|
||||
|
||||
@@ -23,14 +23,14 @@ from watcher.tests.decision_engine.strategy.strategies import \
|
||||
|
||||
class TestDummyStrategy(base.TestCase):
|
||||
def test_dummy_strategy(self):
|
||||
dummy = strategies.DummyStrategy("dummy", "Dummy strategy")
|
||||
dummy = strategies.DummyStrategy()
|
||||
fake_cluster = faker_cluster_state.FakerModelCollector()
|
||||
model = fake_cluster.generate_scenario_3_with_2_hypervisors()
|
||||
solution = dummy.execute(model)
|
||||
self.assertEqual(3, len(solution.actions))
|
||||
|
||||
def test_check_parameters(self):
|
||||
dummy = strategies.DummyStrategy("dummy", "Dummy strategy")
|
||||
dummy = strategies.DummyStrategy()
|
||||
fake_cluster = faker_cluster_state.FakerModelCollector()
|
||||
model = fake_cluster.generate_scenario_3_with_2_hypervisors()
|
||||
solution = dummy.execute(model)
|
||||
|
||||
@@ -19,38 +19,10 @@ import mock
|
||||
from watcher.common import context
|
||||
from watcher.common import utils
|
||||
from watcher.decision_engine.strategy.loading import default
|
||||
from watcher.decision_engine.strategy.strategies import base as base_strategy
|
||||
from watcher.decision_engine import sync
|
||||
from watcher import objects
|
||||
from watcher.tests.db import base
|
||||
|
||||
|
||||
class FakeStrategy(base_strategy.BaseStrategy):
|
||||
DEFAULT_NAME = ""
|
||||
DEFAULT_DESCRIPTION = ""
|
||||
|
||||
def execute(self, original_model):
|
||||
pass
|
||||
|
||||
|
||||
class FakeDummy1Strategy1(FakeStrategy):
|
||||
DEFAULT_NAME = "DUMMY_1"
|
||||
DEFAULT_DESCRIPTION = "Dummy 1"
|
||||
|
||||
|
||||
class FakeDummy1Strategy2(FakeStrategy):
|
||||
DEFAULT_NAME = "DUMMY_1"
|
||||
DEFAULT_DESCRIPTION = "Dummy 1"
|
||||
|
||||
|
||||
class FakeDummy2Strategy3(FakeStrategy):
|
||||
DEFAULT_NAME = "DUMMY_2"
|
||||
DEFAULT_DESCRIPTION = "Dummy 2"
|
||||
|
||||
|
||||
class FakeDummy2Strategy4(FakeStrategy):
|
||||
DEFAULT_NAME = "DUMMY_2"
|
||||
DEFAULT_DESCRIPTION = "Other Dummy 2"
|
||||
from watcher.tests.decision_engine import fake_strategies
|
||||
|
||||
|
||||
class TestSyncer(base.DbTestCase):
|
||||
@@ -60,10 +32,14 @@ class TestSyncer(base.DbTestCase):
|
||||
self.ctx = context.make_context()
|
||||
|
||||
self.m_available_strategies = mock.Mock(return_value={
|
||||
FakeDummy1Strategy1.__name__: FakeDummy1Strategy1,
|
||||
FakeDummy1Strategy2.__name__: FakeDummy1Strategy2,
|
||||
FakeDummy2Strategy3.__name__: FakeDummy2Strategy3,
|
||||
FakeDummy2Strategy4.__name__: FakeDummy2Strategy4,
|
||||
fake_strategies.FakeDummy1Strategy1.get_name():
|
||||
fake_strategies.FakeDummy1Strategy1,
|
||||
fake_strategies.FakeDummy1Strategy2.get_name():
|
||||
fake_strategies.FakeDummy1Strategy2,
|
||||
fake_strategies.FakeDummy2Strategy3.get_name():
|
||||
fake_strategies.FakeDummy2Strategy3,
|
||||
fake_strategies.FakeDummy2Strategy4.get_name():
|
||||
fake_strategies.FakeDummy2Strategy4,
|
||||
})
|
||||
|
||||
p_strategies = mock.patch.object(
|
||||
@@ -150,8 +126,8 @@ class TestSyncer(base.DbTestCase):
|
||||
name="DUMMY_1", display_name="Dummy 1")
|
||||
]
|
||||
m_s_list.return_value = [
|
||||
objects.Strategy(self.ctx, id=1, name="FakeDummy1Strategy1",
|
||||
goal_id=1, display_name="Dummy 1")
|
||||
objects.Strategy(self.ctx, id=1, name="STRATEGY_1",
|
||||
goal_id=1, display_name="Strategy 1")
|
||||
]
|
||||
self.syncer.sync()
|
||||
|
||||
@@ -211,7 +187,7 @@ class TestSyncer(base.DbTestCase):
|
||||
name="DUMMY_1", display_name="Dummy 1")
|
||||
]
|
||||
m_s_list.return_value = [
|
||||
objects.Strategy(self.ctx, id=1, name="FakeDummy1Strategy1",
|
||||
objects.Strategy(self.ctx, id=1, name="STRATEGY_1",
|
||||
goal_id=1, display_name="original")
|
||||
]
|
||||
self.syncer.sync()
|
||||
@@ -229,7 +205,7 @@ class TestSyncer(base.DbTestCase):
|
||||
name="DUMMY_1", display_name="Original")
|
||||
goal.create()
|
||||
strategy = objects.Strategy(
|
||||
self.ctx, id=1, name="FakeDummy1Strategy1",
|
||||
self.ctx, id=1, name="STRATEGY_1",
|
||||
display_name="Original", goal_id=goal.id)
|
||||
strategy.create()
|
||||
# audit_template = objects.AuditTemplate(
|
||||
@@ -260,8 +236,7 @@ class TestSyncer(base.DbTestCase):
|
||||
{"DUMMY_1", "DUMMY_2"},
|
||||
set([g.name for g in after_goals]))
|
||||
self.assertEqual(
|
||||
{'FakeDummy1Strategy1', 'FakeDummy1Strategy2',
|
||||
'FakeDummy2Strategy3', 'FakeDummy2Strategy4'},
|
||||
{"STRATEGY_1", "STRATEGY_2", "STRATEGY_3", "STRATEGY_4"},
|
||||
set([s.name for s in after_strategies]))
|
||||
created_goals = [ag for ag in after_goals
|
||||
if ag.uuid not in [bg.uuid for bg in before_goals]]
|
||||
|
||||
Reference in New Issue
Block a user