2 # -*- coding: utf-8 -*-
5 from nepi.util.constants import ApplicationStatus as AS
6 from nepi.testbeds import planetlab
16 class PlanetLabExecuteTestCase(unittest.TestCase):
17 testbed_id = "planetlab"
18 slicename = "inria_nepi"
19 plchost = "nepiplc.pl.sophia.inria.fr"
21 host1 = "nepi1.pl.sophia.inria.fr"
22 host2 = "nepi2.pl.sophia.inria.fr"
24 port_base = 2000 + (os.getpid() % 1000) * 13
27 self.root_dir = tempfile.mkdtemp()
28 self.__class__.port_base = self.port_base + 100
32 shutil.rmtree(self.root_dir)
36 shutil.rmtree(self.root_dir)
38 def make_instance(self):
39 testbed_id = self.testbed_id
40 slicename = self.slicename
41 plchost = self.plchost
43 instance = planetlab.TestbedController()
44 pl_ssh_key = os.environ.get(
46 "%s/.ssh/id_rsa_planetlab" % (os.environ['HOME'],) )
47 pl_user, pl_pwd = test_util.pl_auth()
49 instance.defer_configure("homeDirectory", self.root_dir)
50 instance.defer_configure("slice", slicename)
51 instance.defer_configure("sliceSSHKey", pl_ssh_key)
52 instance.defer_configure("authUser", pl_user)
53 instance.defer_configure("authPass", pl_pwd)
54 instance.defer_configure("plcHost", plchost)
55 instance.defer_configure("tapPortBase", self.port_base)
59 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
60 def test_simple(self):
61 instance = self.make_instance()
63 instance.defer_create(2, "Node")
64 instance.defer_create_set(2, "hostname", self.host1)
65 instance.defer_create(3, "Node")
66 instance.defer_create_set(3, "hostname", self.host2)
67 instance.defer_create(4, "NodeInterface")
68 instance.defer_connect(2, "devs", 4, "node")
69 instance.defer_create(5, "NodeInterface")
70 instance.defer_connect(3, "devs", 5, "node")
71 instance.defer_create(6, "Internet")
72 instance.defer_connect(4, "inet", 6, "devs")
73 instance.defer_connect(5, "inet", 6, "devs")
74 instance.defer_create(7, "Application")
75 instance.defer_create_set(7, "command", "ping -qc1 {#[GUID-5].addr[0].[Address]#}")
76 instance.defer_add_trace(7, "stdout")
77 instance.defer_add_trace(7, "stderr")
78 instance.defer_connect(7, "node", 2, "apps")
80 comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
82 --- .* ping statistics ---
83 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
89 instance.do_connect_init()
90 instance.do_connect_compl()
91 instance.do_preconfigure()
93 # Manually replace netref
94 instance.set(7, "command",
95 instance.get(7, "command")
96 .replace("{#[GUID-5].addr[0].[Address]#}",
97 instance.get_address(5, 0, "Address") )
100 instance.do_configure()
102 instance.do_prestart()
104 while instance.status(7) != AS.STATUS_FINISHED:
106 ping_result = instance.trace(7, "stdout") or ""
111 # asserts at the end, to make sure there's proper cleanup
112 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
113 "Unexpected trace:\n" + ping_result)
115 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
116 def test_depends(self):
117 instance = self.make_instance()
119 instance.defer_create(2, "Node")
120 instance.defer_create_set(2, "hostname", self.host1)
121 instance.defer_create(3, "NodeInterface")
122 instance.defer_connect(2, "devs", 3, "node")
123 instance.defer_create(4, "Internet")
124 instance.defer_connect(3, "inet", 4, "devs")
125 instance.defer_create(5, "Application")
126 instance.defer_create_set(5, "command", "gfortran --version")
127 instance.defer_create_set(5, "depends", "gcc-gfortran")
128 instance.defer_add_trace(5, "stdout")
129 instance.defer_add_trace(5, "stderr")
130 instance.defer_connect(5, "node", 2, "apps")
135 instance.do_connect_init()
136 instance.do_connect_compl()
137 instance.do_preconfigure()
138 instance.do_configure()
140 instance.do_prestart()
142 while instance.status(5) != AS.STATUS_FINISHED:
144 ping_result = instance.trace(5, "stdout") or ""
145 comp_result = r".*GNU Fortran \(GCC\).*"
150 # asserts at the end, to make sure there's proper cleanup
151 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
152 "Unexpected trace:\n" + ping_result)
154 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
155 def test_build(self):
156 instance = self.make_instance()
158 instance.defer_create(2, "Node")
159 instance.defer_create_set(2, "hostname", self.host1)
160 instance.defer_create(3, "NodeInterface")
161 instance.defer_connect(2, "devs", 3, "node")
162 instance.defer_create(4, "Internet")
163 instance.defer_connect(3, "inet", 4, "devs")
164 instance.defer_create(10, "Application")
165 instance.defer_create_set(10, "command", "./consts")
166 instance.defer_create_set(10, "buildDepends", "gcc")
167 instance.defer_create_set(10, "build", "gcc ${SOURCES}/consts.c -o consts")
168 instance.defer_create_set(10, "install", "cp consts ${SOURCES}/consts")
169 instance.defer_create_set(10, "sources", os.path.join(os.path.dirname(planetlab.__file__),'scripts','consts.c'))
170 instance.defer_add_trace(10, "stdout")
171 instance.defer_add_trace(10, "stderr")
172 instance.defer_connect(10, "node", 2, "apps")
175 r""".*ETH_P_ALL = 0x[0-9a-fA-F]{8}
176 ETH_P_IP = 0x[0-9a-fA-F]{8}
177 TUNGETIFF = 0x[0-9a-fA-F]{8}
178 TUNSETIFF = 0x[0-9a-fA-F]{8}
179 IFF_NO_PI = 0x[0-9a-fA-F]{8}
180 IFF_TAP = 0x[0-9a-fA-F]{8}
181 IFF_TUN = 0x[0-9a-fA-F]{8}
182 IFF_VNET_HDR = 0x[0-9a-fA-F]{8}
183 TUN_PKT_STRIP = 0x[0-9a-fA-F]{8}
184 IFHWADDRLEN = 0x[0-9a-fA-F]{8}
185 IFNAMSIZ = 0x[0-9a-fA-F]{8}
186 IFREQ_SZ = 0x[0-9a-fA-F]{8}
187 FIONREAD = 0x[0-9a-fA-F]{8}.*
193 instance.do_connect_init()
194 instance.do_connect_compl()
195 instance.do_preconfigure()
196 instance.do_configure()
198 instance.do_prestart()
200 while instance.status(10) != AS.STATUS_FINISHED:
202 ping_result = instance.trace(10, "stdout") or ""
207 # asserts at the end, to make sure there's proper cleanup
208 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
209 "Unexpected trace:\n" + ping_result)
211 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
212 def test_simple_vsys(self):
213 instance = self.make_instance()
215 instance.defer_create(2, "Node")
216 instance.defer_create_set(2, "hostname", self.host1)
217 instance.defer_create(3, "NodeInterface")
218 instance.defer_connect(2, "devs", 3, "node")
219 instance.defer_create(4, "Internet")
220 instance.defer_connect(3, "inet", 4, "devs")
221 instance.defer_create(5, "TunInterface")
222 instance.defer_add_address(5, "192.168.2.2", 24, False)
223 instance.defer_connect(2, "devs", 5, "node")
224 instance.defer_create(6, "Application")
225 instance.defer_create_set(6, "command", """
227 netconfig help > /dev/null
228 test -e /vsys/vif_up.in > /dev/null
229 test -e /vsys/vif_up.out > /dev/null
230 test -e /vsys/fd_tuntap.control > /dev/null
233 instance.defer_create_set(6, "sudo", True) # only sudo has access to /vsys
234 instance.defer_add_trace(6, "stdout")
235 instance.defer_add_trace(6, "stderr")
236 instance.defer_connect(6, "node", 2, "apps")
241 instance.do_connect_init()
242 instance.do_connect_compl()
243 instance.do_preconfigure()
244 instance.do_configure()
246 instance.do_prestart()
248 while instance.status(6) != AS.STATUS_FINISHED:
250 test_result = (instance.trace(6, "stdout") or "").strip()
251 comp_result = "OKIDOKI"
256 # asserts at the end, to make sure there's proper cleanup
257 self.assertEqual(comp_result, test_result)
259 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
260 def test_emulation(self):
261 instance = self.make_instance()
263 instance.defer_create(2, "Node")
264 instance.defer_create_set(2, "hostname", self.host1)
265 instance.defer_create(3, "NodeInterface")
266 instance.defer_connect(2, "devs", 3, "node")
267 instance.defer_create(4, "Internet")
268 instance.defer_connect(3, "inet", 4, "devs")
269 instance.defer_create(7, "NetPipe")
270 instance.defer_create_set(7, "mode", "CLIENT")
271 instance.defer_create_set(7, "portList", "80")
272 instance.defer_create_set(7, "bwOut", 12.0/1024.0) # 12kbps
273 instance.defer_create_set(7, "bwIn", 64.0/1024.0) # 64kbps
274 instance.defer_create_set(7, "plrOut", 0.01) # 1% plr outbound - high loss
275 instance.defer_create_set(7, "plrIn", 0.001) # 0.1% plr inbound - regular loss
276 instance.defer_create_set(7, "delayOut", int(1500 * 8 / (12.0/1024.0) / 1000)) # tx delay at 12kbps in ms
277 instance.defer_create_set(7, "delayIn", int(1500 * 8 / (64.0/1024.0) / 1000)) # rx delay at 64kbps in ms
278 instance.defer_add_trace(7, "netpipeStats")
279 instance.defer_connect(2, "pipes", 7, "node")
280 instance.defer_create(8, "Application")
281 instance.defer_create_set(8, "command", "time wget -q -O /dev/null http://www.google.com/") # Fetch ~10kb
282 instance.defer_add_trace(8, "stdout")
283 instance.defer_add_trace(8, "stderr")
284 instance.defer_connect(8, "node", 2, "apps")
289 instance.do_connect_init()
290 instance.do_connect_compl()
291 instance.do_preconfigure()
292 instance.do_configure()
294 instance.do_prestart()
296 while instance.status(8) != AS.STATUS_FINISHED:
298 test_result = (instance.trace(8, "stderr") or "").strip()
299 comp_result = r".*real\s*(?P<min>[0-9]+)m(?P<sec>[0-9]+[.][0-9]+)s.*"
300 netpipe_stats = instance.trace(7, "netpipeStats")
306 # asserts at the end, to make sure there's proper cleanup
307 match = re.match(comp_result, test_result, re.MULTILINE)
308 self.assertTrue(match, "Unexpected output: %s" % (test_result,))
310 minutes = int(match.group("min"))
311 seconds = float(match.group("sec"))
312 self.assertTrue((minutes * 60 + seconds) > 1.0, "Emulation not effective: %s" % (test_result,))
314 self.assertTrue(netpipe_stats, "Unavailable netpipe stats")
316 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
317 def _pingtest(self, TunClass, ConnectionProto):
318 instance = self.make_instance()
320 instance.defer_create(2, "Node")
321 instance.defer_create_set(2, "hostname", self.host1)
322 instance.defer_create(3, "Node")
323 instance.defer_create_set(3, "hostname", self.host2)
324 instance.defer_create(4, "NodeInterface")
325 instance.defer_connect(2, "devs", 4, "node")
326 instance.defer_create(5, "Internet")
327 instance.defer_connect(4, "inet", 5, "devs")
328 instance.defer_create(6, "NodeInterface")
329 instance.defer_connect(3, "devs", 6, "node")
330 instance.defer_connect(6, "inet", 5, "devs")
331 instance.defer_create(7, TunClass)
332 instance.defer_add_trace(7, "packets")
333 instance.defer_add_address(7, "192.168.2.2", 24, False)
334 instance.defer_connect(2, "devs", 7, "node")
335 instance.defer_create(8, TunClass)
336 instance.defer_add_trace(8, "packets")
337 instance.defer_add_address(8, "192.168.2.3", 24, False)
338 instance.defer_connect(3, "devs", 8, "node")
339 instance.defer_connect(7, ConnectionProto, 8, ConnectionProto)
340 instance.defer_create(9, "Application")
341 instance.defer_create_set(9, "command", "ping -qc1 {#[GUID-8].addr[0].[Address]#}")
342 instance.defer_add_trace(9, "stdout")
343 instance.defer_add_trace(9, "stderr")
344 instance.defer_connect(9, "node", 2, "apps")
346 comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
348 --- .* ping statistics ---
349 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
355 instance.do_connect_init()
356 instance.do_connect_compl()
357 instance.do_preconfigure()
359 # Manually replace netref
360 instance.set(9, "command",
361 instance.get(9, "command")
362 .replace("{#[GUID-8].addr[0].[Address]#}",
363 instance.get_address(8, 0, "Address") )
366 instance.do_configure()
368 instance.do_prestart()
370 while instance.status(9) != AS.STATUS_FINISHED:
372 ping_result = instance.trace(9, "stdout") or ""
373 packets1 = instance.trace(7, "packets") or ""
374 packets2 = instance.trace(8, "packets") or ""
379 # asserts at the end, to make sure there's proper cleanup
380 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
381 "Unexpected trace:\n%s\nPackets @ source:\n%s\nPackets @ target:\n%s" % (
386 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
387 def test_tun_ping(self):
388 self._pingtest("TunInterface", "tcp")
390 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
391 def test_tun_ping_udp(self):
392 self._pingtest("TunInterface", "udp")
394 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
395 def test_tap_ping(self):
396 self._pingtest("TapInterface", "tcp")
398 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
399 def test_tap_ping_udp(self):
400 self._pingtest("TapInterface", "udp")
402 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
403 def test_nepi_depends(self):
404 instance = self.make_instance()
406 instance.defer_create(2, "Node")
407 instance.defer_create_set(2, "hostname", self.host1)
408 instance.defer_create(3, "NodeInterface")
409 instance.defer_connect(2, "devs", 3, "node")
410 instance.defer_create(4, "Internet")
411 instance.defer_connect(3, "inet", 4, "devs")
412 instance.defer_create(5, "NepiDependency")
413 instance.defer_connect(5, "node", 2, "deps")
414 instance.defer_create(12, "Application")
415 instance.defer_connect(12, "node", 2, "apps")
416 instance.defer_create_set(12, "command", "python -c 'import nepi'")
417 instance.defer_add_trace(12, "stderr")
422 instance.do_connect_init()
423 instance.do_connect_compl()
424 instance.do_preconfigure()
425 instance.do_configure()
427 instance.do_prestart()
429 while instance.status(12) != AS.STATUS_FINISHED:
431 ping_result = (instance.trace(12, "stderr") or "").strip()
436 # asserts at the end, to make sure there's proper cleanup
437 self.assertEqual(ping_result, "")
439 @test_util.skipUnless(test_util.pl_auth() is not None,
440 "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
441 @test_util.skipUnless(os.environ.get('NEPI_FULL_TESTS','').lower() in ('1','yes','true','on'),
442 "Test is expensive, requires NEPI_FULL_TESTS=yes")
443 def test_ns3_depends(self):
444 instance = self.make_instance()
446 instance.defer_create(2, "Node")
447 instance.defer_create_set(2, "hostname", self.host1)
448 instance.defer_create(3, "NodeInterface")
449 instance.defer_connect(2, "devs", 3, "node")
450 instance.defer_create(4, "Internet")
451 instance.defer_connect(3, "inet", 4, "devs")
452 instance.defer_create(5, "NepiDependency")
453 instance.defer_connect(5, "node", 2, "deps")
454 instance.defer_create(6, "NS3Dependency")
455 instance.defer_connect(6, "node", 2, "deps")
456 instance.defer_create(12, "Application")
457 instance.defer_connect(12, "node", 2, "apps")
458 instance.defer_create_set(12, "command", "python -c 'import nepi.testbeds.ns3.execute ; tb = nepi.testbeds.ns3.execute.TestbedController() ; mod = tb._load_ns3_module()'")
459 instance.defer_add_trace(12, "stderr")
464 instance.do_connect_init()
465 instance.do_connect_compl()
466 instance.do_preconfigure()
467 instance.do_configure()
469 instance.do_prestart()
471 while instance.status(12) != AS.STATUS_FINISHED:
473 ping_result = (instance.trace(12, "stderr") or "").strip()
478 # asserts at the end, to make sure there's proper cleanup
479 self.assertEqual(ping_result, "")
481 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
482 def test_discovery(self):
483 instance = self.make_instance()
485 instance.defer_create(2, "Node")
486 instance.defer_create_set(2, "operatingSystem", "f12")
487 instance.defer_create(3, "Node")
488 instance.defer_create_set(3, "operatingSystem", "f12")
489 instance.defer_create(4, "NodeInterface")
490 instance.defer_connect(2, "devs", 4, "node")
491 instance.defer_create(5, "NodeInterface")
492 instance.defer_connect(3, "devs", 5, "node")
493 instance.defer_create(6, "Internet")
494 instance.defer_connect(4, "inet", 6, "devs")
495 instance.defer_connect(5, "inet", 6, "devs")
496 instance.defer_create(7, "Application")
497 instance.defer_create_set(7, "command", "ping -qc1 {#[GUID-5].addr[0].[Address]#}")
498 instance.defer_add_trace(7, "stdout")
499 instance.defer_add_trace(7, "stderr")
500 instance.defer_connect(7, "node", 2, "apps")
502 comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
504 --- .* ping statistics ---
505 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
511 instance.do_connect_init()
512 instance.do_connect_compl()
513 instance.do_preconfigure()
515 # Manually replace netref
516 instance.set(7, "command",
517 instance.get(7, "command")
518 .replace("{#[GUID-5].addr[0].[Address]#}",
519 instance.get_address(5, 0, "Address") )
522 instance.do_configure()
524 instance.do_prestart()
526 while instance.status(7) != AS.STATUS_FINISHED:
528 ping_result = instance.trace(7, "stdout") or ""
533 # asserts at the end, to make sure there's proper cleanup
534 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
535 "Unexpected trace:\n" + ping_result)
539 if __name__ == '__main__':