eea7912aad8c4221ffb22c83c6debba073b79a20
[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.timefuncs import *
32
33 import time
34 import unittest
35
36 class DummyEC(ExperimentController):
37     pass
38
39 class DummyRM(ResourceManager):
40     pass
41
42
43 class OMFResourceFactoryTestCase(unittest.TestCase):
44
45     def test_creation_phase(self):
46         ResourceFactory.register_type(OMFNode)
47         ResourceFactory.register_type(OMFWifiInterface)
48         ResourceFactory.register_type(OMFChannel)
49         ResourceFactory.register_type(OMFApplication)
50
51         self.assertEquals(OMFNode.rtype(), "OMFNode")
52         self.assertEquals(len(OMFNode._attributes), 11)
53
54         self.assertEquals(OMFWifiInterface.rtype(), "OMFWifiInterface")
55         self.assertEquals(len(OMFWifiInterface._attributes), 9)
56
57         self.assertEquals(OMFChannel.rtype(), "OMFChannel")
58         self.assertEquals(len(OMFChannel._attributes), 5)
59
60         self.assertEquals(OMFApplication.rtype(), "OMFApplication")
61         self.assertEquals(len(OMFApplication._attributes), 8)
62
63         self.assertEquals(len(ResourceFactory.resource_types()), 4)
64
65
66 class OMFVLCTestCase(unittest.TestCase):
67
68     def setUp(self):
69         self.ec = DummyEC()
70         ResourceFactory.register_type(OMFNode)
71         ResourceFactory.register_type(OMFWifiInterface)
72         ResourceFactory.register_type(OMFChannel)
73         ResourceFactory.register_type(OMFApplication)
74
75         self.node1 = self.ec.register_resource("OMFNode")
76         self.ec.set(self.node1, 'hostname', 'omf.plexus.wlab17')
77         self.ec.set(self.node1, 'xmppSlice', "nepi")
78         self.ec.set(self.node1, 'xmppHost', "xmpp-plexus.onelab.eu")
79         self.ec.set(self.node1, 'xmppPort', "5222")
80         self.ec.set(self.node1, 'xmppPassword', "1234")
81
82         self.node2 = self.ec.register_resource("OMFNode")
83         
84         self.iface1 = self.ec.register_resource("OMFWifiInterface")
85         self.ec.set(self.iface1, 'alias', "w0")
86         self.ec.set(self.iface1, 'mode', "adhoc")
87         self.ec.set(self.iface1, 'type', "g")
88         self.ec.set(self.iface1, 'essid', "vlcexp")
89         self.ec.set(self.iface1, 'ip', "10.0.0.17")
90         self.ec.set(self.iface1, 'xmppSlice', "nepi")
91         self.ec.set(self.iface1, 'xmppHost', "xmpp-plexus.onelab.eu")
92         self.ec.set(self.iface1, 'xmppPort', "5222")
93         self.ec.set(self.iface1, 'xmppPassword', "1234")
94
95         self.iface2 = self.ec.register_resource("OMFWifiInterface")
96         
97         self.channel = self.ec.register_resource("OMFChannel")
98         self.ec.set(self.channel, 'channel', "6")
99         self.ec.set(self.channel, 'xmppSlice', "nepi")
100         self.ec.set(self.channel, 'xmppHost', "xmpp-plexus.onelab.eu")
101         self.ec.set(self.channel, 'xmppPort', "5222")
102         self.ec.set(self.channel, 'xmppPassword', "1234")
103         
104         self.app1 = self.ec.register_resource("OMFApplication")
105         self.ec.set(self.app1, 'appid', 'Vlc#1')
106         self.ec.set(self.app1, 'path', "/opt/vlc-1.1.13/cvlc")
107         self.ec.set(self.app1, 'args', "/opt/10-by-p0d.avi --sout '#rtp{dst=10.0.0.37,port=1234,mux=ts}'")
108         self.ec.set(self.app1, 'env', "DISPLAY=localhost:10.0 XAUTHORITY=/root/.Xauthority")
109         self.ec.set(self.app1, 'xmppSlice', "nepi")
110         self.ec.set(self.app1, 'xmppHost', "xmpp-plexus.onelab.eu")
111         self.ec.set(self.app1, 'xmppPort', "5222")
112         self.ec.set(self.app1, 'xmppPassword', "1234")
113
114         self.app2 = self.ec.register_resource("OMFApplication")
115         self.ec.set(self.app2, 'xmppSlice', "nepi")
116         self.ec.set(self.app2, 'xmppHost', "xmpp-plexus.onelab.eu")
117         self.ec.set(self.app2, 'xmppPort', "5222")
118         self.ec.set(self.app2, 'xmppPassword', "1234")
119
120         self.app3 = self.ec.register_resource("OMFApplication")
121         self.ec.set(self.app3, 'appid', 'Kill#2')
122         self.ec.set(self.app3, 'path', "/usr/bin/killall")
123         self.ec.set(self.app3, 'args', "vlc")
124         self.ec.set(self.app3, 'env', " ")
125
126         self.app4 = self.ec.register_resource("OMFApplication")
127
128         self.app5 = self.ec.register_resource("OMFApplication")
129         self.ec.set(self.app5, 'appid', 'Kill#2')
130         self.ec.set(self.app5, 'path', "/usr/bin/killall")
131         self.ec.set(self.app5, 'args', "vlc")
132         self.ec.set(self.app5, 'env', " ")
133         self.ec.set(self.app5, 'xmppSlice', "nepi")
134         self.ec.set(self.app5, 'xmppHost', "xmpp-plexus.onelab.eu")
135         self.ec.set(self.app5, 'xmppPort', "5222")
136         self.ec.set(self.app5, 'xmppPassword', "1234")
137
138         self.ec.register_connection(self.app1, self.node1)
139         self.ec.register_connection(self.app2, self.node1)
140         self.ec.register_connection(self.app3, self.node1)
141         self.ec.register_connection(self.app4, self.node1)
142         self.ec.register_connection(self.app5, self.node1)
143         self.ec.register_connection(self.node1, self.iface1)
144         self.ec.register_connection(self.iface1, self.channel)
145         self.ec.register_connection(self.node2, self.iface2)
146         self.ec.register_connection(self.iface2, self.channel)
147
148         self.ec.register_condition(self.app2, ResourceAction.START, self.app1, ResourceState.STARTED , "3s")
149         self.ec.register_condition(self.app3, ResourceAction.START, self.app2, ResourceState.STARTED , "2s")
150         self.ec.register_condition(self.app4, ResourceAction.START, self.app3, ResourceState.STARTED , "3s")
151         self.ec.register_condition(self.app5, ResourceAction.START, [self.app3, self.app2], ResourceState.STARTED , "2s")
152         self.ec.register_condition(self.app5, ResourceAction.START, self.app1, ResourceState.STARTED , "25s")
153
154         self.ec.register_condition([self.app1, self.app2, self.app3,self.app4, self.app5], ResourceAction.STOP, self.app5, ResourceState.STARTED , "1s")
155
156     def tearDown(self):
157         self.ec.shutdown()
158
159     def test_creation_and_configuration_node(self):
160         self.assertEquals(self.ec.get(self.node1, 'hostname'), 'omf.plexus.wlab17')
161         self.assertEquals(self.ec.get(self.node1, 'xmppSlice'), 'nepi')
162         self.assertEquals(self.ec.get(self.node1, 'xmppHost'), 'xmpp-plexus.onelab.eu')
163         self.assertEquals(self.ec.get(self.node1, 'xmppPort'), '5222')
164         self.assertEquals(self.ec.get(self.node1, 'xmppPassword'), '1234')
165
166     def test_creation_and_configuration_interface(self):
167         self.assertEquals(self.ec.get(self.iface1, 'alias'), 'w0')
168         self.assertEquals(self.ec.get(self.iface1, 'mode'), 'adhoc')
169         self.assertEquals(self.ec.get(self.iface1, 'type'), 'g')
170         self.assertEquals(self.ec.get(self.iface1, 'essid'), 'vlcexp')
171         self.assertEquals(self.ec.get(self.iface1, 'ip'), '10.0.0.17')
172         self.assertEquals(self.ec.get(self.iface1, 'xmppSlice'), 'nepi')
173         self.assertEquals(self.ec.get(self.iface1, 'xmppHost'), 'xmpp-plexus.onelab.eu')
174         self.assertEquals(self.ec.get(self.iface1, 'xmppPort'), '5222')
175         self.assertEquals(self.ec.get(self.iface1, 'xmppPassword'), '1234')
176
177     def test_creation_and_configuration_channel(self):
178         self.assertEquals(self.ec.get(self.channel, 'channel'), '6')
179         self.assertEquals(self.ec.get(self.channel, 'xmppSlice'), 'nepi')
180         self.assertEquals(self.ec.get(self.channel, 'xmppHost'), 'xmpp-plexus.onelab.eu')
181         self.assertEquals(self.ec.get(self.channel, 'xmppPort'), '5222')
182         self.assertEquals(self.ec.get(self.channel, 'xmppPassword'), '1234')
183
184     def test_creation_and_configuration_application(self):
185         self.assertEquals(self.ec.get(self.app1, 'appid'), 'Vlc#1')
186         self.assertEquals(self.ec.get(self.app1, 'path'), '/opt/vlc-1.1.13/cvlc')
187         self.assertEquals(self.ec.get(self.app1, 'args'), "/opt/10-by-p0d.avi --sout '#rtp{dst=10.0.0.37,port=1234,mux=ts}'")
188         self.assertEquals(self.ec.get(self.app1, 'env'), 'DISPLAY=localhost:10.0 XAUTHORITY=/root/.Xauthority')
189         self.assertEquals(self.ec.get(self.app1, 'xmppSlice'), 'nepi')
190         self.assertEquals(self.ec.get(self.app1, 'xmppHost'), 'xmpp-plexus.onelab.eu')
191         self.assertEquals(self.ec.get(self.app1, 'xmppPort'), '5222')
192         self.assertEquals(self.ec.get(self.app1, 'xmppPassword'), '1234')
193
194     def test_connection(self):
195         self.assertEquals(len(self.ec.get_resource(self.node1).connections), 6)
196         self.assertEquals(len(self.ec.get_resource(self.iface1).connections), 2)
197         self.assertEquals(len(self.ec.get_resource(self.channel).connections), 2)
198         self.assertEquals(len(self.ec.get_resource(self.app1).connections), 1)
199         self.assertEquals(len(self.ec.get_resource(self.app2).connections), 1)
200
201     def test_condition(self):
202         self.assertEquals(len(self.ec.get_resource(self.app1).conditions[ResourceAction.STOP]), 1)
203         self.assertEquals(len(self.ec.get_resource(self.app2).conditions[ResourceAction.START]), 1)
204         self.assertEquals(len(self.ec.get_resource(self.app3).conditions[ResourceAction.START]), 1)
205         self.assertEquals(len(self.ec.get_resource(self.app4).conditions[ResourceAction.STOP]), 1)
206         self.assertEquals(len(self.ec.get_resource(self.app5).conditions[ResourceAction.START]), 2)
207
208     def test_deploy(self):
209         
210         self.ec.deploy()
211
212         self.ec.wait_finished([self.app1, self.app2, self.app3,self.app4, self.app5])
213
214         self.assertEquals(round(strfdiff(self.ec.get_resource(self.app2).start_time, self.ec.get_resource(self.app1).start_time),1), 3.0)
215         self.assertEquals(round(strfdiff(self.ec.get_resource(self.app3).start_time, self.ec.get_resource(self.app2).start_time),1), 2.0)
216         self.assertEquals(round(strfdiff(self.ec.get_resource(self.app4).start_time, self.ec.get_resource(self.app3).start_time),1), 3.0)
217         self.assertEquals(round(strfdiff(self.ec.get_resource(self.app5).start_time, self.ec.get_resource(self.app3).start_time),1), 20.0)
218         self.assertEquals(round(strfdiff(self.ec.get_resource(self.app5).start_time, self.ec.get_resource(self.app1).start_time),1), 25.0)
219         # Precision is at 1/10. So this one returns an error 7.03 != 7.0
220         #self.assertEquals(strfdiff(self.ec.get_resource(self.app5).start_time, self.ec.get_resource(self.app1).start_time), 7)
221     #In order to release everythings
222         time.sleep(1)
223
224
225 if __name__ == '__main__':
226     unittest.main()
227
228
229