1 # ================================================================================
2 # Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
3 # ================================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # ============LICENSE_END=========================================================
17 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
19 """mock cloudify context with relationships and type_hierarchy"""
21 from cloudify.mocks import (MockCloudifyContext, MockNodeContext,
22 MockNodeInstanceContext)
24 TARGET_NODE_ID = "target_node_id"
25 TARGET_NODE_NAME = "target_node_name"
27 class MockContextNode(MockNodeContext):
28 """ctx.node with type and type_hierarchy"""
30 def __init__(self, id=None, properties=None, node_type=None, type_hierarchy=None):
31 super(MockContextNode, self).__init__(id, properties or {})
32 self._type = node_type
33 self._type_hierarchy = type_hierarchy or [self._type]
34 MockCloudifyContextFull.nodes[id] = self
42 def type_hierarchy(self):
43 """node type hierarchy is a list of types"""
44 return self._type_hierarchy
46 class MockContextNodeInstance(MockNodeInstanceContext):
47 """ctx.instance with relationships"""
49 def __init__(self, id=None, runtime_properties=None, relationships=None):
50 super(MockContextNodeInstance, self).__init__(id, runtime_properties or {})
51 self._relationships = []
52 self.add_relationships(relationships)
53 MockCloudifyContextFull.instances[id] = self
55 def add_relationships(self, relationships):
56 """add more relationships to the node instance"""
59 if not self._relationships:
60 self._relationships = []
61 self._relationships.extend([
62 MockContextRelationship(relationship)
63 for relationship in (relationships or []) if TARGET_NODE_ID in relationship
67 def relationships(self):
68 """list of relationships to other node instances"""
69 return self._relationships
71 class MockContextRelationshipTarget(object):
72 """target of relationship"""
73 def __init__(self, relationship):
74 target_node_name = relationship[TARGET_NODE_NAME]
75 target_node_id = relationship[TARGET_NODE_ID]
77 self.node = MockCloudifyContextFull.nodes.get(target_node_name)
78 self.instance = MockCloudifyContextFull.instances.get(target_node_id)
81 self.node = MockContextNode(target_node_name)
83 self.instance = MockContextNodeInstance(target_node_id)
85 class MockContextRelationship(object):
86 """item of ctx.instance.relationships"""
88 def __init__(self, relationship):
89 self.target = MockContextRelationshipTarget(relationship)
90 self.type = relationship.get("type", "cloudify.relationships.depends_on")
91 self.type_hierarchy = relationship.get("type_hierarchy") or [self.type]
93 class MockCloudifyContextFull(MockCloudifyContext):
95 ctx1 = MockCloudifyContextFull(node_id='node_1',
96 node_name='my_1', properties={'foo': 'bar'})
97 ctx2 = MockCloudifyContextFull(node_id='node_2',
99 relationships=[{'target_node_id': 'node_1',
100 'target_node_name': 'my_1'}])
111 properties=None, node_type=None, type_hierarchy=None,
112 runtime_properties=None,
119 provider_context=None,
120 bootstrap_context=None,
122 super(MockCloudifyContextFull, self).__init__(
125 blueprint_id=blueprint_id,
126 deployment_id=deployment_id,
127 execution_id=execution_id,
128 properties=properties,
129 capabilities=capabilities,
135 provider_context=provider_context,
136 bootstrap_context=bootstrap_context,
137 runtime_properties=runtime_properties
139 self._node = MockContextNode(node_name, properties, node_type, type_hierarchy)
140 self._instance = MockContextNodeInstance(node_id, runtime_properties, relationships)
144 """clean up the context links"""
145 MockCloudifyContextFull.instances.clear()
146 MockCloudifyContextFull.nodes.clear()