Esempio n. 1
0
    def test_get_task_execution_result(self):
        task_ex = models.TaskExecution(name='task1',
                                       spec={
                                           "version": '2.0',
                                           'name': 'task1',
                                           'with-items': 'var in [1]',
                                           'type': 'direct'
                                       })

        task_ex.executions.append(
            models.ActionExecution(name='my_action',
                                   output={'result': 1},
                                   accepted=True,
                                   runtime_context={'with_items_index': 0}))

        self.assertEqual([1], data_flow.get_task_execution_result(task_ex))

        task_ex.executions.append(
            models.ActionExecution(name='my_action',
                                   output={'result': 1},
                                   accepted=True,
                                   runtime_context={'with_items_index': 0}))
        task_ex.executions.append(
            models.ActionExecution(name='my_action',
                                   output={'result': 1},
                                   accepted=False,
                                   runtime_context={'with_items_index': 0}))

        self.assertEqual([1, 1], data_flow.get_task_execution_result(task_ex))
Esempio n. 2
0
    def test_get_indices(self):
        # Task execution for running 6 items with concurrency=3.
        task_ex = models.TaskExecution(spec={'action': 'myaction'},
                                       runtime_context={
                                           'with_items_context': {
                                               'capacity': 3,
                                               'count': 6
                                           }
                                       },
                                       action_executions=[],
                                       workflow_executions=[])

        # Set 3 items: 2 success and 1 error unaccepted.
        task_ex.action_executions += [
            self.get_action_ex(True, states.SUCCESS, 0),
            self.get_action_ex(True, states.SUCCESS, 1),
            self.get_action_ex(False, states.ERROR, 2)
        ]

        # Then call get_indices and expect [2, 3, 4].
        indices = with_items.get_indices_for_loop(task_ex)

        # TODO(rakhmerov): Restore concurrency support.
        # With disabled 'concurrency' support we expect indices 2,3,4,5
        # because overall count is 6 and two indices were already processed.
        self.assertListEqual([2, 3, 4, 5], indices)
Esempio n. 3
0
    def test_get_indices(self):
        # Task execution for running 6 items with concurrency=3.
        task_ex = models.TaskExecution(
            spec={
                'action': 'myaction'
            },
            runtime_context={
                'with_items_context': {
                    'capacity': 3,
                    'count': 6
                }
            },
            action_executions=[],
            workflow_executions=[]
        )

        # Set 3 items: 2 success and 1 error unaccepted.
        task_ex.action_executions += [
            self.get_action_ex(True, states.SUCCESS, 0),
            self.get_action_ex(True, states.SUCCESS, 1),
            self.get_action_ex(False, states.ERROR, 2)
        ]

        # Then call get_indices and expect [2, 3, 4].
        indices = with_items.get_indices_for_loop(task_ex)

        self.assertListEqual([2, 3, 4], indices)
Esempio n. 4
0
    def test_task_policy_class(self):
        policy = policies.base.TaskPolicy()

        policy._schema = {"properties": {"delay": {"type": "integer"}}}

        wf_ex = models.WorkflowExecution(id='1-2-3-4',
                                         context={},
                                         input={},
                                         params={})

        task_ex = models.TaskExecution(in_context={'int_var': 5})
        task_ex.workflow_execution = wf_ex

        policy.delay = "<% $.int_var %>"

        # Validation is ok.
        policy.before_task_start(task_ex, None)

        policy.delay = "some_string"

        # Validation is failing now.
        exception = self.assertRaises(exc.InvalidModelException,
                                      policy.before_task_start, task_ex, None)

        self.assertIn("Invalid data type in TaskPolicy", str(exception))
Esempio n. 5
0
def update_task_execution_state(id, cur_state, state, session=None):
    wf_ex = None

    # Use WHERE clause to exclude possible conflicts if the state has
    # already been changed.
    try:
        specimen = models.TaskExecution(
            id=id,
            state=cur_state
        )

        wf_ex = b.model_query(
            models.TaskExecution).update_on_match(
            specimen=specimen,
            surrogate_key='id',
            values={'state': state}
        )
    except oslo_sqlalchemy.update_match.NoRowsMatched:
        LOG.info(
            "Can't change task execution state from %s to %s, "
            "because it has already been changed. [execution_id=%s]",
            cur_state, state, id
        )

    return wf_ex
Esempio n. 6
0
    def _create_task_execution(self, name, state):
        tasks_spec = self.wb_spec.get_workflows()['wf'].get_tasks()

        task_ex = models.TaskExecution(name=name,
                                       spec=tasks_spec[name].to_dict(),
                                       state=state)

        self.wf_ex.task_executions.append(task_ex)

        return task_ex
Esempio n. 7
0
    def _create_task_execution(self, name, state):
        tasks_spec = self.wf_spec.get_tasks()

        task_ex = models.TaskExecution(id=self.getUniqueString('id'),
                                       name=name,
                                       spec=tasks_spec[name].to_dict(),
                                       state=state)

        self.wf_ex.task_executions.append(task_ex)

        return task_ex
Esempio n. 8
0
def create_task_execution(values, session=None):
    task_ex = models.TaskExecution()

    task_ex.update(values)

    try:
        task_ex.save(session=session)
    except db_exc.DBDuplicateEntry as e:
        raise exc.DBDuplicateEntryError(
            "Duplicate entry for TaskExecution: %s" % e.columns)

    return task_ex
