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
This commit is contained in:
Renat Akhmerov 2013-12-31 12:43:40 +07:00
parent 2a8f9a7745
commit d17ea38b27
4 changed files with 37 additions and 12 deletions

View File

@ -18,6 +18,7 @@ from pecan import request
from mistral.openstack.common import log as logging from mistral.openstack.common import log as logging
from mistral.db import api as db_api from mistral.db import api as db_api
from mistral.services import scheduler
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -38,4 +39,7 @@ class WorkbookDefinitionController(rest.RestController):
LOG.debug("Update workbook definition [workbook_name=%s, text=%s]" % LOG.debug("Update workbook definition [workbook_name=%s, text=%s]" %
(workbook_name, text)) (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']

View File

@ -16,7 +16,6 @@
from mistral.openstack.common.db import api as db_api from mistral.openstack.common.db import api as db_api
from mistral.openstack.common import log as logging from mistral.openstack.common import log as logging
from mistral.services import scheduler
# Workbooks # Workbooks
@ -83,9 +82,7 @@ def workbook_definition_get(workbook_name):
def workbook_definition_put(workbook_name, text): def workbook_definition_put(workbook_name, text):
workbook = IMPL.workbook_update(workbook_name, {'definition': text}) return IMPL.workbook_update(workbook_name, {'definition': text})
scheduler.create_associated_events(workbook)
return workbook
# Executions # Executions

View File

@ -46,10 +46,21 @@ class TestWorkbookDefinitionController(base.FunctionalTest):
self.assertEqual(DEFINITION, resp.text) self.assertEqual(DEFINITION, resp.text)
def test_put(self): 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 = \ db_api.workbook_definition_put = \
mock.MagicMock(return_value=new_definition) mock.MagicMock(return_value={
'name': 'my_workbook',
'definition': new_definition
})
resp = self.app.put('/v1/workbooks/my_workbook/definition', resp = self.app.put('/v1/workbooks/my_workbook/definition',
new_definition, new_definition,
@ -57,3 +68,10 @@ class TestWorkbookDefinitionController(base.FunctionalTest):
self.assertEqual(resp.status_int, 200) self.assertEqual(resp.status_int, 200)
self.assertEqual(new_definition, resp.body) 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')

View File

@ -20,6 +20,7 @@ from mistral.db import api as db_api
from mistral import dsl from mistral import dsl
from mistral.tests.unit import base from mistral.tests.unit import base
from mistral import version from mistral import version
from mistral.services import scheduler
class EventsTest(base.DbTestCase): class EventsTest(base.DbTestCase):
@ -30,9 +31,14 @@ class EventsTest(base.DbTestCase):
"tests/resources/test_rest.yaml")).read() "tests/resources/test_rest.yaml")).read()
self.dsl = dsl.Parser(self.doc) self.dsl = dsl.Parser(self.doc)
def test_events_create_after_wb_put(self): def test_create_associated_events(self):
db_api.workbook_create({'name': 'my_workbook'}) workbook = {
db_api.workbook_definition_put('my_workbook', self.doc) 'name': 'my_workbook',
'definition': self.doc
}
scheduler.create_associated_events(workbook)
events = db_api.events_get(workbook_name='my_workbook') events = db_api.events_get(workbook_name='my_workbook')
self.assertEqual(events[0]['name'], 'create-vms') self.assertEqual(events[0]['name'], 'create-vms')