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.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)
58 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
59 def test_simple(self):
60 instance = self.make_instance()
62 instance.defer_create(2, "Node")
63 instance.defer_create_set(2, "hostname", self.host1)
64 instance.defer_create(3, "Node")
65 instance.defer_create_set(3, "hostname", self.host2)
66 instance.defer_create(4, "NodeInterface")
67 instance.defer_connect(2, "devs", 4, "node")
68 instance.defer_create(5, "NodeInterface")
69 instance.defer_connect(3, "devs", 5, "node")
70 instance.defer_create(6, "Internet")
71 instance.defer_connect(4, "inet", 6, "devs")
72 instance.defer_connect(5, "inet", 6, "devs")
73 instance.defer_create(7, "Application")
74 instance.defer_create_set(7, "command", "ping -qc1 {#[GUID-5].addr[0].[Address]#}")
75 instance.defer_add_trace(7, "stdout")
76 instance.defer_add_trace(7, "stderr")
77 instance.defer_connect(7, "node", 2, "apps")
79 comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
81 --- .* ping statistics ---
82 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
88 instance.do_connect_init()
89 instance.do_connect_compl()
90 instance.do_preconfigure()
92 # Manually replace netref
93 instance.set(7, "command",
94 instance.get(7, "command")
95 .replace("{#[GUID-5].addr[0].[Address]#}",
96 instance.get_address(5, 0, "Address") )
99 instance.do_configure()
101 instance.do_prestart()
103 while instance.status(7) != AS.STATUS_FINISHED:
105 ping_result = instance.trace(7, "stdout") or ""
110 # asserts at the end, to make sure there's proper cleanup
111 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
112 "Unexpected trace:\n" + ping_result)
114 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
115 def test_depends(self):
116 instance = self.make_instance()
118 instance.defer_create(2, "Node")
119 instance.defer_create_set(2, "hostname", self.host1)
120 instance.defer_create(3, "NodeInterface")
121 instance.defer_connect(2, "devs", 3, "node")
122 instance.defer_create(4, "Internet")
123 instance.defer_connect(3, "inet", 4, "devs")
124 instance.defer_create(5, "Application")
125 instance.defer_create_set(5, "command", "gfortran --version")
126 instance.defer_create_set(5, "depends", "gcc-gfortran")
127 instance.defer_add_trace(5, "stdout")
128 instance.defer_add_trace(5, "stderr")
129 instance.defer_connect(5, "node", 2, "apps")
134 instance.do_connect_init()
135 instance.do_connect_compl()
136 instance.do_preconfigure()
137 instance.do_configure()
139 instance.do_prestart()
141 while instance.status(5) != AS.STATUS_FINISHED:
143 ping_result = instance.trace(5, "stdout") or ""
144 comp_result = r".*GNU Fortran \(GCC\).*"
149 # asserts at the end, to make sure there's proper cleanup
150 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
151 "Unexpected trace:\n" + ping_result)
153 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
154 def test_build(self):
155 instance = self.make_instance()
157 instance.defer_create(2, "Node")
158 instance.defer_create_set(2, "hostname", self.host1)
159 instance.defer_create(3, "NodeInterface")
160 instance.defer_connect(2, "devs", 3, "node")
161 instance.defer_create(4, "Internet")
162 instance.defer_connect(3, "inet", 4, "devs")
163 instance.defer_create(10, "Application")
164 instance.defer_create_set(10, "command", "./consts")
165 instance.defer_create_set(10, "buildDepends", "gcc")
166 instance.defer_create_set(10, "build", "gcc ${SOURCES}/consts.c -o consts")
167 instance.defer_create_set(10, "install", "cp consts ${SOURCES}/consts")
168 instance.defer_create_set(10, "sources", os.path.join(os.path.dirname(planetlab.__file__),'scripts','consts.c'))
169 instance.defer_add_trace(10, "stdout")
170 instance.defer_add_trace(10, "stderr")
171 instance.defer_connect(10, "node", 2, "apps")
174 r""".*ETH_P_ALL = 0x[0-9a-fA-F]{8}
175 ETH_P_IP = 0x[0-9a-fA-F]{8}
176 TUNGETIFF = 0x[0-9a-fA-F]{8}
177 TUNSETIFF = 0x[0-9a-fA-F]{8}
178 IFF_NO_PI = 0x[0-9a-fA-F]{8}
179 IFF_TAP = 0x[0-9a-fA-F]{8}
180 IFF_TUN = 0x[0-9a-fA-F]{8}
181 IFF_VNET_HDR = 0x[0-9a-fA-F]{8}
182 TUN_PKT_STRIP = 0x[0-9a-fA-F]{8}
183 IFHWADDRLEN = 0x[0-9a-fA-F]{8}
184 IFNAMSIZ = 0x[0-9a-fA-F]{8}
185 IFREQ_SZ = 0x[0-9a-fA-F]{8}
186 FIONREAD = 0x[0-9a-fA-F]{8}.*
192 instance.do_connect_init()
193 instance.do_connect_compl()
194 instance.do_preconfigure()
195 instance.do_configure()
197 instance.do_prestart()
199 while instance.status(10) != AS.STATUS_FINISHED:
201 ping_result = instance.trace(10, "stdout") or ""
206 # asserts at the end, to make sure there's proper cleanup
207 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
208 "Unexpected trace:\n" + ping_result)
210 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
211 def test_simple_vsys(self):
212 instance = self.make_instance()
214 instance.defer_create(2, "Node")
215 instance.defer_create_set(2, "hostname", self.host1)
216 instance.defer_create(3, "NodeInterface")
217 instance.defer_connect(2, "devs", 3, "node")
218 instance.defer_create(4, "Internet")
219 instance.defer_connect(3, "inet", 4, "devs")
220 instance.defer_create(5, "TunInterface")
221 instance.defer_add_address(5, "192.168.2.2", 24, False)
222 instance.defer_connect(2, "devs", 5, "node")
223 instance.defer_create(6, "Application")
224 instance.defer_create_set(6, "command", """
226 netconfig help > /dev/null
227 test -e /vsys/vif_up.in > /dev/null
228 test -e /vsys/vif_up.out > /dev/null
229 test -e /vsys/fd_tuntap.control > /dev/null
232 instance.defer_create_set(6, "sudo", True) # only sudo has access to /vsys
233 instance.defer_add_trace(6, "stdout")
234 instance.defer_add_trace(6, "stderr")
235 instance.defer_connect(6, "node", 2, "apps")
240 instance.do_connect_init()
241 instance.do_connect_compl()
242 instance.do_preconfigure()
243 instance.do_configure()
245 instance.do_prestart()
247 while instance.status(6) != AS.STATUS_FINISHED:
249 test_result = (instance.trace(6, "stdout") or "").strip()
250 comp_result = "OKIDOKI"
255 # asserts at the end, to make sure there's proper cleanup
256 self.assertEqual(comp_result, test_result)
258 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
259 def test_emulation(self):
260 instance = self.make_instance()
262 instance.defer_create(2, "Node")
263 instance.defer_create_set(2, "hostname", self.host1)
264 instance.defer_create(3, "NodeInterface")
265 instance.defer_connect(2, "devs", 3, "node")
266 instance.defer_create(4, "Internet")
267 instance.defer_connect(3, "inet", 4, "devs")
268 instance.defer_create(7, "NetPipe")
269 instance.defer_create_set(7, "mode", "CLIENT")
270 instance.defer_create_set(7, "portList", "80")
271 instance.defer_create_set(7, "bwOut", 12.0/1024.0) # 12kbps
272 instance.defer_create_set(7, "bwIn", 64.0/1024.0) # 64kbps
273 instance.defer_create_set(7, "plrOut", 0.01) # 1% plr outbound - high loss
274 instance.defer_create_set(7, "plrIn", 0.001) # 0.1% plr inbound - regular loss
275 instance.defer_create_set(7, "delayOut", int(1500 * 8 / (12.0/1024.0) / 1000)) # tx delay at 12kbps in ms
276 instance.defer_create_set(7, "delayIn", int(1500 * 8 / (64.0/1024.0) / 1000)) # rx delay at 64kbps in ms
277 instance.defer_add_trace(7, "netpipeStats")
278 instance.defer_connect(2, "pipes", 7, "node")
279 instance.defer_create(8, "Application")
280 instance.defer_create_set(8, "command", "time wget -q -O /dev/null http://www.google.com/") # Fetch ~10kb
281 instance.defer_add_trace(8, "stdout")
282 instance.defer_add_trace(8, "stderr")
283 instance.defer_connect(8, "node", 2, "apps")
288 instance.do_connect_init()
289 instance.do_connect_compl()
290 instance.do_preconfigure()
291 instance.do_configure()
293 instance.do_prestart()
295 while instance.status(8) != AS.STATUS_FINISHED:
297 test_result = (instance.trace(8, "stderr") or "").strip()
298 comp_result = r".*real\s*(?P<min>[0-9]+)m(?P<sec>[0-9]+[.][0-9]+)s.*"
299 netpipe_stats = instance.trace(7, "netpipeStats")
305 # asserts at the end, to make sure there's proper cleanup
306 match = re.match(comp_result, test_result, re.MULTILINE)
307 self.assertTrue(match, "Unexpected output: %s" % (test_result,))
309 minutes = int(match.group("min"))
310 seconds = float(match.group("sec"))
311 self.assertTrue((minutes * 60 + seconds) > 1.0, "Emulation not effective: %s" % (test_result,))
313 self.assertTrue(netpipe_stats, "Unavailable netpipe stats")
315 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
316 def _pingtest(self, TunClass, ConnectionProto):
317 instance = self.make_instance()
319 instance.defer_create(2, "Node")
320 instance.defer_create_set(2, "hostname", self.host1)
321 instance.defer_create(3, "Node")
322 instance.defer_create_set(3, "hostname", self.host2)
323 instance.defer_create(4, "NodeInterface")
324 instance.defer_connect(2, "devs", 4, "node")
325 instance.defer_create(5, "Internet")
326 instance.defer_connect(4, "inet", 5, "devs")
327 instance.defer_create(6, "NodeInterface")
328 instance.defer_connect(3, "devs", 6, "node")
329 instance.defer_connect(6, "inet", 5, "devs")
330 instance.defer_create(7, TunClass)
331 instance.defer_add_trace(7, "packets")
332 instance.defer_add_address(7, "192.168.2.2", 24, False)
333 instance.defer_connect(2, "devs", 7, "node")
334 instance.defer_create(8, TunClass)
335 instance.defer_add_trace(8, "packets")
336 instance.defer_add_address(8, "192.168.2.3", 24, False)
337 instance.defer_connect(3, "devs", 8, "node")
338 instance.defer_connect(7, ConnectionProto, 8, ConnectionProto)
339 instance.defer_create(9, "Application")
340 instance.defer_create_set(9, "command", "ping -qc1 {#[GUID-8].addr[0].[Address]#}")
341 instance.defer_add_trace(9, "stdout")
342 instance.defer_add_trace(9, "stderr")
343 instance.defer_connect(9, "node", 2, "apps")
345 comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
347 --- .* ping statistics ---
348 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
354 instance.do_connect_init()
355 instance.do_connect_compl()
356 instance.do_preconfigure()
358 # Manually replace netref
359 instance.set(9, "command",
360 instance.get(9, "command")
361 .replace("{#[GUID-8].addr[0].[Address]#}",
362 instance.get_address(8, 0, "Address") )
365 instance.do_configure()
367 instance.do_prestart()
369 while instance.status(9) != AS.STATUS_FINISHED:
371 ping_result = instance.trace(9, "stdout") or ""
372 packets1 = instance.trace(7, "packets") or ""
373 packets2 = instance.trace(8, "packets") or ""
378 # asserts at the end, to make sure there's proper cleanup
379 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
380 "Unexpected trace:\n%s\nPackets @ source:\n%s\nPackets @ target:\n%s" % (
385 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
386 def test_tun_ping(self):
387 self._pingtest("TunInterface", "tcp")
389 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
390 def test_tun_ping_udp(self):
391 self._pingtest("TunInterface", "udp")
393 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
394 def test_tap_ping(self):
395 self._pingtest("TapInterface", "tcp")
397 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
398 def test_tap_ping_udp(self):
399 self._pingtest("TapInterface", "udp")
401 @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
402 def test_nepi_depends(self):
403 instance = self.make_instance()
405 instance.defer_create(2, "Node")
406 instance.defer_create_set(2, "hostname", self.host1)
407 instance.defer_create(3, "NodeInterface")
408 instance.defer_connect(2, "devs", 3, "node")
409 instance.defer_create(4, "Internet")
410 instance.defer_connect(3, "inet", 4, "devs")
411 instance.defer_create(5, "NepiDependency")
412 instance.defer_connect(5, "node", 2, "deps")
413 instance.defer_create(12, "Application")
414 instance.defer_connect(12, "node", 2, "apps")
415 instance.defer_create_set(12, "command", "python -c 'import nepi'")
416 instance.defer_add_trace(12, "stderr")
421 instance.do_connect_init()
422 instance.do_connect_compl()
423 instance.do_preconfigure()
424 instance.do_configure()
426 instance.do_prestart()
428 while instance.status(12) != AS.STATUS_FINISHED:
430 ping_result = (instance.trace(12, "stderr") or "").strip()
435 # asserts at the end, to make sure there's proper cleanup
436 self.assertEqual(ping_result, "")
438 @test_util.skipUnless(test_util.pl_auth() is not None,
439 "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
440 @test_util.skipUnless(os.environ.get('NEPI_FULL_TESTS','').lower() in ('1','yes','true','on'),
441 "Test is expensive, requires NEPI_FULL_TESTS=yes")
442 def test_ns3_depends(self):
443 instance = self.make_instance()
445 instance.defer_create(2, "Node")
446 instance.defer_create_set(2, "hostname", self.host1)
447 instance.defer_create(3, "NodeInterface")
448 instance.defer_connect(2, "devs", 3, "node")
449 instance.defer_create(4, "Internet")
450 instance.defer_connect(3, "inet", 4, "devs")
451 instance.defer_create(5, "NepiDependency")
452 instance.defer_connect(5, "node", 2, "deps")
453 instance.defer_create(6, "NS3Dependency")
454 instance.defer_connect(6, "node", 2, "deps")
455 instance.defer_create(12, "Application")
456 instance.defer_connect(12, "node", 2, "apps")
457 instance.defer_create_set(12, "command", "python -c 'import nepi.testbeds.ns3.execute ; tb = nepi.testbeds.ns3.execute.TestbedController() ; mod = tb._load_ns3_module()'")
458 instance.defer_add_trace(12, "stderr")
463 instance.do_connect_init()
464 instance.do_connect_compl()
465 instance.do_preconfigure()
466 instance.do_configure()
468 instance.do_prestart()
470 while instance.status(12) != AS.STATUS_FINISHED:
472 ping_result = (instance.trace(12, "stderr") or "").strip()
477 # asserts at the end, to make sure there's proper cleanup
478 self.assertEqual(ping_result, "")
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_discovery(self):
482 instance = self.make_instance()
484 instance.defer_create(2, "Node")
485 instance.defer_create_set(2, "operatingSystem", "f12")
486 instance.defer_create(3, "Node")
487 instance.defer_create_set(3, "operatingSystem", "f12")
488 instance.defer_create(4, "NodeInterface")
489 instance.defer_connect(2, "devs", 4, "node")
490 instance.defer_create(5, "NodeInterface")
491 instance.defer_connect(3, "devs", 5, "node")
492 instance.defer_create(6, "Internet")
493 instance.defer_connect(4, "inet", 6, "devs")
494 instance.defer_connect(5, "inet", 6, "devs")
495 instance.defer_create(7, "Application")
496 instance.defer_create_set(7, "command", "ping -qc1 {#[GUID-5].addr[0].[Address]#}")
497 instance.defer_add_trace(7, "stdout")
498 instance.defer_add_trace(7, "stderr")
499 instance.defer_connect(7, "node", 2, "apps")
501 comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
503 --- .* ping statistics ---
504 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
510 instance.do_connect_init()
511 instance.do_connect_compl()
512 instance.do_preconfigure()
514 # Manually replace netref
515 instance.set(7, "command",
516 instance.get(7, "command")
517 .replace("{#[GUID-5].addr[0].[Address]#}",
518 instance.get_address(5, 0, "Address") )
521 instance.do_configure()
523 instance.do_prestart()
525 while instance.status(7) != AS.STATUS_FINISHED:
527 ping_result = instance.trace(7, "stdout") or ""
532 # asserts at the end, to make sure there's proper cleanup
533 self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
534 "Unexpected trace:\n" + ping_result)
538 if __name__ == '__main__':