Ticket #69: tun/tap port base allows concurrent experiments in the same slice (if...
[nepi.git] / test / testbeds / planetlab / execute.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import getpass
5 from nepi.util.constants import ApplicationStatus as AS
6 from nepi.testbeds import planetlab
7 import os
8 import shutil
9 import tempfile
10 import time
11 import unittest
12 import re
13 import test_util
14 import sys
15
16 class PlanetLabExecuteTestCase(unittest.TestCase):
17     testbed_id = "planetlab"
18     slicename = "inria_nepi"
19     plchost = "nepiplc.pl.sophia.inria.fr"
20     
21     host1 = "nepi1.pl.sophia.inria.fr"
22     host2 = "nepi2.pl.sophia.inria.fr"
23     
24     port_base = 2000 + (os.getpid() % 1000) * 13
25     
26     def setUp(self):
27         self.root_dir = tempfile.mkdtemp()
28         self.port_base = self.port_base + 100
29         
30     def tearDown(self):
31         try:
32             shutil.rmtree(self.root_dir)
33         except:
34             # retry
35             time.sleep(0.1)
36             shutil.rmtree(self.root_dir)
37
38     def make_instance(self):
39         testbed_id = self.testbed_id
40         slicename = self.slicename
41         plchost = self.plchost
42         
43         instance = planetlab.TestbedController()
44         pl_ssh_key = os.environ.get(
45             "PL_SSH_KEY",
46             "%s/.ssh/id_rsa_planetlab" % (os.environ['HOME'],) )
47         pl_user, pl_pwd = test_util.pl_auth()
48         
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         
56         return instance
57
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()
61         
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")
78
79         comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
80
81 --- .* ping statistics ---
82 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
83 """
84
85         try:
86             instance.do_setup()
87             instance.do_create()
88             instance.do_connect_init()
89             instance.do_connect_compl()
90             instance.do_preconfigure()
91             
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") )
97             )
98
99             instance.do_configure()
100             
101             instance.do_prestart()
102             instance.start()
103             while instance.status(7) != AS.STATUS_FINISHED:
104                 time.sleep(0.5)
105             ping_result = instance.trace(7, "stdout") or ""
106             instance.stop()
107         finally:
108             instance.shutdown()
109
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)
113         
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()
117         
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")
130
131         try:
132             instance.do_setup()
133             instance.do_create()
134             instance.do_connect_init()
135             instance.do_connect_compl()
136             instance.do_preconfigure()
137             instance.do_configure()
138             
139             instance.do_prestart()
140             instance.start()
141             while instance.status(5) != AS.STATUS_FINISHED:
142                 time.sleep(0.5)
143             ping_result = instance.trace(5, "stdout") or ""
144             comp_result = r".*GNU Fortran \(GCC\).*"
145             instance.stop()
146         finally:
147             instance.shutdown()
148
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)
152         
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()
156         
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")
172
173         comp_result = \
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}.*
187 """
188
189         try:
190             instance.do_setup()
191             instance.do_create()
192             instance.do_connect_init()
193             instance.do_connect_compl()
194             instance.do_preconfigure()
195             instance.do_configure()
196             
197             instance.do_prestart()
198             instance.start()
199             while instance.status(10) != AS.STATUS_FINISHED:
200                 time.sleep(0.5)
201             ping_result = instance.trace(10, "stdout") or ""
202             instance.stop()
203         finally:
204             instance.shutdown()
205
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)
209         
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()
213         
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", """
225 set -e
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
230 echo 'OKIDOKI'
231 """)
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")
236
237         try:
238             instance.do_setup()
239             instance.do_create()
240             instance.do_connect_init()
241             instance.do_connect_compl()
242             instance.do_preconfigure()
243             instance.do_configure()
244             
245             instance.do_prestart()
246             instance.start()
247             while instance.status(6) != AS.STATUS_FINISHED:
248                 time.sleep(0.5)
249             test_result = (instance.trace(6, "stdout") or "").strip()
250             comp_result = "OKIDOKI"
251             instance.stop()
252         finally:
253             instance.shutdown()
254
255         # asserts at the end, to make sure there's proper cleanup
256         self.assertEqual(comp_result, test_result)
257
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()
261         
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")
284
285         try:
286             instance.do_setup()
287             instance.do_create()
288             instance.do_connect_init()
289             instance.do_connect_compl()
290             instance.do_preconfigure()
291             instance.do_configure()
292             
293             instance.do_prestart()
294             instance.start()
295             while instance.status(8) != AS.STATUS_FINISHED:
296                 time.sleep(0.5)
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")
300             
301             instance.stop()
302         finally:
303             instance.shutdown()
304
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,))
308         
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,))
312
313         self.assertTrue(netpipe_stats, "Unavailable netpipe stats")
314
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()
318         
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")
344
345         comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
346
347 --- .* ping statistics ---
348 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
349 """
350
351         try:
352             instance.do_setup()
353             instance.do_create()
354             instance.do_connect_init()
355             instance.do_connect_compl()
356             instance.do_preconfigure()
357             
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") )
363             )
364             
365             instance.do_configure()
366             
367             instance.do_prestart()
368             instance.start()
369             while instance.status(9) != AS.STATUS_FINISHED:
370                 time.sleep(0.5)
371             ping_result = instance.trace(9, "stdout") or ""
372             packets1 = instance.trace(7, "packets") or ""
373             packets2 = instance.trace(8, "packets") or ""
374             instance.stop()
375         finally:
376             instance.shutdown()
377
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" % (
381                 ping_result,
382                 packets1,
383                 packets2))
384
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")
388
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")
392
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")
396
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")
400
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()
404         
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")
417
418         try:
419             instance.do_setup()
420             instance.do_create()
421             instance.do_connect_init()
422             instance.do_connect_compl()
423             instance.do_preconfigure()
424             instance.do_configure()
425             
426             instance.do_prestart()
427             instance.start()
428             while instance.status(12) != AS.STATUS_FINISHED:
429                 time.sleep(0.5)
430             ping_result = (instance.trace(12, "stderr") or "").strip()
431             instance.stop()
432         finally:
433             instance.shutdown()
434         
435         # asserts at the end, to make sure there's proper cleanup
436         self.assertEqual(ping_result, "")
437
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()
444         
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")
459
460         try:
461             instance.do_setup()
462             instance.do_create()
463             instance.do_connect_init()
464             instance.do_connect_compl()
465             instance.do_preconfigure()
466             instance.do_configure()
467             
468             instance.do_prestart()
469             instance.start()
470             while instance.status(12) != AS.STATUS_FINISHED:
471                 time.sleep(0.5)
472             ping_result = (instance.trace(12, "stderr") or "").strip()
473             instance.stop()
474         finally:
475             instance.shutdown()
476         
477         # asserts at the end, to make sure there's proper cleanup
478         self.assertEqual(ping_result, "")
479
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()
483         
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")
500
501         comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
502
503 --- .* ping statistics ---
504 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
505 """
506
507         try:
508             instance.do_setup()
509             instance.do_create()
510             instance.do_connect_init()
511             instance.do_connect_compl()
512             instance.do_preconfigure()
513             
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") )
519             )
520
521             instance.do_configure()
522             
523             instance.do_prestart()
524             instance.start()
525             while instance.status(7) != AS.STATUS_FINISHED:
526                 time.sleep(0.5)
527             ping_result = instance.trace(7, "stdout") or ""
528             instance.stop()
529         finally:
530             instance.shutdown()
531
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)
535         
536         
537
538 if __name__ == '__main__':
539     unittest.main()
540