From 422def65a85aeae4edaceed512e4229746e4f9e6 Mon Sep 17 00:00:00 2001 From: Renat Akhmerov Date: Fri, 7 Oct 2016 15:28:45 +0700 Subject: [PATCH] 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 --- .../tests/unit/engine/test_yaql_functions.py | 40 +++++++++++++++++++ mistral/utils/expression_utils.py | 4 +- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/mistral/tests/unit/engine/test_yaql_functions.py b/mistral/tests/unit/engine/test_yaql_functions.py index 1fc98245..5a17e1a2 100644 --- a/mistral/tests/unit/engine/test_yaql_functions.py +++ b/mistral/tests/unit/engine/test_yaql_functions.py @@ -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' diff --git a/mistral/utils/expression_utils.py b/mistral/utils/expression_utils.py index d215d6e1..0c7837d7 100644 --- a/mistral/utils/expression_utils.py +++ b/mistral/utils/expression_utils.py @@ -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.