Esempio n. 9
0
    def test_get_task_execution_result(self):
        task_ex = models.TaskExecution(
            name='task1',
            spec={
                "version": '2.0',
                'name': 'task1',
                'with-items': 'var in [1]',
                'type': 'direct'
            },
            runtime_context={
                'with_items_context': {'count': 1}
            }
        )

        action_exs = [models.ActionExecution(
            name='my_action',
            output={'result': 1},
            accepted=True,
            runtime_context={'index': 0}
        )]

        with mock.patch.object(db_api, 'get_action_executions',
                               return_value=action_exs):
            self.assertEqual([1], data_flow.get_task_execution_result(task_ex))

        action_exs.append(models.ActionExecution(
            name='my_action',
            output={'result': 1},
            accepted=True,
            runtime_context={'index': 0}
        ))

        action_exs.append(models.ActionExecution(
            name='my_action',
            output={'result': 1},
            accepted=False,
            runtime_context={'index': 0}
        ))

        with mock.patch.object(db_api, 'get_action_executions',
                               return_value=action_exs):
            self.assertEqual(
                [1, 1],
                data_flow.get_task_execution_result(task_ex)
            )
Esempio n. 10
0
    def test_task_executions(self):
        # Add an associated object into collection.
        with db_api.transaction():
            wf_ex = db_api.create_workflow_execution(WF_EXECS[0])

            self.assertEqual(0, len(wf_ex.task_executions))

            wf_ex.task_executions.append(
                db_models.TaskExecution(**TASK_EXECS[0]))

        # Make sure task execution has been saved.
        with db_api.transaction():
            wf_ex = db_api.get_workflow_execution(wf_ex.id)

            self.assertIsNotNone(wf_ex)

            self.assertEqual(1, len(wf_ex.task_executions))

            task_ex = wf_ex.task_executions[0]

            self.assertEqual(TASK_EXECS[0]['name'], task_ex.name)

        # Make sure that polymorphic load works correctly.
        self.assertEqual(2, len(db_api.get_executions()))
        self.assertEqual(1, len(db_api.get_workflow_executions()))
        self.assertEqual(1, len(db_api.get_task_executions()))

        # Remove task execution from collection.
        with db_api.transaction():
            wf_ex = db_api.get_workflow_execution(wf_ex.id)

            del wf_ex.task_executions[:]

        # Make sure task execution has been removed.
        with db_api.transaction():
            wf_ex = db_api.get_workflow_execution(wf_ex.id)

            self.assertEqual(0, len(wf_ex.task_executions))
            self.assertIsNone(db_api.load_task_execution(task_ex.id))
Esempio n. 11
0
                                 params={'env': {
                                     'k1': 'abc'
                                 }},
                                 created_at=datetime.datetime(1970, 1, 1),
                                 updated_at=datetime.datetime(1970, 1, 1))

TASK_EX = models.TaskExecution(
    id='123',
    name='task',
    workflow_name='flow',
    workflow_id='123e4567-e89b-12d3-a456-426655441111',
    spec={
        'type': 'direct',
        'version': '2.0',
        'name': 'task'
    },
    action_spec={},
    state=states.RUNNING,
    tags=['a', 'b'],
    in_context={},
    runtime_context={},
    workflow_execution_id=WF_EX.id,
    created_at=datetime.datetime(1970, 1, 1),
    updated_at=datetime.datetime(1970, 1, 1),
    published=PUBLISHED,
    processed=True)

WITH_ITEMS_TASK_EX = models.TaskExecution(
    id='123',
    name='task',
    workflow_name='flow',
    workflow_id='123e4567-e89b-12d3-a456-426655441111',
from mistral.db.v2.sqlalchemy import models
from mistral import exceptions as exc
from mistral.rpc import base as rpc_base
from mistral.rpc import clients as rpc_clients
from mistral.tests.unit.api import base
from mistral.utils import rest_utils
from mistral.workflow import states
from mistral_lib import actions as ml_actions

# This line is needed for correct initialization of messaging config.
oslo_messaging.get_rpc_transport(cfg.CONF)

ACTION_EX_DB = models.ActionExecution(
    id='123',
    workflow_name='flow',
    task_execution=models.TaskExecution(name='task1'),
    task_execution_id='333',
    state=states.SUCCESS,
    state_info=states.SUCCESS,
    tags=['foo', 'fee'],
    name='std.echo',
    description='something',
    accepted=True,
    input={},
    output={},
    created_at=datetime.datetime(1970, 1, 1),
    updated_at=datetime.datetime(1970, 1, 1))

AD_HOC_ACTION_EX_DB = models.ActionExecution(
    id='123',
    state=states.SUCCESS,
Esempio n. 13
0
def update_task_execution_state(id, cur_state, state):
    specimen = models.TaskExecution(id=id, state=cur_state)

    return update_on_match(id, specimen, values={'state': state}, attempts=1)
Esempio n. 14
0
from mistral import exceptions as exc
from mistral.tests.unit.api import base
from mistral.workflow import data_flow
from mistral.workflow import states

# TODO(everyone): later we need additional tests verifying all the errors etc.

RESULT = {"some": "result"}
PUBLISHED = {"var": "val"}
task_ex = models.TaskExecution(id='123',
                               name='task',
                               workflow_name='flow',
                               spec={},
                               action_spec={},
                               state=states.RUNNING,
                               tags=['a', 'b'],
                               in_context={},
                               runtime_context={},
                               workflow_execution_id='123',
                               created_at=datetime.datetime(1970, 1, 1),
                               updated_at=datetime.datetime(1970, 1, 1),
                               published=PUBLISHED)

TASK = {
    'id': '123',
    'name': 'task',
    'workflow_name': 'flow',
    'state': 'RUNNING',
    'workflow_execution_id': '123',
    'created_at': '1970-01-01 00:00:00',
    'updated_at': '1970-01-01 00:00:00',