Adding authors and correcting licence information
[nepi.git] / test / resources / omf / vlc.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: Julien Tribino <julien.tribino@inria.fr>
20
21
22 from nepi.execution.resource import ResourceFactory, ResourceManager, ResourceAction, ResourceState
23 from nepi.execution.ec import ExperimentController
24
25 from nepi.resources.omf.node import OMFNode
26 from nepi.resources.omf.application import OMFApplication
27 from nepi.resources.omf.interface import OMFWifiInterface
28 from nepi.resources.omf.channel import OMFChannel
29 from nepi.resources.omf.omf_api import OMFAPIFactory
30
31 from nepi.util import guid
32 from nepi.util.timefuncs import *
33
34 import time
35 import unittest
36 import logging
37
38 logging.basicConfig()
39
40
41 class DummyEC(ExperimentController):
42     pass
43
44 class DummyRM(ResourceManager):
45     pass
46
47
48 class OMFResourceFactoryTestCase(unittest.TestCase):
49
50     def test_creation_phase(self):
51         ResourceFactory.register_type(OMFNode)
52         ResourceFactory.register_type(OMFWifiInterface)
53         ResourceFactory.register_type(OMFChannel)
54         ResourceFactory.register_type(OMFApplication)
55
56         self.assertEquals(OMFNode.rtype(), "OMFNode")
57         self.assertEquals(len(OMFNode._attributes), 11)
58
59         self.assertEquals(OMFWifiInterface.rtype(), "OMFWifiInterface")
60         self.assertEquals(len(OMFWifiInterface._attributes), 9)
61
62         self.assertEquals(OMFChannel.rtype(), "OMFChannel")
63         self.assertEquals(len(OMFChannel._attributes), 5)
64
65         self.assertEquals(OMFApplication.rtype(), "OMFApplication")
66         self.assertEquals(len(OMFApplication._attributes), 8)
67
68         self.assertEquals(len(ResourceFactory.resource_types()), 4)
69
70
71 class OMFVLCTestCase(unittest.TestCase):
72
73     def setUp(self):
74         self.ec = DummyEC()
75         ResourceFactory.register_type(OMFNode)
76         ResourceFactory.register_type(OMFWifiInterface)
77         ResourceFactory.register_type(OMFChannel)
78         ResourceFactory.register_type(OMFApplication)
79
80     def tearDown(self):
81         self.ec.shutdown()
82
83     def test_creation_and_configuration_node(self):
84
85         node1 = self.ec.register_resource("OMFNode")
86         self.ec.set(node1, 'hostname', 'omf.plexus.wlab17')
87         self.ec.set(node1, 'xmppSlice', "nepi")
88         self.ec.set(node1, 'xmppHost', "xmpp-plexus.onelab.eu")
89         self.ec.set(node1, 'xmppPort', "5222")
90         self.ec.set(node1, 'xmppPassword', "1234")
91
92         self.assertEquals(self.ec.get(node1, 'hostname'), 'omf.plexus.wlab17')
93         self.assertEquals(self.ec.get(node1, 'xmppSlice'), 'nepi')
94         self.assertEquals(self.ec.get(node1, 'xmppHost'), 'xmpp-plexus.onelab.eu')
95         self.assertEquals(self.ec.get(node1, 'xmppPort'), '5222')
96         self.assertEquals(self.ec.get(node1, 'xmppPassword'), '1234')
97
98     def test_creation_and_configuration_interface(self):
99
100         iface1 = self.ec.register_resource("OMFWifiInterface")
101         self.ec.set(iface1, 'alias', "w0")
102         self.ec.set(iface1, 'mode', "adhoc")
103         self.ec.set(iface1, 'type', "g")
104         self.ec.set(iface1, 'essid', "vlcexp")
105         self.ec.set(iface1, 'ip', "10.0.0.17")
106         self.ec.set(iface1, 'xmppSlice', "nepi")
107         self.ec.set(iface1, 'xmppHost', "xmpp-plexus.onelab.eu")
108         self.ec.set(iface1, 'xmppPort', "5222")
109         self.ec.set(iface1, 'xmppPassword', "1234")
110
111         self.assertEquals(self.ec.get(iface1, 'alias'), 'w0')
112         self.assertEquals(self.ec.get(iface1, 'mode'), 'adhoc')
113         self.assertEquals(self.ec.get(iface1, 'type'), 'g')
114         self.assertEquals(self.ec.get(iface1, 'essid'), 'vlcexp')
115         self.assertEquals(self.ec.get(iface1, 'ip'), '10.0.0.17')
116         self.assertEquals(self.ec.get(iface1, 'xmppSlice'), 'nepi')
117         self.assertEquals(self.ec.get(iface1, 'xmppHost'), 'xmpp-plexus.onelab.eu')
118         self.assertEquals(self.ec.get(iface1, 'xmppPort'), '5222')
119         self.assertEquals(self.ec.get(iface1, 'xmppPassword'), '1234')
120
121     def test_creation_and_configuration_channel(self):
122
123         channel = self.ec.register_resource("OMFChannel")
124         self.ec.set(channel, 'channel', "6")
125         self.ec.set(channel, 'xmppSlice', "nepi")
126         self.ec.set(channel, 'xmppHost', "xmpp-plexus.onelab.eu")
127         self.ec.set(channel, 'xmppPort', "5222")
128         self.ec.set(channel, 'xmppPassword', "1234")
129
130         self.assertEquals(self.ec.get(channel, 'channel'), '6')
131         self.assertEquals(self.ec.get(channel, 'xmppSlice'), 'nepi')
132         self.assertEquals(self.ec.get(channel, 'xmppHost'), 'xmpp-plexus.onelab.eu')
133         self.assertEquals(self.ec.get(channel, 'xmppPort'), '5222')
134         self.assertEquals(self.ec.get(channel, 'xmppPassword'), '1234')
135
136     def test_creation_and_configuration_application(self):
137
138         app1 = self.ec.register_resource("OMFApplication")
139         self.ec.set(app1, 'appid', 'Vlc#1')
140         self.ec.set(app1, 'path', "/opt/vlc-1.1.13/cvlc")
141         self.ec.set(app1, 'args', "/opt/10-by-p0d.avi --sout '#rtp{dst=10.0.0.37,port=1234,mux=ts}'")
142         self.ec.set(app1, 'env', "DISPLAY=localhost:10.0 XAUTHORITY=/root/.Xauthority")
143         self.ec.set(app1, 'xmppSlice', "nepi")
144         self.ec.set(app1, 'xmppHost', "xmpp-plexus.onelab.eu")
145         self.ec.set(app1, 'xmppPort', "5222")
146         self.ec.set(app1, 'xmppPassword', "1234")
147
148         self.assertEquals(self.ec.get(app1, 'appid'), 'Vlc#1')
149         self.assertEquals(self.ec.get(app1, 'path'), '/opt/vlc-1.1.13/cvlc')
150         self.assertEquals(self.ec.get(app1, 'args'), "/opt/10-by-p0d.avi --sout '#rtp{dst=10.0.0.37,port=1234,mux=ts}'")
151         self.assertEquals(self.ec.get(app1, 'env'), 'DISPLAY=localhost:10.0 XAUTHORITY=/root/.Xauthority')
152         self.assertEquals(self.ec.get(app1, 'xmppSlice'), 'nepi')
153         self.assertEquals(self.ec.get(app1, 'xmppHost'), 'xmpp-plexus.onelab.eu')
154         self.assertEquals(self.ec.get(app1, 'xmppPort'), '5222')
155         self.assertEquals(self.ec.get(app1, 'xmppPassword'), '1234')
156
157     def test_connection(self):
158
159         node1 = self.ec.register_resource("OMFNode")
160         iface1 = self.ec.register_resource("OMFWifiInterface")
161         channel = self.ec.register_resource("OMFChannel")
162         app1 = self.ec.register_resource("OMFApplication")
163         app2 = self.ec.register_resource("OMFApplication")
164
165         self.ec.register_connection(app1, node1)
166         self.ec.register_connection(app2, node1)
167         self.ec.register_connection(node1, iface1)
168         self.ec.register_connection(iface1, channel)
169
170         self.assertEquals(len(self.ec.get_resource(node1).connections), 3)
171         self.assertEquals(len(self.ec.get_resource(iface1).connections), 2)
172         self.assertEquals(len(self.ec.get_resource(channel).connections), 1)
173         self.assertEquals(len(self.ec.get_resource(app1).connections), 1)
174         self.assertEquals(len(self.ec.get_resource(app2).connections), 1)
175
176     def test_condition(self):
177
178         node1 = self.ec.register_resource("OMFNode")
179         iface1 = self.ec.register_resource("OMFWifiInterface")
180         channel = self.ec.register_resource("OMFChannel")
181         app1 = self.ec.register_resource("OMFApplication")
182         app2 = self.ec.register_resource("OMFApplication")
183
184         self.ec.register_connection(app1, node1)
185         self.ec.register_connection(app2, node1)
186         self.ec.register_connection(node1, iface1)
187         self.ec.register_connection(iface1, channel)
188
189         self.ec.register_condition(app2, ResourceAction.START, app1, ResourceState.STARTED , "4s")
190
191         self.assertEquals(len(self.ec.get_resource(app2).conditions), 1)
192
193     def test_deploy(self):
194         node1 = self.ec.register_resource("OMFNode")
195         self.ec.set(node1, 'hostname', 'omf.plexus.wlab17')
196         self.ec.set(node1, 'xmppSlice', "nepi")
197         self.ec.set(node1, 'xmppHost', "xmpp-plexus.onelab.eu")
198         self.ec.set(node1, 'xmppPort', "5222")
199         self.ec.set(node1, 'xmppPassword', "1234")
200         
201         iface1 = self.ec.register_resource("OMFWifiInterface")
202         self.ec.set(iface1, 'alias', "w0")
203         self.ec.set(iface1, 'mode', "adhoc")
204         self.ec.set(iface1, 'type', "g")
205         self.ec.set(iface1, 'essid', "vlcexp")
206         self.ec.set(iface1, 'ip', "10.0.0.17")
207         self.ec.set(iface1, 'xmppSlice', "nepi")
208         self.ec.set(iface1, 'xmppHost', "xmpp-plexus.onelab.eu")
209         self.ec.set(iface1, 'xmppPort', "5222")
210         self.ec.set(iface1, 'xmppPassword', "1234")
211         
212         channel = self.ec.register_resource("OMFChannel")
213         self.ec.set(channel, 'channel', "6")
214         self.ec.set(channel, 'xmppSlice', "nepi")
215         self.ec.set(channel, 'xmppHost', "xmpp-plexus.onelab.eu")
216         self.ec.set(channel, 'xmppPort', "5222")
217         self.ec.set(channel, 'xmppPassword', "1234")
218         
219         app1 = self.ec.register_resource("OMFApplication")
220         self.ec.set(app1, 'xmppSlice', "nepi")
221         self.ec.set(app1, 'xmppHost', "xmpp-plexus.onelab.eu")
222         self.ec.set(app1, 'xmppPort', "5222")
223         self.ec.set(app1, 'xmppPassword', "1234")
224
225         app2 = self.ec.register_resource("OMFApplication")
226         self.ec.set(app2, 'xmppSlice', "nepi")
227         self.ec.set(app2, 'xmppHost', "xmpp-plexus.onelab.eu")
228         self.ec.set(app2, 'xmppPort', "5222")
229         self.ec.set(app2, 'xmppPassword', "1234")
230
231         app3 = self.ec.register_resource("OMFApplication")
232         self.ec.set(app3, 'xmppSlice', "nepi")
233         self.ec.set(app3, 'xmppHost', "xmpp-plexus.onelab.eu")
234         self.ec.set(app3, 'xmppPort', "5222")
235         self.ec.set(app3, 'xmppPassword', "1234")
236
237         app4 = self.ec.register_resource("OMFApplication")
238         self.ec.set(app4, 'xmppSlice', "nepi")
239         self.ec.set(app4, 'xmppHost', "xmpp-plexus.onelab.eu")
240         self.ec.set(app4, 'xmppPort', "5222")
241         self.ec.set(app4, 'xmppPassword', "1234")
242
243         app5 = self.ec.register_resource("OMFApplication")
244         self.ec.set(app5, 'xmppSlice', "nepi")
245         self.ec.set(app5, 'xmppHost', "xmpp-plexus.onelab.eu")
246         self.ec.set(app5, 'xmppPort', "5222")
247         self.ec.set(app5, 'xmppPassword', "1234")
248
249         self.ec.register_connection(app1, node1)
250         self.ec.register_connection(app2, node1)
251         self.ec.register_connection(app3, node1)
252         self.ec.register_connection(app4, node1)
253         self.ec.register_connection(app5, node1)
254         self.ec.register_connection(node1, iface1)
255         self.ec.register_connection(iface1, channel)
256
257         self.ec.register_condition(app2, ResourceAction.START, app1, ResourceState.STARTED , "3s")
258         self.ec.register_condition(app3, ResourceAction.START, app2, ResourceState.STARTED , "2s")
259         self.ec.register_condition(app4, ResourceAction.START, app3, ResourceState.STARTED , "3s")
260         self.ec.register_condition(app5, ResourceAction.START, [app3, app2], ResourceState.STARTED , "2s")
261         self.ec.register_condition(app5, ResourceAction.START, app1, ResourceState.STARTED , "25s")
262
263         self.ec.register_condition([app1, app2, app3,app4, app5], ResourceAction.STOP, app5, ResourceState.STARTED , "1s")
264
265         self.ec.deploy()
266
267         self.ec.wait_finished([app1, app2, app3,app4, app5])
268
269         self.assertEquals(round(strfdiff(self.ec.get_resource(app2).start_time, self.ec.get_resource(app1).start_time),1), 3.0)
270         self.assertEquals(round(strfdiff(self.ec.get_resource(app3).start_time, self.ec.get_resource(app2).start_time),1), 2.0)
271         self.assertEquals(round(strfdiff(self.ec.get_resource(app4).start_time, self.ec.get_resource(app3).start_time),1), 3.0)
272         self.assertEquals(round(strfdiff(self.ec.get_resource(app5).start_time, self.ec.get_resource(app3).start_time),1), 20.0)
273         self.assertEquals(round(strfdiff(self.ec.get_resource(app5).start_time, self.ec.get_resource(app1).start_time),1), 25.0)
274
275         # Precision is at 1/10. So this one returns an error 7.03 != 7.0
276         #self.assertEquals(strfdiff(self.ec.get_resource(app5).start_time, self.ec.get_resource(app1).start_time), 7)
277     #In order to release everythings
278         time.sleep(1)
279
280
281 if __name__ == '__main__':
282     unittest.main()
283
284
285