Merge neco to nepi-3.0
[nepi.git] / test / resources / omf / vlc.py
1 """
2     NEPI, a framework to manage network experiments
3     Copyright (C) 2013 INRIA
4
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 """
19
20 #!/usr/bin/env python
21 from nepi.execution.resource import ResourceFactory, ResourceManager, ResourceAction, ResourceState
22 from nepi.execution.ec import ExperimentController
23
24 from nepi.resources.omf.omf_node import OMFNode
25 from nepi.resources.omf.omf_application import OMFApplication
26 from nepi.resources.omf.omf_interface import OMFWifiInterface
27 from nepi.resources.omf.omf_channel import OMFChannel
28 from nepi.resources.omf.omf_api import OMFAPIFactory
29
30 from nepi.util import guid
31 from nepi.util.timefuncs import *
32
33 import time
34 import unittest
35 import logging
36
37 logging.basicConfig()
38
39
40 class DummyEC(ExperimentController):
41     pass
42
43 class DummyRM(ResourceManager):
44     pass
45
46
47 class OMFResourceFactoryTestCase(unittest.TestCase):
48
49     def test_creation_phase(self):
50         ResourceFactory.register_type(OMFNode)
51         ResourceFactory.register_type(OMFWifiInterface)
52         ResourceFactory.register_type(OMFChannel)
53         ResourceFactory.register_type(OMFApplication)
54
55         self.assertEquals(OMFNode.rtype(), "OMFNode")
56         self.assertEquals(len(OMFNode._attributes), 7)
57
58         self.assertEquals(OMFWifiInterface.rtype(), "OMFWifiInterface")
59         self.assertEquals(len(OMFWifiInterface._attributes), 9)
60
61         self.assertEquals(OMFChannel.rtype(), "OMFChannel")
62         self.assertEquals(len(OMFChannel._attributes), 5)
63
64         self.assertEquals(OMFApplication.rtype(), "OMFApplication")
65         self.assertEquals(len(OMFApplication._attributes), 8)
66
67         self.assertEquals(len(ResourceFactory.resource_types()), 4)
68
69
70 class OMFVLCTestCase(unittest.TestCase):
71
72     def setUp(self):
73         self.ec = DummyEC()
74         ResourceFactory.register_type(OMFNode)
75         ResourceFactory.register_type(OMFWifiInterface)
76         ResourceFactory.register_type(OMFChannel)
77         ResourceFactory.register_type(OMFApplication)
78
79     def tearDown(self):
80         self.ec.shutdown()
81
82     def test_creation_and_configuration_node(self):
83
84         node1 = self.ec.register_resource("OMFNode")
85         self.ec.set(node1, 'hostname', 'omf.plexus.wlab17')
86         self.ec.set(node1, 'xmppSlice', "nepi")
87         self.ec.set(node1, 'xmppHost', "xmpp-plexus.onelab.eu")
88         self.ec.set(node1, 'xmppPort', "5222")
89         self.ec.set(node1, 'xmppPassword', "1234")
90
91         self.assertEquals(self.ec.get(node1, 'hostname'), 'omf.plexus.wlab17')
92         self.assertEquals(self.ec.get(node1, 'xmppSlice'), 'nepi')
93         self.assertEquals(self.ec.get(node1, 'xmppHost'), 'xmpp-plexus.onelab.eu')
94         self.assertEquals(self.ec.get(node1, 'xmppPort'), '5222')
95         self.assertEquals(self.ec.get(node1, 'xmppPassword'), '1234')
96
97     def test_creation_and_configuration_interface(self):
98
99         iface1 = self.ec.register_resource("OMFWifiInterface")
100         self.ec.set(iface1, 'alias', "w0")
101         self.ec.set(iface1, 'mode', "adhoc")
102         self.ec.set(iface1, 'type', "g")
103         self.ec.set(iface1, 'essid', "vlcexp")
104         self.ec.set(iface1, 'ip', "10.0.0.17")
105         self.ec.set(iface1, 'xmppSlice', "nepi")
106         self.ec.set(iface1, 'xmppHost', "xmpp-plexus.onelab.eu")
107         self.ec.set(iface1, 'xmppPort', "5222")
108         self.ec.set(iface1, 'xmppPassword', "1234")
109
110         self.assertEquals(self.ec.get(iface1, 'alias'), 'w0')
111         self.assertEquals(self.ec.get(iface1, 'mode'), 'adhoc')
112         self.assertEquals(self.ec.get(iface1, 'type'), 'g')
113         self.assertEquals(self.ec.get(iface1, 'essid'), 'vlcexp')
114         self.assertEquals(self.ec.get(iface1, 'ip'), '10.0.0.17')
115         self.assertEquals(self.ec.get(iface1, 'xmppSlice'), 'nepi')
116         self.assertEquals(self.ec.get(iface1, 'xmppHost'), 'xmpp-plexus.onelab.eu')
117         self.assertEquals(self.ec.get(iface1, 'xmppPort'), '5222')
118         self.assertEquals(self.ec.get(iface1, 'xmppPassword'), '1234')
119
120     def test_creation_and_configuration_channel(self):
121
122         channel = self.ec.register_resource("OMFChannel")
123         self.ec.set(channel, 'channel', "6")
124         self.ec.set(channel, 'xmppSlice', "nepi")
125         self.ec.set(channel, 'xmppHost', "xmpp-plexus.onelab.eu")
126         self.ec.set(channel, 'xmppPort', "5222")
127         self.ec.set(channel, 'xmppPassword', "1234")
128
129         self.assertEquals(self.ec.get(channel, 'channel'), '6')
130         self.assertEquals(self.ec.get(channel, 'xmppSlice'), 'nepi')
131         self.assertEquals(self.ec.get(channel, 'xmppHost'), 'xmpp-plexus.onelab.eu')
132         self.assertEquals(self.ec.get(channel, 'xmppPort'), '5222')
133         self.assertEquals(self.ec.get(channel, 'xmppPassword'), '1234')
134
135     def test_creation_and_configuration_application(self):
136
137         app1 = self.ec.register_resource("OMFApplication")
138         self.ec.set(app1, 'appid', 'Vlc#1')
139         self.ec.set(app1, 'path', "/opt/vlc-1.1.13/cvlc")
140         self.ec.set(app1, 'args', "/opt/10-by-p0d.avi --sout '#rtp{dst=10.0.0.37,port=1234,mux=ts}'")
141         self.ec.set(app1, 'env', "DISPLAY=localhost:10.0 XAUTHORITY=/root/.Xauthority")
142         self.ec.set(app1, 'xmppSlice', "nepi")
143         self.ec.set(app1, 'xmppHost', "xmpp-plexus.onelab.eu")
144         self.ec.set(app1, 'xmppPort', "5222")
145         self.ec.set(app1, 'xmppPassword', "1234")
146
147         self.assertEquals(self.ec.get(app1, 'appid'), 'Vlc#1')
148         self.assertEquals(self.ec.get(app1, 'path'), '/opt/vlc-1.1.13/cvlc')
149         self.assertEquals(self.ec.get(app1, 'args'), "/opt/10-by-p0d.avi --sout '#rtp{dst=10.0.0.37,port=1234,mux=ts}'")
150         self.assertEquals(self.ec.get(app1, 'env'), 'DISPLAY=localhost:10.0 XAUTHORITY=/root/.Xauthority')
151         self.assertEquals(self.ec.get(app1, 'xmppSlice'), 'nepi')
152         self.assertEquals(self.ec.get(app1, 'xmppHost'), 'xmpp-plexus.onelab.eu')
153         self.assertEquals(self.ec.get(app1, 'xmppPort'), '5222')
154         self.assertEquals(self.ec.get(app1, 'xmppPassword'), '1234')
155
156     def test_connection(self):
157
158         node1 = self.ec.register_resource("OMFNode")
159         iface1 = self.ec.register_resource("OMFWifiInterface")
160         channel = self.ec.register_resource("OMFChannel")
161         app1 = self.ec.register_resource("OMFApplication")
162         app2 = self.ec.register_resource("OMFApplication")
163
164         self.ec.register_connection(app1, node1)
165         self.ec.register_connection(app2, node1)
166         self.ec.register_connection(node1, iface1)
167         self.ec.register_connection(iface1, channel)
168
169         self.assertEquals(len(self.ec.get_resource(node1).connections), 3)
170         self.assertEquals(len(self.ec.get_resource(iface1).connections), 2)
171         self.assertEquals(len(self.ec.get_resource(channel).connections), 1)
172         self.assertEquals(len(self.ec.get_resource(app1).connections), 1)
173         self.assertEquals(len(self.ec.get_resource(app2).connections), 1)
174
175     def test_condition(self):
176
177         node1 = self.ec.register_resource("OMFNode")
178         iface1 = self.ec.register_resource("OMFWifiInterface")
179         channel = self.ec.register_resource("OMFChannel")
180         app1 = self.ec.register_resource("OMFApplication")
181         app2 = self.ec.register_resource("OMFApplication")
182
183         self.ec.register_connection(app1, node1)
184         self.ec.register_connection(app2, node1)
185         self.ec.register_connection(node1, iface1)
186         self.ec.register_connection(iface1, channel)
187
188         self.ec.register_condition(app2, ResourceAction.START, app1, ResourceState.STARTED , "4s")
189
190         self.assertEquals(len(self.ec.get_resource(app2).conditions), 1)
191
192     def test_deploy(self):
193         node1 = self.ec.register_resource("OMFNode")
194         self.ec.set(node1, 'hostname', 'omf.plexus.wlab17')
195         self.ec.set(node1, 'xmppSlice', "nepi")
196         self.ec.set(node1, 'xmppHost', "xmpp-plexus.onelab.eu")
197         self.ec.set(node1, 'xmppPort', "5222")
198         self.ec.set(node1, 'xmppPassword', "1234")
199         
200         iface1 = self.ec.register_resource("OMFWifiInterface")
201         self.ec.set(iface1, 'alias', "w0")
202         self.ec.set(iface1, 'mode', "adhoc")
203         self.ec.set(iface1, 'type', "g")
204         self.ec.set(iface1, 'essid', "vlcexp")
205         self.ec.set(iface1, 'ip', "10.0.0.17")
206         self.ec.set(iface1, 'xmppSlice', "nepi")
207         self.ec.set(iface1, 'xmppHost', "xmpp-plexus.onelab.eu")
208         self.ec.set(iface1, 'xmppPort', "5222")
209         self.ec.set(iface1, 'xmppPassword', "1234")
210         
211         channel = self.ec.register_resource("OMFChannel")
212         self.ec.set(channel, 'channel', "6")
213         self.ec.set(channel, 'xmppSlice', "nepi")
214         self.ec.set(channel, 'xmppHost', "xmpp-plexus.onelab.eu")
215         self.ec.set(channel, 'xmppPort', "5222")
216         self.ec.set(channel, 'xmppPassword', "1234")
217         
218         app1 = self.ec.register_resource("OMFApplication")
219         self.ec.set(app1, 'xmppSlice', "nepi")
220         self.ec.set(app1, 'xmppHost', "xmpp-plexus.onelab.eu")
221         self.ec.set(app1, 'xmppPort', "5222")
222         self.ec.set(app1, 'xmppPassword', "1234")
223
224         app2 = self.ec.register_resource("OMFApplication")
225         self.ec.set(app2, 'xmppSlice', "nepi")
226         self.ec.set(app2, 'xmppHost', "xmpp-plexus.onelab.eu")
227         self.ec.set(app2, 'xmppPort', "5222")
228         self.ec.set(app2, 'xmppPassword', "1234")
229
230         app3 = self.ec.register_resource("OMFApplication")
231         self.ec.set(app3, 'xmppSlice', "nepi")
232         self.ec.set(app3, 'xmppHost', "xmpp-plexus.onelab.eu")
233         self.ec.set(app3, 'xmppPort', "5222")
234         self.ec.set(app3, 'xmppPassword', "1234")
235
236         app4 = self.ec.register_resource("OMFApplication")
237         self.ec.set(app4, 'xmppSlice', "nepi")
238         self.ec.set(app4, 'xmppHost', "xmpp-plexus.onelab.eu")
239         self.ec.set(app4, 'xmppPort', "5222")
240         self.ec.set(app4, 'xmppPassword', "1234")
241
242         app5 = self.ec.register_resource("OMFApplication")
243         self.ec.set(app5, 'xmppSlice', "nepi")
244         self.ec.set(app5, 'xmppHost', "xmpp-plexus.onelab.eu")
245         self.ec.set(app5, 'xmppPort', "5222")
246         self.ec.set(app5, 'xmppPassword', "1234")
247
248         self.ec.register_connection(app1, node1)
249         self.ec.register_connection(app2, node1)
250         self.ec.register_connection(app3, node1)
251         self.ec.register_connection(app4, node1)
252         self.ec.register_connection(app5, node1)
253         self.ec.register_connection(node1, iface1)
254         self.ec.register_connection(iface1, channel)
255
256         self.ec.register_condition(app2, ResourceAction.START, app1, ResourceState.STARTED , "3s")
257         self.ec.register_condition(app3, ResourceAction.START, app2, ResourceState.STARTED , "2s")
258         self.ec.register_condition(app4, ResourceAction.START, app3, ResourceState.STARTED , "3s")
259         self.ec.register_condition(app5, ResourceAction.START, [app3, app2], ResourceState.STARTED , "2s")
260         self.ec.register_condition(app5, ResourceAction.START, app1, ResourceState.STARTED , "1m20s")
261
262         self.ec.deploy()
263         time.sleep(150)
264
265         self.assertEquals(round(strfdiff(self.ec.get_resource(app2).start_time, self.ec.get_resource(app1).start_time),1), 3.0)
266         self.assertEquals(round(strfdiff(self.ec.get_resource(app3).start_time, self.ec.get_resource(app2).start_time),1), 2.0)
267         self.assertEquals(round(strfdiff(self.ec.get_resource(app4).start_time, self.ec.get_resource(app3).start_time),1), 3.0)
268         self.assertEquals(round(strfdiff(self.ec.get_resource(app5).start_time, self.ec.get_resource(app3).start_time),1), 2.0)
269         self.assertEquals(round(strfdiff(self.ec.get_resource(app5).start_time, self.ec.get_resource(app1).start_time),1), 7.0)
270
271         # Precision is at 1/10. So this one returns an error 7.03 != 7.0
272         #self.assertEquals(strfdiff(self.ec.get_resource(app5).start_time, self.ec.get_resource(app1).start_time), 7)
273     #In order to release everythings
274         time.sleep(5)
275
276
277 if __name__ == '__main__':
278     unittest.main()
279
280
281