ariatosca-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From emblempar...@apache.org
Subject incubator-ariatosca git commit: Fixes, cleanups, more tests
Date Mon, 07 Aug 2017 17:05:58 GMT
Repository: incubator-ariatosca
Updated Branches:
  refs/heads/ARIA-324-refactor-ctx-access 607f1dd1b -> 5b95c26fd


Fixes, cleanups, more tests


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

Branch: refs/heads/ARIA-324-refactor-ctx-access
Commit: 5b95c26fdaf7784808dad52036c0cb66a0397a4f
Parents: 607f1dd
Author: Tal Liron <tal.liron@gmail.com>
Authored: Mon Aug 7 12:05:34 2017 -0500
Committer: Tal Liron <tal.liron@gmail.com>
Committed: Mon Aug 7 12:05:34 2017 -0500

----------------------------------------------------------------------
 .../execution_plugin/ctx_proxy/server.py        | 44 ++++++++++----------
 .../execution_plugin/test_ctx_proxy_server.py   | 22 +++++++---
 2 files changed, 40 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/5b95c26f/aria/orchestrator/execution_plugin/ctx_proxy/server.py
----------------------------------------------------------------------
diff --git a/aria/orchestrator/execution_plugin/ctx_proxy/server.py b/aria/orchestrator/execution_plugin/ctx_proxy/server.py
index b248a5d..5f4b72b 100644
--- a/aria/orchestrator/execution_plugin/ctx_proxy/server.py
+++ b/aria/orchestrator/execution_plugin/ctx_proxy/server.py
@@ -121,7 +121,7 @@ class CtxProxy(object):
     def _process(self, request):
         try:
             with self.ctx.model.instrument(*self.ctx.INSTRUMENTATION_FIELDS):
-                payload = _parse_request(self.ctx, request)
+                payload = _process_request(self.ctx, request)
                 result_type = 'result'
                 if isinstance(payload, exceptions.ScriptException):
                     payload = dict(message=str(payload))
@@ -146,41 +146,44 @@ class CtxProxy(object):
         self.close()
 
 
-class ProcessingError(RuntimeError):
+class CtxError(RuntimeError):
     pass
 
 
-class ParsingError(ProcessingError):
+class CtxParsingError(CtxError):
     pass
 
 
-def _parse_request(ctx, request):
+def _process_request(ctx, request):
     request = json.loads(request)
     args = request['args']
-    return _parse_arguments(ctx, args)
+    return _process_arguments(ctx, args)
 
 
-def _parse_arguments(obj, args):
+def _process_arguments(obj, args):
     # Modifying?
     try:
         # TODO: should there be a way to escape "=" in case it is needed as real argument?
         equals_index = args.index('=') # raises ValueError if not found
+    except ValueError:
+        equals_index = None
+    if equals_index is not None:
         if equals_index == 0:
-            raise ParsingError('The "=" argument cannot be first')
-        if equals_index != len(args) - 2:
-            raise ParsingError('The "=" argument must be penultimate')
+            raise CtxParsingError('The "=" argument cannot be first')
+        elif equals_index != len(args) - 2:
+            raise CtxParsingError('The "=" argument must be penultimate')
         modifying = True
         modifying_key = args[-3]
         modifying_value = args[-1]
         args = args[:-3]
-    except ValueError:
+    else:
         modifying = False
         modifying_key = None
         modifying_value = None
 
     # Parse all arguments
     while len(args) > 0:
-        obj, args = _parse_argument(obj, args, modifying)
+        obj, args = _process_next_operation(obj, args, modifying)
 
     if modifying:
         if hasattr(obj, '__setitem__'):
@@ -192,13 +195,12 @@ def _parse_arguments(obj, args):
             # Modify object attribute
             setattr(obj, modifying_key, modifying_value)
         else:
-            raise ProcessingError('Cannot modify `{0}` of `{1!r}`'
-                                  .format(modifying_key, obj))
+            raise CtxError('Cannot modify `{0}` of `{1!r}`'.format(modifying_key, obj))
 
     return obj
 
 
