Repository: incubator-ariatosca
Updated Branches:
  refs/heads/operation_graph 8f83ce939 -> ee1a69170


added tests and fixed the transcription mechanism


Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/ee1a6917
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/ee1a6917
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/ee1a6917

Branch: refs/heads/operation_graph
Commit: ee1a691707b01e604e6dfcc6954a1c3ffe242755
Parents: 8f83ce9
Author: mxmrlv <mxm...@gmail.com>
Authored: Sun Oct 16 19:06:32 2016 +0300
Committer: mxmrlv <mxm...@gmail.com>
Committed: Sun Oct 16 19:06:32 2016 +0300

----------------------------------------------------------------------
 aria/workflows/engine/engine.py                 | 27 +++----
 aria/workflows/engine/tasks.py                  |  4 +-
 tests/engine/__init__.py                        | 38 +++++++++
 .../test_task_graph_into_exececution_graph.py   | 85 ++++++++++++++++++++
 tests/requirements.txt                          |  1 +
 5 files changed, 139 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/ee1a6917/aria/workflows/engine/engine.py
----------------------------------------------------------------------
diff --git a/aria/workflows/engine/engine.py b/aria/workflows/engine/engine.py
index 34aee58..4229fe9 100644
--- a/aria/workflows/engine/engine.py
+++ b/aria/workflows/engine/engine.py
@@ -60,7 +60,7 @@ class Engine(LoggerMixin):
             depends_on=()):
 
         # Insert start marker
-        start_task = StartClass(task_name=self._start_task_name(graph.name), 
context=self._workflow_context)
+        start_task = StartClass(name=self._start_task_name(graph.name), 
context=self._workflow_context)
         self._add_task_and_dependencies(start_task, depends_on)
 
         for operation_or_workflow, dependencies in 
graph.task_tree(reverse=True):
@@ -69,7 +69,7 @@ class Engine(LoggerMixin):
             if self._is_operation(operation_or_workflow):
                 # Add the task an the dependencies
                 operation_task = OperationTask(
-                    task_name=operation_or_workflow.name,
+                    name=operation_or_workflow.name,
                     context=operation_or_workflow,
                     **operation_or_workflow.engine_options
                 )
@@ -85,27 +85,26 @@ class Engine(LoggerMixin):
 
         # Insert end marker
         workflow_dependencies = 
self._get_tasks_from_dependencies(graph.leaf_tasks, default=[start_task])
-        end_task = EndClass(task_name=self._end_task_name(graph.name), 
context=self._workflow_context)
+        end_task = EndClass(name=self._end_task_name(graph.name), 
context=self._workflow_context)
         self._add_task_and_dependencies(end_task, workflow_dependencies)
 
     def _add_task_and_dependencies(self, operation_task, 
operation_dependencies=()):
-        self._execution_graph.add_node(operation_task)
+        self._execution_graph.add_node(operation_task.name, 
task=operation_task)
         for dependency in operation_dependencies:
-            
self._execution_graph.add_edge(self._execution_graph[dependency.task_name],
-                                           
self._execution_graph[operation_task.task_name])
+            self._execution_graph.add_edge(dependency.name, 
operation_task.name)
 
     def _get_tasks_from_dependencies(self, dependencies, default=()):
         """
         Returns task list from dependencies.
         """
-        return [
-            self._workflow_tasks[
-                context.name
-                if self._is_operation(context) else
-                self._end_task_name(context.name)
-            ]
-            for context in dependencies
-        ] or default
+        tasks = [self._execution_graph.node[
+                    dependency.name
+                    if self._is_operation(dependency) else
+                    # The task is an entire subgraph, so we need to check that 
the wrapper task has been reached
+                    self._end_task_name(dependency.name)]['task']
+                 for dependency in dependencies]
+
+        return tasks or default
 
     @staticmethod
     def _is_operation(task):

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/ee1a6917/aria/workflows/engine/tasks.py
----------------------------------------------------------------------
diff --git a/aria/workflows/engine/tasks.py b/aria/workflows/engine/tasks.py
index 688b38b..1403ce5 100644
--- a/aria/workflows/engine/tasks.py
+++ b/aria/workflows/engine/tasks.py
@@ -16,8 +16,8 @@
 
 class BaseTask(object):
 
