Adding trace Collector RM
[nepi.git] / test / execution / resource.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20
21
22 from nepi.execution.attribute import Attribute
23 from nepi.execution.ec import ExperimentController 
24 from nepi.execution.resource import ResourceManager, ResourceState, clsinit, \
25         ResourceAction
26
27 import random
28 import time
29 import unittest
30
31 @clsinit
32 class MyResource(ResourceManager):
33     _rtype = "MyResource"
34
35     @classmethod
36     def _register_attributes(cls):
37         cool_attr = Attribute("my_attr", "is a really nice attribute!")
38         cls._register_attribute(cool_attr)
39
40     def __init__(self, ec, guid):
41         super(MyResource, self).__init__(ec, guid)
42
43 @clsinit
44 class AnotherResource(ResourceManager):
45     _rtype = "AnotherResource"
46
47     def __init__(self, ec, guid):
48         super(AnotherResource, self).__init__(ec, guid)
49
50
51 class Channel(ResourceManager):
52     _rtype = "Channel"
53
54     def __init__(self, ec, guid):
55         super(Channel, self).__init__(ec, guid)
56
57     def deploy(self):
58         time.sleep(1)
59         super(Channel, self).deploy()
60         self.logger.debug(" -------- DEPLOYED ------- ")
61        
62 class Interface(ResourceManager):
63     _rtype = "Interface"
64
65     def __init__(self, ec, guid):
66         super(Interface, self).__init__(ec, guid)
67
68     def deploy(self):
69         node = self.get_connected(Node.rtype())[0]
70         chan = self.get_connected(Channel.rtype())[0]
71
72         if node.state < ResourceState.PROVISIONED:
73             self.ec.schedule("0.5s", self.deploy)
74         elif chan.state < ResourceState.READY:
75             self.ec.schedule("0.5s", self.deploy)
76         else:
77             time.sleep(2)
78             super(Interface, self).deploy()
79             self.logger.debug(" -------- DEPLOYED ------- ")
80
81 class Node(ResourceManager):
82     _rtype = "Node"
83
84     def __init__(self, ec, guid):
85         super(Node, self).__init__(ec, guid)
86
87     def deploy(self):
88         if self.state == ResourceState.NEW:
89             self.discover()
90             self.provision()
91             self.logger.debug(" -------- PROVISIONED ------- ")
92             self.ec.schedule("3s", self.deploy)
93         elif self.state == ResourceState.PROVISIONED:
94             ifaces = self.get_connected(Interface.rtype())
95             for rm in ifaces:
96                 if rm.state < ResourceState.READY:
97                     self.ec.schedule("0.5s", self.deploy)
98                     return 
99
100             super(Node, self).deploy()
101             self.logger.debug(" -------- DEPLOYED ------- ")
102
103 class Application(ResourceManager):
104     _rtype = "Application"
105
106     def __init__(self, ec, guid):
107         super(Application, self).__init__(ec, guid)
108
109     def deploy(self):
110         node = self.get_connected(Node.rtype())[0]
111         if node.state < ResourceState.READY:
112             self.ec.schedule("0.5s", self.deploy)
113         else:
114             time.sleep(random.random() * 5)
115             super(Application, self).deploy()
116             self.logger.debug(" -------- DEPLOYED ------- ")
117
118     def start(self):
119         super(Application, self).start()
120         time.sleep(random.random() * 5)
121         self._state = ResourceState.FINISHED
122    
123
124 class ResourceFactoryTestCase(unittest.TestCase):
125     def test_add_resource_factory(self):
126         from nepi.execution.resource import ResourceFactory
127
128         ResourceFactory.register_type(MyResource)
129         ResourceFactory.register_type(AnotherResource)
130
131         self.assertEquals(MyResource.rtype(), "MyResource")
132         self.assertEquals(len(MyResource._attributes), 1)
133
134         self.assertEquals(ResourceManager.rtype(), "Resource")
135         self.assertEquals(len(ResourceManager._attributes), 0)
136
137         self.assertEquals(AnotherResource.rtype(), "AnotherResource")
138         self.assertEquals(len(AnotherResource._attributes), 0)
139
140         self.assertEquals(len(ResourceFactory.resource_types()), 2)
141
142 class ResourceManagerTestCase(unittest.TestCase):
143     def test_register_condition(self):
144         ec = ExperimentController()
145         rm = ResourceManager(ec, 15)
146
147         group = [1,3,5,7]
148         rm.register_condition(ResourceAction.START, group,
149                 ResourceState.STARTED)
150
151         group = [10,8]
152         rm.register_condition(ResourceAction.START,
153                 group, ResourceState.STARTED, time = "10s")
154
155         waiting_for = []
156         conditions = rm.conditions.get(ResourceAction.START)
157         for (group, state, time) in conditions:
158             waiting_for.extend(group)
159
160         self.assertEquals(waiting_for, [1, 3, 5, 7, 10, 8])
161
162         group = [1, 2, 3, 4, 6]
163         rm.unregister_condition(group)
164
165         waiting_for = []
166         conditions = rm.conditions.get(ResourceAction.START)
167         for (group, state, time) in conditions:
168             waiting_for.extend(group)
169
170         self.assertEquals(waiting_for, [5, 7, 10, 8])
171
172     def test_deploy_in_order(self):
173         """
174         Test scenario: 2 applications running one on 1 node each. 
175         Nodes are connected to Interfaces which are connected
176         through a channel between them.
177
178          - Application needs to wait until Node is ready to be ready
179          - Node needs to wait until Interface is ready to be ready
180          - Interface needs to wait until Node is provisioned to be ready
181          - Interface needs to wait until Channel is ready to be ready
182          - The channel doesn't wait for any other resource to be ready
183
184         """
185         from nepi.execution.resource import ResourceFactory
186         
187         ResourceFactory.register_type(Application)
188         ResourceFactory.register_type(Node)
189         ResourceFactory.register_type(Interface)
190         ResourceFactory.register_type(Channel)
191
192         ec = ExperimentController()
193
194         app1 = ec.register_resource("Application")
195         app2 = ec.register_resource("Application")
196         node1 = ec.register_resource("Node")
197         node2 = ec.register_resource("Node")
198         iface1 = ec.register_resource("Interface")
199         iface2 = ec.register_resource("Interface")
200         chan = ec.register_resource("Channel")
201
202         ec.register_connection(app1, node1)
203         ec.register_connection(app2, node2)
204         ec.register_connection(iface1, node1)
205         ec.register_connection(iface2, node2)
206         ec.register_connection(iface1, chan)
207         ec.register_connection(iface2, chan)
208
209         ec.deploy()
210
211         guids = [app1, app2]
212         ec.wait_finished(guids)
213
214         ec.shutdown()
215
216         rmapp1 = ec.get_resource(app1)
217         rmapp2 = ec.get_resource(app2)
218         rmnode1 = ec.get_resource(node1)
219         rmnode2 = ec.get_resource(node2)
220         rmiface1 = ec.get_resource(iface1)
221         rmiface2 = ec.get_resource(iface2)
222         rmchan = ec.get_resource(chan)
223
224         ## Validate deploy order
225         # - Application needs to wait until Node is ready to be ready
226         self.assertTrue(rmnode1.ready_time < rmapp1.ready_time)
227         self.assertTrue(rmnode2.ready_time < rmapp2.ready_time)
228
229          # - Node needs to wait until Interface is ready to be ready
230         self.assertTrue(rmnode1.ready_time > rmiface1.ready_time)
231         self.assertTrue(rmnode2.ready_time > rmiface2.ready_time)
232
233          # - Interface needs to wait until Node is provisioned to be ready
234         self.assertTrue(rmnode1.provision_time < rmiface1.ready_time)
235         self.assertTrue(rmnode2.provision_time < rmiface2.ready_time)
236
237          # - Interface needs to wait until Channel is ready to be ready
238         self.assertTrue(rmchan.ready_time < rmiface1.ready_time)
239         self.assertTrue(rmchan.ready_time < rmiface2.ready_time)
240
241     def test_concurrency(self):
242         from nepi.execution.resource import ResourceFactory
243         
244         ResourceFactory.register_type(Application)
245         ResourceFactory.register_type(Node)
246         ResourceFactory.register_type(Interface)
247         ResourceFactory.register_type(Channel)
248
249         ec = ExperimentController()
250
251         node = ec.register_resource("Node")
252
253         apps = list()
254         for i in xrange(1000):
255             app = ec.register_resource("Application")
256             ec.register_connection(app, node)
257             apps.append(app)
258
259         ec.deploy()
260
261         ec.wait_finished(apps)
262         
263         self.assertTrue(ec.state(node) == ResourceState.STARTED)
264         self.assertTrue(
265                all([ec.state(guid) == ResourceState.FINISHED \
266                 for guid in apps])
267                 )
268
269         ec.shutdown()
270
271     def test_start_with_condition(self):
272         # TODO!!!
273         pass
274     
275     def test_stop_with_condition(self):
276         # TODO!!!
277         pass
278
279     def test_set_with_condition(self):
280         # TODO!!!
281         pass
282
283
284 if __name__ == '__main__':
285     unittest.main()
286