From d17ea38b272cdf0e71f4577f58b12ff7f6c46395 Mon Sep 17 00:00:00 2001 From: Renat Akhmerov Date: Tue, 31 Dec 2013 12:43:40 +0700 Subject: [PATCH] Fixing workbook events creation * Workbook events creation is now called from a REST controller rather than from DB API. This is more proper place (level of abstraction). * Fixed workbook definition controller and evets tests. Change-Id: Ib17d0b0dae614c474c161cd12ecbb1b05aea74a8 --- .../api/controllers/v1/workbook_definition.py | 6 ++++- mistral/db/api.py | 5 +--- .../controllers/test_workbook_definition.py | 26 ++++++++++++++++--- mistral/tests/unit/events/test_events.py | 12 ++++++--- 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/mistral/api/controllers/v1/workbook_definition.py b/mistral/api/controllers/v1/workbook_definition.py index 9fcd231c..d432032f 100644 --- a/mistral/api/controllers/v1/workbook_definition.py +++ b/mistral/api/controllers/v1/workbook_definition.py @@ -18,6 +18,7 @@ from pecan import request from mistral.openstack.common import log as logging from mistral.db import api as db_api +from mistral.services import scheduler LOG = logging.getLogger(__name__) @@ -38,4 +39,7 @@ class WorkbookDefinitionController(rest.RestController): LOG.debug("Update workbook definition [workbook_name=%s, text=%s]" % (workbook_name, text)) - return db_api.workbook_definition_put(workbook_name, text) + wb = db_api.workbook_definition_put(workbook_name, text) + scheduler.create_associated_events(wb) + + return wb['definition'] diff --git a/mistral/db/api.py b/mistral/db/api.py index 4c07a53a..2a9e4fc5 100644 --- a/mistral/db/api.py +++ b/mistral/db/api.py @@ -16,7 +16,6 @@ from mistral.openstack.common.db import api as db_api from mistral.openstack.common import log as logging -from mistral.services import scheduler # Workbooks @@ -83,9 +82,7 @@ def workbook_definition_get(workbook_name): def workbook_definition_put(workbook_name, text): - workbook = IMPL.workbook_update(workbook_name, {'definition': text}) - scheduler.create_associated_events(workbook) - return workbook + return IMPL.workbook_update(workbook_name, {'definition': text}) # Executions diff --git a/mistral/tests/api/v1/controllers/test_workbook_definition.py b/mistral/tests/api/v1/controllers/test_workbook_definition.py index a340205d..08571c15 100644 --- a/mistral/tests/api/v1/controllers/test_workbook_definition.py +++ b/mistral/tests/api/v1/controllers/test_workbook_definition.py @@ -36,7 +36,7 @@ class TestWorkbookDefinitionController(base.FunctionalTest): db_api.workbook_definition_get = self.workbook_definition_get def test_get(self): - db_api.workbook_definition_get =\ + db_api.workbook_definition_get = \ mock.MagicMock(return_value=DEFINITION) resp = self.app.get('/v1/workbooks/my_workbook/definition', @@ -46,10 +46,21 @@ class TestWorkbookDefinitionController(base.FunctionalTest): self.assertEqual(DEFINITION, resp.text) def test_put(self): - new_definition = "new definition" + new_definition = """ + Workflow: + events: + create-vms: + type: periodic + tasks: create-vms + parameters: + cron-pattern: "* * * * *" + """ - db_api.workbook_definition_put =\ - mock.MagicMock(return_value=new_definition) + db_api.workbook_definition_put = \ + mock.MagicMock(return_value={ + 'name': 'my_workbook', + 'definition': new_definition + }) resp = self.app.put('/v1/workbooks/my_workbook/definition', new_definition, @@ -57,3 +68,10 @@ class TestWorkbookDefinitionController(base.FunctionalTest): self.assertEqual(resp.status_int, 200) self.assertEqual(new_definition, resp.body) + + # Check that associated events have been created in DB. + events = db_api.events_get(workbook_name='my_workbook') + + self.assertEqual(events[0]['name'], 'create-vms') + self.assertEqual(events[0]['pattern'], '* * * * *') + self.assertEqual(events[0]['workbook_name'], 'my_workbook') diff --git a/mistral/tests/unit/events/test_events.py b/mistral/tests/unit/events/test_events.py index 858af438..c03b8edd 100644 --- a/mistral/tests/unit/events/test_events.py +++ b/mistral/tests/unit/events/test_events.py @@ -20,6 +20,7 @@ from mistral.db import api as db_api from mistral import dsl from mistral.tests.unit import base from mistral import version +from mistral.services import scheduler class EventsTest(base.DbTestCase): @@ -30,9 +31,14 @@ class EventsTest(base.DbTestCase): "tests/resources/test_rest.yaml")).read() self.dsl = dsl.Parser(self.doc) - def test_events_create_after_wb_put(self): - db_api.workbook_create({'name': 'my_workbook'}) - db_api.workbook_definition_put('my_workbook', self.doc) + def test_create_associated_events(self): + workbook = { + 'name': 'my_workbook', + 'definition': self.doc + } + + scheduler.create_associated_events(workbook) + events = db_api.events_get(workbook_name='my_workbook') self.assertEqual(events[0]['name'], 'create-vms')