You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ariatosca.apache.org by mx...@apache.org on 2017/02/06 14:52:15 UTC

incubator-ariatosca git commit: cleaning up

Repository: incubator-ariatosca
Updated Branches:
  refs/heads/ARIA-42-Generic-ctx-serialization-mechanism 03246963c -> 641cf81d7


cleaning up


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

Branch: refs/heads/ARIA-42-Generic-ctx-serialization-mechanism
Commit: 641cf81d7386e9d98545d3c740cf37b284affe5a
Parents: 0324696
Author: mxmrlv <mx...@gmail.com>
Authored: Mon Feb 6 16:52:06 2017 +0200
Committer: mxmrlv <mx...@gmail.com>
Committed: Mon Feb 6 16:52:06 2017 +0200

----------------------------------------------------------------------
 aria/__init__.py                                |  5 ++--
 aria/orchestrator/workflows/executor/process.py |  6 -----
 aria/storage/api.py                             | 24 ++++++++++++++------
 tests/__init__.py                               |  4 ++++
 tests/mock/context.py                           |  5 +++-
 tests/orchestrator/context/test_workflow.py     |  5 ++--
 .../workflows/executor/test_process_executor.py |  6 +++--
 tests/storage/__init__.py                       |  2 +-
 tests/storage/test_model_storage.py             |  3 ++-
 tests/storage/test_models.py                    |  3 ++-
 tests/storage/test_structures.py                |  5 ++--
 11 files changed, 42 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/641cf81d/aria/__init__.py
----------------------------------------------------------------------
diff --git a/aria/__init__.py b/aria/__init__.py
index f3bc043..da77f03 100644
--- a/aria/__init__.py
+++ b/aria/__init__.py
@@ -78,8 +78,7 @@ def application_model_storage(api, api_kwargs=None, storage_initiator_func=None)
         storage.model.Execution,
         storage.model.Task,
     ]
-    # if api not in _model_storage:
-    api.storage_initiator(storage_initiator_func or storage.sql_mapi.init_storage)
+    api.storage_initiator(storage_initiator_func)
     return storage.ModelStorage(api, items=models, api_kwargs=api_kwargs or {})
 
 