-def _parse_argument(obj, args, modifying):
+def _process_next_operation(obj, args, modifying):
     args = list(args)
     arg = args.pop(0)
 
@@ -207,22 +209,22 @@ def _parse_argument(obj, args, modifying):
         # TODO: should there be a way to escape "[" and "]" in case they are needed as real
         # arguments?
         try:
-            closing_index = args.index(']')
+            closing_index = args.index(']') # raises ValueError if not found
         except ValueError:
-            raise ParsingError('Opening "[" without a closing "]')
+            raise CtxParsingError('Opening "[" without a closing "]')
         callable_args = args[:closing_index]
         args = args[closing_index + 1:]
         if not callable(obj):
-            raise ProcessingError('Used "[" and "] on an object that is not callable')
+            raise CtxError('Used "[" and "] on an object that is not callable')
         return obj(*callable_args), args
 
     # Attribute?
     if isinstance(arg, basestring):
         if hasattr(obj, arg):
             return getattr(obj, arg), args
-        arg_sugared = arg.replace('-', '_')
-        if hasattr(obj, arg_sugared):
-            return getattr(obj, arg_sugared), args
+        token_sugared = arg.replace('-', '_')
+        if hasattr(obj, token_sugared):
+            return getattr(obj, token_sugared), args
 
     # Item? (dict, lists, and similar)
     if hasattr(obj, '__getitem__'):
@@ -230,7 +232,7 @@ def _parse_argument(obj, args, modifying):
             obj[arg] = {}
         return obj[arg], args
 
-    raise ParsingError('Cannot parse argument: `{0!r}`'.format(arg))
+    raise CtxParsingError('Cannot parse argument: `{0!r}`'.format(arg))
 
 
 def _get_unused_port():

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/5b95c26f/tests/orchestrator/execution_plugin/test_ctx_proxy_server.py
----------------------------------------------------------------------
diff --git a/tests/orchestrator/execution_plugin/test_ctx_proxy_server.py b/tests/orchestrator/execution_plugin/test_ctx_proxy_server.py
index 4b92787..94b7409 100644
--- a/tests/orchestrator/execution_plugin/test_ctx_proxy_server.py
+++ b/tests/orchestrator/execution_plugin/test_ctx_proxy_server.py
@@ -73,6 +73,15 @@ class TestCtxProxy(object):
         response = self.request(server, 'stub-method', '[', ']')
         assert response == []
 
+    def test_method_return_value(self, server, ctx):
+        response_args = self.request(server, 'node', 'get_prop', '[', 'prop2', ']', 'nested_prop1')
+        assert response_args == 'nested_value1'
+
+    def test_method_return_value_set(self, server, ctx):
+        self.request(
+            server, 'node', 'get_prop', '[', 'prop2', ']', 'nested_prop1', '=', 'new_value')
+        assert ctx.node.properties['prop2']['nested_prop1'] == 'new_value'
+
     def test_empty_return_value(self, server):
         response = self.request(server, 'stub_none')
         assert response is None
@@ -103,6 +112,9 @@ class TestCtxProxy(object):
         def __init__(self, properties):
             self.properties = properties
 
+        def get_prop(self, name):
+            return self.properties[name]
+
     @staticmethod
     def stub_method(*args):
         return args
@@ -141,11 +153,11 @@ class TestCtxProxy(object):
             }
         }
         ctx.stub_none = None
-        ctx.stub_method = self.stub_method
-        ctx.stub_sleep = self.stub_sleep
-        ctx.stub_args = self.stub_args
-        ctx.stub_attr = self.StubAttribute()
-        ctx.node = self.NodeAttribute(properties)
+        ctx.stub_method = TestCtxProxy.stub_method
+        ctx.stub_sleep = TestCtxProxy.stub_sleep
+        ctx.stub_args = TestCtxProxy.stub_args
+        ctx.stub_attr = TestCtxProxy.StubAttribute()
+        ctx.node = TestCtxProxy.NodeAttribute(properties)
         ctx.model = mocker.MagicMock()
         return ctx
 


Mime
View raw message