ariatosca-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mxm...@apache.org
Subject [1/3] incubator-ariatosca git commit: removed operation_context getattr directing to workflow_context and added a test
Date Tue, 08 Nov 2016 17:05:02 GMT
Repository: incubator-ariatosca
Updated Branches:
  refs/heads/ARIA-9-API-for-operation-context ffe368b25 -> 101bbd28a


removed operation_context getattr directing to workflow_context and added a test


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

Branch: refs/heads/ARIA-9-API-for-operation-context
Commit: c477255b8f17da675bd1dae13aeb2e278c50deb9
Parents: ffe368b
Author: mxmrlv <mxmrlv@gmail.com>
Authored: Tue Nov 8 16:31:59 2016 +0200
Committer: mxmrlv <mxmrlv@gmail.com>
Committed: Tue Nov 8 16:31:59 2016 +0200

----------------------------------------------------------------------
 aria/context/operation.py   |  24 ++++---
 aria/decorators.py          |  13 +---
 aria/workflows/api/task.py  |   2 +-
 tests/mock/models.py        |  33 +++++++---
 tests/mocks/__init__.py     |   0
 tests/mocks/context_mock.py | 136 ---------------------------------------
 tests/test_context.py       |  59 +++++++++++++----
 7 files changed, 91 insertions(+), 176 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c477255b/aria/context/operation.py
----------------------------------------------------------------------
diff --git a/aria/context/operation.py b/aria/context/operation.py
index a3c0611..3f6570b 100644
--- a/aria/context/operation.py
+++ b/aria/context/operation.py
@@ -21,6 +21,8 @@ from uuid import uuid4
 
 from aria.logger import LoggerMixin
 
+from . import workflow
+
 
 class OperationContext(LoggerMixin):
     """
@@ -31,15 +33,15 @@ class OperationContext(LoggerMixin):
             self,
             name,
             operation_details,
-            workflow_context,
             operation_host,
+            workflow_context=None,
             inputs=None):
         super(OperationContext, self).__init__()
         self.name = name
         self._id = str(uuid4())
         self._operation_details = operation_details
-        self._workflow_context = workflow_context
         self._operation_host = operation_host
+        self._workflow_context = workflow_context or workflow.current.get()
         self._inputs = inputs or {}
 
     @property
@@ -63,14 +65,23 @@ class OperationContext(LoggerMixin):
         """
         The model operation
         """
-        return self.storage.operation.get(self.id)
+        return self.model.task.get(self.id)
 
     @operation.setter
     def operation(self, value):
         """
         Store the operation in the model storage
         """
-        self.storage.operation.store(value)
+        self.model.task.store(value)
+
+    @property
+    def execution_id(self):
+        return self.workflow_context.execution_id
+
+    @property
+    def execution(self):
+        return self.workflow_context.execution
+
 
     def __repr__(self):
         details = ', '.join(
@@ -86,7 +97,4 @@ class OperationContext(LoggerMixin):
         try:
             return getattr(self.operation_host, attr)
         except AttributeError:
-            try:
-                return getattr(self.workflow_context, attr)
-            except AttributeError:
-                return super(OperationContext, self).__getattribute__(attr)
+            return super(OperationContext, self).__getattribute__(attr)

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c477255b/aria/decorators.py
----------------------------------------------------------------------
diff --git a/aria/decorators.py b/aria/decorators.py
index a07e2ee..26f1891 100644
--- a/aria/decorators.py
+++ b/aria/decorators.py
@@ -65,10 +65,7 @@ def operation(
         return partial(operation)
 
     @wraps(func)
-    def _wrapper(ctx, **custom_kwargs):
-        func_kwargs = _create_func_kwargs(
-            custom_kwargs,
-            ctx)
+    def _wrapper(ctx, **func_kwargs):
         validate_function_arguments(func, func_kwargs)
         ctx.description = func.__doc__
         return func(**func_kwargs)
@@ -79,11 +76,3 @@ def _generate_workflow_name(func_name, ctx, suffix_template, **custom_kwargs):
     return '{func_name}.{suffix}'.format(
         func_name=func_name,
         suffix=suffix_template.format(ctx=ctx, **custom_kwargs) or str(uuid4()))
-
-
-def _create_func_kwargs(
-        kwargs,
-        ctx,
-        workflow_name=None):
-    kwargs.setdefault('graph', ctx.task_graph(workflow_name))
-    return kwargs

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c477255b/aria/workflows/api/task.py
----------------------------------------------------------------------
diff --git a/aria/workflows/api/task.py b/aria/workflows/api/task.py
index bbd39d3..e2f471a 100644
--- a/aria/workflows/api/task.py
+++ b/aria/workflows/api/task.py
@@ -63,7 +63,7 @@ class OperationTask(BaseTask):
         Creates an operation task using the name, details, node instance and any additional
kwargs.
         :param name: the operation of the name.
         :param operation_details: the details for the operation.
-        :param node_instance: the node instance on which this operation is registered.
+        :param operation_host: the operation host on which this operation is registered.
         :param inputs: operation inputs.
         """
         super(OperationTask, self).__init__()

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c477255b/tests/mock/models.py
----------------------------------------------------------------------
diff --git a/tests/mock/models.py b/tests/mock/models.py
index 633adbb..bf40039 100644
--- a/tests/mock/models.py
+++ b/tests/mock/models.py
@@ -24,11 +24,16 @@ BLUEPRINT_ID = 'test_blueprint_id'
 WORKFLOW_ID = 'test_workflow_id'
 EXECUTION_ID = 'test_execution_id'
 
