From 527c5898823398d3f8bd1306e4902214bfd3dbe7 Mon Sep 17 00:00:00 2001 From: Alex Shatov Date: Tue, 26 Sep 2017 16:58:37 -0400 Subject: UT 78% dcaepolicy plugin * unit test withcoverage of 78 % * minor refactoring for lazy init for discovery - useful for UT Change-Id: I7bee1d5dc5c66477c6e8f19484b0179d5f726fe0 Issue-Id: DCAEGEN2-62 Signed-off-by: Alex Shatov --- dcae-policy/tests/mock_cloudify_ctx.py | 146 +++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 dcae-policy/tests/mock_cloudify_ctx.py (limited to 'dcae-policy/tests/mock_cloudify_ctx.py') diff --git a/dcae-policy/tests/mock_cloudify_ctx.py b/dcae-policy/tests/mock_cloudify_ctx.py new file mode 100644 index 0000000..0c130c0 --- /dev/null +++ b/dcae-policy/tests/mock_cloudify_ctx.py @@ -0,0 +1,146 @@ + +# ============LICENSE_START======================================================= +# org.onap.dcae +# ================================================================================ +# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed 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. +# ============LICENSE_END========================================================= +# +# ECOMP is a trademark and service mark of AT&T Intellectual Property. + +from cloudify.mocks import MockCloudifyContext, MockNodeInstanceContext, MockNodeContext + +TARGET_NODE_ID = "target_node_id" +TARGET_NODE_NAME = "target_node_name" + +class MockContextNode(MockNodeContext): + """ctx.node with type and type_hierarchy""" + + def __init__(self, id=None, properties=None, node_type=None, type_hierarchy=None): + super(MockContextNode, self).__init__(id, properties or {}) + self._type = node_type + self._type_hierarchy = type_hierarchy or [self._type] + MockCloudifyContextFull.nodes[id] = self + + @property + def type(self): + """node type""" + return self._type + + @property + def type_hierarchy(self): + """node type hierarchy is a list of types""" + return self._type_hierarchy + +class MockContextNodeInstance(MockNodeInstanceContext): + """ctx.instance with relationships""" + + def __init__(self, id=None, runtime_properties=None, relationships=None): + super(MockContextNodeInstance, self).__init__(id, runtime_properties or {}) + self._relationships = [] + self.add_relationships(relationships) + MockCloudifyContextFull.instances[id] = self + + def add_relationships(self, relationships): + """add more relationships to the node instance""" + if not relationships: + return + if not self._relationships: + self._relationships = [] + self._relationships.extend([ + MockContextRelationship(relationship) + for relationship in (relationships or []) if TARGET_NODE_ID in relationship + ]) + + @property + def relationships(self): + """list of relationships to other node instances""" + return self._relationships + +class MockContextRelationshipTarget(object): + """target of relationship""" + def __init__(self, relationship): + target_node_name = relationship[TARGET_NODE_NAME] + target_node_id = relationship[TARGET_NODE_ID] + + self.node = MockCloudifyContextFull.nodes.get(target_node_name) + self.instance = MockCloudifyContextFull.instances.get(target_node_id) + + if not self.node: + self.node = MockContextNode(target_node_name) + if not self.instance: + self.instance = MockContextNodeInstance(target_node_id) + +class MockContextRelationship(object): + """item of ctx.instance.relationships""" + + def __init__(self, relationship): + self.target = MockContextRelationshipTarget(relationship) + self.type = relationship.get("type", "cloudify.relationships.depends_on") + self.type_hierarchy = relationship.get("type_hierarchy") or [self.type] + +class MockCloudifyContextFull(MockCloudifyContext): + """ + ctx1 = MockCloudifyContextFull(node_id='node_1', + node_name='my_1', properties={'foo': 'bar'}) + ctx2 = MockCloudifyContextFull(node_id='node_2', + node_name='my_2', + relationships=[{'target_node_id': 'node_1', + 'target_node_name': 'my_1'}]) + """ + nodes = {} + instances = {} + + def __init__(self, + node_id=None, + node_name=None, + blueprint_id=None, + deployment_id=None, + execution_id=None, + properties=None, node_type=None, type_hierarchy=None, + runtime_properties=None, + capabilities=None, + related=None, + source=None, + target=None, + operation=None, + resources=None, + provider_context=None, + bootstrap_context=None, + relationships=None): + super(MockCloudifyContextFull, self).__init__( + node_id=node_id, + node_name=node_name, + blueprint_id=blueprint_id, + deployment_id=deployment_id, + execution_id=execution_id, + properties=properties, + capabilities=capabilities, + related=related, + source=source, + target=target, + operation=operation, + resources=resources, + provider_context=provider_context, + bootstrap_context=bootstrap_context, + runtime_properties=runtime_properties + ) + self._node = MockContextNode(node_name, properties, node_type, type_hierarchy) + self._instance = MockContextNodeInstance(node_id, runtime_properties, relationships) + + @staticmethod + def clear(): + """clean up the context links""" + MockCloudifyContextFull.instances.clear() + MockCloudifyContextFull.nodes.clear() -- cgit 1.2.3-korg