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__),
40 self.root_dir = tempfile.mkdtemp()
41 self.__class__.port_base = self.port_base + 100
45 shutil.rmtree(self.root_dir)
49 shutil.rmtree(self.root_dir)
51 def make_instance(self):
52 testbed_id = self.testbed_id
53 slicename = self.slicename
54 plchost = self.plchost
56 instance = planetlab.TestbedController()
57 pl_ssh_key = os.environ.get(
59 "%s/.ssh/id_rsa_planetlab" % (os.environ['HOME'],) )
60 slicename = os.environ.get(
63 pl_user, pl_pwd = test_util.pl_auth()
65 instance.defer_configure("homeDirectory", self.root_dir)
66 instance.defer_configure("slice", slicename)
67 instance.defer_configure("sliceSSHKey", pl_ssh_key)
68 instance.defer_configure("authUser", pl_user)
69 instance.defer_configure("authPass", pl_pwd)
70 instance.defer_configure("plcHost", plchost)
71 instance.defer_configure("tapPortBase", self.port_base)
72 instance.defer_configure("p2pDeployment", False) # it's interactive, we don't want it in tests
73 instance.defer_configure("dedicatedSlice", True)
75 # Hack, but we need vsys_vnet
77 vnet = instance.vsys_vnet
78 self.net_prefix = vnet.rsplit('.',1)[0]
82 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
83 def test_simple(self):
84 instance = self.make_instance()
86 instance.defer_create(2, "Node")
87 instance.defer_create_set(2, "hostname", self.host1)
88 instance.defer_create(3, "Node")
89 instance.defer_create_set(3, "hostname", self.host2)
90 instance.defer_create(4, "NodeInterface")
91 instance.defer_connect(2, "devs", 4, "node")
92 instance.defer_create(5, "NodeInterface")
93 instance.defer_connect(3, "devs", 5, "node")
94 instance.defer_create(6, "Internet")
95 instance.defer_connect(4, "inet", 6, "devs")
96 instance.defer_connect(5, "inet", 6, "devs")
97 instance.defer_create(7, "Application")
98 instance.defer_create_set(7, "command", "ping -qc1 {#[GUID-5].addr[0].[Address]#}")
99 instance.defer_add_trace(7, "stdout")
100 instance.defer_add_trace(7, "stderr")
101 instance.defer_connect(7, "node", 2, "apps")
103 comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
105 --- .* ping statistics ---
106 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
112 instance.do_connect_init()
113 instance.do_connect_compl()
114 instance.do_preconfigure()
116 # Manually replace netref
117 instance.set(7, "command",
118 instance.get(7, "command")
119 .replace("{#[GUID-5].addr[0].[Address]#}",
120 instance.get_address(5, 0, "Address") )
123 instance.do_configure()
125 instance.do_prestart()
127 while instance.status(7) != AS.STATUS_FINISHED:
129 ping_result = instance.trace(7, "stdout") or ""
137 # asserts at the end, to make sure there's proper cleanup
138 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
139 "Unexpected trace:\n" + ping_result)
141 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
142 def test_depends(self):
143 instance = self.make_instance()
145 instance.defer_create(2, "Node")
146 instance.defer_create_set(2, "hostname", self.host1)
147 instance.defer_create(3, "NodeInterface")
148 instance.defer_connect(2, "devs", 3, "node")
149 instance.defer_create(4, "Internet")
150 instance.defer_connect(3, "inet", 4, "devs")
151 instance.defer_create(5, "Application")
152 instance.defer_create_set(5, "command", "gfortran --version")
153 instance.defer_create_set(5, "depends", "gcc-gfortran")
154 instance.defer_add_trace(5, "stdout")
155 instance.defer_add_trace(5, "stderr")
156 instance.defer_connect(5, "node", 2, "apps")
161 instance.do_connect_init()
162 instance.do_connect_compl()
163 instance.do_preconfigure()
164 instance.do_configure()
166 instance.do_prestart()
168 while instance.status(5) != AS.STATUS_FINISHED:
170 ping_result = instance.trace(5, "stdout") or ""
171 comp_result = r".*GNU Fortran \(GCC\).*"
179 # asserts at the end, to make sure there's proper cleanup
180 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
181 "Unexpected trace:\n" + ping_result)
183 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
184 def test_build(self):
185 instance = self.make_instance()
187 instance.defer_create(2, "Node")
188 instance.defer_create_set(2, "hostname", self.host1)
189 instance.defer_create(3, "NodeInterface")
190 instance.defer_connect(2, "devs", 3, "node")
191 instance.defer_create(4, "Internet")
192 instance.defer_connect(3, "inet", 4, "devs")
193 instance.defer_create(10, "Application")
194 instance.defer_create_set(10, "command", "./consts")
195 instance.defer_create_set(10, "buildDepends", "gcc")
196 instance.defer_create_set(10, "build", "gcc ${SOURCES}/consts.c -o consts")
197 instance.defer_create_set(10, "install", "cp consts ${SOURCES}/consts")
198 instance.defer_create_set(10, "sources", os.path.join(os.path.dirname(planetlab.__file__),'scripts','consts.c'))
199 instance.defer_add_trace(10, "stdout")
200 instance.defer_add_trace(10, "stderr")
201 instance.defer_connect(10, "node", 2, "apps")
204 r""".*ETH_P_ALL = 0x[0-9a-fA-F]{8}
205 ETH_P_IP = 0x[0-9a-fA-F]{8}
206 TUNGETIFF = 0x[0-9a-fA-F]{8}
207 TUNSETIFF = 0x[0-9a-fA-F]{8}
208 IFF_NO_PI = 0x[0-9a-fA-F]{8}
209 IFF_TAP = 0x[0-9a-fA-F]{8}
210 IFF_TUN = 0x[0-9a-fA-F]{8}
211 IFF_VNET_HDR = 0x[0-9a-fA-F]{8}
212 TUN_PKT_STRIP = 0x[0-9a-fA-F]{8}
213 IFHWADDRLEN = 0x[0-9a-fA-F]{8}
214 IFNAMSIZ = 0x[0-9a-fA-F]{8}
215 IFREQ_SZ = 0x[0-9a-fA-F]{8}
216 FIONREAD = 0x[0-9a-fA-F]{8}.*
222 instance.do_connect_init()
223 instance.do_connect_compl()
224 instance.do_preconfigure()
225 instance.do_configure()
227 instance.do_prestart()
229 while instance.status(10) != AS.STATUS_FINISHED:
231 ping_result = instance.trace(10, "stdout") or ""
239 # asserts at the end, to make sure there's proper cleanup
240 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
241 "Unexpected trace:\n" + ping_result)
243 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
244 def test_simple_vsys(self):
245 instance = self.make_instance()
247 instance.defer_create(2, "Node")
248 instance.defer_create_set(2, "hostname", self.host1)
249 instance.defer_create(3, "NodeInterface")
250 instance.defer_connect(2, "devs", 3, "node")
251 instance.defer_create(4, "Internet")
252 instance.defer_connect(3, "inet", 4, "devs")
253 instance.defer_create(5, "TunInterface")
254 instance.defer_add_address(5, self.net_prefix+".2", 24, False)
255 instance.defer_connect(2, "devs", 5, "node")
256 instance.defer_create(6, "Application")
257 instance.defer_create_set(6, "command", """
259 netconfig help > /dev/null
260 test -e /vsys/vif_up.in > /dev/null
261 test -e /vsys/vif_up.out > /dev/null
262 test -e /vsys/fd_tuntap.control > /dev/null
265 instance.defer_create_set(6, "sudo", True) # only sudo has access to /vsys
266 instance.defer_add_trace(6, "stdout")
267 instance.defer_add_trace(6, "stderr")
268 instance.defer_connect(6, "node", 2, "apps")
273 instance.do_connect_init()
274 instance.do_connect_compl()
275 instance.do_preconfigure()
276 instance.do_configure()
278 instance.do_prestart()
280 while instance.status(6) != AS.STATUS_FINISHED:
282 test_result = (instance.trace(6, "stdout") or "").strip()
283 comp_result = "OKIDOKI"
291 # asserts at the end, to make sure there's proper cleanup
292 self.assertEqual(comp_result, test_result)
294 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
295 def test_emulation(self):
296 instance = self.make_instance()
298 instance.defer_create(2, "Node")
299 instance.defer_create_set(2, "hostname", self.host1)
300 instance.defer_create(3, "NodeInterface")
301 instance.defer_connect(2, "devs", 3, "node")
302 instance.defer_create(4, "Internet")
303 instance.defer_connect(3, "inet", 4, "devs")
304 instance.defer_create(7, "NetPipe")
305 instance.defer_create_set(7, "mode", "CLIENT")
306 instance.defer_create_set(7, "portList", "80")
307 instance.defer_create_set(7, "bwOut", 12.0/1024.0) # 12kbps
308 instance.defer_create_set(7, "bwIn", 64.0/1024.0) # 64kbps
309 instance.defer_create_set(7, "plrOut", 0.01) # 1% plr outbound - high loss
310 instance.defer_create_set(7, "plrIn", 0.001) # 0.1% plr inbound - regular loss
311 instance.defer_create_set(7, "delayOut", int(1500 * 8 / (12.0/1024.0) / 1000)) # tx delay at 12kbps in ms
312 instance.defer_create_set(7, "delayIn", int(1500 * 8 / (64.0/1024.0) / 1000)) # rx delay at 64kbps in ms
313 instance.defer_add_trace(7, "netpipeStats")
314 instance.defer_connect(2, "pipes", 7, "node")
315 instance.defer_create(8, "Application")
316 instance.defer_create_set(8, "command", "time wget -q -O /dev/null http://www.google.com/") # Fetch ~10kb
317 instance.defer_add_trace(8, "stdout")
318 instance.defer_add_trace(8, "stderr")
319 instance.defer_connect(8, "node", 2, "apps")
324 instance.do_connect_init()
325 instance.do_connect_compl()
326 instance.do_preconfigure()
327 instance.do_configure()
329 instance.do_prestart()
331 while instance.status(8) != AS.STATUS_FINISHED:
333 test_result = (instance.trace(8, "stderr") or "").strip()
334 comp_result = r".*real\s*(?P<min>[0-9]+)m(?P<sec>[0-9]+[.][0-9]+)s.*"
335 netpipe_stats = instance.trace(7, "netpipeStats")
344 # asserts at the end, to make sure there's proper cleanup
345 match = re.match(comp_result, test_result, re.MULTILINE)
346 self.assertTrue(match, "Unexpected output: %s" % (test_result,))
348 minutes = int(match.group("min"))
349 seconds = float(match.group("sec"))
350 self.assertTrue((minutes * 60 + seconds) > 1.0, "Emulation not effective: %s" % (test_result,))
352 self.assertTrue(netpipe_stats, "Unavailable netpipe stats")
354 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
355 def _pingtest(self, TunClass, ConnectionProto, Cipher, Filter1=None, Filter2=None, Filter1args=None, Filter2args=None):
356 instance = self.make_instance()
358 instance.defer_create(2, "Node")
359 instance.defer_create_set(2, "hostname", self.host1)
360 instance.defer_create(3, "Node")
361 instance.defer_create_set(3, "hostname", self.host2)
362 instance.defer_create(4, "NodeInterface")
363 instance.defer_connect(2, "devs", 4, "node")
364 instance.defer_create(5, "Internet")
365 instance.defer_connect(4, "inet", 5, "devs")
366 instance.defer_create(6, "NodeInterface")
367 instance.defer_connect(3, "devs", 6, "node")
368 instance.defer_connect(6, "inet", 5, "devs")
369 instance.defer_create(7, TunClass)
370 instance.defer_create_set(7, "tun_cipher", Cipher)
371 instance.defer_add_trace(7, "packets")
372 instance.defer_add_address(7, self.net_prefix+".2", 24, False)
373 instance.defer_connect(2, "devs", 7, "node")
374 instance.defer_create(8, TunClass)
375 instance.defer_create_set(8, "tun_cipher", Cipher)
376 instance.defer_add_trace(8, "packets")
377 instance.defer_add_address(8, self.net_prefix+".3", 24, False)
378 instance.defer_connect(3, "devs", 8, "node")
379 instance.defer_create(9, "Application")
380 instance.defer_create_set(9, "command", "ping -qc10 {#[GUID-8].addr[0].[Address]#}")
381 instance.defer_add_trace(9, "stdout")
382 instance.defer_add_trace(9, "stderr")
383 instance.defer_connect(9, "node", 2, "apps")
386 instance.defer_create(10, "TunFilter")
387 instance.defer_create_set(10, "module", Filter1)
389 instance.defer_create_set(10, "args", Filter1args)
390 instance.defer_connect(7, "fd->", 10, "->fd")
393 instance.defer_create(11, "TunFilter")
394 instance.defer_create_set(11, "module", Filter2)
396 instance.defer_create_set(11, "args", Filter2args)
397 instance.defer_connect(8, "fd->", 11, "->fd")
399 if Filter1 and Filter2:
401 elif Filter1 or Filter2:
406 instance.defer_connect(
407 (10 if Filter1 else 7), ConnectionProto,
408 (11 if Filter2 else 8), ConnectionProto)
410 comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
412 --- .* ping statistics ---
413 10 packets transmitted, [0-9]+ received,.* %s%% packet loss, time \d*ms.*
419 instance.do_connect_init()
420 instance.do_connect_compl()
421 instance.do_preconfigure()
423 # Manually replace netref
424 instance.set(9, "command",
425 instance.get(9, "command")
426 .replace("{#[GUID-8].addr[0].[Address]#}",
427 instance.get_address(8, 0, "Address") )
430 instance.do_configure()
432 instance.do_prestart()
434 while instance.status(9) != AS.STATUS_FINISHED:
436 ping_result = instance.trace(9, "stdout") or ""
437 packets1 = instance.trace(7, "packets") or ""
438 packets2 = instance.trace(8, "packets") or ""
446 # asserts at the end, to make sure there's proper cleanup
447 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
448 "Unexpected trace:\n%s\nPackets @ source:\n%s\nPackets @ target:\n%s" % (
453 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
454 def test_tun_ping(self):
455 self._pingtest("TunInterface", "tcp", "AES")
457 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
458 def test_tun_ping_udp(self):
459 self._pingtest("TunInterface", "udp", "AES")
461 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
462 def test_tun_ping_gre(self):
463 self._pingtest("TunInterface", "gre", "PLAIN")
465 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
466 def test_tap_ping(self):
467 self._pingtest("TapInterface", "tcp", "AES")
469 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
470 def test_tap_ping_udp(self):
471 self._pingtest("TapInterface", "udp", "AES")
473 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
474 def test_tap_ping_gre(self):
475 self._pingtest("TapInterface", "gre", "PLAIN")
477 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
478 def test_tap_ping_udp_loss1_py(self):
479 self._pingtest("TapInterface", "udp", "AES", self.PLR50_PY, None, "plr=50")
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_tap_ping_udp_loss2_py(self):
483 self._pingtest("TapInterface", "udp", "AES", self.PLR50_PY, self.PLR50_PY, "plr=50", "plr=50")
485 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
486 def test_tap_ping_udp_loss1_c(self):
487 self._pingtest("TapInterface", "udp", "AES", self.PLR50_C, None, "plr=50")
489 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
490 def test_tap_ping_udp_loss2_c(self):
491 self._pingtest("TapInterface", "udp", "AES", self.PLR50_C, self.PLR50_C, "plr=50", "plr=50")
493 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
494 def test_tap_ping_udp_tos(self):
495 self._pingtest("TapInterface", "udp", "AES", self.TOS_PY, self.TOS_PY, "size=1000", "size=1000")
497 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
498 def test_nepi_depends(self):
499 instance = self.make_instance()
501 instance.defer_create(2, "Node")
502 instance.defer_create_set(2, "hostname", self.host1)
503 instance.defer_create(3, "NodeInterface")
504 instance.defer_connect(2, "devs", 3, "node")
505 instance.defer_create(4, "Internet")
506 instance.defer_connect(3, "inet", 4, "devs")
507 instance.defer_create(5, "NepiDependency")
508 instance.defer_connect(5, "node", 2, "deps")
509 instance.defer_create(12, "Application")
510 instance.defer_connect(12, "node", 2, "apps")
511 instance.defer_create_set(12, "command", "python -c 'import nepi'")
512 instance.defer_add_trace(12, "stderr")
517 instance.do_connect_init()
518 instance.do_connect_compl()
519 instance.do_preconfigure()
520 instance.do_configure()
522 instance.do_prestart()
524 while instance.status(12) != AS.STATUS_FINISHED:
526 ping_result = (instance.trace(12, "stderr") or "").strip()
534 # asserts at the end, to make sure there's proper cleanup
535 self.assertEqual(ping_result, "")
537 @test_util.skipUnless(test_util.pl_auth() is not None,
538 "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
539 @test_util.skipUnless(os.environ.get('NEPI_FULL_TESTS','').lower() in ('1','yes','true','on'),
540 "Test is expensive, requires NEPI_FULL_TESTS=yes")
541 def test_ns3_depends(self):
542 instance = self.make_instance()
544 instance.defer_create(2, "Node")
545 instance.defer_create_set(2, "hostname", self.host1)
546 instance.defer_create(3, "NodeInterface")
547 instance.defer_connect(2, "devs", 3, "node")
548 instance.defer_create(4, "Internet")
549 instance.defer_connect(3, "inet", 4, "devs")
550 instance.defer_create(5, "NepiDependency")
551 instance.defer_connect(5, "node", 2, "deps")
552 instance.defer_create(6, "NS3Dependency")
553 instance.defer_connect(6, "node", 2, "deps")
554 instance.defer_create(12, "Application")
555 instance.defer_connect(12, "node", 2, "apps")
556 instance.defer_create_set(12, "command", "python -c 'import nepi.testbeds.ns3.execute ; tb = nepi.testbeds.ns3.execute.TestbedController() ; mod = tb._configure_ns3_module()'")
557 instance.defer_add_trace(12, "stderr")
562 instance.do_connect_init()
563 instance.do_connect_compl()
564 instance.do_preconfigure()
565 instance.do_configure()
567 instance.do_prestart()
569 while instance.status(12) != AS.STATUS_FINISHED:
571 ping_result = (instance.trace(12, "stderr") or "").strip()
579 # asserts at the end, to make sure there's proper cleanup
580 self.assertEqual(ping_result, "")
582 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
583 def test_discovery(self):
584 instance = self.make_instance()
586 instance.defer_create(2, "Node")
587 instance.defer_create_set(2, "operatingSystem", "f12")
588 instance.defer_create(3, "Node")
589 instance.defer_create_set(3, "operatingSystem", "f12")
590 instance.defer_create(4, "NodeInterface")
591 instance.defer_connect(2, "devs", 4, "node")
592 instance.defer_create(5, "NodeInterface")
593 instance.defer_connect(3, "devs", 5, "node")
594 instance.defer_create(6, "Internet")
595 instance.defer_connect(4, "inet", 6, "devs")
596 instance.defer_connect(5, "inet", 6, "devs")
597 instance.defer_create(7, "Application")
598 instance.defer_create_set(7, "command", "ping -qc1 {#[GUID-5].addr[0].[Address]#}")
599 instance.defer_add_trace(7, "stdout")
600 instance.defer_add_trace(7, "stderr")
601 instance.defer_connect(7, "node", 2, "apps")
603 comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
605 --- .* ping statistics ---
606 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
612 instance.do_connect_init()
613 instance.do_connect_compl()
614 instance.do_preconfigure()
616 # Manually replace netref
617 instance.set(7, "command",
618 instance.get(7, "command")
619 .replace("{#[GUID-5].addr[0].[Address]#}",
620 instance.get_address(5, 0, "Address") )
623 instance.do_configure()
625 instance.do_prestart()
627 while instance.status(7) != AS.STATUS_FINISHED:
629 ping_result = instance.trace(7, "stdout") or ""
637 # asserts at the end, to make sure there's proper cleanup
638 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
639 "Unexpected trace:\n" + ping_result)
643 if __name__ == '__main__':