ARIA-408 remove execution creation from workflow runner
Separated the creation of the models and the execution of the workflow.
This enables creating the execution, and executing it from a different
process.
diff --git a/aria/cli/commands/executions.py b/aria/cli/commands/executions.py
index cecbbc5..2415e19 100644
--- a/aria/cli/commands/executions.py
+++ b/aria/cli/commands/executions.py
@@ -25,9 +25,11 @@
from .. import logger as cli_logger
from .. import execution_logging
from ..core import aria
+from ...orchestrator import execution_preparer
from ...modeling.models import Execution
-from ...orchestrator.workflow_runner import WorkflowRunner
+from ...orchestrator.workflows.core.engine import Engine
from ...orchestrator.workflows.executor.dry import DryExecutor
+from ...orchestrator.workflows.executor.process import ProcessExecutor
from ...utils import formatting
from ...utils import threading
@@ -141,17 +143,21 @@
WORKFLOW_NAME is the unique name of the workflow within the service (e.g. "uninstall").
"""
service = model_storage.service.get_by_name(service_name)
- executor = DryExecutor() if dry else None # use WorkflowRunner's default executor
+ executor = DryExecutor() if dry else ProcessExecutor(plugin_manager=plugin_manager)
- workflow_runner = \
- WorkflowRunner(
- model_storage, resource_storage, plugin_manager,
- service_id=service.id, workflow_name=workflow_name, inputs=inputs, executor=executor,
- task_max_attempts=task_max_attempts, task_retry_interval=task_retry_interval
- )
+ compiler = execution_preparer.ExecutionPreparer(
+ model_storage,
+ resource_storage,
+ plugin_manager,
+ service,
+ workflow_name
+ )
+ workflow_ctx = compiler.prepare(inputs, executor=executor)
+
+ engine = Engine(executor)
logger.info('Starting {0}execution. Press Ctrl+C cancel'.format('dry ' if dry else ''))
- _run_execution(workflow_runner, logger, model_storage, dry, mark_pattern)
+ _run_execution(engine, workflow_ctx, logger, model_storage, dry, mark_pattern)
@executions.command(name='resume',
@@ -178,45 +184,61 @@
EXECUTION_ID is the unique ID of the execution.
"""
- executor = DryExecutor() if dry else None # use WorkflowRunner's default executor
+ executor = DryExecutor() if dry else ProcessExecutor(plugin_manager=plugin_manager)
- execution = model_storage.execution.get(execution_id)
- if execution.status != execution.CANCELLED:
+ execution_to_resume = model_storage.execution.get(execution_id)
+ if execution_to_resume.status != execution_to_resume.CANCELLED:
logger.info("Can't resume execution {execution.id} - "
"execution is in status {execution.status}. "
- "Can only resume executions in status {valid_status}"
- .format(execution=execution, valid_status=execution.CANCELLED))
+ "Can only resume executions in status {execution.CANCELLED}"
+ .format(execution=execution_to_resume))
return
- workflow_runner = \
- WorkflowRunner(
- model_storage, resource_storage, plugin_manager,
- execution_id=execution_id, retry_failed_tasks=retry_failed_tasks, executor=executor,
- )
+ workflow_ctx = execution_preparer.ExecutionPreparer(
+ model_storage,
+ resource_storage,
+ plugin_manager,
+ execution_to_resume.service,
+ execution_to_resume.workflow_name
+ ).prepare(execution_id=execution_to_resume.id)
+
+ engine = Engine(executor)
logger.info('Resuming {0}execution. Press Ctrl+C cancel'.format('dry ' if dry else ''))
- _run_execution(workflow_runner, logger, model_storage, dry, mark_pattern)
+ _run_execution(engine, workflow_ctx, logger, model_storage, dry, mark_pattern,
+ engine_kwargs=dict(resuming=True, retry_failed=retry_failed_tasks))
-def _run_execution(workflow_runner, logger, model_storage, dry, mark_pattern):
- execution_thread_name = '{0}_{1}'.format(workflow_runner.service.name,
- workflow_runner.execution.workflow_name)
- execution_thread = threading.ExceptionThread(target=workflow_runner.execute,
- name=execution_thread_name)
+def _run_execution(
+ engine,
+ ctx,
+ logger,
+ model_storage,
+ dry,
+ mark_pattern,
+ engine_kwargs=None
+):
+ engine_kwargs = engine_kwargs or {}
+ engine_kwargs['ctx'] = ctx
+ execution_thread_name = '{0}_{1}'.format(ctx.execution.service.name,
+ ctx.execution.workflow_name)
+ execution_thread = threading.ExceptionThread(target=engine.execute,
+ name=execution_thread_name,
+ kwargs=engine_kwargs)
execution_thread.start()
- last_task_id = workflow_runner.execution.logs[-1].id if workflow_runner.execution.logs else 0
- log_iterator = cli_logger.ModelLogIterator(model_storage,
- workflow_runner.execution_id,
- offset=last_task_id)
+ last_task_id = ctx.execution.logs[-1].id if ctx.execution.logs else 0
+ log_iterator = cli_logger.ModelLogIterator(model_storage, ctx.execution.id, offset=last_task_id)
try:
while execution_thread.is_alive():
execution_logging.log_list(log_iterator, mark_pattern=mark_pattern)
execution_thread.join(1)
except KeyboardInterrupt:
- _cancel_execution(workflow_runner, execution_thread, logger, log_iterator)
+ _cancel_execution(engine, ctx, execution_thread, logger, log_iterator)
+
+ model_storage.execution.refresh(ctx.execution)
# It might be the case where some logs were written and the execution was terminated, thus we
# need to drain the remaining logs.
@@ -225,19 +247,18 @@
# raise any errors from the execution thread (note these are not workflow execution errors)
execution_thread.raise_error_if_exists()
- execution = workflow_runner.execution
- logger.info('Execution has ended with "{0}" status'.format(execution.status))
- if execution.status == Execution.FAILED and execution.error:
- logger.info('Execution error:{0}{1}'.format(os.linesep, execution.error))
+ logger.info('Execution has ended with "{0}" status'.format(ctx.execution.status))
+ if ctx.execution.status == Execution.FAILED and ctx.execution.error:
+ logger.info('Execution error:{0}{1}'.format(os.linesep, ctx.execution.error))
if dry:
# remove traces of the dry execution (including tasks, logs, inputs..)
- model_storage.execution.delete(execution)
+ model_storage.execution.delete(ctx.execution)
-def _cancel_execution(workflow_runner, execution_thread, logger, log_iterator):
+def _cancel_execution(engine, ctx, execution_thread, logger, log_iterator):
logger.info('Cancelling execution. Press Ctrl+C again to force-cancel.')
- workflow_runner.cancel()
+ engine.cancel_execution(ctx)
while execution_thread.is_alive():
try:
execution_logging.log_list(log_iterator)
diff --git a/aria/orchestrator/execution_preparer.py b/aria/orchestrator/execution_preparer.py
new file mode 100644
index 0000000..145577a
--- /dev/null
+++ b/aria/orchestrator/execution_preparer.py
@@ -0,0 +1,170 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+
+import os
+import sys
+from datetime import datetime
+
+from . import exceptions
+from .context.workflow import WorkflowContext
+from .workflows import builtin
+from .workflows.core import graph_compiler
+from .workflows.executor.process import ProcessExecutor
+from ..modeling import models
+from ..modeling import utils as modeling_utils
+from ..utils.imports import import_fullname
+
+
+DEFAULT_TASK_MAX_ATTEMPTS = 30
+DEFAULT_TASK_RETRY_INTERVAL = 30
+
+
+class ExecutionPreparer(object):
+ """
+ This class manages any execution and tasks related preparation for an execution of a workflow.
+ """
+ def __init__(
+ self,
+ model_storage,
+ resource_storage,
+ plugin_manager,
+ service,
+ workflow_name,
+ task_max_attempts=None,
+ task_retry_interval=None
+ ):
+ self._model = model_storage
+ self._resource = resource_storage
+ self._plugin = plugin_manager
+ self._service = service
+ self._workflow_name = workflow_name
+ self._task_max_attempts = task_max_attempts or DEFAULT_TASK_MAX_ATTEMPTS
+ self._task_retry_interval = task_retry_interval or DEFAULT_TASK_RETRY_INTERVAL
+
+ def get_workflow_ctx(self, execution):
+ return WorkflowContext(
+ name=self._workflow_name,
+ model_storage=self._model,
+ resource_storage=self._resource,
+ service_id=execution.service.id,
+ execution_id=execution.id,
+ workflow_name=execution.workflow_name,
+ task_max_attempts=self._task_max_attempts,
+ task_retry_interval=self._task_retry_interval,
+ )
+
+ def prepare(self, execution_inputs=None, executor=None, execution_id=None):
+ """
+ If there is a need to prepare a new execution (e.g. execution_id is not provided),
+ a new execution and task models are created. Any any case, a corresponding workflow
+ context is returned.
+
+ :param execution_inputs: inputs for the execution.
+ :param executor: the execution for the tasks
+ :param execution_id: used for an existing execution (mainly for resuming).
+ :return:
+ """
+ assert not (execution_inputs and executor and execution_id)
+
+ if execution_id is None:
+ # If the execution is new
+ execution = self._create_execution_model(execution_inputs)
+ self._model.execution.put(execution)
+ ctx = self.get_workflow_ctx(execution)
+ self._create_tasks(ctx, executor)
+ self._model.execution.update(execution)
+ else:
+ # If resuming an execution
+ execution = self._model.execution.get(execution_id)
+ ctx = self.get_workflow_ctx(execution)
+
+ return ctx
+
+ def _create_tasks(self, ctx, executor=None):
+
+ # Set default executor and kwargs
+ executor = executor or ProcessExecutor(plugin_manager=self._plugin)
+
+ # transforming the execution inputs to dict, to pass them to the workflow function
+ execution_inputs_dict = dict(inp.unwrapped for inp in ctx.execution.inputs.itervalues())
+
+ workflow_fn = self._get_workflow_fn(ctx.execution.workflow_name)
+ api_tasks_graph = workflow_fn(ctx=ctx, **execution_inputs_dict)
+ compiler = graph_compiler.GraphCompiler(ctx, executor.__class__)
+ compiler.compile(api_tasks_graph)
+
+ def _create_execution_model(self, inputs=None):
+ self._validate_workflow_exists_for_service()
+ self._validate_no_active_executions()
+
+ execution = models.Execution(
+ created_at=datetime.utcnow(),
+ service_fk=self._service.id,
+ workflow_name=self._workflow_name,
+ inputs={})
+
+ if self._workflow_name in builtin.BUILTIN_WORKFLOWS:
+ workflow_inputs = dict() # built-in workflows don't have any inputs
+ else:
+ workflow_inputs = self._service.workflows[self._workflow_name].inputs
+
+ modeling_utils.validate_no_undeclared_inputs(declared_inputs=workflow_inputs,
+ supplied_inputs=inputs or {})
+ modeling_utils.validate_required_inputs_are_supplied(declared_inputs=workflow_inputs,
+ supplied_inputs=inputs or {})
+ execution.inputs = modeling_utils.merge_parameter_values(
+ inputs, workflow_inputs, model_cls=models.Input)
+
+ return execution
+
+ def _validate_no_active_executions(self):
+ active_executions = [e for e in self._service.executions if
+ e.is_active()]
+ if active_executions:
+ raise exceptions.ActiveExecutionsError(
+ "Can't start execution; Service {0} has an active execution with ID {1}"
+ .format(self._service.name, active_executions[0].id))
+
+ def _validate_workflow_exists_for_service(self):
+ if self._workflow_name not in self._service.workflows and \
+ self._workflow_name not in builtin.BUILTIN_WORKFLOWS:
+ raise exceptions.UndeclaredWorkflowError(
+ 'No workflow policy {0} declared in service {1}'
+ .format(self._workflow_name, self._service.name))
+
+ def _get_workflow_fn(self, workflow_name):
+ if workflow_name in builtin.BUILTIN_WORKFLOWS:
+ return import_fullname('{0}.{1}'.format(builtin.BUILTIN_WORKFLOWS_PATH_PREFIX,
+ workflow_name))
+
+ workflow = self._service.workflows[workflow_name]
+
+ # TODO: Custom workflow support needs improvement, currently this code uses internal
+ # knowledge of the resource storage; Instead, workflows should probably be loaded
+ # in a similar manner to operation plugins. Also consider passing to import_fullname
+ # as paths instead of appending to sys path.
+ service_template_resources_path = os.path.join(
+ self._resource.service_template.base_path,
+ str(self._service.service_template.id))
+ sys.path.append(service_template_resources_path)
+
+ try:
+ workflow_fn = import_fullname(workflow.function)
+ except ImportError:
+ raise exceptions.WorkflowImplementationNotFoundError(
+ 'Could not find workflow {0} function at {1}'.format(
+ workflow_name, workflow.function))
+
+ return workflow_fn
diff --git a/aria/orchestrator/workflow_runner.py b/aria/orchestrator/workflow_runner.py
deleted file mode 100644
index 4dbf29b..0000000
--- a/aria/orchestrator/workflow_runner.py
+++ /dev/null
@@ -1,188 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements. See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.
-# The ASF licenses this file to You 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.
-
-"""
-Running workflows.
-"""
-
-import os
-import sys
-from datetime import datetime
-
-from . import exceptions
-from .context.workflow import WorkflowContext
-from .workflows import builtin
-from .workflows.core import engine, graph_compiler
-from .workflows.executor.process import ProcessExecutor
-from ..modeling import models
-from ..modeling import utils as modeling_utils
-from ..utils.imports import import_fullname
-
-
-DEFAULT_TASK_MAX_ATTEMPTS = 30
-DEFAULT_TASK_RETRY_INTERVAL = 30
-
-
-class WorkflowRunner(object):
-
- def __init__(self, model_storage, resource_storage, plugin_manager,
- execution_id=None, retry_failed_tasks=False,
- service_id=None, workflow_name=None, inputs=None, executor=None,
- task_max_attempts=DEFAULT_TASK_MAX_ATTEMPTS,
- task_retry_interval=DEFAULT_TASK_RETRY_INTERVAL):
- """
- Manages a single workflow execution on a given service.
-
- :param workflow_name: workflow name
- :param service_id: service ID
- :param inputs: key-value dict of inputs for the execution
- :param model_storage: model storage API ("MAPI")
- :param resource_storage: resource storage API ("RAPI")
- :param plugin_manager: plugin manager
- :param executor: executor for tasks; defaults to a
- :class:`~aria.orchestrator.workflows.executor.process.ProcessExecutor` instance
- :param task_max_attempts: maximum attempts of repeating each failing task
- :param task_retry_interval: retry interval between retry attempts of a failing task
- """
-
- if not (execution_id or (workflow_name and service_id)):
- exceptions.InvalidWorkflowRunnerParams(
- "Either provide execution id in order to resume a workflow or workflow name "
- "and service id with inputs")
-
- self._is_resume = execution_id is not None
- self._retry_failed_tasks = retry_failed_tasks
-
- self._model_storage = model_storage
- self._resource_storage = resource_storage
-
- # the IDs are stored rather than the models themselves, so this module could be used
- # by several threads without raising errors on model objects shared between threads
-
- if self._is_resume:
- self._execution_id = execution_id
- self._service_id = self.execution.service.id
- self._workflow_name = model_storage.execution.get(self._execution_id).workflow_name
- else:
- self._service_id = service_id
- self._workflow_name = workflow_name
- self._validate_workflow_exists_for_service()
- self._execution_id = self._create_execution_model(inputs).id
-
- self._workflow_context = WorkflowContext(
- name=self.__class__.__name__,
- model_storage=self._model_storage,
- resource_storage=resource_storage,
- service_id=service_id,
- execution_id=self._execution_id,
- workflow_name=self._workflow_name,
- task_max_attempts=task_max_attempts,
- task_retry_interval=task_retry_interval)
-
- # Set default executor and kwargs
- executor = executor or ProcessExecutor(plugin_manager=plugin_manager)
-
- # transforming the execution inputs to dict, to pass them to the workflow function
- execution_inputs_dict = dict(inp.unwrapped for inp in self.execution.inputs.itervalues())
-
- if not self._is_resume:
- workflow_fn = self._get_workflow_fn()
- self._tasks_graph = workflow_fn(ctx=self._workflow_context, **execution_inputs_dict)
- compiler = graph_compiler.GraphCompiler(self._workflow_context, executor.__class__)
- compiler.compile(self._tasks_graph)
-
- self._engine = engine.Engine(executors={executor.__class__: executor})
-
- @property
- def execution_id(self):
- return self._execution_id
-
- @property
- def execution(self):
- return self._model_storage.execution.get(self.execution_id)
-
- @property
- def service(self):
- return self._model_storage.service.get(self._service_id)
-
- def execute(self):
- self._engine.execute(ctx=self._workflow_context,
- resuming=self._is_resume,
- retry_failed=self._retry_failed_tasks)
-
- def cancel(self):
- self._engine.cancel_execution(ctx=self._workflow_context)
-
- def _create_execution_model(self, inputs):
- execution = models.Execution(
- created_at=datetime.utcnow(),
- service=self.service,
- workflow_name=self._workflow_name,
- inputs={})
-
- if self._workflow_name in builtin.BUILTIN_WORKFLOWS:
- workflow_inputs = dict() # built-in workflows don't have any inputs
- else:
- workflow_inputs = self.service.workflows[self._workflow_name].inputs
-
- modeling_utils.validate_no_undeclared_inputs(declared_inputs=workflow_inputs,
- supplied_inputs=inputs or {})
- modeling_utils.validate_required_inputs_are_supplied(declared_inputs=workflow_inputs,
- supplied_inputs=inputs or {})
- execution.inputs = modeling_utils.merge_parameter_values(
- inputs, workflow_inputs, model_cls=models.Input)
- # TODO: these two following calls should execute atomically
- self._validate_no_active_executions(execution)
- self._model_storage.execution.put(execution)
- return execution
-
- def _validate_workflow_exists_for_service(self):
- if self._workflow_name not in self.service.workflows and \
- self._workflow_name not in builtin.BUILTIN_WORKFLOWS:
- raise exceptions.UndeclaredWorkflowError(
- 'No workflow policy {0} declared in service {1}'
- .format(self._workflow_name, self.service.name))
-
- def _validate_no_active_executions(self, execution):
- active_executions = [e for e in self.service.executions if e.is_active()]
- if active_executions:
- raise exceptions.ActiveExecutionsError(
- "Can't start execution; Service {0} has an active execution with ID {1}"
- .format(self.service.name, active_executions[0].id))
-
- def _get_workflow_fn(self):
- if self._workflow_name in builtin.BUILTIN_WORKFLOWS:
- return import_fullname('{0}.{1}'.format(builtin.BUILTIN_WORKFLOWS_PATH_PREFIX,
- self._workflow_name))
-
- workflow = self.service.workflows[self._workflow_name]
-
- # TODO: Custom workflow support needs improvement, currently this code uses internal
- # knowledge of the resource storage; Instead, workflows should probably be loaded
- # in a similar manner to operation plugins. Also consider passing to import_fullname
- # as paths instead of appending to sys path.
- service_template_resources_path = os.path.join(
- self._resource_storage.service_template.base_path,
- str(self.service.service_template.id))
- sys.path.append(service_template_resources_path)
-
- try:
- workflow_fn = import_fullname(workflow.function)
- except ImportError:
- raise exceptions.WorkflowImplementationNotFoundError(
- 'Could not find workflow {0} function at {1}'.format(
- self._workflow_name, workflow.function))
-
- return workflow_fn
diff --git a/aria/orchestrator/workflows/core/engine.py b/aria/orchestrator/workflows/core/engine.py
index 0ec3cd8..0d7d2ae 100644
--- a/aria/orchestrator/workflows/core/engine.py
+++ b/aria/orchestrator/workflows/core/engine.py
@@ -36,9 +36,9 @@
Executes workflows.
"""
- def __init__(self, executors, **kwargs):
+ def __init__(self, *executors, **kwargs):
super(Engine, self).__init__(**kwargs)
- self._executors = executors.copy()
+ self._executors = dict((e.__class__, e) for e in executors)
self._executors.setdefault(StubTaskExecutor, StubTaskExecutor())
def execute(self, ctx, resuming=False, retry_failed=False):
diff --git a/docs/aria.orchestrator.rst b/docs/aria.orchestrator.rst
index 33454e6..6e6d659 100644
--- a/docs/aria.orchestrator.rst
+++ b/docs/aria.orchestrator.rst
@@ -40,7 +40,7 @@
.. automodule:: aria.orchestrator.plugin
-:mod:`aria.orchestrator.workflow_runner`
-----------------------------------------
+:mod:`aria.orchestrator.execution_preparer`
+-------------------------------------------
-.. automodule:: aria.orchestrator.workflow_runner
+.. automodule:: aria.orchestrator.execution_preparer
diff --git a/test_ssh.py b/test_ssh.py
deleted file mode 100644
index 5256cf8..0000000
--- a/test_ssh.py
+++ /dev/null
@@ -1,528 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements. See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.
-# The ASF licenses this file to You 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.
-
-import contextlib
-import json
-import logging
-import os
-
-import pytest
-
-import fabric.api
-from fabric.contrib import files
-from fabric import context_managers
-
-from aria.modeling import models
-from aria.orchestrator import events
-from aria.orchestrator import workflow
-from aria.orchestrator.workflows import api
-from aria.orchestrator.workflows.executor import process
-from aria.orchestrator.workflows.core import engine, graph_compiler
-from aria.orchestrator.workflows.exceptions import ExecutorException
-from aria.orchestrator.exceptions import TaskAbortException, TaskRetryException
-from aria.orchestrator.execution_plugin import operations
-from aria.orchestrator.execution_plugin import constants
-from aria.orchestrator.execution_plugin.exceptions import ProcessException, TaskException
-from aria.orchestrator.execution_plugin.ssh import operations as ssh_operations
-
-from tests import mock, storage, resources
-from tests.orchestrator.workflows.helpers import events_collector
-
-_CUSTOM_BASE_DIR = '/tmp/new-aria-ctx'
-
-import tests
-KEY_FILENAME = os.path.join(tests.ROOT_DIR, 'tests/resources/keys/test')
-
-_FABRIC_ENV = {
- 'disable_known_hosts': True,
- 'user': 'test',
- 'key_filename': KEY_FILENAME
-}
-
-
-import mockssh
-@pytest.fixture(scope='session')
-def server():
- with mockssh.Server({'test': KEY_FILENAME}) as s:
- yield s
-
-
-#@pytest.mark.skipif(not os.environ.get('TRAVIS'), reason='actual ssh server required')
-class TestWithActualSSHServer(object):
-
- def test_run_script_basic(self):
- expected_attribute_value = 'some_value'
- props = self._execute(env={'test_value': expected_attribute_value})
- assert props['test_value'].value == expected_attribute_value
-
- @pytest.mark.skip(reason='sudo privileges are required')
- def test_run_script_as_sudo(self):
- self._execute(use_sudo=True)
- with self._ssh_env():
- assert files.exists('/opt/test_dir')
- fabric.api.sudo('rm -rf /opt/test_dir')
-
- def test_run_script_default_base_dir(self):
- props = self._execute()
- assert props['work_dir'].value == '{0}/work'.format(constants.DEFAULT_BASE_DIR)
-
- @pytest.mark.skip(reason='Re-enable once output from process executor can be captured')
- @pytest.mark.parametrize('hide_groups', [[], ['everything']])
- def test_run_script_with_hide(self, hide_groups):
- self._execute(hide_output=hide_groups)
- output = 'TODO'
- expected_log_message = ('[localhost] run: source {0}/scripts/'
- .format(constants.DEFAULT_BASE_DIR))
- if hide_groups:
- assert expected_log_message not in output
- else:
- assert expected_log_message in output
-
- def test_run_script_process_config(self):
- expected_env_value = 'test_value_env'
- expected_arg1_value = 'test_value_arg1'
- expected_arg2_value = 'test_value_arg2'
- expected_cwd = '/tmp'
- expected_base_dir = _CUSTOM_BASE_DIR
- props = self._execute(
- env={'test_value_env': expected_env_value},
- process={
- 'args': [expected_arg1_value, expected_arg2_value],
- 'cwd': expected_cwd,
- 'base_dir': expected_base_dir
- })
- assert props['env_value'].value == expected_env_value
- assert len(props['bash_version'].value) > 0
- assert props['arg1_value'].value == expected_arg1_value
- assert props['arg2_value'].value == expected_arg2_value
- assert props['cwd'].value == expected_cwd
- assert props['ctx_path'].value == '{0}/ctx'.format(expected_base_dir)
-
- def test_run_script_command_prefix(self):
- props = self._execute(process={'command_prefix': 'bash -i'})
- assert 'i' in props['dollar_dash'].value
-
- def test_run_script_reuse_existing_ctx(self):
- expected_test_value_1 = 'test_value_1'
- expected_test_value_2 = 'test_value_2'
- props = self._execute(
- test_operations=['{0}_1'.format(self.test_name),
- '{0}_2'.format(self.test_name)],
- env={'test_value1': expected_test_value_1,
- 'test_value2': expected_test_value_2})
- assert props['test_value1'].value == expected_test_value_1
- assert props['test_value2'].value == expected_test_value_2
-
- def test_run_script_download_resource_plain(self, tmpdir):
- resource = tmpdir.join('resource')
- resource.write('content')
- self._upload(str(resource), 'test_resource')
- props = self._execute()
- assert props['test_value'].value == 'content'
-
- def test_run_script_download_resource_and_render(self, tmpdir):
- resource = tmpdir.join('resource')
- resource.write('{{ctx.service.name}}')
- self._upload(str(resource), 'test_resource')
- props = self._execute()
- assert props['test_value'].value == self._workflow_context.service.name
-
- @pytest.mark.parametrize('value', ['string-value', [1, 2, 3], {'key': 'value'}])
- def test_run_script_inputs_as_env_variables_no_override(self, value):
- props = self._execute(custom_input=value)
- return_value = props['test_value'].value
- expected = return_value if isinstance(value, basestring) else json.loads(return_value)
- assert value == expected
-
- @pytest.mark.parametrize('value', ['string-value', [1, 2, 3], {'key': 'value'}])
- def test_run_script_inputs_as_env_variables_process_env_override(self, value):
- props = self._execute(custom_input='custom-input-value',
- env={'custom_env_var': value})
- return_value = props['test_value'].value
- expected = return_value if isinstance(value, basestring) else json.loads(return_value)
- assert value == expected
-
- def test_run_script_error_in_script(self):
- exception = self._execute_and_get_task_exception()
- assert isinstance(exception, TaskException)
-
- def test_run_script_abort_immediate(self):
- exception = self._execute_and_get_task_exception()
- assert isinstance(exception, TaskAbortException)
- assert exception.message == 'abort-message'
-
- def test_run_script_retry(self):
- exception = self._execute_and_get_task_exception()
- assert isinstance(exception, TaskRetryException)
- assert exception.message == 'retry-message'
-
- def test_run_script_abort_error_ignored_by_script(self):
- exception = self._execute_and_get_task_exception()
- assert isinstance(exception, TaskAbortException)
- assert exception.message == 'abort-message'
-
- def test_run_commands(self):
- temp_file_path = '/tmp/very_temporary_file'
- with self._ssh_env():
- if files.exists(temp_file_path):
- fabric.api.run('rm {0}'.format(temp_file_path))
- self._execute(commands=['touch {0}'.format(temp_file_path)])
- with self._ssh_env():
- assert files.exists(temp_file_path)
- fabric.api.run('rm {0}'.format(temp_file_path))
-
- @pytest.fixture(autouse=True)
- def _setup(self, request, workflow_context, executor, capfd, server):
- print 'HI!!!!!!!!!!', server.port
- self._workflow_context = workflow_context
- self._executor = executor
- self._capfd = capfd
- self.test_name = request.node.originalname or request.node.name
- with self._ssh_env(server):
- for directory in [constants.DEFAULT_BASE_DIR, _CUSTOM_BASE_DIR]:
- if files.exists(directory):
- fabric.api.run('rm -rf {0}'.format(directory))
-
- @contextlib.contextmanager
- def _ssh_env(self, server):
- with self._capfd.disabled():
- with context_managers.settings(fabric.api.hide('everything'),
- host_string='localhost:{0}'.format(server.port),
- **_FABRIC_ENV):
- yield
-
- def _execute(self,
- env=None,
- use_sudo=False,
- hide_output=None,
- process=None,
- custom_input='',
- test_operations=None,
- commands=None):
- process = process or {}
- if env:
- process.setdefault('env', {}).update(env)
-
- test_operations = test_operations or [self.test_name]
-
- local_script_path = os.path.join(resources.DIR, 'scripts', 'test_ssh.sh')
- script_path = os.path.basename(local_script_path)
- self._upload(local_script_path, script_path)
-
- if commands:
- operation = operations.run_commands_with_ssh
- else:
- operation = operations.run_script_with_ssh
-
- node = self._workflow_context.model.node.get_by_name(mock.models.DEPENDENCY_NODE_NAME)
- arguments = {
- 'script_path': script_path,
- 'fabric_env': _FABRIC_ENV,
- 'process': process,
- 'use_sudo': use_sudo,
- 'custom_env_var': custom_input,
- 'test_operation': '',
- }
- if hide_output:
- arguments['hide_output'] = hide_output
- if commands:
- arguments['commands'] = commands
- interface = mock.models.create_interface(
- node.service,
- 'test',
- 'op',
- operation_kwargs=dict(
- function='{0}.{1}'.format(
- operations.__name__,
- operation.__name__),
- arguments=arguments)
- )
- node.interfaces[interface.name] = interface
-
- @workflow
- def mock_workflow(ctx, graph):
- ops = []
- for test_operation in test_operations:
- op_arguments = arguments.copy()
- op_arguments['test_operation'] = test_operation
- ops.append(api.task.OperationTask(
- node,
- interface_name='test',
- operation_name='op',
- arguments=op_arguments))
-
- graph.sequence(*ops)
- return graph
- tasks_graph = mock_workflow(ctx=self._workflow_context) # pylint: disable=no-value-for-parameter
- graph_compiler.GraphCompiler(
- self._workflow_context, self._executor.__class__).compile(tasks_graph)
- eng = engine.Engine({self._executor.__class__: self._executor})
- eng.execute(self._workflow_context)
- return self._workflow_context.model.node.get_by_name(
- mock.models.DEPENDENCY_NODE_NAME).attributes
-
- def _execute_and_get_task_exception(self, *args, **kwargs):
- signal = events.on_failure_task_signal
- with events_collector(signal) as collected:
- with pytest.raises(ExecutorException):
- self._execute(*args, **kwargs)
- return collected[signal][0]['kwargs']['exception']
-
- def _upload(self, source, path):
- self._workflow_context.resource.service.upload(
- entry_id=str(self._workflow_context.service.id),
- source=source,
- path=path)
-
- @pytest.fixture
- def executor(self):
- result = process.ProcessExecutor()
- try:
- yield result
- finally:
- result.close()
-
- @pytest.fixture
- def workflow_context(self, tmpdir):
- workflow_context = mock.context.simple(str(tmpdir))
- workflow_context.states = []
- workflow_context.exception = None
- yield workflow_context
- storage.release_sqlite_storage(workflow_context.model)
-
-
-class TestFabricEnvHideGroupsAndRunCommands(object):
-
- def test_fabric_env_default_override(self):
- # first sanity for no override
- self._run()
- assert self.mock.settings_merged['timeout'] == constants.FABRIC_ENV_DEFAULTS['timeout']
- # now override
- invocation_fabric_env = self.default_fabric_env.copy()
- timeout = 1000000
- invocation_fabric_env['timeout'] = timeout
- self._run(fabric_env=invocation_fabric_env)
- assert self.mock.settings_merged['timeout'] == timeout
-
- def test_implicit_host_string(self, mocker):
- expected_host_address = '1.1.1.1'
- mocker.patch.object(self._Ctx.task.actor, 'host')
- mocker.patch.object(self._Ctx.task.actor.host, 'host_address', expected_host_address)
- fabric_env = self.default_fabric_env.copy()
- del fabric_env['host_string']
- self._run(fabric_env=fabric_env)
- assert self.mock.settings_merged['host_string'] == expected_host_address
-
- def test_explicit_host_string(self):
- fabric_env = self.default_fabric_env.copy()
- host_string = 'explicit_host_string'
- fabric_env['host_string'] = host_string
- self._run(fabric_env=fabric_env)
- assert self.mock.settings_merged['host_string'] == host_string
-
- def test_override_warn_only(self):
- fabric_env = self.default_fabric_env.copy()
- self._run(fabric_env=fabric_env)
- assert self.mock.settings_merged['warn_only'] is True
- fabric_env = self.default_fabric_env.copy()
- fabric_env['warn_only'] = False
- self._run(fabric_env=fabric_env)
- assert self.mock.settings_merged['warn_only'] is False
-
- def test_missing_host_string(self):
- with pytest.raises(TaskAbortException) as exc_ctx:
- fabric_env = self.default_fabric_env.copy()
- del fabric_env['host_string']
- self._run(fabric_env=fabric_env)
- assert '`host_string` not supplied' in str(exc_ctx.value)
-
- def test_missing_user(self):
- with pytest.raises(TaskAbortException) as exc_ctx:
- fabric_env = self.default_fabric_env.copy()
- del fabric_env['user']
- self._run(fabric_env=fabric_env)
- assert '`user` not supplied' in str(exc_ctx.value)
-
- def test_missing_key_or_password(self):
- with pytest.raises(TaskAbortException) as exc_ctx:
- fabric_env = self.default_fabric_env.copy()
- del fabric_env['key_filename']
- self._run(fabric_env=fabric_env)
- assert 'Access credentials not supplied' in str(exc_ctx.value)
-
- def test_hide_in_settings_and_non_viable_groups(self):
- groups = ('running', 'stdout')
- self._run(hide_output=groups)
- assert set(self.mock.settings_merged['hide_output']) == set(groups)
- with pytest.raises(TaskAbortException) as exc_ctx:
- self._run(hide_output=('running', 'bla'))
- assert '`hide_output` must be a subset of' in str(exc_ctx.value)
-
- def test_run_commands(self):
- def test(use_sudo):
- commands = ['command1', 'command2']
- self._run(
- commands=commands,
- use_sudo=use_sudo)
- assert all(item in self.mock.settings_merged.items() for
- item in self.default_fabric_env.items())
- assert self.mock.settings_merged['warn_only'] is True
- assert self.mock.settings_merged['use_sudo'] == use_sudo
- assert self.mock.commands == commands
- self.mock.settings_merged = {}
- self.mock.commands = []
- test(use_sudo=False)
- test(use_sudo=True)
-
- def test_failed_command(self):
- with pytest.raises(ProcessException) as exc_ctx:
- self._run(commands=['fail'])
- exception = exc_ctx.value
- assert exception.stdout == self.MockCommandResult.stdout
- assert exception.stderr == self.MockCommandResult.stderr
- assert exception.command == self.MockCommandResult.command
- assert exception.exit_code == self.MockCommandResult.return_code
-
- class MockCommandResult(object):
- stdout = 'mock_stdout'
- stderr = 'mock_stderr'
- command = 'mock_command'
- return_code = 1
-
- def __init__(self, failed):
- self.failed = failed
-
- class MockFabricApi(object):
-
- def __init__(self):
- self.commands = []
- self.settings_merged = {}
-
- @contextlib.contextmanager
- def settings(self, *args, **kwargs):
- self.settings_merged.update(kwargs)
- if args:
- groups = args[0]
- self.settings_merged.update({'hide_output': groups})
- yield
-
- def run(self, command):
- self.commands.append(command)
- self.settings_merged['use_sudo'] = False
- return TestFabricEnvHideGroupsAndRunCommands.MockCommandResult(command == 'fail')
-
- def sudo(self, command):
- self.commands.append(command)
- self.settings_merged['use_sudo'] = True
- return TestFabricEnvHideGroupsAndRunCommands.MockCommandResult(command == 'fail')
-
- def hide(self, *groups):
- return groups
-
- def exists(self, *args, **kwargs):
- raise RuntimeError
-
- class _Ctx(object):
- INSTRUMENTATION_FIELDS = ()
-
- class Task(object):
- @staticmethod
- def abort(message=None):
- models.Task.abort(message)
- actor = None
-
- class Actor(object):
- host = None
-
- class Model(object):
- @contextlib.contextmanager
- def instrument(self, *args, **kwargs):
- yield
- task = Task
- task.actor = Actor
- model = Model()
- logger = logging.getLogger()
-
- @staticmethod
- @contextlib.contextmanager
- def _mock_self_logging(*args, **kwargs):
- yield
- _Ctx.logging_handlers = _mock_self_logging
-
- @pytest.fixture(autouse=True)
- def _setup(self, mocker):
- self.default_fabric_env = {
- 'host_string': 'test',
- 'user': 'test',
- 'key_filename': 'test',
- }
- self.mock = self.MockFabricApi()
- mocker.patch('fabric.api', self.mock)
-
- def _run(self,
- commands=(),
- fabric_env=None,
- process=None,
- use_sudo=False,
- hide_output=None):
- operations.run_commands_with_ssh(
- ctx=self._Ctx,
- commands=commands,
- process=process,
- fabric_env=fabric_env or self.default_fabric_env,
- use_sudo=use_sudo,
- hide_output=hide_output)
-
-
-class TestUtilityFunctions(object):
-
- def test_paths(self):
- base_dir = '/path'
- local_script_path = '/local/script/path.py'
- paths = ssh_operations._Paths(base_dir=base_dir,
- local_script_path=local_script_path)
- assert paths.local_script_path == local_script_path
- assert paths.remote_ctx_dir == base_dir
- assert paths.base_script_path == 'path.py'
- assert paths.remote_ctx_path == '/path/ctx'
- assert paths.remote_scripts_dir == '/path/scripts'
- assert paths.remote_work_dir == '/path/work'
- assert paths.remote_env_script_path.startswith('/path/scripts/env-path.py-')
- assert paths.remote_script_path.startswith('/path/scripts/path.py-')
-
- def test_write_environment_script_file(self):
- base_dir = '/path'
- local_script_path = '/local/script/path.py'
- paths = ssh_operations._Paths(base_dir=base_dir,
- local_script_path=local_script_path)
- env = {'one': "'1'"}
- local_socket_url = 'local_socket_url'
- remote_socket_url = 'remote_socket_url'
- env_script_lines = set([l for l in ssh_operations._write_environment_script_file(
- process={'env': env},
- paths=paths,
- local_socket_url=local_socket_url,
- remote_socket_url=remote_socket_url
- ).getvalue().split('\n') if l])
- expected_env_script_lines = set([
- 'export PATH=/path:$PATH',
- 'export PYTHONPATH=/path:$PYTHONPATH',
- 'chmod +x /path/ctx',
- 'chmod +x {0}'.format(paths.remote_script_path),
- 'export CTX_SOCKET_URL={0}'.format(remote_socket_url),
- 'export LOCAL_CTX_SOCKET_URL={0}'.format(local_socket_url),
- 'export one=\'1\''
- ])
- assert env_script_lines == expected_env_script_lines
diff --git a/tests/end2end/testenv.py b/tests/end2end/testenv.py
index 43ec274..c3d055d 100644
--- a/tests/end2end/testenv.py
+++ b/tests/end2end/testenv.py
@@ -68,6 +68,7 @@
assert len(self.model_storage.node_template.list()) == 0
assert len(self.model_storage.node.list()) == 0
assert len(self.model_storage.log.list()) == 0
+ assert len(self.model_storage.task.list()) == 0
def _get_cli(self):
cli = sh.aria.bake('-vvv', _out=sys.stdout, _err=sys.stderr)
diff --git a/tests/orchestrator/context/__init__.py b/tests/orchestrator/context/__init__.py
index 780db07..257cbf7 100644
--- a/tests/orchestrator/context/__init__.py
+++ b/tests/orchestrator/context/__init__.py
@@ -27,6 +27,6 @@
graph = workflow_func(ctx=workflow_context)
graph_compiler.GraphCompiler(workflow_context, executor.__class__).compile(graph)
- eng = engine.Engine(executors={executor.__class__: executor})
+ eng = engine.Engine(executor)
eng.execute(workflow_context)
diff --git a/tests/orchestrator/context/test_serialize.py b/tests/orchestrator/context/test_serialize.py
index 091e23c..6e9c950 100644
--- a/tests/orchestrator/context/test_serialize.py
+++ b/tests/orchestrator/context/test_serialize.py
@@ -49,7 +49,7 @@
graph = _mock_workflow(ctx=context) # pylint: disable=no-value-for-parameter
graph_compiler.GraphCompiler(context, executor.__class__).compile(graph)
- eng = engine.Engine({executor.__class__: executor})
+ eng = engine.Engine(executor)
eng.execute(context)
diff --git a/tests/orchestrator/execution/__init__.py b/tests/orchestrator/execution/__init__.py
new file mode 100644
index 0000000..ae1e83e
--- /dev/null
+++ b/tests/orchestrator/execution/__init__.py
@@ -0,0 +1,14 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
diff --git a/tests/orchestrator/test_workflow_runner.py b/tests/orchestrator/execution/test_execution_compiler.py
similarity index 62%
rename from tests/orchestrator/test_workflow_runner.py
rename to tests/orchestrator/execution/test_execution_compiler.py
index 011c4cc..42e5272 100644
--- a/tests/orchestrator/test_workflow_runner.py
+++ b/tests/orchestrator/execution/test_execution_compiler.py
@@ -18,15 +18,13 @@
from threading import Thread, Event
from datetime import datetime
-import mock
import pytest
from aria.modeling import exceptions as modeling_exceptions
from aria.modeling import models
from aria.orchestrator import exceptions
from aria.orchestrator import events
-from aria.orchestrator.workflow_runner import WorkflowRunner
-from aria.orchestrator.workflows.executor.process import ProcessExecutor
+from aria.orchestrator import execution_preparer
from aria.orchestrator.workflows import api
from aria.orchestrator.workflows.core import engine, graph_compiler
from aria.orchestrator.workflows.executor import thread
@@ -40,7 +38,7 @@
storage
)
-from ..fixtures import ( # pylint: disable=unused-import
+from ...fixtures import ( # pylint: disable=unused-import
plugins_dir,
plugin_manager,
fs_model as model,
@@ -66,7 +64,7 @@
def test_undeclared_workflow(request):
# validating a proper error is raised when the workflow is not declared in the service
with pytest.raises(exceptions.UndeclaredWorkflowError):
- _create_workflow_runner(request, 'undeclared_workflow')
+ _get_preparer(request, 'undeclared_workflow').prepare()
def test_missing_workflow_implementation(service, request):
@@ -78,24 +76,23 @@
service.workflows['test_workflow'] = workflow
with pytest.raises(exceptions.WorkflowImplementationNotFoundError):
- _create_workflow_runner(request, 'test_workflow')
+ _get_preparer(request, 'test_workflow').prepare()
def test_builtin_workflow_instantiation(request):
# validates the workflow runner instantiates properly when provided with a builtin workflow
# (expecting no errors to be raised on undeclared workflow or missing workflow implementation)
- workflow_runner = _create_workflow_runner(request, 'install')
- tasks = list(workflow_runner.execution.tasks)
- assert len(tasks) == 18 # expecting 18 tasks for 2 node topology
+ workflow_ctx = _get_preparer(request, 'install').prepare()
+ assert len(workflow_ctx.execution.tasks) == 18 # expecting 18 tasks for 2 node topology
def test_custom_workflow_instantiation(request):
# validates the workflow runner instantiates properly when provided with a custom workflow
# (expecting no errors to be raised on undeclared workflow or missing workflow implementation)
mock_workflow = _setup_mock_workflow_in_service(request)
- workflow_runner = _create_workflow_runner(request, mock_workflow)
- tasks = list(workflow_runner.execution.tasks)
- assert len(tasks) == 2 # mock workflow creates only start workflow and end workflow task
+ workflow_ctx = _get_preparer(request, mock_workflow).prepare()
+ assert len(workflow_ctx.execution.tasks) == 2 # mock workflow creates only start workflow
+ # and end workflow task
def test_existing_active_executions(request, service, model):
@@ -105,7 +102,7 @@
workflow_name='uninstall')
model.execution.put(existing_active_execution)
with pytest.raises(exceptions.ActiveExecutionsError):
- _create_workflow_runner(request, 'install')
+ _get_preparer(request, 'install').prepare()
def test_existing_executions_but_no_active_ones(request, service, model):
@@ -115,82 +112,18 @@
workflow_name='uninstall')
model.execution.put(existing_terminated_execution)
# no active executions exist, so no error should be raised
- _create_workflow_runner(request, 'install')
+ _get_preparer(request, 'install').prepare()
-def test_default_executor(request):
- # validates the ProcessExecutor is used by the workflow runner by default
+def test_execution_model_creation(request, service):
mock_workflow = _setup_mock_workflow_in_service(request)
- with mock.patch('aria.orchestrator.workflow_runner.engine.Engine') as mock_engine_cls:
- _create_workflow_runner(request, mock_workflow)
- _, engine_kwargs = mock_engine_cls.call_args
- assert isinstance(engine_kwargs.get('executors').values()[0], ProcessExecutor)
+ workflow_ctx = _get_preparer(request, mock_workflow).prepare()
-
-def test_custom_executor(request):
- mock_workflow = _setup_mock_workflow_in_service(request)
-
- custom_executor = mock.MagicMock()
- with mock.patch('aria.orchestrator.workflow_runner.engine.Engine') as mock_engine_cls:
- _create_workflow_runner(request, mock_workflow, executor=custom_executor)
- _, engine_kwargs = mock_engine_cls.call_args
- assert engine_kwargs.get('executors').values()[0] == custom_executor
-
-
-def test_task_configuration_parameters(request):
- mock_workflow = _setup_mock_workflow_in_service(request)
-
- task_max_attempts = 5
- task_retry_interval = 7
- with mock.patch('aria.orchestrator.workflow_runner.engine.Engine.execute') as \
- mock_engine_execute:
- _create_workflow_runner(request, mock_workflow, task_max_attempts=task_max_attempts,
- task_retry_interval=task_retry_interval).execute()
- _, engine_kwargs = mock_engine_execute.call_args
- assert engine_kwargs['ctx']._task_max_attempts == task_max_attempts
- assert engine_kwargs['ctx']._task_retry_interval == task_retry_interval
-
-
-def test_execute(request, service):
- mock_workflow = _setup_mock_workflow_in_service(request)
-
- mock_engine = mock.MagicMock()
- with mock.patch('aria.orchestrator.workflow_runner.engine.Engine.execute',
- return_value=mock_engine) as mock_engine_execute:
- workflow_runner = _create_workflow_runner(request, mock_workflow)
- workflow_runner.execute()
-
- _, engine_kwargs = mock_engine_execute.call_args
- assert engine_kwargs['ctx'].service.id == service.id
- assert engine_kwargs['ctx'].execution.workflow_name == 'test_workflow'
-
- mock_engine_execute.assert_called_once_with(ctx=workflow_runner._workflow_context,
- resuming=False,
- retry_failed=False)
-
-
-def test_cancel_execution(request):
- mock_workflow = _setup_mock_workflow_in_service(request)
-
- mock_engine = mock.MagicMock()
- with mock.patch('aria.orchestrator.workflow_runner.engine.Engine', return_value=mock_engine):
- workflow_runner = _create_workflow_runner(request, mock_workflow)
- workflow_runner.cancel()
- mock_engine.cancel_execution.assert_called_once_with(ctx=workflow_runner._workflow_context)
-
-
-def test_execution_model_creation(request, service, model):
- mock_workflow = _setup_mock_workflow_in_service(request)
-
- with mock.patch('aria.orchestrator.workflow_runner.engine.Engine'):
- workflow_runner = _create_workflow_runner(request, mock_workflow)
-
- assert model.execution.get(workflow_runner.execution.id) == workflow_runner.execution
- assert workflow_runner.execution.service.id == service.id
- assert workflow_runner.execution.workflow_name == mock_workflow
- assert workflow_runner.execution.created_at <= datetime.utcnow()
- assert workflow_runner.execution.inputs == dict()
+ assert workflow_ctx.execution.service.id == service.id
+ assert workflow_ctx.execution.workflow_name == mock_workflow
+ assert workflow_ctx.execution.created_at <= datetime.utcnow()
+ assert workflow_ctx.execution.inputs == dict()
def test_execution_inputs_override_workflow_inputs(request):
@@ -200,24 +133,25 @@
inputs=dict((name, models.Input.wrap(name, val)) for name, val
in wf_inputs.iteritems()))
- with mock.patch('aria.orchestrator.workflow_runner.engine.Engine'):
- workflow_runner = _create_workflow_runner(
- request, mock_workflow, inputs={'input2': 'overriding-value2', 'input3': 7})
+ workflow_ctx = _get_preparer(request, mock_workflow).prepare(
+ execution_inputs={'input2': 'overriding-value2', 'input3': 7}
+ )
- assert len(workflow_runner.execution.inputs) == 3
- # did not override input1 - expecting the default value from the workflow inputs
- assert workflow_runner.execution.inputs['input1'].value == 'value1'
- # overrode input2
- assert workflow_runner.execution.inputs['input2'].value == 'overriding-value2'
- # overrode input of integer type
- assert workflow_runner.execution.inputs['input3'].value == 7
+ assert len(workflow_ctx.execution.inputs) == 3
+ # did not override input1 - expecting the default value from the workflow inputs
+ assert workflow_ctx.execution.inputs['input1'].value == 'value1'
+ # overrode input2
+ assert workflow_ctx.execution.inputs['input2'].value == 'overriding-value2'
+ # overrode input of integer type
+ assert workflow_ctx.execution.inputs['input3'].value == 7
def test_execution_inputs_undeclared_inputs(request):
mock_workflow = _setup_mock_workflow_in_service(request)
with pytest.raises(modeling_exceptions.UndeclaredInputsException):
- _create_workflow_runner(request, mock_workflow, inputs={'undeclared_input': 'value'})
+ _get_preparer(request, mock_workflow).prepare(
+ execution_inputs={'undeclared_input': 'value'})
def test_execution_inputs_missing_required_inputs(request):
@@ -225,7 +159,7 @@
request, inputs={'required_input': models.Input.wrap('required_input', value=None)})
with pytest.raises(modeling_exceptions.MissingRequiredInputsException):
- _create_workflow_runner(request, mock_workflow, inputs={})
+ _get_preparer(request, mock_workflow).prepare(execution_inputs={})
def test_execution_inputs_wrong_type_inputs(request):
@@ -233,13 +167,13 @@
request, inputs={'input': models.Input.wrap('input', 'value')})
with pytest.raises(modeling_exceptions.ParametersOfWrongTypeException):
- _create_workflow_runner(request, mock_workflow, inputs={'input': 5})
+ _get_preparer(request, mock_workflow).prepare(execution_inputs={'input': 5})
def test_execution_inputs_builtin_workflow_with_inputs(request):
# built-in workflows don't have inputs
with pytest.raises(modeling_exceptions.UndeclaredInputsException):
- _create_workflow_runner(request, 'install', inputs={'undeclared_input': 'value'})
+ _get_preparer(request, 'install').prepare(execution_inputs={'undeclared_input': 'value'})
def test_workflow_function_parameters(request, tmpdir):
@@ -254,8 +188,8 @@
request, inputs=dict((name, models.Input.wrap(name, val)) for name, val
in wf_inputs.iteritems()))
- _create_workflow_runner(request, mock_workflow,
- inputs={'input2': 'overriding-value2', 'input3': 7})
+ _get_preparer(request, mock_workflow).prepare(
+ execution_inputs={'input2': 'overriding-value2', 'input3': 7})
with open(output_path) as f:
wf_call_kwargs = json.load(f)
@@ -296,39 +230,33 @@
return mock_workflow_name
-def _create_workflow_runner(request, workflow_name, inputs=None, executor=None,
- task_max_attempts=None, task_retry_interval=None):
+def _get_preparer(request, workflow_name):
# helper method for instantiating a workflow runner
- service_id = request.getfixturevalue('service').id
+ service = request.getfixturevalue('service')
model = request.getfixturevalue('model')
resource = request.getfixturevalue('resource')
plugin_manager = request.getfixturevalue('plugin_manager')
- # task configuration parameters can't be set to None, therefore only
- # passing those if they've been set by the test
- task_configuration_kwargs = dict()
- if task_max_attempts is not None:
- task_configuration_kwargs['task_max_attempts'] = task_max_attempts
- if task_retry_interval is not None:
- task_configuration_kwargs['task_retry_interval'] = task_retry_interval
-
- return WorkflowRunner(
- workflow_name=workflow_name,
- service_id=service_id,
- inputs=inputs or {},
- executor=executor,
- model_storage=model,
- resource_storage=resource,
- plugin_manager=plugin_manager,
- **task_configuration_kwargs)
+ return execution_preparer.ExecutionPreparer(
+ model,
+ resource,
+ plugin_manager,
+ service,
+ workflow_name
+ )
class TestResumableWorkflows(object):
- def _create_initial_workflow_runner(
- self, workflow_context, workflow, executor, inputs=None):
+ def _prepare_execution_and_get_workflow_ctx(
+ self,
+ model,
+ resource,
+ service,
+ workflow,
+ executor,
+ inputs=None):
- service = workflow_context.service
service.workflows['custom_workflow'] = tests_mock.models.create_operation(
'custom_workflow',
operation_kwargs={
@@ -336,23 +264,20 @@
'inputs': dict((k, models.Input.wrap(k, v)) for k, v in (inputs or {}).items())
}
)
- workflow_context.model.service.update(service)
+ model.service.update(service)
+ compiler = execution_preparer.ExecutionPreparer(
+ model, resource, None, service, 'custom_workflow'
+ )
+ ctx = compiler.prepare(inputs, executor)
+ model.execution.update(ctx.execution)
- wf_runner = WorkflowRunner(
- service_id=workflow_context.service.id,
- inputs=inputs or {},
- model_storage=workflow_context.model,
- resource_storage=workflow_context.resource,
- plugin_manager=None,
- workflow_name='custom_workflow',
- executor=executor)
- return wf_runner
+ return ctx
@staticmethod
- def _wait_for_active_and_cancel(workflow_runner):
+ def _cancel_active_execution(eng, ctx):
if custom_events['is_active'].wait(60) is False:
raise TimeoutError("is_active wasn't set to True")
- workflow_runner.cancel()
+ eng.cancel_execution(ctx)
if custom_events['execution_cancelled'].wait(60) is False:
raise TimeoutError("Execution did not end")
@@ -360,78 +285,73 @@
node = workflow_context.model.node.get_by_name(tests_mock.models.DEPENDENCY_NODE_NAME)
node.attributes['invocations'] = models.Attribute.wrap('invocations', 0)
self._create_interface(workflow_context, node, mock_pass_first_task_only)
+ ctx = self._prepare_execution_and_get_workflow_ctx(
+ workflow_context.model,
+ workflow_context.resource,
+ workflow_context.model.service.list()[0],
+ mock_parallel_tasks_workflow,
+ thread_executor,
+ inputs={'number_of_tasks': 2}
+ )
- wf_runner = self._create_initial_workflow_runner(
- workflow_context, mock_parallel_tasks_workflow, thread_executor,
- inputs={'number_of_tasks': 2})
+ eng = engine.Engine(thread_executor)
- wf_thread = Thread(target=wf_runner.execute)
+ wf_thread = Thread(target=eng.execute, kwargs=dict(ctx=ctx))
wf_thread.daemon = True
wf_thread.start()
# Wait for the execution to start
- self._wait_for_active_and_cancel(wf_runner)
- node = workflow_context.model.node.refresh(node)
+ self._cancel_active_execution(eng, ctx)
+ node = ctx.model.node.refresh(node)
- tasks = workflow_context.model.task.list(filters={'_stub_type': None})
+ tasks = ctx.model.task.list(filters={'_stub_type': None})
assert any(task.status == task.SUCCESS for task in tasks)
assert any(task.status == task.RETRYING for task in tasks)
custom_events['is_resumed'].set()
assert any(task.status == task.RETRYING for task in tasks)
- # Create a new workflow runner, with an existing execution id. This would cause
+ # Create a new workflow engine, with an existing execution id. This would cause
# the old execution to restart.
- new_wf_runner = WorkflowRunner(
- service_id=wf_runner.service.id,
- inputs={},
- model_storage=workflow_context.model,
- resource_storage=workflow_context.resource,
- plugin_manager=None,
- execution_id=wf_runner.execution.id,
- executor=thread_executor)
-
- new_wf_runner.execute()
+ new_engine = engine.Engine(thread_executor)
+ new_engine.execute(ctx, resuming=True)
# Wait for it to finish and assert changes.
node = workflow_context.model.node.refresh(node)
assert all(task.status == task.SUCCESS for task in tasks)
assert node.attributes['invocations'].value == 3
- assert wf_runner.execution.status == wf_runner.execution.SUCCEEDED
+ assert ctx.execution.status == ctx.execution.SUCCEEDED
def test_resume_started_task(self, workflow_context, thread_executor):
node = workflow_context.model.node.get_by_name(tests_mock.models.DEPENDENCY_NODE_NAME)
node.attributes['invocations'] = models.Attribute.wrap('invocations', 0)
self._create_interface(workflow_context, node, mock_stuck_task)
- wf_runner = self._create_initial_workflow_runner(
- workflow_context, mock_parallel_tasks_workflow, thread_executor,
- inputs={'number_of_tasks': 1})
+ ctx = self._prepare_execution_and_get_workflow_ctx(
+ workflow_context.model,
+ workflow_context.resource,
+ workflow_context.model.service.list()[0],
+ mock_parallel_tasks_workflow,
+ thread_executor,
+ inputs={'number_of_tasks': 1}
+ )
- wf_thread = Thread(target=wf_runner.execute)
+ eng = engine.Engine(thread_executor)
+ wf_thread = Thread(target=eng.execute, kwargs=dict(ctx=ctx))
wf_thread.daemon = True
wf_thread.start()
- self._wait_for_active_and_cancel(wf_runner)
+ self._cancel_active_execution(eng, ctx)
node = workflow_context.model.node.refresh(node)
task = workflow_context.model.task.list(filters={'_stub_type': None})[0]
assert node.attributes['invocations'].value == 1
assert task.status == task.STARTED
- assert wf_runner.execution.status in (wf_runner.execution.CANCELLED,
- wf_runner.execution.CANCELLING)
+ assert ctx.execution.status in (ctx.execution.CANCELLED, ctx.execution.CANCELLING)
custom_events['is_resumed'].set()
new_thread_executor = thread.ThreadExecutor()
try:
- new_wf_runner = WorkflowRunner(
- service_id=wf_runner.service.id,
- inputs={},
- model_storage=workflow_context.model,
- resource_storage=workflow_context.resource,
- plugin_manager=None,
- execution_id=wf_runner.execution.id,
- executor=new_thread_executor)
-
- new_wf_runner.execute()
+ new_engine = engine.Engine(new_thread_executor)
+ new_engine.execute(ctx, resuming=True)
finally:
new_thread_executor.close()
@@ -439,28 +359,32 @@
node = workflow_context.model.node.refresh(node)
assert node.attributes['invocations'].value == 2
assert task.status == task.SUCCESS
- assert wf_runner.execution.status == wf_runner.execution.SUCCEEDED
+ assert ctx.execution.status == ctx.execution.SUCCEEDED
def test_resume_failed_task(self, workflow_context, thread_executor):
node = workflow_context.model.node.get_by_name(tests_mock.models.DEPENDENCY_NODE_NAME)
node.attributes['invocations'] = models.Attribute.wrap('invocations', 0)
self._create_interface(workflow_context, node, mock_failed_before_resuming)
- wf_runner = self._create_initial_workflow_runner(workflow_context,
- mock_parallel_tasks_workflow,
- thread_executor)
- wf_thread = Thread(target=wf_runner.execute)
+ ctx = self._prepare_execution_and_get_workflow_ctx(
+ workflow_context.model,
+ workflow_context.resource,
+ workflow_context.model.service.list()[0],
+ mock_parallel_tasks_workflow,
+ thread_executor)
+
+ eng = engine.Engine(thread_executor)
+ wf_thread = Thread(target=eng.execute, kwargs=dict(ctx=ctx))
wf_thread.setDaemon(True)
wf_thread.start()
- self._wait_for_active_and_cancel(wf_runner)
+ self._cancel_active_execution(eng, ctx)
node = workflow_context.model.node.refresh(node)
task = workflow_context.model.task.list(filters={'_stub_type': None})[0]
assert node.attributes['invocations'].value == 2
assert task.status == task.STARTED
- assert wf_runner.execution.status in (wf_runner.execution.CANCELLED,
- wf_runner.execution.CANCELLING)
+ assert ctx.execution.status in (ctx.execution.CANCELLED, ctx.execution.CANCELLING)
custom_events['is_resumed'].set()
assert node.attributes['invocations'].value == 2
@@ -469,16 +393,8 @@
# the old execution to restart.
new_thread_executor = thread.ThreadExecutor()
try:
- new_wf_runner = WorkflowRunner(
- service_id=wf_runner.service.id,
- inputs={},
- model_storage=workflow_context.model,
- resource_storage=workflow_context.resource,
- plugin_manager=None,
- execution_id=wf_runner.execution.id,
- executor=new_thread_executor)
-
- new_wf_runner.execute()
+ new_engine = engine.Engine(new_thread_executor)
+ new_engine.execute(ctx, resuming=True)
finally:
new_thread_executor.close()
@@ -486,20 +402,23 @@
node = workflow_context.model.node.refresh(node)
assert node.attributes['invocations'].value == task.max_attempts - 1
assert task.status == task.SUCCESS
- assert wf_runner.execution.status == wf_runner.execution.SUCCEEDED
+ assert ctx.execution.status == ctx.execution.SUCCEEDED
def test_resume_failed_task_and_successful_task(self, workflow_context, thread_executor):
node = workflow_context.model.node.get_by_name(tests_mock.models.DEPENDENCY_NODE_NAME)
node.attributes['invocations'] = models.Attribute.wrap('invocations', 0)
self._create_interface(workflow_context, node, mock_pass_first_task_only)
- wf_runner = self._create_initial_workflow_runner(
- workflow_context,
+ ctx = self._prepare_execution_and_get_workflow_ctx(
+ workflow_context.model,
+ workflow_context.resource,
+ workflow_context.model.service.list()[0],
mock_parallel_tasks_workflow,
thread_executor,
inputs={'retry_interval': 1, 'max_attempts': 2, 'number_of_tasks': 2}
)
- wf_thread = Thread(target=wf_runner.execute)
+ eng = engine.Engine(thread_executor)
+ wf_thread = Thread(target=eng.execute, kwargs=dict(ctx=ctx))
wf_thread.setDaemon(True)
wf_thread.start()
@@ -516,22 +435,13 @@
assert failed_task.attempts_count == 2
# Second task fails
assert any(task.status == task.SUCCESS for task in tasks)
- assert wf_runner.execution.status in wf_runner.execution.FAILED
+ assert ctx.execution.status in ctx.execution.FAILED
custom_events['is_resumed'].set()
new_thread_executor = thread.ThreadExecutor()
try:
- new_wf_runner = WorkflowRunner(
- service_id=wf_runner.service.id,
- retry_failed_tasks=True,
- inputs={},
- model_storage=workflow_context.model,
- resource_storage=workflow_context.resource,
- plugin_manager=None,
- execution_id=wf_runner.execution.id,
- executor=new_thread_executor)
-
- new_wf_runner.execute()
+ new_engine = engine.Engine(new_thread_executor)
+ new_engine.execute(ctx, resuming=True, retry_failed=True)
finally:
new_thread_executor.close()
@@ -540,20 +450,23 @@
assert failed_task.attempts_count == 1
assert node.attributes['invocations'].value == 4
assert all(task.status == task.SUCCESS for task in tasks)
- assert wf_runner.execution.status == wf_runner.execution.SUCCEEDED
+ assert ctx.execution.status == ctx.execution.SUCCEEDED
def test_two_sequential_task_first_task_failed(self, workflow_context, thread_executor):
node = workflow_context.model.node.get_by_name(tests_mock.models.DEPENDENCY_NODE_NAME)
node.attributes['invocations'] = models.Attribute.wrap('invocations', 0)
self._create_interface(workflow_context, node, mock_fail_first_task_only)
- wf_runner = self._create_initial_workflow_runner(
- workflow_context,
+ ctx = self._prepare_execution_and_get_workflow_ctx(
+ workflow_context.model,
+ workflow_context.resource,
+ workflow_context.model.service.list()[0],
mock_sequential_tasks_workflow,
thread_executor,
inputs={'retry_interval': 1, 'max_attempts': 1, 'number_of_tasks': 2}
)
- wf_thread = Thread(target=wf_runner.execute)
+ eng = engine.Engine(thread_executor)
+ wf_thread = Thread(target=eng.execute, kwargs=dict(ctx=ctx))
wf_thread.setDaemon(True)
wf_thread.start()
@@ -569,16 +482,8 @@
custom_events['is_resumed'].set()
new_thread_executor = thread.ThreadExecutor()
try:
- new_wf_runner = WorkflowRunner(
- service_id=wf_runner.service.id,
- inputs={},
- model_storage=workflow_context.model,
- resource_storage=workflow_context.resource,
- plugin_manager=None,
- execution_id=wf_runner.execution.id,
- executor=new_thread_executor)
-
- new_wf_runner.execute()
+ new_engine = engine.Engine(new_thread_executor)
+ new_engine.execute(ctx, resuming=True)
finally:
new_thread_executor.close()
@@ -587,9 +492,7 @@
assert node.attributes['invocations'].value == 2
assert any(t.status == t.SUCCESS for t in tasks)
assert any(t.status == t.FAILED for t in tasks)
- assert wf_runner.execution.status == wf_runner.execution.SUCCEEDED
-
-
+ assert ctx.execution.status == ctx.execution.SUCCEEDED
@staticmethod
@pytest.fixture
@@ -630,7 +533,7 @@
graph_compiler.GraphCompiler(workflow_context, executor.__class__).compile(graph)
workflow_context.execution = execution
- return engine.Engine(executors={executor.__class__: executor})
+ return engine.Engine(executor)
@pytest.fixture(autouse=True)
def register_to_events(self):
@@ -674,7 +577,6 @@
]
-
@operation
def mock_failed_before_resuming(ctx):
"""
diff --git a/tests/orchestrator/execution_plugin/test_local.py b/tests/orchestrator/execution_plugin/test_local.py
index 7f33318..5af6a2f 100644
--- a/tests/orchestrator/execution_plugin/test_local.py
+++ b/tests/orchestrator/execution_plugin/test_local.py
@@ -501,7 +501,7 @@
return graph
tasks_graph = mock_workflow(ctx=workflow_context) # pylint: disable=no-value-for-parameter
graph_compiler.GraphCompiler(workflow_context, executor.__class__).compile(tasks_graph)
- eng = engine.Engine({executor.__class__: executor})
+ eng = engine.Engine(executor)
eng.execute(workflow_context)
return workflow_context.model.node.get_by_name(
mock.models.DEPENDENCY_NODE_NAME).attributes
diff --git a/tests/orchestrator/execution_plugin/test_ssh.py b/tests/orchestrator/execution_plugin/test_ssh.py
index b5df939..e39f3ba 100644
--- a/tests/orchestrator/execution_plugin/test_ssh.py
+++ b/tests/orchestrator/execution_plugin/test_ssh.py
@@ -263,7 +263,7 @@
tasks_graph = mock_workflow(ctx=self._workflow_context) # pylint: disable=no-value-for-parameter
graph_compiler.GraphCompiler(
self._workflow_context, self._executor.__class__).compile(tasks_graph)
- eng = engine.Engine({self._executor.__class__: self._executor})
+ eng = engine.Engine(self._executor)
eng.execute(self._workflow_context)
return self._workflow_context.model.node.get_by_name(
mock.models.DEPENDENCY_NODE_NAME).attributes
diff --git a/tests/orchestrator/workflows/core/test_engine.py b/tests/orchestrator/workflows/core/test_engine.py
index 0c704f5..2c2a06a 100644
--- a/tests/orchestrator/workflows/core/test_engine.py
+++ b/tests/orchestrator/workflows/core/test_engine.py
@@ -52,7 +52,7 @@
graph = workflow_func(ctx=workflow_context)
graph_compiler.GraphCompiler(workflow_context, executor.__class__).compile(graph)
- return engine.Engine(executors={executor.__class__: executor})
+ return engine.Engine(executor)
@staticmethod
def _create_interface(ctx, func, arguments=None):
diff --git a/tests/orchestrator/workflows/core/test_events.py b/tests/orchestrator/workflows/core/test_events.py
index d804de5..bb5bb75 100644
--- a/tests/orchestrator/workflows/core/test_events.py
+++ b/tests/orchestrator/workflows/core/test_events.py
@@ -132,7 +132,7 @@
single_operation_workflow(ctx, node=node, interface_name=interface_name, op_name=op_name)
)
- eng = engine.Engine(executors={executor.__class__: executor})
+ eng = engine.Engine(executor)
eng.execute(ctx)
return node
diff --git a/tests/orchestrator/workflows/executor/test_process_executor_extension.py b/tests/orchestrator/workflows/executor/test_process_executor_extension.py
index b26fa43..0093976 100644
--- a/tests/orchestrator/workflows/executor/test_process_executor_extension.py
+++ b/tests/orchestrator/workflows/executor/test_process_executor_extension.py
@@ -58,7 +58,7 @@
return graph
graph = mock_workflow(ctx=context) # pylint: disable=no-value-for-parameter
graph_compiler.GraphCompiler(context, executor.__class__).compile(graph)
- eng = engine.Engine({executor.__class__: executor})
+ eng = engine.Engine(executor)
eng.execute(context)
out = get_node(context).attributes.get('out').value
assert out['wrapper_arguments'] == arguments
diff --git a/tests/orchestrator/workflows/executor/test_process_executor_tracked_changes.py b/tests/orchestrator/workflows/executor/test_process_executor_tracked_changes.py
index 47ee2f7..8aaf4ef 100644
--- a/tests/orchestrator/workflows/executor/test_process_executor_tracked_changes.py
+++ b/tests/orchestrator/workflows/executor/test_process_executor_tracked_changes.py
@@ -108,7 +108,7 @@
return graph
graph = mock_workflow(ctx=context) # pylint: disable=no-value-for-parameter
graph_compiler.GraphCompiler(context, executor.__class__).compile(graph)
- eng = engine.Engine({executor.__class__: executor})
+ eng = engine.Engine(executor)
eng.execute(context)
out = context.model.node.get_by_name(mock.models.DEPENDENCY_NODE_NAME).attributes.get('out')
return out.value if out else None