Repository: incubator-ariatosca Updated Branches: refs/heads/ARIA-262-Inconsistent-node-attributes-behavior cc729757a -> ea220b8dc (forced update)
fixed some bugs 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/ea220b8d Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/ea220b8d Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/ea220b8d Branch: refs/heads/ARIA-262-Inconsistent-node-attributes-behavior Commit: ea220b8dce5b3104b1d57d2fdec1abf84498f410 Parents: b3abdce Author: max-orlov <[email protected]> Authored: Tue Jun 6 12:01:28 2017 +0300 Committer: max-orlov <[email protected]> Committed: Tue Jun 6 12:12:22 2017 +0300 ---------------------------------------------------------------------- .../execution_plugin/ctx_proxy/server.py | 17 +-- aria/storage/collection_instrumentation.py | 5 +- .../context/test_collection_instrumentation.py | 134 ++++++++++++++----- .../execution_plugin/test_ctx_proxy_server.py | 2 +- 4 files changed, 113 insertions(+), 45 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/ea220b8d/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 102ff9a..50d4c3a 100644 --- a/aria/orchestrator/execution_plugin/ctx_proxy/server.py +++ b/aria/orchestrator/execution_plugin/ctx_proxy/server.py @@ -117,14 +117,15 @@ class CtxProxy(object): def _process(self, request): try: - typed_request = json.loads(request) - args = typed_request['args'] - payload = _process_ctx_request(self.ctx, args) - result_type = 'result' - if isinstance(payload, exceptions.ScriptException): - payload = dict(message=str(payload)) - result_type = 'stop_operation' - result = {'type': result_type, 'payload': payload} + with self.ctx.model.instrument(*self.ctx.INSTRUMENTATION_FIELDS): + typed_request = json.loads(request) + args = typed_request['args'] + payload = _process_ctx_request(self.ctx, args) + result_type = 'result' + if isinstance(payload, exceptions.ScriptException): + payload = dict(message=str(payload)) + result_type = 'stop_operation' + result = {'type': result_type, 'payload': payload} except Exception as e: traceback_out = StringIO.StringIO() traceback.print_exc(file=traceback_out) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/ea220b8d/aria/storage/collection_instrumentation.py ---------------------------------------------------------------------- diff --git a/aria/storage/collection_instrumentation.py b/aria/storage/collection_instrumentation.py index 6f02b34..27d8322 100644 --- a/aria/storage/collection_instrumentation.py +++ b/aria/storage/collection_instrumentation.py @@ -270,9 +270,8 @@ class _WrappedModel(object): if value.__class__ in (class_.class_ for class_ in self._instrumentation): return _create_instrumented_model( value, instrumentation=self._instrumentation, **self._kwargs) - elif getattr(value, 'metadata', True) == getattr(self._wrapped, 'metadata', False): - # Basically checks that the value is indeed an sqlmodel and the metadata of it, - # and the current wrapped model is the same + elif hasattr(value, 'metadata') or isinstance(value, (dict, list)): + # Basically checks that the value is indeed an sqlmodel (it should have metadata) return _create_wrapped_model( value, instrumentation=self._instrumentation, **self._kwargs) return value http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/ea220b8d/tests/orchestrator/context/test_collection_instrumentation.py ---------------------------------------------------------------------- diff --git a/tests/orchestrator/context/test_collection_instrumentation.py b/tests/orchestrator/context/test_collection_instrumentation.py index 4afc737..ae3e8ac 100644 --- a/tests/orchestrator/context/test_collection_instrumentation.py +++ b/tests/orchestrator/context/test_collection_instrumentation.py @@ -15,8 +15,14 @@ import pytest -from aria.modeling.models import Attribute +from aria.modeling import models from aria.storage import collection_instrumentation +from aria.orchestrator.context import operation + +from tests import ( + mock, + storage +) class MockActor(object): @@ -49,11 +55,11 @@ class CollectionInstrumentation(object): @pytest.fixture def dict_(self, actor, model): - return collection_instrumentation._InstrumentedDict(model, actor, 'dict_', Attribute) + return collection_instrumentation._InstrumentedDict(model, actor, 'dict_', models.Attribute) @pytest.fixture def list_(self, actor, model): - return collection_instrumentation._InstrumentedList(model, actor, 'list_', Attribute) + return collection_instrumentation._InstrumentedList(model, actor, 'list_', models.Attribute) class TestDict(CollectionInstrumentation): @@ -61,16 +67,16 @@ class TestDict(CollectionInstrumentation): def test_keys(self, actor, dict_): dict_.update( { - 'key1': Attribute.wrap('key1', 'value1'), - 'key2': Attribute.wrap('key2', 'value2') + 'key1': models.Attribute.wrap('key1', 'value1'), + 'key2': models.Attribute.wrap('key2', 'value2') } ) assert sorted(dict_.keys()) == sorted(['key1', 'key2']) == sorted(actor.dict_.keys()) def test_values(self, actor, dict_): dict_.update({ - 'key1': Attribute.wrap('key1', 'value1'), - 'key2': Attribute.wrap('key1', 'value2') + 'key1': models.Attribute.wrap('key1', 'value1'), + 'key2': models.Attribute.wrap('key1', 'value2') }) assert (sorted(dict_.values()) == sorted(['value1', 'value2']) == @@ -78,34 +84,34 @@ class TestDict(CollectionInstrumentation): def test_items(self, dict_): dict_.update({ - 'key1': Attribute.wrap('key1', 'value1'), - 'key2': Attribute.wrap('key1', 'value2') + 'key1': models.Attribute.wrap('key1', 'value1'), + 'key2': models.Attribute.wrap('key1', 'value2') }) assert sorted(dict_.items()) == sorted([('key1', 'value1'), ('key2', 'value2')]) def test_iter(self, actor, dict_): dict_.update({ - 'key1': Attribute.wrap('key1', 'value1'), - 'key2': Attribute.wrap('key1', 'value2') + 'key1': models.Attribute.wrap('key1', 'value1'), + 'key2': models.Attribute.wrap('key1', 'value2') }) assert sorted(list(dict_)) == sorted(['key1', 'key2']) == sorted(actor.dict_.keys()) def test_bool(self, dict_): assert not dict_ dict_.update({ - 'key1': Attribute.wrap('key1', 'value1'), - 'key2': Attribute.wrap('key1', 'value2') + 'key1': models.Attribute.wrap('key1', 'value1'), + 'key2': models.Attribute.wrap('key1', 'value2') }) assert dict_ def test_set_item(self, actor, dict_): - dict_['key1'] = Attribute.wrap('key1', 'value1') + dict_['key1'] = models.Attribute.wrap('key1', 'value1') assert dict_['key1'] == 'value1' == actor.dict_['key1'].value - assert isinstance(actor.dict_['key1'], Attribute) + assert isinstance(actor.dict_['key1'], models.Attribute) def test_nested(self, actor, dict_): dict_['key'] = {} - assert isinstance(actor.dict_['key'], Attribute) + assert isinstance(actor.dict_['key'], models.Attribute) assert dict_['key'] == actor.dict_['key'].value == {} dict_['key']['inner_key'] = 'value' @@ -116,7 +122,7 @@ class TestDict(CollectionInstrumentation): assert dict_['key'].keys() == ['inner_key'] assert dict_['key'].values() == ['value'] assert dict_['key'].items() == [('inner_key', 'value')] - assert isinstance(actor.dict_['key'], Attribute) + assert isinstance(actor.dict_['key'], models.Attribute) assert isinstance(dict_['key'], collection_instrumentation._InstrumentedDict) dict_['key'].update({'updated_key': 'updated_value'}) @@ -127,7 +133,7 @@ class TestDict(CollectionInstrumentation): assert sorted(dict_['key'].values()) == sorted(['value', 'updated_value']) assert sorted(dict_['key'].items()) == sorted([('inner_key', 'value'), ('updated_key', 'updated_value')]) - assert isinstance(actor.dict_['key'], Attribute) + assert isinstance(actor.dict_['key'], models.Attribute) assert isinstance(dict_['key'], collection_instrumentation._InstrumentedDict) dict_.update({'key': 'override_value'}) @@ -135,12 +141,12 @@ class TestDict(CollectionInstrumentation): assert 'key' in dict_ assert dict_['key'] == 'override_value' assert len(actor.dict_) == 1 - assert isinstance(actor.dict_['key'], Attribute) + assert isinstance(actor.dict_['key'], models.Attribute) assert actor.dict_['key'].value == 'override_value' def test_get_item(self, actor, dict_): - dict_['key1'] = Attribute.wrap('key1', 'value1') - assert isinstance(actor.dict_['key1'], Attribute) + dict_['key1'] = models.Attribute.wrap('key1', 'value1') + assert isinstance(actor.dict_['key1'], models.Attribute) def test_update(self, actor, dict_): dict_['key1'] = 'value1' @@ -149,7 +155,7 @@ class TestDict(CollectionInstrumentation): dict_.update(new_dict) assert len(dict_) == 2 assert dict_['key2'] == 'value2' - assert isinstance(actor.dict_['key2'], Attribute) + assert isinstance(actor.dict_['key2'], models.Attribute) new_dict = {} new_dict.update(dict_) @@ -176,20 +182,20 @@ class TestDict(CollectionInstrumentation): class TestList(CollectionInstrumentation): def test_append(self, actor, list_): - list_.append(Attribute.wrap('name', 'value1')) + list_.append(models.Attribute.wrap('name', 'value1')) list_.append('value2') assert len(actor.list_) == 2 assert len(list_) == 2 - assert isinstance(actor.list_[0], Attribute) + assert isinstance(actor.list_[0], models.Attribute) assert list_[0] == 'value1' - assert isinstance(actor.list_[1], Attribute) + assert isinstance(actor.list_[1], models.Attribute) assert list_[1] == 'value2' list_[0] = 'new_value1' list_[1] = 'new_value2' - assert isinstance(actor.list_[1], Attribute) - assert isinstance(actor.list_[1], Attribute) + assert isinstance(actor.list_[1], models.Attribute) + assert isinstance(actor.list_[1], models.Attribute) assert list_[0] == 'new_value1' assert list_[1] == 'new_value2' @@ -218,12 +224,12 @@ class TestList(CollectionInstrumentation): list_.append([]) list_[0].append('inner_item') - assert isinstance(actor.list_[0], Attribute) + assert isinstance(actor.list_[0], models.Attribute) assert len(list_) == 1 assert list_[0][0] == 'inner_item' list_[0].append('new_item') - assert isinstance(actor.list_[0], Attribute) + assert isinstance(actor.list_[0], models.Attribute) assert len(list_) == 1 assert list_[0][1] == 'new_item' @@ -235,23 +241,85 @@ class TestDictList(CollectionInstrumentation): def test_dict_in_list(self, actor, list_): list_.append({}) assert len(list_) == 1 - assert isinstance(actor.list_[0], Attribute) + assert isinstance(actor.list_[0], models.Attribute) assert actor.list_[0].value == {} list_[0]['key'] = 'value' assert list_[0]['key'] == 'value' assert len(actor.list_) == 1 - assert isinstance(actor.list_[0], Attribute) + assert isinstance(actor.list_[0], models.Attribute) assert actor.list_[0].value['key'] == 'value' def test_list_in_dict(self, actor, dict_): dict_['key'] = [] assert len(dict_) == 1 - assert isinstance(actor.dict_['key'], Attribute) + assert isinstance(actor.dict_['key'], models.Attribute) assert actor.dict_['key'].value == [] dict_['key'].append('value') assert dict_['key'][0] == 'value' assert len(actor.dict_) == 1 - assert isinstance(actor.dict_['key'], Attribute) + assert isinstance(actor.dict_['key'], models.Attribute) assert actor.dict_['key'].value[0] == 'value' + + +class TestModelInstrumentation(object): + + @pytest.fixture + def workflow_ctx(self, tmpdir): + context = mock.context.simple(str(tmpdir), inmemory=True) + yield context + storage.release_sqlite_storage(context.model) + + def test_attributes_access(self, workflow_ctx): + node = workflow_ctx.model.node.list()[0] + task = models.Task(node=node) + workflow_ctx.model.task.put(task) + + ctx = operation.NodeOperationContext( + task.id, node.id, name='', service_id=workflow_ctx.model.service.list()[0].id, + model_storage=workflow_ctx.model, resource_storage=workflow_ctx.resource, + execution_id=1) + + def _run_assertions(is_under_ctx): + def ctx_assert(expr): + if is_under_ctx: + assert expr + else: + assert not expr + + ctx_assert(isinstance(ctx.node.attributes, + collection_instrumentation._InstrumentedDict)) + assert not isinstance(ctx.node.properties, + collection_instrumentation._InstrumentedCollection) + + for rel in ctx.node.inbound_relationships: + ctx_assert(isinstance(rel, collection_instrumentation._WrappedModel)) + ctx_assert(isinstance(rel.source_node.attributes, + collection_instrumentation._InstrumentedDict)) + ctx_assert(isinstance(rel.target_node.attributes, + collection_instrumentation._InstrumentedDict)) + + for node in ctx.model.node: + ctx_assert(isinstance(node.attributes, + collection_instrumentation._InstrumentedDict)) + assert not isinstance(node.properties, + collection_instrumentation._InstrumentedCollection) + + for rel in ctx.model.relationship: + ctx_assert(isinstance(rel, collection_instrumentation._WrappedModel)) + + ctx_assert(isinstance(rel.source_node.attributes, + collection_instrumentation._InstrumentedDict)) + ctx_assert(isinstance(rel.target_node.attributes, + collection_instrumentation._InstrumentedDict)) + + assert not isinstance(rel.source_node.properties, + collection_instrumentation._InstrumentedCollection) + assert not isinstance(rel.target_node.properties, + collection_instrumentation._InstrumentedCollection) + + with ctx.model.instrument(models.Node.attributes): + _run_assertions(True) + + _run_assertions(False) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/ea220b8d/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 1b19fd9..7ab1bdb 100644 --- a/tests/orchestrator/execution_plugin/test_ctx_proxy_server.py +++ b/tests/orchestrator/execution_plugin/test_ctx_proxy_server.py @@ -138,7 +138,7 @@ class TestCtxProxy(object): @pytest.fixture def ctx(self, mocker): class MockCtx(object): - pass + INSTRUMENTATION_FIELDS = () ctx = MockCtx() properties = { 'prop1': 'value1',
