From 92787df902411c1a741b0930086c3f726705e9a2 Mon Sep 17 00:00:00 2001 From: Renat Akhmerov Date: Thu, 25 Jun 2015 16:09:37 +0600 Subject: [PATCH] Workflow variables: modifying engine so that variables work Implements blueprint mistral-workflow-variables Change-Id: I92a64526ea70d2c6b8674b12d4cc6d4a5c101151 --- mistral/engine/default_engine.py | 1 + .../unit/engine/test_workflow_variables.py | 84 +++++++++++++++++++ mistral/workflow/data_flow.py | 10 +++ 3 files changed, 95 insertions(+) create mode 100644 mistral/tests/unit/engine/test_workflow_variables.py diff --git a/mistral/engine/default_engine.py b/mistral/engine/default_engine.py index 15af15cce..d0939f90a 100644 --- a/mistral/engine/default_engine.py +++ b/mistral/engine/default_engine.py @@ -439,6 +439,7 @@ class DefaultEngine(base.Engine): data_flow.add_openstack_data_to_context(wf_ex.context) data_flow.add_execution_to_context(wf_ex, wf_ex.context) data_flow.add_environment_to_context(wf_ex, wf_ex.context) + data_flow.add_workflow_variables_to_context(wf_spec, wf_ex.context) return wf_ex diff --git a/mistral/tests/unit/engine/test_workflow_variables.py b/mistral/tests/unit/engine/test_workflow_variables.py new file mode 100644 index 000000000..bff0a78ab --- /dev/null +++ b/mistral/tests/unit/engine/test_workflow_variables.py @@ -0,0 +1,84 @@ +# Copyright 2015 - Mirantis, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from oslo_config import cfg +from oslo_log import log as logging + +from mistral.db.v2 import api as db_api +from mistral.services import workflows as wf_service +from mistral.tests.unit.engine import base +from mistral.workflow import states + + +LOG = logging.getLogger(__name__) + +# Use the set_default method to set value otherwise in certain test cases +# the change in value is not permanent. +cfg.CONF.set_default('auth_enable', False, group='pecan') + + +class WorkflowVariablesTest(base.EngineTestCase): + def test_workflow_variables(self): + wf_text = """--- + version: '2.0' + + wf: + input: + - param1: "Hello" + - param2 + + vars: + literal_var: "Literal value" + yaql_var: "<% $.param1 %> <% $.param2 %>" + + output: + literal_var: <% $.literal_var %> + yaql_var: <% $.yaql_var %> + + tasks: + task1: + action: std.noop + """ + + wf_service.create_workflows(wf_text) + + # Start workflow. + wf_ex = self.engine.start_workflow('wf', {'param2': 'Renat'}) + + self._await(lambda: self.is_execution_success(wf_ex.id)) + + # Note: We need to reread execution to access related tasks. + wf_ex = db_api.get_workflow_execution(wf_ex.id) + + tasks = wf_ex.task_executions + + task1 = self._assert_single_item(tasks, name='task1') + + self.assertEqual(states.SUCCESS, task1.state) + + self._assert_dict_contains_subset( + { + 'literal_var': 'Literal value', + 'yaql_var': 'Hello Renat' + }, + task1.in_context + ) + + self.assertDictEqual( + { + 'literal_var': 'Literal value', + 'yaql_var': 'Hello Renat' + }, + wf_ex.output + ) diff --git a/mistral/workflow/data_flow.py b/mistral/workflow/data_flow.py index d42ae4bae..02ec20888 100644 --- a/mistral/workflow/data_flow.py +++ b/mistral/workflow/data_flow.py @@ -267,6 +267,16 @@ def add_environment_to_context(wf_ex, context): return context +def add_workflow_variables_to_context(wf_spec, context): + if context is None: + context = {} + + return utils.merge_dicts( + context, + expr.evaluate_recursively(wf_spec.get_vars(), context) + ) + + # TODO(rakhmerov): Think how to get rid of this method. It should not be # exposed in API. def extract_task_result_proxies_to_context(ctx):