1 # ================================================================================
2 # Copyright (c) 2017-2020 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=========================================================
18 """mock cloudify context with relationships and type_hierarchy"""
20 from cloudify.mocks import (MockCloudifyContext, MockNodeContext,
21 MockNodeInstanceContext)
23 TARGET_NODE_ID = "target_node_id"
24 TARGET_NODE_NAME = "target_node_name"
26 class MockContextNode(MockNodeContext):
27 """ctx.node with type and type_hierarchy"""
29 def __init__(self, id=None, properties=None, node_type=None, type_hierarchy=None):
30 super(MockContextNode, self).__init__(id, properties or {})
31 self._type = node_type
32 self._type_hierarchy = type_hierarchy or [self._type]
33 MockCloudifyContextFull.nodes[id] = self
41 def type_hierarchy(self):
42 """node type hierarchy is a list of types"""
43 return self._type_hierarchy
45 class MockContextNodeInstance(MockNodeInstanceContext):
46 """ctx.instance with relationships"""
48 def __init__(self, id=None, runtime_properties=None, relationships=None):
49 super(MockContextNodeInstance, self).__init__(id, runtime_properties or {})
50 self._relationships = []
51 self.add_relationships(relationships)
52 MockCloudifyContextFull.instances[id] = self
54 def add_relationships(self, relationships):
55 """add more relationships to the node instance"""
58 if not self._relationships:
59 self._relationships = []
60 self._relationships.extend([
61 MockContextRelationship(relationship)
62 for relationship in (relationships or []) if TARGET_NODE_ID in relationship
66 def relationships(self):
67 """list of relationships to other node instances"""
68 return self._relationships
70 class MockContextRelationshipTarget(object):
71 """target of relationship"""
72 def __init__(self, relationship):
73 target_node_name = relationship[TARGET_NODE_NAME]
74 target_node_id = relationship[TARGET_NODE_ID]
76 self.node = MockCloudifyContextFull.nodes.get(target_node_name)
77 self.instance = MockCloudifyContextFull.instances.get(target_node_id)
80 self.node = MockContextNode(target_node_name)
82 self.instance = MockContextNodeInstance(target_node_id)
84 class MockContextRelationship(object):
85 """item of ctx.instance.relationships"""
87 def __init__(self, relationship):
88 self.target = MockContextRelationshipTarget(relationship)
89 self.type = relationship.get("type", "cloudify.relationships.depends_on")
90 self.type_hierarchy = relationship.get("type_hierarchy") or [self.type]
92 class MockCloudifyContextFull(MockCloudifyContext):
94 ctx1 = MockCloudifyContextFull(node_id='node_1',
95 node_name='my_1', properties={'foo': 'bar'})
96 ctx2 = MockCloudifyContextFull(node_id='node_2',
98 relationships=[{'target_node_id': 'node_1',
99 'target_node_name': 'my_1'}])
110 properties=None, node_type=None, type_hierarchy=None,
111 runtime_properties=None,
118 provider_context=None,
119 bootstrap_context=None,
121 super(MockCloudifyContextFull, self).__init__(
124 blueprint_id=blueprint_id,
125 deployment_id=deployment_id,
126 execution_id=execution_id,
127 properties=properties,
128 capabilities=capabilities,
134 provider_context=provider_context,
135 bootstrap_context=bootstrap_context,
136 runtime_properties=runtime_properties
138 self._node = MockContextNode(node_name, properties, node_type, type_hierarchy)
139 self._instance = MockContextNodeInstance(node_id, runtime_properties, relationships)
143 """clean up the context links"""
144 MockCloudifyContextFull.instances.clear()
145 MockCloudifyContextFull.nodes.clear()