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)
56 instance.defer_configure("p2pDeployment", False) # it's interactive, we don't want it in tests
60 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
61 def test_simple(self):
62 instance = self.make_instance()
64 instance.defer_create(2, "Node")
65 instance.defer_create_set(2, "hostname", self.host1)
66 instance.defer_create(3, "Node")
67 instance.defer_create_set(3, "hostname", self.host2)
68 instance.defer_create(4, "NodeInterface")
69 instance.defer_connect(2, "devs", 4, "node")
70 instance.defer_create(5, "NodeInterface")
71 instance.defer_connect(3, "devs", 5, "node")
72 instance.defer_create(6, "Internet")
73 instance.defer_connect(4, "inet", 6, "devs")
74 instance.defer_connect(5, "inet", 6, "devs")
75 instance.defer_create(7, "Application")
76 instance.defer_create_set(7, "command", "ping -qc1 {#[GUID-5].addr[0].[Address]#}")
77 instance.defer_add_trace(7, "stdout")
78 instance.defer_add_trace(7, "stderr")
79 instance.defer_connect(7, "node", 2, "apps")
81 comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
83 --- .* ping statistics ---
84 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
90 instance.do_connect_init()
91 instance.do_connect_compl()
92 instance.do_preconfigure()
94 # Manually replace netref
95 instance.set(7, "command",
96 instance.get(7, "command")
97 .replace("{#[GUID-5].addr[0].[Address]#}",
98 instance.get_address(5, 0, "Address") )
101 instance.do_configure()
103 instance.do_prestart()
105 while instance.status(7) != AS.STATUS_FINISHED:
107 ping_result = instance.trace(7, "stdout") or ""
112 # asserts at the end, to make sure there's proper cleanup
113 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
114 "Unexpected trace:\n" + ping_result)
116 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
117 def test_depends(self):
118 instance = self.make_instance()
120 instance.defer_create(2, "Node")
121 instance.defer_create_set(2, "hostname", self.host1)
122 instance.defer_create(3, "NodeInterface")
123 instance.defer_connect(2, "devs", 3, "node")
124 instance.defer_create(4, "Internet")
125 instance.defer_connect(3, "inet", 4, "devs")
126 instance.defer_create(5, "Application")
127 instance.defer_create_set(5, "command", "gfortran --version")
128 instance.defer_create_set(5, "depends", "gcc-gfortran")
129 instance.defer_add_trace(5, "stdout")
130 instance.defer_add_trace(5, "stderr")
131 instance.defer_connect(5, "node", 2, "apps")
136 instance.do_connect_init()
137 instance.do_connect_compl()
138 instance.do_preconfigure()
139 instance.do_configure()
141 instance.do_prestart()
143 while instance.status(5) != AS.STATUS_FINISHED:
145 ping_result = instance.trace(5, "stdout") or ""
146 comp_result = r".*GNU Fortran \(GCC\).*"
151 # asserts at the end, to make sure there's proper cleanup
152 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
153 "Unexpected trace:\n" + ping_result)
155 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
156 def test_build(self):
157 instance = self.make_instance()
159 instance.defer_create(2, "Node")
160 instance.defer_create_set(2, "hostname", self.host1)
161 instance.defer_create(3, "NodeInterface")
162 instance.defer_connect(2, "devs", 3, "node")
163 instance.defer_create(4, "Internet")
164 instance.defer_connect(3, "inet", 4, "devs")
165 instance.defer_create(10, "Application")
166 instance.defer_create_set(10, "command", "./consts")
167 instance.defer_create_set(10, "buildDepends", "gcc")
168 instance.defer_create_set(10, "build", "gcc ${SOURCES}/consts.c -o consts")
169 instance.defer_create_set(10, "install", "cp consts ${SOURCES}/consts")
170 instance.defer_create_set(10, "sources", os.path.join(os.path.dirname(planetlab.__file__),'scripts','consts.c'))
171 instance.defer_add_trace(10, "stdout")
172 instance.defer_add_trace(10, "stderr")
173 instance.defer_connect(10, "node", 2, "apps")
176 r""".*ETH_P_ALL = 0x[0-9a-fA-F]{8}
177 ETH_P_IP = 0x[0-9a-fA-F]{8}
178 TUNGETIFF = 0x[0-9a-fA-F]{8}
179 TUNSETIFF = 0x[0-9a-fA-F]{8}
180 IFF_NO_PI = 0x[0-9a-fA-F]{8}
181 IFF_TAP = 0x[0-9a-fA-F]{8}
182 IFF_TUN = 0x[0-9a-fA-F]{8}
183 IFF_VNET_HDR = 0x[0-9a-fA-F]{8}
184 TUN_PKT_STRIP = 0x[0-9a-fA-F]{8}
185 IFHWADDRLEN = 0x[0-9a-fA-F]{8}
186 IFNAMSIZ = 0x[0-9a-fA-F]{8}
187 IFREQ_SZ = 0x[0-9a-fA-F]{8}
188 FIONREAD = 0x[0-9a-fA-F]{8}.*
194 instance.do_connect_init()
195 instance.do_connect_compl()
196 instance.do_preconfigure()
197 instance.do_configure()
199 instance.do_prestart()
201 while instance.status(10) != AS.STATUS_FINISHED:
203 ping_result = instance.trace(10, "stdout") or ""
208 # asserts at the end, to make sure there's proper cleanup
209 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
210 "Unexpected trace:\n" + ping_result)
212 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
213 def test_simple_vsys(self):
214 instance = self.make_instance()
216 instance.defer_create(2, "Node")
217 instance.defer_create_set(2, "hostname", self.host1)
218 instance.defer_create(3, "NodeInterface")
219 instance.defer_connect(2, "devs", 3, "node")
220 instance.defer_create(4, "Internet")
221 instance.defer_connect(3, "inet", 4, "devs")
222 instance.defer_create(5, "TunInterface")
223 instance.defer_add_address(5, "192.168.2.2", 24, False)
224 instance.defer_connect(2, "devs", 5, "node")
225 instance.defer_create(6, "Application")
226 instance.defer_create_set(6, "command", """
228 netconfig help > /dev/null
229 test -e /vsys/vif_up.in > /dev/null
230 test -e /vsys/vif_up.out > /dev/null
231 test -e /vsys/fd_tuntap.control > /dev/null
234 instance.defer_create_set(6, "sudo", True) # only sudo has access to /vsys
235 instance.defer_add_trace(6, "stdout")
236 instance.defer_add_trace(6, "stderr")
237 instance.defer_connect(6, "node", 2, "apps")
242 instance.do_connect_init()
243 instance.do_connect_compl()
244 instance.do_preconfigure()
245 instance.do_configure()
247 instance.do_prestart()
249 while instance.status(6) != AS.STATUS_FINISHED:
251 test_result = (instance.trace(6, "stdout") or "").strip()
252 comp_result = "OKIDOKI"
257 # asserts at the end, to make sure there's proper cleanup
258 self.assertEqual(comp_result, test_result)
260 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
261 def test_emulation(self):
262 instance = self.make_instance()
264 instance.defer_create(2, "Node")
265 instance.defer_create_set(2, "hostname", self.host1)
266 instance.defer_create(3, "NodeInterface")
267 instance.defer_connect(2, "devs", 3, "node")
268 instance.defer_create(4, "Internet")
269 instance.defer_connect(3, "inet", 4, "devs")
270 instance.defer_create(7, "NetPipe")
271 instance.defer_create_set(7, "mode", "CLIENT")
272 instance.defer_create_set(7, "portList", "80")
273 instance.defer_create_set(7, "bwOut", 12.0/1024.0) # 12kbps
274 instance.defer_create_set(7, "bwIn", 64.0/1024.0) # 64kbps
275 instance.defer_create_set(7, "plrOut", 0.01) # 1% plr outbound - high loss
276 instance.defer_create_set(7, "plrIn", 0.001) # 0.1% plr inbound - regular loss
277 instance.defer_create_set(7, "delayOut", int(1500 * 8 / (12.0/1024.0) / 1000)) # tx delay at 12kbps in ms
278 instance.defer_create_set(7, "delayIn", int(1500 * 8 / (64.0/1024.0) / 1000)) # rx delay at 64kbps in ms
279 instance.defer_add_trace(7, "netpipeStats")
280 instance.defer_connect(2, "pipes", 7, "node")
281 instance.defer_create(8, "Application")
282 instance.defer_create_set(8, "command", "time wget -q -O /dev/null http://www.google.com/") # Fetch ~10kb
283 instance.defer_add_trace(8, "stdout")
284 instance.defer_add_trace(8, "stderr")
285 instance.defer_connect(8, "node", 2, "apps")
290 instance.do_connect_init()
291 instance.do_connect_compl()
292 instance.do_preconfigure()
293 instance.do_configure()
295 instance.do_prestart()
297 while instance.status(8) != AS.STATUS_FINISHED:
299 test_result = (instance.trace(8, "stderr") or "").strip()
300 comp_result = r".*real\s*(?P<min>[0-9]+)m(?P<sec>[0-9]+[.][0-9]+)s.*"
301 netpipe_stats = instance.trace(7, "netpipeStats")
307 # asserts at the end, to make sure there's proper cleanup
308 match = re.match(comp_result, test_result, re.MULTILINE)
309 self.assertTrue(match, "Unexpected output: %s" % (test_result,))
311 minutes = int(match.group("min"))
312 seconds = float(match.group("sec"))
313 self.assertTrue((minutes * 60 + seconds) > 1.0, "Emulation not effective: %s" % (test_result,))
315 self.assertTrue(netpipe_stats, "Unavailable netpipe stats")
317 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
318 def _pingtest(self, TunClass, ConnectionProto):
319 instance = self.make_instance()
321 instance.defer_create(2, "Node")
322 instance.defer_create_set(2, "hostname", self.host1)
323 instance.defer_create(3, "Node")
324 instance.defer_create_set(3, "hostname", self.host2)
325 instance.defer_create(4, "NodeInterface")
326 instance.defer_connect(2, "devs", 4, "node")
327 instance.defer_create(5, "Internet")
328 instance.defer_connect(4, "inet", 5, "devs")
329 instance.defer_create(6, "NodeInterface")
330 instance.defer_connect(3, "devs", 6, "node")
331 instance.defer_connect(6, "inet", 5, "devs")
332 instance.defer_create(7, TunClass)
333 instance.defer_add_trace(7, "packets")
334 instance.defer_add_address(7, "192.168.2.2", 24, False)
335 instance.defer_connect(2, "devs", 7, "node")
336 instance.defer_create(8, TunClass)
337 instance.defer_add_trace(8, "packets")
338 instance.defer_add_address(8, "192.168.2.3", 24, False)
339 instance.defer_connect(3, "devs", 8, "node")
340 instance.defer_connect(7, ConnectionProto, 8, ConnectionProto)
341 instance.defer_create(9, "Application")
342 instance.defer_create_set(9, "command", "ping -qc1 {#[GUID-8].addr[0].[Address]#}")
343 instance.defer_add_trace(9, "stdout")
344 instance.defer_add_trace(9, "stderr")
345 instance.defer_connect(9, "node", 2, "apps")
347 comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
349 --- .* ping statistics ---
350 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
356 instance.do_connect_init()
357 instance.do_connect_compl()
358 instance.do_preconfigure()
360 # Manually replace netref
361 instance.set(9, "command",
362 instance.get(9, "command")
363 .replace("{#[GUID-8].addr[0].[Address]#}",
364 instance.get_address(8, 0, "Address") )
367 instance.do_configure()
369 instance.do_prestart()
371 while instance.status(9) != AS.STATUS_FINISHED:
373 ping_result = instance.trace(9, "stdout") or ""
374 packets1 = instance.trace(7, "packets") or ""
375 packets2 = instance.trace(8, "packets") or ""
380 # asserts at the end, to make sure there's proper cleanup
381 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
382 "Unexpected trace:\n%s\nPackets @ source:\n%s\nPackets @ target:\n%s" % (
387 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
388 def test_tun_ping(self):
389 self._pingtest("TunInterface", "tcp")
391 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
392 def test_tun_ping_udp(self):
393 self._pingtest("TunInterface", "udp")
395 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
396 def test_tun_ping_gre(self):
397 self._pingtest("TunInterface", "gre")
399 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
400 def test_tap_ping(self):
401 self._pingtest("TapInterface", "tcp")
403 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
404 def test_tap_ping_udp(self):
405 self._pingtest("TapInterface", "udp")
407 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
408 def test_tap_ping_gre(self):
409 self._pingtest("TapInterface", "gre")
411 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
412 def test_nepi_depends(self):
413 instance = self.make_instance()
415 instance.defer_create(2, "Node")
416 instance.defer_create_set(2, "hostname", self.host1)
417 instance.defer_create(3, "NodeInterface")
418 instance.defer_connect(2, "devs", 3, "node")
419 instance.defer_create(4, "Internet")
420 instance.defer_connect(3, "inet", 4, "devs")
421 instance.defer_create(5, "NepiDependency")
422 instance.defer_connect(5, "node", 2, "deps")
423 instance.defer_create(12, "Application")
424 instance.defer_connect(12, "node", 2, "apps")
425 instance.defer_create_set(12, "command", "python -c 'import nepi'")
426 instance.defer_add_trace(12, "stderr")
431 instance.do_connect_init()
432 instance.do_connect_compl()
433 instance.do_preconfigure()
434 instance.do_configure()
436 instance.do_prestart()
438 while instance.status(12) != AS.STATUS_FINISHED:
440 ping_result = (instance.trace(12, "stderr") or "").strip()
445 # asserts at the end, to make sure there's proper cleanup
446 self.assertEqual(ping_result, "")
448 @test_util.skipUnless(test_util.pl_auth() is not None,
449 "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
450 @test_util.skipUnless(os.environ.get('NEPI_FULL_TESTS','').lower() in ('1','yes','true','on'),
451 "Test is expensive, requires NEPI_FULL_TESTS=yes")
452 def test_ns3_depends(self):
453 instance = self.make_instance()
455 instance.defer_create(2, "Node")
456 instance.defer_create_set(2, "hostname", self.host1)
457 instance.defer_create(3, "NodeInterface")
458 instance.defer_connect(2, "devs", 3, "node")
459 instance.defer_create(4, "Internet")
460 instance.defer_connect(3, "inet", 4, "devs")
461 instance.defer_create(5, "NepiDependency")
462 instance.defer_connect(5, "node", 2, "deps")
463 instance.defer_create(6, "NS3Dependency")
464 instance.defer_connect(6, "node", 2, "deps")
465 instance.defer_create(12, "Application")
466 instance.defer_connect(12, "node", 2, "apps")
467 instance.defer_create_set(12, "command", "python -c 'import nepi.testbeds.ns3.execute ; tb = nepi.testbeds.ns3.execute.TestbedController() ; mod = tb._configure_ns3_module()'")
468 instance.defer_add_trace(12, "stderr")
473 instance.do_connect_init()
474 instance.do_connect_compl()
475 instance.do_preconfigure()
476 instance.do_configure()
478 instance.do_prestart()
480 while instance.status(12) != AS.STATUS_FINISHED:
482 ping_result = (instance.trace(12, "stderr") or "").strip()
487 # asserts at the end, to make sure there's proper cleanup
488 self.assertEqual(ping_result, "")
490 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
491 def test_discovery(self):
492 instance = self.make_instance()
494 instance.defer_create(2, "Node")
495 instance.defer_create_set(2, "operatingSystem", "f12")
496 instance.defer_create(3, "Node")
497 instance.defer_create_set(3, "operatingSystem", "f12")
498 instance.defer_create(4, "NodeInterface")
499 instance.defer_connect(2, "devs", 4, "node")
500 instance.defer_create(5, "NodeInterface")
501 instance.defer_connect(3, "devs", 5, "node")
502 instance.defer_create(6, "Internet")
503 instance.defer_connect(4, "inet", 6, "devs")
504 instance.defer_connect(5, "inet", 6, "devs")
505 instance.defer_create(7, "Application")
506 instance.defer_create_set(7, "command", "ping -qc1 {#[GUID-5].addr[0].[Address]#}")
507 instance.defer_add_trace(7, "stdout")
508 instance.defer_add_trace(7, "stderr")
509 instance.defer_connect(7, "node", 2, "apps")
511 comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
513 --- .* ping statistics ---
514 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
520 instance.do_connect_init()
521 instance.do_connect_compl()
522 instance.do_preconfigure()
524 # Manually replace netref
525 instance.set(7, "command",
526 instance.get(7, "command")
527 .replace("{#[GUID-5].addr[0].[Address]#}",
528 instance.get_address(5, 0, "Address") )
531 instance.do_configure()
533 instance.do_prestart()
535 while instance.status(7) != AS.STATUS_FINISHED:
537 ping_result = instance.trace(7, "stdout") or ""
542 # asserts at the end, to make sure there's proper cleanup
543 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
544 "Unexpected trace:\n" + ping_result)
548 if __name__ == '__main__':