+DEPENDENCY_NODE_ID = 'dependency_node'
+DEPENDENCY_NODE_INSTANCE_ID = 'dependency_node_instance'
+DEPENDENT_NODE_ID = 'dependent_node'
+DEPENDENT_NODE_INSTANCE_ID = 'dependent_node_instance'
+
 
 def get_dependency_node():
     return models.Node(
-        id='dependency_node',
-        host_id='dependency_node',
+        id=DEPENDENCY_NODE_ID,
+        host_id=DEPENDENCY_NODE_ID,
         blueprint_id=BLUEPRINT_ID,
         type='test_node_type',
         type_hierarchy=[],
@@ -45,8 +50,8 @@ def get_dependency_node():
 
 def get_dependency_node_instance(dependency_node=None):
     return models.NodeInstance(
-        id='dependency_node_instance',
-        host_id='dependency_node_instance',
+        id=DEPENDENCY_NODE_INSTANCE_ID,
+        host_id=DEPENDENCY_NODE_INSTANCE_ID,
         deployment_id=DEPLOYMENT_ID,
         runtime_properties={},
         version=None,
@@ -80,8 +85,8 @@ def get_relationship_instance(target_instance=None, relationship=None):
 
 def get_dependent_node(relationship=None):
     return models.Node(
-        id='dependent_node',
-        host_id='dependent_node',
+        id=DEPENDENCY_NODE_ID,
+        host_id=DEPENDENCY_NODE_ID,
         blueprint_id=BLUEPRINT_ID,
         type='test_node_type',
         type_hierarchy=[],
@@ -98,8 +103,8 @@ def get_dependent_node(relationship=None):
 
 def get_dependent_node_instance(relationship_instance, dependent_node=None):
     return models.NodeInstance(
-        id='dependent_node_instance',
-        host_id='dependent_node_instance',
+        id=DEPENDENT_NODE_INSTANCE_ID,
+        host_id=DEPENDENT_NODE_INSTANCE_ID,
         deployment_id=DEPLOYMENT_ID,
         runtime_properties={},
         version=None,
@@ -108,6 +113,18 @@ def get_dependent_node_instance(relationship_instance, dependent_node=None):
     )
 
 
+def get_blueprint():
+    now = datetime.now()
+    return models.Blueprint(
+        plan={},
+        id=BLUEPRINT_ID,
+        description=None,
+        created_at=now,
+        updated_at=now,
+        main_file_name='main_file_name'
+    )
+
+
 def get_execution():
     return models.Execution(
         id=EXECUTION_ID,

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c477255b/tests/mocks/__init__.py
----------------------------------------------------------------------
diff --git a/tests/mocks/__init__.py b/tests/mocks/__init__.py
deleted file mode 100644
index e69de29..0000000

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c477255b/tests/mocks/context_mock.py
----------------------------------------------------------------------
diff --git a/tests/mocks/context_mock.py b/tests/mocks/context_mock.py
deleted file mode 100644
index 5e92694..0000000
--- a/tests/mocks/context_mock.py
+++ /dev/null
@@ -1,136 +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 pytest
-
-from aria import contexts
-from aria.storage import ModelStorage, models
-
-from ..storage import InMemoryModelDriver
-
-NODE_OPERATIONS = [
-    'aria.interfaces.lifecycle.create',
-    'aria.interfaces.lifecycle.configure',
-    'aria.interfaces.lifecycle.start',
-    'aria.interfaces.lifecycle.stop',
-    'aria.interfaces.lifecycle.delete',
-]
-
-RELATIONSHIP_OPERATIONS = [
-    'aria.interfaces.relationship_lifecycle.preconfigure',
-    'aria.interfaces.relationship_lifecycle.postconfigure',
-    'aria.interfaces.relationship_lifecycle.establish',
-    'aria.interfaces.relationship_lifecycle.unlink'
-]
-
-
-def simple_context():
-    """
-    Create the following graph in storage:
-    dependency_node <------ dependent_node
-    :return:
-    """
-
-    storage = ModelStorage(InMemoryModelDriver(),
-                           model_classes=[models.NodeInstance,
-                                          models.Node,
-                                          models.Relationship,
-                                          models.RelationshipInstance])
-    storage.setup()
-
-    dependency_node = models.Node(
-        id='dependency_node',
-        host_id='dependency_node',
-        blueprint_id='test_blueprint_id',
-        type='test_node_type',
-        type_hierarchy=[],
-        number_of_instances=1,
-        planned_number_of_instances=1,
-        deploy_number_of_instances=1,
-        properties={},
-        operations=dict((key, None) for key in NODE_OPERATIONS),
-        relationships=[],
-        min_number_of_instances=1,
-        max_number_of_instances=1,
-    )
-
-    dependency_node_instance = models.NodeInstance(
-        id='dependency_node_instance',
-        host_id='dependency_node_instance',
-        deployment_id='test_deployment_id',
-        runtime_properties={},
-        version=None,
-        relationship_instances=[],
-        node=dependency_node
-    )
-
-    relationship = models.Relationship(
-        target_id=dependency_node.id,
-        source_interfaces={},
-        source_operations=dict((key, None) for key in RELATIONSHIP_OPERATIONS),
-        target_interfaces={},
-        target_operations=dict((key, None) for key in RELATIONSHIP_OPERATIONS),
-        type='rel_type',
-        type_hierarchy=[],
-        properties={},
-    )
-
-    relationship_instance = models.RelationshipInstance(
-        target_id=dependency_node_instance.id,
-        target_name='test_target_name',
-        type='some_type',
-        relationship=relationship,
-
-    )
-
-    dependent_node = models.Node(
-        id='dependent_node',
-        host_id='dependent_node',
-        blueprint_id='test_blueprint_id',
-        type='test_node_type',
-        type_hierarchy=[],
-        number_of_instances=1,
-        planned_number_of_instances=1,
-        deploy_number_of_instances=1,
-        properties={},
-        operations=dict((key, None) for key in NODE_OPERATIONS),
-        relationships=[relationship],
-        min_number_of_instances=1,
-        max_number_of_instances=1,
-    )
-
-    dependent_node_instance = models.NodeInstance(
-        id='dependent_node_instance',
-        host_id='dependent_node_instance',
-        deployment_id='test_deployment_id',
-        runtime_properties={},
-        version=None,
-        relationship_instances=[relationship_instance],
-        node=dependent_node
-    )
-
-    storage.node.store(dependency_node)
-    storage.node.store(dependent_node)
-    storage.node_instance.store(dependency_node_instance)
-    storage.node_instance.store(dependent_node_instance)
-    storage.relationship.store(relationship)
-    storage.relationship_instance.store(relationship_instance)
-
-    wf_context = contexts.WorkflowContext(name='test_context',
-                                          model_storage=storage,
-                                          resource_storage=None,
-                                          deployment_id='test_deployment_id',
-                                          workflow_id='test_workflow_id')
-    return wf_context

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c477255b/tests/test_context.py
----------------------------------------------------------------------
diff --git a/tests/test_context.py b/tests/test_context.py
index 48bb742..d74f0a4 100644
--- a/tests/test_context.py
+++ b/tests/test_context.py
@@ -15,20 +15,57 @@
 
 import pytest
 
-from .mocks.context_mock import simple_context
+from aria import context
+
+from . import mock
 
 
 @pytest.fixture
-def context():
-    return simple_context()
+def ctx():
+    simple_context = mock.context.simple()
+
+    blueprint = mock.models.get_blueprint()
+    deployment = mock.models.get_deployment()
+    node = mock.models.get_dependency_node()
+    node_instance = mock.models.get_dependency_node_instance(node)
+    execution = mock.models.get_execution()
+
+    simple_context.model.blueprint.store(blueprint)
+    simple_context.model.deployment.store(deployment)
+    simple_context.model.node.store(node)
+    simple_context.model.node_instance.store(node_instance)
+    simple_context.model.execution.store(execution)
+
+    return simple_context
+
 
+class TestOperationContext(object):
 
-class TestOperationContext():
-    def test_operation(self, context):
-        operation = context.operation(
-            name='test_operation_name',
-            operation_details={},
-            operation_host=context.model.node.get('dependency_node'),
-        )
+    def test_operation(self, ctx):
+        node_instance = ctx.model.node_instance.get(mock.models.DEPENDENCY_NODE_INSTANCE_ID)
+        with context.workflow.current.push(ctx):
+            operation_context = context.operation.OperationContext(
+                name='test_operation_name',
+                operation_details={'dits': True},
+                operation_host=node_instance,
+                inputs={'inputs': True}
+            )
+        # operation host attributes
+        assert operation_context.id == node_instance.id
+        assert operation_context.deployment_id == node_instance.deployment_id
+        assert operation_context.runtime_properties == node_instance.runtime_properties
+        assert operation_context.state == node_instance.state
+        assert operation_context.version == node_instance.version
+        assert operation_context.relationship_instances == node_instance.relationship_instances
+        assert operation_context.node == node_instance.node
+        assert operation_context.host_id == node_instance.host_id
+        assert operation_context.scaling_groups == node_instance.scaling_groups
+        assert operation_context.name == 'test_operation_name'
 
-        pass
\ No newline at end of file
+        # operation context attributes
+        assert operation_context.workflow_context == ctx
+        assert operation_context.operation_details == {'dits': True}
+        assert operation_context.inputs == {'inputs': True}
+        assert operation_context.operation_host == node_instance
+        assert operation_context.execution == ctx.execution
+        assert operation_context.execution_id == ctx.execution_id


Mime
View raw message