Repository: incubator-ariatosca Updated Branches: refs/heads/ARIA-324-refactor-ctx-access 5b95c26fd -> 1baaedf5c (forced update)
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/1baaedf5 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/1baaedf5 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/1baaedf5 Branch: refs/heads/ARIA-324-refactor-ctx-access Commit: 1baaedf5c42fc66ba44e360854456c7e64002b4a Parents: 607f1dd Author: Tal Liron <tal.li...@gmail.com> Authored: Mon Aug 7 12:05:34 2017 -0500 Committer: Tal Liron <tal.li...@gmail.com> Committed: Mon Aug 7 15:12:59 2017 -0500 ---------------------------------------------------------------------- .../execution_plugin/ctx_proxy/server.py | 44 ++++++++++---------- .../execution_plugin/test_ctx_proxy_server.py | 22 +++++++--- tests/requirements.txt | 6 +-- tox.ini | 8 ++-- 4 files changed, 47 insertions(+), 33 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/1baaedf5/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/1baaedf5/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 http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/1baaedf5/tests/requirements.txt ---------------------------------------------------------------------- diff --git a/tests/requirements.txt b/tests/requirements.txt index d86750b..56a7bf5 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -16,7 +16,7 @@ sh==1.12.14 psutil==5.2.2 mock==2.0.0 pylint==1.6.5 -pytest==3.1.3 +pytest==3.2.0 pytest-cov==2.5.1 -pytest-mock==1.6.0 -pytest-xdist==1.18.1 +pytest-mock==1.6.2 +pytest-xdist==1.18.2 http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/1baaedf5/tox.ini ---------------------------------------------------------------------- diff --git a/tox.ini b/tox.ini index 129c557..ff71e05 100644 --- a/tox.ini +++ b/tox.ini @@ -97,14 +97,14 @@ commands= [testenv:pylint_code] commands= pylint aria extensions/aria_extension_tosca/ \ - --rcfile=aria/.pylintrc \ - --disable=fixme,missing-docstring + --rcfile=aria/.pylintrc \ + --disable=fixme,missing-docstring [testenv:pylint_tests] commands= pylint tests \ - --rcfile=tests/.pylintrc \ - --disable=fixme,missing-docstring + --rcfile=tests/.pylintrc \ + --disable=fixme,missing-docstring [testenv:docs] install_command=