Move Audit-template management in DefaultStrategyContext

This aim of this patchset is to move the management of the
Audit-Template into StrategyContext
in order to prepare to pass parameters to strategies
but also to prepare to add more dynamic Actions management

Partially implements: blueprint glossary-related-refactoring

Change-Id: I13ee063da947113ce349855aa331a22f40567051
This commit is contained in:
Jean-Emile DARTOIS
2015-12-21 16:29:35 +01:00
parent 853145f4d1
commit dfaba80252
7 changed files with 94 additions and 78 deletions

View File

@@ -13,10 +13,12 @@
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from mock import call
from mock import MagicMock
import mock
from watcher.decision_engine.audit.default import DefaultAuditHandler
from watcher.decision_engine.messaging.events import Events
from watcher.metrics_engine.cluster_model_collector.manager import \
CollectorManager
from watcher.objects.audit import Audit
from watcher.objects.audit import AuditStatus
from watcher.tests.db.base import DbTestCase
@@ -34,28 +36,31 @@ class TestDefaultAuditHandler(DbTestCase):
self.context,
audit_template_id=self.audit_template.id)
def test_trigger_audit_without_errors(self):
model_collector = FakerModelCollector()
audit_handler = DefaultAuditHandler(MagicMock(), model_collector)
@mock.patch.object(CollectorManager, "get_cluster_model_collector")
def test_trigger_audit_without_errors(self, mock_collector):
mock_collector.return_value = FakerModelCollector()
audit_handler = DefaultAuditHandler(mock.MagicMock())
audit_handler.execute(self.audit.uuid, self.context)
def test_trigger_audit_state_success(self):
model_collector = FakerModelCollector()
audit_handler = DefaultAuditHandler(MagicMock(), model_collector)
@mock.patch.object(CollectorManager, "get_cluster_model_collector")
def test_trigger_audit_state_success(self, mock_collector):
mock_collector.return_value = FakerModelCollector()
audit_handler = DefaultAuditHandler(mock.MagicMock())
audit_handler.execute(self.audit.uuid, self.context)
audit = Audit.get_by_uuid(self.context, self.audit.uuid)
self.assertEqual(AuditStatus.SUCCEEDED, audit.state)
def test_trigger_audit_send_notification(self):
messaging = MagicMock()
model_collector = FakerModelCollector()
audit_handler = DefaultAuditHandler(messaging, model_collector)
@mock.patch.object(CollectorManager, "get_cluster_model_collector")
def test_trigger_audit_send_notification(self, mock_collector):
messaging = mock.MagicMock()
mock_collector.return_value = FakerModelCollector()
audit_handler = DefaultAuditHandler(messaging)
audit_handler.execute(self.audit.uuid, self.context)
call_on_going = call(Events.TRIGGER_AUDIT.name, {
call_on_going = mock.call(Events.TRIGGER_AUDIT.name, {
'audit_status': AuditStatus.ONGOING,
'audit_uuid': self.audit.uuid})
call_succeeded = call(Events.TRIGGER_AUDIT.name, {
call_succeeded = mock.call(Events.TRIGGER_AUDIT.name, {
'audit_status': AuditStatus.SUCCEEDED,
'audit_uuid': self.audit.uuid})

View File

@@ -14,55 +14,51 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from mock import MagicMock
from watcher.common import utils
from watcher.decision_engine.audit.default import DefaultAuditHandler
from watcher.decision_engine.messaging.audit_endpoint import AuditEndpoint
from watcher.metrics_engine.cluster_model_collector.manager import \
CollectorManager
from watcher.tests import base
from watcher.tests.db.base import DbTestCase
from watcher.tests.decision_engine.strategy.strategies.faker_cluster_state \
import FakerModelCollector
from watcher.tests.objects import utils as obj_utils
class DefaultAuditHandlerMock(DefaultAuditHandler):
def setUp(self):
super(DefaultAuditHandlerMock, self).setUp()
def executor(self):
pass
class TestAuditEndpoint(base.TestCase):
class TestAuditEndpoint(DbTestCase):
def setUp(self):
super(TestAuditEndpoint, self).setUp()
self.audit_template = obj_utils.create_test_audit_template(
self.context)
self.audit = obj_utils.create_test_audit(
self.context,
audit_template_id=self.audit_template.id)
def test_do_trigger_audit(self):
@mock.patch.object(CollectorManager, "get_cluster_model_collector")
def test_do_trigger_audit(self, mock_collector):
mock_collector.return_value = FakerModelCollector()
audit_uuid = utils.generate_uuid()
model_collector = FakerModelCollector()
audit_handler = DefaultAuditHandler(MagicMock(), model_collector)
audit_handler = DefaultAuditHandler(mock.MagicMock())
endpoint = AuditEndpoint(audit_handler, max_workers=2)
with mock.patch.object(CollectorManager, 'get_cluster_model_collector') \
as mock_call2:
mock_call2.return_value = 0
with mock.patch.object(DefaultAuditHandler, 'execute') as mock_call:
mock_call.return_value = 0
endpoint.do_trigger_audit(audit_handler, audit_uuid)
with mock.patch.object(DefaultAuditHandler, 'execute') \
as mock_call:
mock_call.return_value = 0
endpoint.do_trigger_audit(audit_handler, audit_uuid)
# mock_call.assert_called_once_with()
mock_call2.assert_called_once_with()
mock_call.assert_called_once_with(audit_uuid, audit_handler)
def test_trigger_audit(self):
@mock.patch.object(CollectorManager, "get_cluster_model_collector")
def test_trigger_audit(self, mock_collector):
mock_collector.return_value = FakerModelCollector()
audit_uuid = utils.generate_uuid()
model_collector = FakerModelCollector()
audit_handler = DefaultAuditHandlerMock(MagicMock(),
model_collector)
audit_handler = DefaultAuditHandler(mock.MagicMock())
endpoint = AuditEndpoint(audit_handler, max_workers=2)
with mock.patch.object(DefaultAuditHandlerMock, 'executor') \
with mock.patch.object(DefaultAuditHandler, 'execute') \
as mock_call:
mock_call.return_value = 0
endpoint.trigger_audit(audit_handler, audit_uuid)
mock_call.assert_called_once_with(audit_uuid, audit_handler)

View File

@@ -13,8 +13,7 @@
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from mock import MagicMock
from mock import patch
import mock
from watcher.decision_engine.solution.default import DefaultSolution
from watcher.decision_engine.strategy.context.default import \
@@ -23,16 +22,28 @@ from watcher.decision_engine.strategy.selection.default import \
DefaultStrategySelector
from watcher.decision_engine.strategy.strategies.dummy_strategy import \
DummyStrategy
from watcher.tests import base
from watcher.metrics_engine.cluster_model_collector.manager import \
CollectorManager
from watcher.tests.db import base
from watcher.tests.objects import utils as obj_utils
class TestStrategyContext(base.BaseTestCase):
class TestStrategyContext(base.DbTestCase):
def setUp(self):
super(TestStrategyContext, self).setUp()
self.audit_template = obj_utils. \
create_test_audit_template(self.context)
self.audit = obj_utils. \
create_test_audit(self.context,
audit_template_id=self.audit_template.id)
strategy_context = DefaultStrategyContext()
@patch.object(DefaultStrategySelector, 'define_from_goal')
@mock.patch.object(DefaultStrategySelector, 'define_from_goal')
@mock.patch.object(CollectorManager, "get_cluster_model_collector",
mock.Mock())
def test_execute_strategy(self, mock_call):
mock_call.return_value = DummyStrategy()
cluster_data_model = MagicMock()
solution = self.strategy_context.execute_strategy("dummy",
cluster_data_model)
solution = self.strategy_context.execute_strategy(self.audit.uuid,
self.context)
self.assertIsInstance(solution, DefaultSolution)