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
26 PLR50_PY = os.path.join(
27 os.path.dirname(planetlab.__file__),
30 PLR50_C = os.path.join(
31 os.path.dirname(planetlab.__file__),
34 TOS_PY = os.path.join(
35 os.path.dirname(planetlab.__file__),
38 CLS_PY = os.path.join(
39 os.path.dirname(planetlab.__file__),
44 self.root_dir = tempfile.mkdtemp()
45 self.__class__.port_base = self.port_base + 100
49 shutil.rmtree(self.root_dir)
53 shutil.rmtree(self.root_dir)
55 def make_instance(self):
56 testbed_id = self.testbed_id
57 slicename = self.slicename
58 plchost = self.plchost
60 instance = planetlab.TestbedController()
61 pl_ssh_key = os.environ.get(
63 "%s/.ssh/id_rsa_planetlab" % (os.environ['HOME'],) )
64 slicename = os.environ.get(
67 pl_user, pl_pwd = test_util.pl_auth()
69 instance.defer_configure("homeDirectory", self.root_dir)
70 instance.defer_configure("slice", slicename)
71 instance.defer_configure("sliceSSHKey", pl_ssh_key)
72 instance.defer_configure("authUser", pl_user)
73 instance.defer_configure("authPass", pl_pwd)
74 instance.defer_configure("plcHost", plchost)
75 instance.defer_configure("tapPortBase", self.port_base)
76 instance.defer_configure("p2pDeployment", False) # it's interactive, we don't want it in tests
77 instance.defer_configure("dedicatedSlice", True)
79 # Hack, but we need vsys_vnet
81 vnet = instance.vsys_vnet
82 self.net_prefix = vnet.rsplit('.',1)[0]
86 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
87 def test_simple(self):
88 instance = self.make_instance()
90 instance.defer_create(2, "Node")
91 instance.defer_create_set(2, "hostname", self.host1)
92 instance.defer_create(3, "Node")
93 instance.defer_create_set(3, "hostname", self.host2)
94 instance.defer_create(4, "NodeInterface")
95 instance.defer_connect(2, "devs", 4, "node")
96 instance.defer_create(5, "NodeInterface")
97 instance.defer_connect(3, "devs", 5, "node")
98 instance.defer_create(6, "Internet")
99 instance.defer_connect(4, "inet", 6, "devs")
100 instance.defer_connect(5, "inet", 6, "devs")
101 instance.defer_create(7, "Application")
102 instance.defer_create_set(7, "command", "ping -qc1 {#[GUID-5].addr[0].[Address]#}")
103 instance.defer_add_trace(7, "stdout")
104 instance.defer_add_trace(7, "stderr")
105 instance.defer_connect(7, "node", 2, "apps")
107 comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
109 --- .* ping statistics ---
110 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
116 instance.do_connect_init()
117 instance.do_connect_compl()
118 instance.do_preconfigure()
120 # Manually replace netref
121 instance.set(7, "command",
122 instance.get(7, "command")
123 .replace("{#[GUID-5].addr[0].[Address]#}",
124 instance.get_address(5, 0, "Address") )
127 instance.do_configure()
129 instance.do_prestart()
131 while instance.status(7) != AS.STATUS_FINISHED:
133 ping_result = instance.trace(7, "stdout") or ""
141 # asserts at the end, to make sure there's proper cleanup
142 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
143 "Unexpected trace:\n" + ping_result)
145 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
146 def test_depends(self):
147 instance = self.make_instance()
149 instance.defer_create(2, "Node")
150 instance.defer_create_set(2, "hostname", self.host1)
151 instance.defer_create(3, "NodeInterface")
152 instance.defer_connect(2, "devs", 3, "node")
153 instance.defer_create(4, "Internet")
154 instance.defer_connect(3, "inet", 4, "devs")
155 instance.defer_create(5, "Application")
156 instance.defer_create_set(5, "command", "gfortran --version")
157 instance.defer_create_set(5, "depends", "gcc-gfortran")
158 instance.defer_add_trace(5, "stdout")
159 instance.defer_add_trace(5, "stderr")
160 instance.defer_connect(5, "node", 2, "apps")
165 instance.do_connect_init()
166 instance.do_connect_compl()
167 instance.do_preconfigure()
168 instance.do_configure()
170 instance.do_prestart()
172 while instance.status(5) != AS.STATUS_FINISHED:
174 ping_result = instance.trace(5, "stdout") or ""
175 comp_result = r".*GNU Fortran \(GCC\).*"
183 # asserts at the end, to make sure there's proper cleanup
184 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
185 "Unexpected trace:\n" + ping_result)
187 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
188 def test_build(self):
189 instance = self.make_instance()
191 instance.defer_create(2, "Node")
192 instance.defer_create_set(2, "hostname", self.host1)
193 instance.defer_create(3, "NodeInterface")
194 instance.defer_connect(2, "devs", 3, "node")
195 instance.defer_create(4, "Internet")
196 instance.defer_connect(3, "inet", 4, "devs")
197 instance.defer_create(10, "Application")
198 instance.defer_create_set(10, "command", "./consts")
199 instance.defer_create_set(10, "buildDepends", "gcc")
200 instance.defer_create_set(10, "build", "gcc ${SOURCES}/consts.c -o consts")
201 instance.defer_create_set(10, "install", "cp consts ${SOURCES}/consts")
202 instance.defer_create_set(10, "sources", os.path.join(os.path.dirname(planetlab.__file__),'scripts','consts.c'))
203 instance.defer_add_trace(10, "stdout")
204 instance.defer_add_trace(10, "stderr")
205 instance.defer_connect(10, "node", 2, "apps")
208 r""".*ETH_P_ALL = 0x[0-9a-fA-F]{8}
209 ETH_P_IP = 0x[0-9a-fA-F]{8}
210 TUNGETIFF = 0x[0-9a-fA-F]{8}
211 TUNSETIFF = 0x[0-9a-fA-F]{8}
212 IFF_NO_PI = 0x[0-9a-fA-F]{8}
213 IFF_TAP = 0x[0-9a-fA-F]{8}
214 IFF_TUN = 0x[0-9a-fA-F]{8}
215 IFF_VNET_HDR = 0x[0-9a-fA-F]{8}
216 TUN_PKT_STRIP = 0x[0-9a-fA-F]{8}
217 IFHWADDRLEN = 0x[0-9a-fA-F]{8}
218 IFNAMSIZ = 0x[0-9a-fA-F]{8}
219 IFREQ_SZ = 0x[0-9a-fA-F]{8}
220 FIONREAD = 0x[0-9a-fA-F]{8}.*
226 instance.do_connect_init()
227 instance.do_connect_compl()
228 instance.do_preconfigure()
229 instance.do_configure()
231 instance.do_prestart()
233 while instance.status(10) != AS.STATUS_FINISHED:
235 ping_result = instance.trace(10, "stdout") or ""
243 # asserts at the end, to make sure there's proper cleanup
244 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
245 "Unexpected trace:\n" + ping_result)
247 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
248 def test_simple_vsys(self):
249 instance = self.make_instance()
251 instance.defer_create(2, "Node")
252 instance.defer_create_set(2, "hostname", self.host1)
253 instance.defer_create(3, "NodeInterface")
254 instance.defer_connect(2, "devs", 3, "node")
255 instance.defer_create(4, "Internet")
256 instance.defer_connect(3, "inet", 4, "devs")
257 instance.defer_create(5, "TunInterface")
258 instance.defer_add_address(5, self.net_prefix+".2", 24, False)
259 instance.defer_connect(2, "devs", 5, "node")
260 instance.defer_create(6, "Application")
261 instance.defer_create_set(6, "command", """
263 netconfig help > /dev/null
264 test -e /vsys/vif_up.in > /dev/null
265 test -e /vsys/vif_up.out > /dev/null
266 test -e /vsys/fd_tuntap.control > /dev/null
269 instance.defer_create_set(6, "sudo", True) # only sudo has access to /vsys
270 instance.defer_add_trace(6, "stdout")
271 instance.defer_add_trace(6, "stderr")
272 instance.defer_connect(6, "node", 2, "apps")
277 instance.do_connect_init()
278 instance.do_connect_compl()
279 instance.do_preconfigure()
280 instance.do_configure()
282 instance.do_prestart()
284 while instance.status(6) != AS.STATUS_FINISHED:
286 test_result = (instance.trace(6, "stdout") or "").strip()
287 comp_result = "OKIDOKI"
295 # asserts at the end, to make sure there's proper cleanup
296 self.assertEqual(comp_result, test_result)
298 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
299 def test_emulation(self):
300 instance = self.make_instance()
302 instance.defer_create(2, "Node")
303 instance.defer_create_set(2, "hostname", self.host1)
304 instance.defer_create(3, "NodeInterface")
305 instance.defer_connect(2, "devs", 3, "node")
306 instance.defer_create(4, "Internet")
307 instance.defer_connect(3, "inet", 4, "devs")
308 instance.defer_create(7, "NetPipe")
309 instance.defer_create_set(7, "mode", "CLIENT")
310 instance.defer_create_set(7, "portList", "80")
311 instance.defer_create_set(7, "bwOut", 12.0/1024.0) # 12kbps
312 instance.defer_create_set(7, "bwIn", 64.0/1024.0) # 64kbps
313 instance.defer_create_set(7, "plrOut", 0.01) # 1% plr outbound - high loss
314 instance.defer_create_set(7, "plrIn", 0.001) # 0.1% plr inbound - regular loss
315 instance.defer_create_set(7, "delayOut", int(1500 * 8 / (12.0/1024.0) / 1000)) # tx delay at 12kbps in ms
316 instance.defer_create_set(7, "delayIn", int(1500 * 8 / (64.0/1024.0) / 1000)) # rx delay at 64kbps in ms
317 instance.defer_add_trace(7, "netpipeStats")
318 instance.defer_connect(2, "pipes", 7, "node")
319 instance.defer_create(8, "Application")
320 instance.defer_create_set(8, "command", "time wget -q -O /dev/null http://www.google.com/") # Fetch ~10kb
321 instance.defer_add_trace(8, "stdout")
322 instance.defer_add_trace(8, "stderr")
323 instance.defer_connect(8, "node", 2, "apps")
328 instance.do_connect_init()
329 instance.do_connect_compl()
330 instance.do_preconfigure()
331 instance.do_configure()
333 instance.do_prestart()
335 while instance.status(8) != AS.STATUS_FINISHED:
337 test_result = (instance.trace(8, "stderr") or "").strip()
338 comp_result = r".*real\s*(?P<min>[0-9]+)m(?P<sec>[0-9]+[.][0-9]+)s.*"
339 netpipe_stats = instance.trace(7, "netpipeStats")
348 # asserts at the end, to make sure there's proper cleanup
349 match = re.match(comp_result, test_result, re.MULTILINE)
350 self.assertTrue(match, "Unexpected output: %s" % (test_result,))
352 minutes = int(match.group("min"))
353 seconds = float(match.group("sec"))
354 self.assertTrue((minutes * 60 + seconds) > 1.0, "Emulation not effective: %s" % (test_result,))
356 self.assertTrue(netpipe_stats, "Unavailable netpipe stats")
358 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
359 def _pingtest(self, TunClass, ConnectionProto, Cipher, Filter1=None, Filter2=None, Filter1args=None, Filter2args=None, PLREX=None):
360 instance = self.make_instance()
362 instance.defer_create(2, "Node")
363 instance.defer_create_set(2, "hostname", self.host1)
364 instance.defer_create(3, "Node")
365 instance.defer_create_set(3, "hostname", self.host2)
366 instance.defer_create(4, "NodeInterface")
367 instance.defer_connect(2, "devs", 4, "node")
368 instance.defer_create(5, "Internet")
369 instance.defer_connect(4, "inet", 5, "devs")
370 instance.defer_create(6, "NodeInterface")
371 instance.defer_connect(3, "devs", 6, "node")
372 instance.defer_connect(6, "inet", 5, "devs")
373 instance.defer_create(7, TunClass)
374 instance.defer_create_set(7, "tun_cipher", Cipher)
375 instance.defer_add_trace(7, "packets")
376 instance.defer_add_address(7, self.net_prefix+".2", 24, False)
377 instance.defer_connect(2, "devs", 7, "node")
378 instance.defer_create(8, TunClass)
379 instance.defer_create_set(8, "tun_cipher", Cipher)
380 instance.defer_add_trace(8, "packets")
381 instance.defer_add_address(8, self.net_prefix+".3", 24, False)
382 instance.defer_connect(3, "devs", 8, "node")
383 instance.defer_create(9, "Application")
384 instance.defer_create_set(9, "command", "ping -qc10 {#[GUID-8].addr[0].[Address]#}")
385 instance.defer_add_trace(9, "stdout")
386 instance.defer_add_trace(9, "stderr")
387 instance.defer_connect(9, "node", 2, "apps")
390 instance.defer_create(10, "TunFilter")
391 instance.defer_create_set(10, "module", Filter1)
393 instance.defer_create_set(10, "args", Filter1args)
394 instance.defer_connect(7, "fd->", 10, "->fd")
397 instance.defer_create(11, "TunFilter")
398 instance.defer_create_set(11, "module", Filter2)
400 instance.defer_create_set(11, "args", Filter2args)
401 instance.defer_connect(8, "fd->", 11, "->fd")
404 if Filter1 and Filter2:
406 elif Filter1 or Filter2:
413 instance.defer_connect(
414 (10 if Filter1 else 7), ConnectionProto,
415 (11 if Filter2 else 8), ConnectionProto)
417 comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
419 --- .* ping statistics ---
420 10 packets transmitted, [0-9]+ received,.* %s%% packet loss, time \d*ms.*
426 instance.do_connect_init()
427 instance.do_connect_compl()
428 instance.do_preconfigure()
430 # Manually replace netref
431 instance.set(9, "command",
432 instance.get(9, "command")
433 .replace("{#[GUID-8].addr[0].[Address]#}",
434 instance.get_address(8, 0, "Address") )
437 instance.do_configure()
439 instance.do_prestart()
441 while instance.status(9) != AS.STATUS_FINISHED:
443 ping_result = instance.trace(9, "stdout") or ""
444 packets1 = instance.trace(7, "packets") or ""
445 packets2 = instance.trace(8, "packets") or ""
453 # asserts at the end, to make sure there's proper cleanup
454 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
455 "Unexpected trace:\n%s\nPackets @ source:\n%s\nPackets @ target:\n%s" % (
460 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
461 def test_tun_ping(self):
462 self._pingtest("TunInterface", "tcp", "AES")
464 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
465 def test_tun_ping_udp(self):
466 self._pingtest("TunInterface", "udp", "AES")
468 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
469 def test_tun_ping_gre(self):
470 self._pingtest("TunInterface", "gre", "PLAIN")
472 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
473 def test_tap_ping(self):
474 self._pingtest("TapInterface", "tcp", "AES")
476 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
477 def test_tap_ping_udp(self):
478 self._pingtest("TapInterface", "udp", "AES")
480 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
481 def test_tap_ping_gre(self):
482 self._pingtest("TapInterface", "gre", "PLAIN")
484 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
485 def test_tap_ping_udp_loss1_py(self):
486 self._pingtest("TapInterface", "udp", "AES", self.PLR50_PY, None, "plr=50")
488 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
489 def test_tap_ping_udp_loss2_py(self):
490 self._pingtest("TapInterface", "udp", "AES", self.PLR50_PY, self.PLR50_PY, "plr=40", "plr=40")
492 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
493 def test_tap_ping_udp_loss1_c(self):
494 self._pingtest("TapInterface", "udp", "AES", self.PLR50_C, None, "plr=50")
496 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
497 def test_tap_ping_udp_loss2_c(self):
498 self._pingtest("TapInterface", "udp", "AES", self.PLR50_C, self.PLR50_C, "plr=40", "plr=40")
500 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
501 def test_tap_ping_udp_tos(self):
502 self._pingtest("TunInterface", "udp", "AES", self.TOS_PY, self.TOS_PY, "size=1000", "size=1000", "0")
504 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
505 def test_tap_ping_udp_class(self):
506 self._pingtest("TunInterface", "udp", "AES", self.CLS_PY, self.CLS_PY, "size=10", "size=10", "0")
508 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
509 def test_nepi_depends(self):
510 instance = self.make_instance()
512 instance.defer_create(2, "Node")
513 instance.defer_create_set(2, "hostname", self.host1)
514 instance.defer_create(3, "NodeInterface")
515 instance.defer_connect(2, "devs", 3, "node")
516 instance.defer_create(4, "Internet")
517 instance.defer_connect(3, "inet", 4, "devs")
518 instance.defer_create(5, "NepiDependency")
519 instance.defer_connect(5, "node", 2, "deps")
520 instance.defer_create(12, "Application")
521 instance.defer_connect(12, "node", 2, "apps")
522 instance.defer_create_set(12, "command", "python -c 'import nepi'")
523 instance.defer_add_trace(12, "stderr")
528 instance.do_connect_init()
529 instance.do_connect_compl()
530 instance.do_preconfigure()
531 instance.do_configure()
533 instance.do_prestart()
535 while instance.status(12) != AS.STATUS_FINISHED:
537 ping_result = (instance.trace(12, "stderr") or "").strip()
545 # asserts at the end, to make sure there's proper cleanup
546 self.assertEqual(ping_result, "")
548 @test_util.skipUnless(test_util.pl_auth() is not None,
549 "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
550 @test_util.skipUnless(os.environ.get('NEPI_FULL_TESTS','').lower() in ('1','yes','true','on'),
551 "Test is expensive, requires NEPI_FULL_TESTS=yes")
552 def test_ns3_depends(self):
553 instance = self.make_instance()
555 instance.defer_create(2, "Node")
556 instance.defer_create_set(2, "hostname", self.host1)
557 instance.defer_create(3, "NodeInterface")
558 instance.defer_connect(2, "devs", 3, "node")
559 instance.defer_create(4, "Internet")
560 instance.defer_connect(3, "inet", 4, "devs")
561 instance.defer_create(5, "NepiDependency")
562 instance.defer_connect(5, "node", 2, "deps")
563 instance.defer_create(6, "NS3Dependency")
564 instance.defer_connect(6, "node", 2, "deps")
565 instance.defer_create(12, "Application")
566 instance.defer_connect(12, "node", 2, "apps")
567 instance.defer_create_set(12, "command", "python -c 'import nepi.testbeds.ns3.execute ; tb = nepi.testbeds.ns3.execute.TestbedController() ; mod = tb._configure_ns3_module()'")
568 instance.defer_add_trace(12, "stderr")
573 instance.do_connect_init()
574 instance.do_connect_compl()
575 instance.do_preconfigure()
576 instance.do_configure()
578 instance.do_prestart()
580 while instance.status(12) != AS.STATUS_FINISHED:
582 ping_result = (instance.trace(12, "stderr") or "").strip()
590 # asserts at the end, to make sure there's proper cleanup
591 self.assertEqual(ping_result, "")
593 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
594 def test_discovery(self):
595 instance = self.make_instance()
597 instance.defer_create(2, "Node")
598 instance.defer_create_set(2, "operatingSystem", "f12")
599 instance.defer_create(3, "Node")
600 instance.defer_create_set(3, "operatingSystem", "f12")
601 instance.defer_create(4, "NodeInterface")
602 instance.defer_connect(2, "devs", 4, "node")
603 instance.defer_create(5, "NodeInterface")
604 instance.defer_connect(3, "devs", 5, "node")
605 instance.defer_create(6, "Internet")
606 instance.defer_connect(4, "inet", 6, "devs")
607 instance.defer_connect(5, "inet", 6, "devs")
608 instance.defer_create(7, "Application")
609 instance.defer_create_set(7, "command", "ping -qc1 {#[GUID-5].addr[0].[Address]#}")
610 instance.defer_add_trace(7, "stdout")
611 instance.defer_add_trace(7, "stderr")
612 instance.defer_connect(7, "node", 2, "apps")
614 comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
616 --- .* ping statistics ---
617 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
623 instance.do_connect_init()
624 instance.do_connect_compl()
625 instance.do_preconfigure()
627 # Manually replace netref
628 instance.set(7, "command",
629 instance.get(7, "command")
630 .replace("{#[GUID-5].addr[0].[Address]#}",
631 instance.get_address(5, 0, "Address") )
634 instance.do_configure()
636 instance.do_prestart()
638 while instance.status(7) != AS.STATUS_FINISHED:
640 ping_result = instance.trace(7, "stdout") or ""
648 # asserts at the end, to make sure there's proper cleanup
649 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
650 "Unexpected trace:\n" + ping_result)
654 if __name__ == '__main__':