-    def __init__(self, task_name, context, depends_on):
-        self.task_name = task_name
+    def __init__(self, name, context):
+        self.name = name
         self.context = context
 
 

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/ee1a6917/tests/engine/__init__.py
----------------------------------------------------------------------
diff --git a/tests/engine/__init__.py b/tests/engine/__init__.py
new file mode 100644
index 0000000..e7533fd
--- /dev/null
+++ b/tests/engine/__init__.py
@@ -0,0 +1,38 @@
+# 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.
+from uuid import uuid4
+
+from aria.workflows.engine.executor import Executor
+from aria.workflows.engine.tasks import OperationTask
+from aria.contexts import OperationContext
+
+
+class MockExecutor(Executor):
+    def execute(self, task):
+        print task
+
+
+class MockOperationContext(OperationContext):
+    def __init__(self, id=str(uuid4()), **kwargs):
+        self.id = self.name = id
+        self.engine_options = {}
+
+    def __repr__(self):
+        return self.id
+
+
+class MockOperationTask(OperationTask):
+    def _create_operation_in_storage(self):
+        pass

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/ee1a6917/tests/engine/test_task_graph_into_exececution_graph.py
----------------------------------------------------------------------
diff --git a/tests/engine/test_task_graph_into_exececution_graph.py 
b/tests/engine/test_task_graph_into_exececution_graph.py
new file mode 100644
index 0000000..b087a0f
--- /dev/null
+++ b/tests/engine/test_task_graph_into_exececution_graph.py
@@ -0,0 +1,85 @@
+# 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 pytest
+import mock
+from networkx import topological_sort
+
+from aria.workflows.engine.engine import Engine
+from aria.workflows.api.tasks_graph import TaskGraph
+from aria.workflows.engine.tasks import (StartWorkflowTask,
+                                         EndWorkflowTask,
+                                         StartSubWorkflowTask,
+                                         EndSubWorkflowTask,
+                                         OperationTask)
+
+from . import MockExecutor, MockOperationContext, MockOperationTask
+
+
+@mock.patch('aria.workflows.engine.engine.OperationTask', 
new=MockOperationTask)
+def test_task_graph_into_execution_graph():
+    task_graph_name = 'test_task_graph'
+    inner_task_graph_name = 'test_inner_task_graph'
+    simple_before_task_name = 'test_simple_before_task'
+    simple_after_task_name = 'test_simple_after_task'
+    inner_task_name = 'test_inner_task'
+
+    task_graph = TaskGraph(task_graph_name)
+    simple_before_task = MockOperationContext(simple_before_task_name)
+    simple_after_task = MockOperationContext(simple_after_task_name)
+
+    inner_task_graph = TaskGraph(inner_task_graph_name)
+    inner_task = MockOperationContext(inner_task_name)
+    inner_task_graph.add_task(inner_task)
+
+    task_graph.add_task(simple_before_task)
+    task_graph.add_task(simple_after_task)
+    task_graph.add_task(inner_task_graph)
+    task_graph.dependency(inner_task_graph, [simple_before_task])
+    task_graph.dependency(simple_after_task, [inner_task_graph])
+
+    engine = Engine(executor=MockExecutor(),
+                    workflow_context=mock.MagicMock(),
+                    tasks_graph=task_graph)
+
+    # Direct check
+    execution_graph = engine._execution_graph
+    execution_tasks = topological_sort(execution_graph)
+
+    assert len(execution_tasks) == 7
+
+    tasks_names = [
+        'Starting.{0}'.format(task_graph_name),
+        simple_before_task_name,
+        'Starting.{0}'.format(inner_task_graph_name),
+        inner_task_name,
+        'Ending.{0}'.format(inner_task_graph_name),
+        simple_after_task_name,
+        'Ending.{0}'.format(task_graph_name)
+    ]
+
+    assert tasks_names == execution_tasks
+
+    assert isinstance(_get_task_by_name(execution_tasks[0], execution_graph), 
StartWorkflowTask)
+    assert simple_before_task == _get_task_by_name(execution_tasks[1], 
execution_graph).context
+    assert isinstance(_get_task_by_name(execution_tasks[2], execution_graph), 
StartSubWorkflowTask)
+    assert inner_task == _get_task_by_name(execution_tasks[3], 
execution_graph).context
+    assert isinstance(_get_task_by_name(execution_tasks[4], execution_graph), 
EndSubWorkflowTask)
+    assert simple_after_task == _get_task_by_name(execution_tasks[5], 
execution_graph).context
+    assert isinstance(_get_task_by_name(execution_tasks[6], execution_graph), 
EndWorkflowTask)
+
+
+def _get_task_by_name(task_name, graph):
+    return graph.node[task_name]['task']

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/ee1a6917/tests/requirements.txt
----------------------------------------------------------------------
diff --git a/tests/requirements.txt b/tests/requirements.txt
index 07d82a6..f3443d1 100644
--- a/tests/requirements.txt
+++ b/tests/requirements.txt
@@ -4,3 +4,4 @@ tox==1.6.1
 pylint==1.5.5
 pytest==3.0.2
 pytest-cov==2.3.1
+pytest-mock==1.2

Reply via email to