@@ -88,6 +87,6 @@ def application_resource_storage(api, api_kwargs=None, storage_initiator_func=No
     Initiate resource storage
     """
 
-    # api.storage_initiator(storage_initiator_func or (lambda **kwargs: kwargs))
+    api.storage_initiator(storage_initiator_func)
     return storage.ResourceStorage(
         api, api_kwargs=api_kwargs or {}, items=['blueprint', 'deployment', 'plugin', ])

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/641cf81d/aria/orchestrator/workflows/executor/process.py
----------------------------------------------------------------------
diff --git a/aria/orchestrator/workflows/executor/process.py b/aria/orchestrator/workflows/executor/process.py
index cf50ac6..f670dfe 100644
--- a/aria/orchestrator/workflows/executor/process.py
+++ b/aria/orchestrator/workflows/executor/process.py
@@ -327,7 +327,6 @@ def _main():
     storage_type.remove_mutable_association_listener()
 
     with instrumentation.track_changes() as instrument:
-        ctx = None
         try:
             ctx = serialization.operation_context_from_dict(context_dict)
             _patch_session(ctx=ctx, messenger=messenger, instrument=instrument)
@@ -339,11 +338,6 @@ def _main():
             messenger.succeeded(tracked_changes=instrument.tracked_changes)
         except BaseException as e:
             messenger.failed(exception=e, tracked_changes=instrument.tracked_changes)
-        finally:
-            if not ctx:
-                return
-            for session in set(mapi._session for mapi in ctx.model.registered.values() or []):
-                session.close()
 
 
 if __name__ == '__main__':

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/641cf81d/aria/storage/api.py
----------------------------------------------------------------------
diff --git a/aria/storage/api.py b/aria/storage/api.py
index 59ee2e8..e9f2917 100644
--- a/aria/storage/api.py
+++ b/aria/storage/api.py
@@ -19,6 +19,8 @@ from functools import partial
 
 
 class StorageAPI(object):
+    NO_INITIATOR = 'no_initiatior_func'
+
     """
     General storage Base API
     """
@@ -31,21 +33,29 @@ class StorageAPI(object):
         raise NotImplementedError('Subclass must implement abstract create method')
 
     @classmethod
-    def storage_initiator(cls, func=None):
-        if func is None:
+    def storage_initiator(cls, func=NO_INITIATOR):
+        if func is cls.NO_INITIATOR:
+            # Support for decoration syntax
             return partial(cls.storage_initiator, cls=cls)
+
+        if func is None:
+            # If func is None, no storage initiator was set, and nothing should happen.
+            return
+
+        if hasattr(cls, '_original_init') and cls._init_func.__func__ == func:
+            # setting the same function twice could cause a loop. we need to avoid that.
+            return
+
         cls._original_init = cls.__init__
         cls._init_func = func
         cls.__init__ = \
             lambda self, *args, **kwargs: cls._original_init(self, **func(cls=cls, *args, **kwargs))
 
     @classmethod
-    def free_storage_initiator(cls, safe=False):
-        if not hasattr(cls, '_original_init'):
-            if not safe:
-                raise Exception('No storage initiator was registered')
-        elif cls.__init__ != cls._original_init:
+    def free_storage_initiator(cls):
+        if hasattr(cls, '_original_init') and cls.__init__ != cls._original_init:
             cls.__init__ = cls._original_init
+            delattr(cls, '_original_init')
 
         if hasattr(cls, '_engine'):
             delattr(cls, '_engine')

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/641cf81d/tests/__init__.py
----------------------------------------------------------------------
diff --git a/tests/__init__.py b/tests/__init__.py
index d2858d2..e51dd69 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -15,4 +15,8 @@
 
 import os
 
+from aria.storage import sql_mapi
+
+sql_mapi.SQLAlchemyModelAPI.storage_initiator(sql_mapi.init_storage)
+
 ROOT_DIR = os.path.dirname(os.path.dirname(__file__))

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/641cf81d/tests/mock/context.py
----------------------------------------------------------------------
diff --git a/tests/mock/context.py b/tests/mock/context.py
index c5da063..d1fa5ed 100644
--- a/tests/mock/context.py
+++ b/tests/mock/context.py
@@ -25,7 +25,10 @@ from .topology import create_simple_topology_two_nodes
 def simple(tmpdir, model_driver_kwargs=None, resources_driver_kwargs=None, context_kwargs=None):
 
     model_storage = aria.application_model_storage(
-        sql_mapi.SQLAlchemyModelAPI, api_kwargs=model_driver_kwargs or {})
+        sql_mapi.SQLAlchemyModelAPI,
+        api_kwargs=model_driver_kwargs or {},
+        storage_initiator_func=sql_mapi.init_storage
+    )
 
     resource_storage = aria.application_resource_storage(
         FileSystemResourceAPI, api_kwargs=resources_driver_kwargs or dict(directory=str(tmpdir)))

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/641cf81d/tests/orchestrator/context/test_workflow.py
----------------------------------------------------------------------
diff --git a/tests/orchestrator/context/test_workflow.py b/tests/orchestrator/context/test_workflow.py
index 7ae7c1b..6d9ed39 100644
--- a/tests/orchestrator/context/test_workflow.py
+++ b/tests/orchestrator/context/test_workflow.py
@@ -19,7 +19,7 @@ import pytest
 
 from aria import application_model_storage
 from aria.orchestrator import context
-from aria.storage.sql_mapi import SQLAlchemyModelAPI
+from aria.storage import sql_mapi
 from tests import storage as test_storage
 from tests.mock import models
 
@@ -60,7 +60,8 @@ class TestWorkflowContext(object):
 
 @pytest.fixture(scope='function')
 def storage():
-    workflow_storage = application_model_storage(SQLAlchemyModelAPI)
+    workflow_storage = application_model_storage(sql_mapi.SQLAlchemyModelAPI,
+                                                 storage_initiator_func=sql_mapi.init_storage)
     workflow_storage.blueprint.put(models.get_blueprint())
     blueprint = workflow_storage.blueprint.get_by_name(models.BLUEPRINT_NAME)
     workflow_storage.deployment.put(models.get_deployment(blueprint))

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/641cf81d/tests/orchestrator/workflows/executor/test_process_executor.py
----------------------------------------------------------------------
diff --git a/tests/orchestrator/workflows/executor/test_process_executor.py b/tests/orchestrator/workflows/executor/test_process_executor.py
index c6a3fa1..ab47bca 100644
--- a/tests/orchestrator/workflows/executor/test_process_executor.py
+++ b/tests/orchestrator/workflows/executor/test_process_executor.py
@@ -24,7 +24,7 @@ import pytest
 from aria import application_model_storage
 from aria.storage import model as aria_model
 from aria.utils.plugin import create as create_plugin
-from aria.storage.sql_mapi import SQLAlchemyModelAPI
+from aria.storage import sql_mapi
 from aria.orchestrator import events
 from aria.orchestrator import plugin
 from aria.orchestrator.workflows.executor import process
@@ -74,7 +74,9 @@ class TestProcessExecutor(object):
 
 @pytest.fixture
 def model(tmpdir):
-    result = application_model_storage(SQLAlchemyModelAPI, api_kwargs=dict(base_dir=str(tmpdir)))
+    result = application_model_storage(sql_mapi.SQLAlchemyModelAPI,
+                                       api_kwargs=dict(base_dir=str(tmpdir)),
+                                       storage_initiator_func=sql_mapi.init_storage)
     yield result
     tests.storage.release_sqlite_storage(result)
 

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/641cf81d/tests/storage/__init__.py
----------------------------------------------------------------------
diff --git a/tests/storage/__init__.py b/tests/storage/__init__.py
index 589a01f..10c2ee2 100644
--- a/tests/storage/__init__.py
+++ b/tests/storage/__init__.py
@@ -62,7 +62,7 @@ def release_sqlite_storage(storage):
 
     if mapis:
         for mapi in mapis:
-            mapi.free_storage_initiator(safe=True)
+            mapi.free_storage_initiator()
         for session in set(mapi._session for mapi in mapis):
             session.rollback()
             session.close()

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/641cf81d/tests/storage/test_model_storage.py
----------------------------------------------------------------------
diff --git a/tests/storage/test_model_storage.py b/tests/storage/test_model_storage.py
index 02bcee7..3ba92fe 100644
--- a/tests/storage/test_model_storage.py
+++ b/tests/storage/test_model_storage.py
@@ -61,7 +61,8 @@ def test_model_storage(storage):
 
 
 def test_application_storage_factory():
-    storage = application_model_storage(sql_mapi.SQLAlchemyModelAPI)
+    storage = application_model_storage(sql_mapi.SQLAlchemyModelAPI,
+                                        storage_initiator_func=sql_mapi.init_storage)
     assert storage.node
     assert storage.node_instance
     assert storage.plugin

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/641cf81d/tests/storage/test_models.py
----------------------------------------------------------------------
diff --git a/tests/storage/test_models.py b/tests/storage/test_models.py
index a3fa623..c5b1c62 100644
--- a/tests/storage/test_models.py
+++ b/tests/storage/test_models.py
@@ -54,7 +54,8 @@ def sql_storage(storage_func):
 
 
 def _empty_storage():
-    return application_model_storage(sql_mapi.SQLAlchemyModelAPI)
+    return application_model_storage(sql_mapi.SQLAlchemyModelAPI,
+                                     storage_initiator_func=sql_mapi.init_storage)
 
 
 def _blueprint_storage():

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/641cf81d/tests/storage/test_structures.py
----------------------------------------------------------------------
diff --git a/tests/storage/test_structures.py b/tests/storage/test_structures.py
index 853a667..6caa4ff 100644
--- a/tests/storage/test_structures.py
+++ b/tests/storage/test_structures.py
@@ -50,7 +50,9 @@ def module_cleanup():
 
 @pytest.fixture
 def context(tmpdir):
-    return mock_context.simple(str(tmpdir))
+    ctx = mock_context.simple(str(tmpdir))
+    yield ctx
+    release_sqlite_storage(ctx.model)
 
 
 def test_inner_dict_update(storage):
@@ -175,7 +177,6 @@ def test_relationship_model_ordering(context):
         target_node_instance=target_node_instance,
     )
 
-
     context.model.node.put(new_node)
     context.model.node_instance.put(new_node_instance)
     context.model.relationship.put(source_to_new_relationship)