Add a dynamic loading of Actions handlers in the Watcher Applier

In watcher, an audit generates a set of actions which
aims at achieving a given goal (lower energy consumption, ...).
It is possible to configure different strategies in order to achieve
each goal. Each strategy is written as a Python class which produces
a set of actions. Today, the set of possible actions is fixed for a
given version of Watcher and enables optimization algorithms to
include actions such as instance migration, changing hypervisor state,
changing power state (ACPI level, ...).

The objective of this patchset is to give the ability to load
the actions dynamically in order to apply the Action Plan.

DocImpact
Partially implements: blueprint watcher-add-actions-via-conf

Change-Id: Idf295b94dca549ac65d4636e8889c8ab2ecc0df6
This commit is contained in:
Jean-Emile DARTOIS
2016-01-11 16:02:28 +01:00
parent ed438d2eb2
commit 8bac4fd42a
29 changed files with 305 additions and 450 deletions

View File

@@ -18,21 +18,17 @@
#
import mock
from watcher.applier.execution.executor import ActionPlanExecutor
from watcher import objects
from watcher.applier.execution import default
from watcher.common import utils
from watcher.decision_engine.planner.default import Primitives
from watcher.objects.action import Action
from watcher.objects.action import Status
from watcher.tests.db.base import DbTestCase
from watcher import objects
from watcher.tests.db import base
class TestCommandExecutor(DbTestCase):
class TestDefaultActionPlanExecutor(base.DbTestCase):
def setUp(self):
super(TestCommandExecutor, self).setUp()
self.applier = mock.MagicMock()
self.executor = ActionPlanExecutor(self.applier, self.context)
super(TestDefaultActionPlanExecutor, self).setUp()
self.executor = default.DefaultActionPlanExecutor(mock.MagicMock(),
self.context)
def test_execute(self):
actions = mock.MagicMock()
@@ -44,19 +40,17 @@ class TestCommandExecutor(DbTestCase):
action = {
'uuid': utils.generate_uuid(),
'action_plan_id': 0,
'action_type': Primitives.NOP.value,
'action_type': "nop",
'applies_to': '',
'src': '',
'dst': '',
'parameter': '',
'description': '',
'state': Status.PENDING,
'input_parameters': {'state': 'OFFLINE'},
'state': objects.action.Status.PENDING,
'alarm': None,
'next': None,
}
new_action = objects.Action(self.context, **action)
new_action.create(self.context)
new_action.save()
actions.append(Action.get_by_uuid(self.context, action['uuid']))
actions.append(objects.Action.get_by_uuid(self.context,
action['uuid']))
result = self.executor.execute(actions)
self.assertEqual(result, True)

View File

@@ -1,59 +0,0 @@
# -*- encoding: utf-8 -*-
# Copyright (c) 2015 b<>com
#
# Authors: Jean-Emile DARTOIS <jean-emile.dartois@b-com.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.
#
import mock
from watcher.applier.mapping.default import DefaultActionMapper
from watcher.decision_engine.planner.default import Primitives
from watcher.tests import base
class TestDefaultActionMapper(base.TestCase):
def setUp(self):
super(TestDefaultActionMapper, self).setUp()
self.mapper = DefaultActionMapper()
def test_build_command_cold(self):
action = mock.MagicMock()
action.action_type = Primitives.COLD_MIGRATE.value
cmd = self.mapper.build_primitive_from_action(action)
self.assertIsNotNone(cmd)
def test_build_command_live(self):
action = mock.MagicMock()
action.action_type = Primitives.LIVE_MIGRATE.value
cmd = self.mapper.build_primitive_from_action(action)
self.assertIsNotNone(cmd)
def test_build_command_h_s(self):
action = mock.MagicMock()
action.action_type = Primitives.HYPERVISOR_STATE.value
cmd = self.mapper.build_primitive_from_action(action)
self.assertIsNotNone(cmd)
def test_build_command_p_s(self):
action = mock.MagicMock()
action.action_type = Primitives.POWER_STATE.value
cmd = self.mapper.build_primitive_from_action(action)
self.assertIsNotNone(cmd)
def test_build_command_exception_attribute(self):
action = mock.MagicMock
self.assertRaises(AttributeError,
self.mapper.build_primitive_from_action,
action)

View File

@@ -1,59 +0,0 @@
# -*- encoding: utf-8 -*-
# Copyright (c) 2015 b<>com
#
# Authors: Jean-Emile DARTOIS <jean-emile.dartois@b-com.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.
#
import mock
from watcher.applier.mapping.default import DefaultActionMapper
from watcher.decision_engine.planner.default import Primitives
from watcher.tests import base
class TestDefaultActionMapper(base.TestCase):
def setUp(self):
super(TestDefaultActionMapper, self).setUp()
self.mapper = DefaultActionMapper()
def test_build_command_cold(self):
action = mock.MagicMock()
action.action_type = Primitives.COLD_MIGRATE.value
cmd = self.mapper.build_primitive_from_action(action)
self.assertIsNotNone(cmd)
def test_build_command_live(self):
action = mock.MagicMock()
action.action_type = Primitives.LIVE_MIGRATE.value
cmd = self.mapper.build_primitive_from_action(action)
self.assertIsNotNone(cmd)
def test_build_command_h_s(self):
action = mock.MagicMock()
action.action_type = Primitives.HYPERVISOR_STATE.value
cmd = self.mapper.build_primitive_from_action(action)
self.assertIsNotNone(cmd)
def test_build_command_p_s(self):
action = mock.MagicMock()
action.action_type = Primitives.POWER_STATE.value
cmd = self.mapper.build_primitive_from_action(action)
self.assertIsNotNone(cmd)
def test_build_command_exception_attribute(self):
action = mock.MagicMock
self.assertRaises(AttributeError,
self.mapper.build_primitive_from_action,
action)

View File

@@ -129,7 +129,7 @@ class TestModel(base.BaseTestCase):
def test_vm_from_id_raise(self):
fake_cluster = FakerModelCollector()
model = fake_cluster.generate_scenario_1()
self.assertRaises(exception.VMNotFound,
self.assertRaises(exception.InstanceNotFound,
model.get_vm_from_id, "valeur_qcq")
def test_assert_vm_raise(self):

View File

@@ -20,7 +20,9 @@ from watcher.tests import base
class TestDefaultPlannerLoader(base.TestCase):
loader = default.DefaultPlannerLoader()
def setUp(self):
super(TestDefaultPlannerLoader, self).setUp()
self.loader = default.DefaultPlannerLoader()
def test_endpoints(self):
for endpoint in self.loader.list_available():

View File

@@ -16,13 +16,13 @@
from oslo_config import cfg
from watcher.decision_engine.planner.default import DefaultPlanner
from watcher.decision_engine.planner.manager import PlannerManager
from watcher.decision_engine.planner import default
from watcher.decision_engine.planner import manager as planner
from watcher.tests import base
class TestPlannerManager(base.TestCase):
def test_load(self):
cfg.CONF.set_override('planner', "default", group='watcher_planner')
manager = PlannerManager()
self.assertIsInstance(manager.load(), DefaultPlanner)
manager = planner.PlannerManager()
self.assertIsInstance(manager.load(), default.DefaultPlanner)