Change task() function to return 'null' if task doesn't exist

* Before this change function task(task_name) available in Mistral
  expressions would fail with an error if such task doesn't exist.
  It makes impossible to have expressions like <% task(t1) != null %>
  which is very convenient in cases when we need to gather meta
  info about how a workflow was running and what tasks have already
  started and what have not. So now it simply returns 'null'.

Change-Id: Ifd1ea9b5ee946c7072044d1402296710676845db
Closes-Bug: #1631035
This commit is contained in:
Renat Akhmerov 2016-10-07 15:28:45 +07:00
parent 84a68a6210
commit 422def65a8
2 changed files with 41 additions and 3 deletions

View File

@ -108,6 +108,46 @@ class YAQLFunctionsEngineTest(engine_test_base.EngineTestCase):
task2.published
)
def test_task_function_returns_null(self):
wf_text = """---
version: '2.0'
wf:
output:
task2: <% task(task2) %>
task2bool: <% task(task2) = null %>
tasks:
task1:
action: std.noop
on-success:
- task2: <% false %>
task2:
action: std.noop
"""
wf_service.create_workflows(wf_text)
wf_ex = self.engine.start_workflow('wf', {})
self.await_workflow_success(wf_ex.id)
with db_api.transaction():
wf_ex = db_api.get_workflow_execution(wf_ex.id)
self.assertDictEqual(
{
'task2': None,
'task2bool': True
},
wf_ex.output
)
task_execs = wf_ex.task_executions
self.assertEqual(1, len(task_execs))
def test_task_function_non_existing(self):
wf_text = """---
version: '2.0'

View File

@ -138,9 +138,7 @@ def task_(context, task_name):
task_ex = task_execs[-1] if len(task_execs) > 0 else None
if not task_ex:
raise ValueError(
'Failed to find task execution with name: %s' % task_name
)
return None
# We don't use to_dict() db model method because not all fields
# make sense for user.