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