3 # NEPI, a framework to manage network experiments
4 # Copyright (C) 2013 INRIA
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.
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.
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/>.
19 # Author: Julien Tribino <julien.tribino@inria.fr>
22 from nepi.execution.resource import ResourceFactory, ResourceManager, ResourceAction, ResourceState
23 from nepi.execution.ec import ExperimentController
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_factory import OMFAPIFactory
31 from nepi.util.timefuncs import *
36 class OMFResourceFactoryTestCase(unittest.TestCase):
37 def test_creation_phase(self):
39 self.assertEquals(OMFNode.get_rtype(), "OMFNode")
40 self.assertEquals(len(OMFNode._attributes), 8)
42 self.assertEquals(OMFWifiInterface.get_rtype(), "OMFWifiInterface")
43 self.assertEquals(len(OMFWifiInterface._attributes), 12)
45 self.assertEquals(OMFChannel.get_rtype(), "OMFChannel")
46 self.assertEquals(len(OMFChannel._attributes), 8)
48 self.assertEquals(OMFApplication.get_rtype(), "OMFApplication")
49 self.assertEquals(len(OMFApplication._attributes), 14)
51 class OMFEachTestCase(unittest.TestCase):
53 self.ec = ExperimentController(exp_id = "99999")
55 self.node1 = self.ec.register_resource("OMFNode")
56 self.ec.set(self.node1, 'hostname', 'wlab12')
57 self.ec.set(self.node1, 'xmppUser', "nepi")
58 self.ec.set(self.node1, 'xmppServer', "xmpp-plexus.onelab.eu")
59 self.ec.set(self.node1, 'xmppPort', "5222")
60 self.ec.set(self.node1, 'xmppPassword', "1234")
62 self.iface1 = self.ec.register_resource("OMFWifiInterface")
63 self.ec.set(self.iface1, 'name', "wlan0")
64 self.ec.set(self.iface1, 'mode', "adhoc")
65 self.ec.set(self.iface1, 'hw_mode', "g")
66 self.ec.set(self.iface1, 'essid', "vlcexp")
67 self.ec.set(self.iface1, 'ip', "10.0.0.17/24")
69 self.channel = self.ec.register_resource("OMFChannel")
70 self.ec.set(self.channel, 'channel', "6")
71 self.ec.set(self.channel, 'xmppUser', "nepi")
72 self.ec.set(self.channel, 'xmppServer', "xmpp-plexus.onelab.eu")
73 self.ec.set(self.channel, 'xmppPort', "5222")
74 self.ec.set(self.channel, 'xmppPassword', "1234")
76 self.app1 = self.ec.register_resource("OMFApplication")
77 self.ec.set(self.app1, 'appid', 'Vlc#1')
78 self.ec.set(self.app1, 'command', "/opt/vlc-1.1.13/cvlc /opt/10-by-p0d.avi --sout '#rtp{dst=10.0.0.37,port=1234,mux=ts}'")
79 self.ec.set(self.app1, 'env', "DISPLAY=localhost:10.0 XAUTHORITY=/root/.Xauthority")
81 self.app2 = self.ec.register_resource("OMFApplication")
83 self.app3 = self.ec.register_resource("OMFApplication")
85 self.app4 = self.ec.register_resource("OMFApplication")
87 self.app5 = self.ec.register_resource("OMFApplication")
89 self.ec.register_connection(self.app1, self.node1)
90 self.ec.register_connection(self.app2, self.node1)
91 self.ec.register_connection(self.app3, self.node1)
92 self.ec.register_connection(self.app4, self.node1)
93 self.ec.register_connection(self.app5, self.node1)
94 self.ec.register_connection(self.node1, self.iface1)
95 self.ec.register_connection(self.iface1, self.channel)
97 self.ec.register_condition(self.app2, ResourceAction.START, self.app1, ResourceState.STARTED , "3s")
98 self.ec.register_condition(self.app3, ResourceAction.START, self.app2, ResourceState.STARTED , "2s")
99 self.ec.register_condition(self.app4, ResourceAction.START, self.app3, ResourceState.STARTED , "3s")
100 self.ec.register_condition(self.app5, ResourceAction.START, [self.app3, self.app2], ResourceState.STARTED , "2s")
101 self.ec.register_condition(self.app5, ResourceAction.START, self.app1, ResourceState.STARTED , "25s")
103 self.ec.register_condition([self.app1, self.app2, self.app3,self.app4, self.app5], ResourceAction.STOP, self.app5, ResourceState.STARTED , "1s")
108 def test_creation_and_configuration_node(self):
109 self.assertEquals(self.ec.get(self.node1, 'hostname'), 'wlab12')
110 self.assertEquals(self.ec.get(self.node1, 'xmppUser'), 'nepi')
111 self.assertEquals(self.ec.get(self.node1, 'xmppServer'), 'xmpp-plexus.onelab.eu')
112 self.assertEquals(self.ec.get(self.node1, 'xmppPort'), '5222')
113 self.assertEquals(self.ec.get(self.node1, 'xmppPassword'), '1234')
114 self.assertEquals(self.ec.get(self.node1, 'version'), '6')
117 def test_creation_and_configuration_interface(self):
118 self.assertEquals(self.ec.get(self.iface1, 'name'), 'wlan0')
119 self.assertEquals(self.ec.get(self.iface1, 'mode'), 'adhoc')
120 self.assertEquals(self.ec.get(self.iface1, 'hw_mode'), 'g')
121 self.assertEquals(self.ec.get(self.iface1, 'essid'), 'vlcexp')
122 self.assertEquals(self.ec.get(self.iface1, 'ip'), '10.0.0.17/24')
123 self.assertEquals(self.ec.get(self.iface1, 'version'), '6')
125 def test_creation_and_configuration_channel(self):
126 self.assertEquals(self.ec.get(self.channel, 'channel'), '6')
127 self.assertEquals(self.ec.get(self.channel, 'xmppUser'), 'nepi')
128 self.assertEquals(self.ec.get(self.channel, 'xmppServer'), 'xmpp-plexus.onelab.eu')
129 self.assertEquals(self.ec.get(self.channel, 'xmppPort'), '5222')
130 self.assertEquals(self.ec.get(self.channel, 'xmppPassword'), '1234')
131 self.assertEquals(self.ec.get(self.channel, 'version'), '6')
134 def test_creation_and_configuration_application(self):
135 self.assertEquals(self.ec.get(self.app1, 'appid'), 'Vlc#1')
136 self.assertEquals(self.ec.get(self.app1, 'command'), "/opt/vlc-1.1.13/cvlc /opt/10-by-p0d.avi --sout '#rtp{dst=10.0.0.37,port=1234,mux=ts}'")
137 self.assertEquals(self.ec.get(self.app1, 'env'), 'DISPLAY=localhost:10.0 XAUTHORITY=/root/.Xauthority')
138 self.assertEquals(self.ec.get(self.app1, 'version'), '6')
140 def test_connection(self):
141 self.assertEquals(len(self.ec.get_resource(self.node1).connections), 6)
142 self.assertEquals(len(self.ec.get_resource(self.iface1).connections), 2)
143 self.assertEquals(len(self.ec.get_resource(self.channel).connections), 1)
144 self.assertEquals(len(self.ec.get_resource(self.app1).connections), 1)
145 self.assertEquals(len(self.ec.get_resource(self.app2).connections), 1)
147 def test_condition(self):
148 self.assertEquals(len(self.ec.get_resource(self.app1).conditions[ResourceAction.STOP]), 1)
149 self.assertEquals(len(self.ec.get_resource(self.app2).conditions[ResourceAction.START]), 1)
150 self.assertEquals(len(self.ec.get_resource(self.app3).conditions[ResourceAction.START]), 1)
151 self.assertEquals(len(self.ec.get_resource(self.app4).conditions[ResourceAction.STOP]), 1)
152 self.assertEquals(len(self.ec.get_resource(self.app5).conditions[ResourceAction.START]), 2)
154 class OMFVLCNormalCase(unittest.TestCase):
155 def test_deploy(self):
156 ec = ExperimentController(exp_id = "5421" )
158 self.node1 = ec.register_resource("OMFNode")
159 ec.set(self.node1, 'hostname', 'wlab12')
160 ec.set(self.node1, 'xmppUser', "nepi")
161 ec.set(self.node1, 'xmppServer', "xmpp-plexus.onelab.eu")
162 ec.set(self.node1, 'xmppPort', "5222")
163 ec.set(self.node1, 'xmppPassword', "1234")
165 self.iface1 = ec.register_resource("OMFWifiInterface")
166 ec.set(self.iface1, 'name', "wlan0")
167 ec.set(self.iface1, 'mode', "adhoc")
168 ec.set(self.iface1, 'hw_mode', "g")
169 ec.set(self.iface1, 'essid', "vlcexp")
170 ec.set(self.iface1, 'ip', "10.0.0.17/24")
172 self.channel = ec.register_resource("OMFChannel")
173 ec.set(self.channel, 'channel', "6")
174 ec.set(self.channel, 'xmppUser', "nepi")
175 ec.set(self.channel, 'xmppServer', "xmpp-plexus.onelab.eu")
176 ec.set(self.channel, 'xmppPort', "5222")
177 ec.set(self.channel, 'xmppPassword', "1234")
179 self.app1 = ec.register_resource("OMFApplication")
180 ec.set(self.app1, 'appid', 'Vlc#1')
181 ec.set(self.app1, 'command', "/opt/vlc-1.1.13/cvlc /opt/10-by-p0d.avi --sout '#rtp{dst=10.0.0.37,port=1234,mux=ts}'")
182 ec.set(self.app1, 'env', "DISPLAY=localhost:10.0 XAUTHORITY=/root/.Xauthority")
184 self.app2 = ec.register_resource("OMFApplication")
185 ec.set(self.app2, 'appid', 'Test#1')
186 ec.set(self.app2, 'command', "/usr/bin/test -1")
187 ec.set(self.app2, 'env', " ")
189 self.app3 = ec.register_resource("OMFApplication")
190 ec.set(self.app3, 'appid', 'Test#2')
191 ec.set(self.app3, 'command', "/usr/bin/test -2")
192 ec.set(self.app3, 'env', " ")
194 self.app4 = ec.register_resource("OMFApplication")
195 ec.set(self.app4, 'appid', 'Test#3')
196 ec.set(self.app4, 'command', "/usr/bin/test -3")
197 ec.set(self.app4, 'env', " ")
199 self.app5 = ec.register_resource("OMFApplication")
200 ec.set(self.app5, 'appid', 'Kill#2')
201 ec.set(self.app5, 'command', "/usr/bin/killall vlc")
202 ec.set(self.app5, 'env', " ")
204 ec.register_connection(self.app1, self.node1)
205 ec.register_connection(self.app2, self.node1)
206 ec.register_connection(self.app3, self.node1)
207 ec.register_connection(self.app4, self.node1)
208 ec.register_connection(self.app5, self.node1)
209 ec.register_connection(self.node1, self.iface1)
210 ec.register_connection(self.iface1, self.channel)
212 ec.register_condition(self.app2, ResourceAction.START, self.app1, ResourceState.STARTED , "3s")
213 ec.register_condition(self.app3, ResourceAction.START, self.app2, ResourceState.STARTED , "2s")
214 ec.register_condition(self.app4, ResourceAction.START, self.app3, ResourceState.STARTED , "3s")
215 ec.register_condition(self.app5, ResourceAction.START, [self.app3, self.app2], ResourceState.STARTED , "2s")
216 ec.register_condition(self.app5, ResourceAction.START, self.app1, ResourceState.STARTED , "25s")
218 ec.register_condition([self.app1, self.app2, self.app3,self.app4, self.app5], ResourceAction.STOP, self.app5, ResourceState.STARTED , "1s")
222 ec.wait_finished([self.app1, self.app2, self.app3,self.app4, self.app5])
224 self.assertGreaterEqual(round(tdiffsec(ec.get_resource(self.app2).start_time, ec.get_resource(self.app1).start_time),0), 3.0)
225 self.assertGreaterEqual(round(tdiffsec(ec.get_resource(self.app3).start_time, ec.get_resource(self.app2).start_time),0), 2.0)
226 self.assertGreaterEqual(round(tdiffsec(ec.get_resource(self.app4).start_time, ec.get_resource(self.app3).start_time),0), 3.0)
227 self.assertGreaterEqual(round(tdiffsec(ec.get_resource(self.app5).start_time, ec.get_resource(self.app3).start_time),0), 2.0)
228 self.assertGreaterEqual(round(tdiffsec(ec.get_resource(self.app5).start_time, ec.get_resource(self.app1).start_time),0), 25.0)
230 self.assertEquals(ec.get_resource(self.node1).state, ResourceState.STARTED)
231 self.assertEquals(ec.get_resource(self.iface1).state, ResourceState.STARTED)
232 self.assertEquals(ec.get_resource(self.channel).state, ResourceState.STARTED)
233 self.assertEquals(ec.get_resource(self.app1).state, ResourceState.STOPPED)
234 self.assertEquals(ec.get_resource(self.app2).state, ResourceState.STOPPED)
235 self.assertEquals(ec.get_resource(self.app3).state, ResourceState.STOPPED)
236 self.assertEquals(ec.get_resource(self.app4).state, ResourceState.STOPPED)
237 self.assertEquals(ec.get_resource(self.app5).state, ResourceState.STOPPED)
241 self.assertEquals(ec.get_resource(self.node1).state, ResourceState.RELEASED)
242 self.assertEquals(ec.get_resource(self.iface1).state, ResourceState.RELEASED)
243 self.assertEquals(ec.get_resource(self.channel).state, ResourceState.RELEASED)
244 self.assertEquals(ec.get_resource(self.app1).state, ResourceState.RELEASED)
245 self.assertEquals(ec.get_resource(self.app2).state, ResourceState.RELEASED)
246 self.assertEquals(ec.get_resource(self.app3).state, ResourceState.RELEASED)
247 self.assertEquals(ec.get_resource(self.app4).state, ResourceState.RELEASED)
248 self.assertEquals(ec.get_resource(self.app5).state, ResourceState.RELEASED)
251 if __name__ == '__main__':