2 from neco.execution.attribute import Attribute
3 from neco.execution.ec import ExperimentController
4 from neco.execution.resource import ResourceManager, ResourceState, clsinit
10 class MyResource(ResourceManager):
14 def _register_attributes(cls):
15 cool_attr = Attribute("my_attr", "is a really nice attribute!")
16 cls._register_attribute(cool_attr)
18 def __init__(self, ec, guid):
19 super(MyResource, self).__init__(ec, guid)
22 class AnotherResource(ResourceManager):
23 _rtype = "AnotherResource"
25 def __init__(self, ec, guid):
26 super(AnotherResource, self).__init__(ec, guid)
28 class ResourceFactoryTestCase(unittest.TestCase):
29 def test_add_resource_factory(self):
30 from neco.execution.resource import ResourceFactory
32 ResourceFactory.register_type(MyResource)
33 ResourceFactory.register_type(AnotherResource)
35 self.assertEquals(MyResource.rtype(), "MyResource")
36 self.assertEquals(len(MyResource._attributes), 1)
38 self.assertEquals(ResourceManager.rtype(), "Resource")
39 self.assertEquals(len(ResourceManager._attributes), 0)
41 self.assertEquals(AnotherResource.rtype(), "AnotherResource")
42 self.assertEquals(len(AnotherResource._attributes), 0)
44 self.assertEquals(len(ResourceFactory.resource_types()), 2)
46 class Channel(ResourceManager):
49 def __init__(self, ec, guid):
50 super(Channel, self).__init__(ec, guid)
54 super(Channel, self).deploy()
55 self.logger.debug(" -------- DEPLOYED ------- ")
57 class Interface(ResourceManager):
60 def __init__(self, ec, guid):
61 super(Interface, self).__init__(ec, guid)
64 node = self.get_connected(Node.rtype())[0]
65 chan = self.get_connected(Channel.rtype())[0]
67 if node.state < ResourceState.PROVISIONED:
68 self.ec.schedule("0.5s", self.deploy)
69 elif chan.state < ResourceState.READY:
70 self.ec.schedule("0.5s", self.deploy)
73 super(Interface, self).deploy()
74 self.logger.debug(" -------- DEPLOYED ------- ")
76 class Node(ResourceManager):
79 def __init__(self, ec, guid):
80 super(Node, self).__init__(ec, guid)
83 if self.state == ResourceState.NEW:
86 self.logger.debug(" -------- PROVISIONED ------- ")
87 self.ec.schedule("3s", self.deploy)
88 elif self.state == ResourceState.PROVISIONED:
89 ifaces = self.get_connected(Interface.rtype())
91 if rm.state < ResourceState.READY:
92 self.ec.schedule("0.5s", self.deploy)
95 super(Node, self).deploy()
96 self.logger.debug(" -------- DEPLOYED ------- ")
98 class Application(ResourceManager):
99 _rtype = "Application"
101 def __init__(self, ec, guid):
102 super(Application, self).__init__(ec, guid)
105 node = self.get_connected(Node.rtype())[0]
106 if node.state < ResourceState.READY:
107 self.ec.schedule("0.5s", self.deploy)
109 super(Application, self).deploy()
110 self.logger.debug(" -------- DEPLOYED ------- ")
112 class ResourceManagerTestCase(unittest.TestCase):
113 def test_deploy_in_order(self):
115 Test scenario: 2 applications running one on 1 node each.
116 Nodes are connected to Interfaces which are connected
117 through a channel between them.
119 - Application needs to wait until Node is ready to be ready
120 - Node needs to wait until Interface is ready to be ready
121 - Interface needs to wait until Node is provisioned to be ready
122 - Interface needs to wait until Channel is ready to be ready
123 - The channel doesn't wait for any other resource to be ready
126 from neco.execution.resource import ResourceFactory
128 ResourceFactory.register_type(Application)
129 ResourceFactory.register_type(Node)
130 ResourceFactory.register_type(Interface)
131 ResourceFactory.register_type(Channel)
133 ec = ExperimentController()
135 app1 = ec.register_resource("Application")
136 app2 = ec.register_resource("Application")
137 node1 = ec.register_resource("Node")
138 node2 = ec.register_resource("Node")
139 iface1 = ec.register_resource("Interface")
140 iface2 = ec.register_resource("Interface")
141 chan = ec.register_resource("Channel")
143 ec.register_connection(app1, node1)
144 ec.register_connection(app2, node2)
145 ec.register_connection(iface1, node1)
146 ec.register_connection(iface2, node2)
147 ec.register_connection(iface1, chan)
148 ec.register_connection(iface2, chan)
153 while not all([ ec.state(guid) == ResourceState.STARTED \
154 for guid in [app1, app2, node1, node2, iface1, iface2, chan]]):
160 rmapp1 = ec.get_resource(app1)
161 rmapp2 = ec.get_resource(app2)
162 rmnode1 = ec.get_resource(node1)
163 rmnode2 = ec.get_resource(node2)
164 rmiface1 = ec.get_resource(iface1)
165 rmiface2 = ec.get_resource(iface2)
166 rmchan = ec.get_resource(chan)
168 ## Validate deploy order
169 # - Application needs to wait until Node is ready to be ready
170 self.assertTrue(rmnode1.ready_time < rmapp1.ready_time)
171 self.assertTrue(rmnode2.ready_time < rmapp2.ready_time)
173 # - Node needs to wait until Interface is ready to be ready
174 self.assertTrue(rmnode1.ready_time > rmiface1.ready_time)
175 self.assertTrue(rmnode2.ready_time > rmiface2.ready_time)
177 # - Interface needs to wait until Node is provisioned to be ready
178 self.assertTrue(rmnode1.provision_time < rmiface1.ready_time)
179 self.assertTrue(rmnode2.provision_time < rmiface2.ready_time)
181 # - Interface needs to wait until Channel is ready to be ready
182 self.assertTrue(rmchan.ready_time < rmiface1.ready_time)
183 self.assertTrue(rmchan.ready_time < rmiface2.ready_time)
185 def test_start_with_condition(self):
189 def test_stop_with_condition(self):
193 def test_set_with_condition(self):
198 if __name__ == '__main__':