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:
@@ -11,60 +11,109 @@
|
||||
# limitations under the License.
|
||||
|
||||
from oslo_config import cfg
|
||||
from watcher.tests.api import base as api_base
|
||||
from six.moves.urllib import parse as urlparse
|
||||
|
||||
CONF = cfg.CONF
|
||||
from watcher.common import utils
|
||||
from watcher.tests.api import base as api_base
|
||||
from watcher.tests.objects import utils as obj_utils
|
||||
|
||||
|
||||
class TestListGoal(api_base.FunctionalTest):
|
||||
|
||||
def setUp(self):
|
||||
super(TestListGoal, self).setUp()
|
||||
# Override the default to get enough goals to test limit on query
|
||||
cfg.CONF.set_override(
|
||||
"goals", {
|
||||
"DUMMY_1": "dummy", "DUMMY_2": "dummy",
|
||||
"DUMMY_3": "dummy", "DUMMY_4": "dummy",
|
||||
},
|
||||
group='watcher_goals', enforce_type=True)
|
||||
|
||||
def _assert_goal_fields(self, goal):
|
||||
goal_fields = ['name', 'strategy']
|
||||
goal_fields = ['uuid', 'name', 'display_name']
|
||||
for field in goal_fields:
|
||||
self.assertIn(field, goal)
|
||||
|
||||
def test_one(self):
|
||||
goal = obj_utils.create_test_goal(self.context)
|
||||
response = self.get_json('/goals')
|
||||
self.assertEqual(goal.uuid, response['goals'][0]["uuid"])
|
||||
self._assert_goal_fields(response['goals'][0])
|
||||
|
||||
def test_get_one(self):
|
||||
goal_name = list(CONF.watcher_goals.goals.keys())[0]
|
||||
response = self.get_json('/goals/%s' % goal_name)
|
||||
self.assertEqual(goal_name, response['name'])
|
||||
def test_get_one_by_uuid(self):
|
||||
goal = obj_utils.create_test_goal(self.context)
|
||||
response = self.get_json('/goals/%s' % goal.uuid)
|
||||
self.assertEqual(goal.uuid, response["uuid"])
|
||||
self.assertEqual(goal.name, response["name"])
|
||||
self._assert_goal_fields(response)
|
||||
|
||||
def test_get_one_by_name(self):
|
||||
goal = obj_utils.create_test_goal(self.context)
|
||||
response = self.get_json(urlparse.quote(
|
||||
'/goals/%s' % goal['name']))
|
||||
self.assertEqual(goal.uuid, response['uuid'])
|
||||
self._assert_goal_fields(response)
|
||||
|
||||
def test_get_one_soft_deleted(self):
|
||||
goal = obj_utils.create_test_goal(self.context)
|
||||
goal.soft_delete()
|
||||
response = self.get_json(
|
||||
'/goals/%s' % goal['uuid'],
|
||||
headers={'X-Show-Deleted': 'True'})
|
||||
self.assertEqual(goal.uuid, response['uuid'])
|
||||
self._assert_goal_fields(response)
|
||||
|
||||
response = self.get_json(
|
||||
'/goals/%s' % goal['uuid'],
|
||||
expect_errors=True)
|
||||
self.assertEqual(404, response.status_int)
|
||||
|
||||
def test_detail(self):
|
||||
goal_name = list(CONF.watcher_goals.goals.keys())[0]
|
||||
goal = obj_utils.create_test_goal(self.context)
|
||||
response = self.get_json('/goals/detail')
|
||||
self.assertEqual(goal_name, response['goals'][0]["name"])
|
||||
self.assertEqual(goal.uuid, response['goals'][0]["uuid"])
|
||||
self._assert_goal_fields(response['goals'][0])
|
||||
|
||||
def test_detail_against_single(self):
|
||||
goal_name = list(CONF.watcher_goals.goals.keys())[0]
|
||||
response = self.get_json('/goals/%s/detail' % goal_name,
|
||||
goal = obj_utils.create_test_goal(self.context)
|
||||
response = self.get_json('/goals/%s/detail' % goal.uuid,
|
||||
expect_errors=True)
|
||||
self.assertEqual(404, response.status_int)
|
||||
|
||||
def test_many(self):
|
||||
goal_list = []
|
||||
for idx in range(1, 6):
|
||||
goal = obj_utils.create_test_goal(
|
||||
self.context, id=idx,
|
||||
uuid=utils.generate_uuid(),
|
||||
name='GOAL_{0}'.format(idx))
|
||||
goal_list.append(goal.uuid)
|
||||
response = self.get_json('/goals')
|
||||
self.assertEqual(len(CONF.watcher_goals.goals),
|
||||
len(response['goals']))
|
||||
self.assertTrue(len(response['goals']) > 2)
|
||||
|
||||
def test_many_without_soft_deleted(self):
|
||||
goal_list = []
|
||||
for id_ in [1, 2, 3]:
|
||||
goal = obj_utils.create_test_goal(
|
||||
self.context, id=id_, uuid=utils.generate_uuid(),
|
||||
name='GOAL_{0}'.format(id_))
|
||||
goal_list.append(goal.uuid)
|
||||
for id_ in [4, 5]:
|
||||
goal = obj_utils.create_test_goal(
|
||||
self.context, id=id_, uuid=utils.generate_uuid(),
|
||||
name='GOAL_{0}'.format(id_))
|
||||
goal.soft_delete()
|
||||
response = self.get_json('/goals')
|
||||
self.assertEqual(3, len(response['goals']))
|
||||
uuids = [s['uuid'] for s in response['goals']]
|
||||
self.assertEqual(sorted(goal_list), sorted(uuids))
|
||||
|
||||
def test_goals_collection_links(self):
|
||||
for idx in range(1, 6):
|
||||
obj_utils.create_test_goal(
|
||||
self.context, id=idx,
|
||||
uuid=utils.generate_uuid(),
|
||||
name='GOAL_{0}'.format(idx))
|
||||
response = self.get_json('/goals/?limit=2')
|
||||
self.assertEqual(2, len(response['goals']))
|
||||
|
||||
def test_goals_collection_links_default_limit(self):
|
||||
for idx in range(1, 6):
|
||||
obj_utils.create_test_goal(
|
||||
self.context, id=idx,
|
||||
uuid=utils.generate_uuid(),
|
||||
name='GOAL_{0}'.format(idx))
|
||||
cfg.CONF.set_override('max_limit', 3, 'api', enforce_type=True)
|
||||
response = self.get_json('/goals')
|
||||
self.assertEqual(3, len(response['goals']))
|
||||
|
||||
@@ -24,6 +24,7 @@ from oslo_config import cfg
|
||||
from oslo_service import service
|
||||
|
||||
from watcher.cmd import decisionengine
|
||||
from watcher.decision_engine import sync
|
||||
from watcher.tests import base
|
||||
|
||||
|
||||
@@ -45,6 +46,7 @@ class TestDecisionEngine(base.BaseTestCase):
|
||||
super(TestDecisionEngine, self).tearDown()
|
||||
self.conf._parse_cli_opts = self._parse_cli_opts
|
||||
|
||||
@mock.patch.object(sync.Syncer, "sync", mock.Mock())
|
||||
@mock.patch.object(service, "launch")
|
||||
def test_run_de_app(self, m_launch):
|
||||
decisionengine.main()
|
||||
|
||||
@@ -242,17 +242,15 @@ class DbGoalTestCase(base.DbTestCase):
|
||||
self.assertEqual(uuids.sort(), res_uuids.sort())
|
||||
|
||||
def test_get_goal_list_with_filters(self):
|
||||
goal1_uuid = w_utils.generate_uuid()
|
||||
goal2_uuid = w_utils.generate_uuid()
|
||||
goal1 = self._create_test_goal(
|
||||
id=1,
|
||||
uuid=goal1_uuid,
|
||||
uuid=w_utils.generate_uuid(),
|
||||
name="GOAL_1",
|
||||
display_name='Goal 1',
|
||||
)
|
||||
goal2 = self._create_test_goal(
|
||||
id=2,
|
||||
uuid=goal2_uuid,
|
||||
uuid=w_utils.generate_uuid(),
|
||||
name="GOAL_2",
|
||||
display_name='Goal 2',
|
||||
)
|
||||
|
||||
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]]
|
||||
|
||||
@@ -135,3 +135,30 @@ def create_test_action(context, **kw):
|
||||
action = get_test_action(context, **kw)
|
||||
action.create()
|
||||
return action
|
||||
|
||||
|
||||
def get_test_goal(context, **kw):
|
||||
"""Return a Goal object with appropriate attributes.
|
||||
|
||||
NOTE: The object leaves the attributes marked as changed, such
|
||||
that a create() could be used to commit it to the DB.
|
||||
"""
|
||||
db_goal = db_utils.get_test_goal(**kw)
|
||||
# Let DB generate ID if it isn't specified explicitly
|
||||
if 'id' not in kw:
|
||||
del db_goal['id']
|
||||
goal = objects.Goal(context)
|
||||
for key in db_goal:
|
||||
setattr(goal, key, db_goal[key])
|
||||
return goal
|
||||
|
||||
|
||||
def create_test_goal(context, **kw):
|
||||
"""Create and return a test goal object.
|
||||
|
||||
Create a goal in the DB and return a Goal object with appropriate
|
||||
attributes.
|
||||
"""
|
||||
goal = get_test_goal(context, **kw)
|
||||
goal.create()
|
||||
return goal
|
||||
|
||||
Reference in New Issue
Block a user