Merge with tip
[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.__class__.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         instance.defer_configure("tapPortBase", self.port_base)
56         instance.defer_configure("p2pDeployment", False) # it's interactive, we don't want it in tests
57         
58         return instance
59
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()
63         
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")
80
81         comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
82
83 --- .* ping statistics ---
84 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
85 """
86
87         try:
88             instance.do_setup()
89             instance.do_create()
90             instance.do_connect_init()
91             instance.do_connect_compl()
92             instance.do_preconfigure()
93             
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") )
99             )
100
101             instance.do_configure()
102             
103             instance.do_prestart()
104             instance.start()
105             while instance.status(7) != AS.STATUS_FINISHED:
106                 time.sleep(0.5)
107             ping_result = instance.trace(7, "stdout") or ""
108             instance.stop()
109         finally:
110             instance.shutdown()
111
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)
115         
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()
119         
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")
132
133         try:
134             instance.do_setup()
135             instance.do_create()
136             instance.do_connect_init()
137             instance.do_connect_compl()
138             instance.do_preconfigure()
139             instance.do_configure()
140             
141             instance.do_prestart()
142             instance.start()
143             while instance.status(5) != AS.STATUS_FINISHED:
144                 time.sleep(0.5)
145             ping_result = instance.trace(5, "stdout") or ""
146             comp_result = r".*GNU Fortran \(GCC\).*"
147             instance.stop()
148         finally:
149             instance.shutdown()
150
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)
154         
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()
158         
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")
174
175         comp_result = \
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}.*
189 """
190
191         try:
192             instance.do_setup()
193             instance.do_create()
194             instance.do_connect_init()
195             instance.do_connect_compl()
196             instance.do_preconfigure()
197             instance.do_configure()
198             
199             instance.do_prestart()
200             instance.start()
201             while instance.status(10) != AS.STATUS_FINISHED:
202                 time.sleep(0.5)
203             ping_result = instance.trace(10, "stdout") or ""
204             instance.stop()
205         finally:
206             instance.shutdown()
207
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)
211         
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()
215         
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", """
227 set -e
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
232 echo 'OKIDOKI'
233 """)
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")
238
239         try:
240             instance.do_setup()
241             instance.do_create()
242             instance.do_connect_init()
243             instance.do_connect_compl()
244             instance.do_preconfigure()
245             instance.do_configure()
246             
247             instance.do_prestart()
248             instance.start()
249             while instance.status(6) != AS.STATUS_FINISHED:
250                 time.sleep(0.5)
251             test_result = (instance.trace(6, "stdout") or "").strip()
252             comp_result = "OKIDOKI"
253             instance.stop()
254         finally:
255             instance.shutdown()
256
257         # asserts at the end, to make sure there's proper cleanup
258         self.assertEqual(comp_result, test_result)
259
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()
263         
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")
286
287         try:
288             instance.do_setup()
289             instance.do_create()
290             instance.do_connect_init()
291             instance.do_connect_compl()
292             instance.do_preconfigure()
293             instance.do_configure()
294             
295             instance.do_prestart()
296             instance.start()
297             while instance.status(8) != AS.STATUS_FINISHED:
298                 time.sleep(0.5)
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")
302             
303             instance.stop()
304         finally:
305             instance.shutdown()
306
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,))
310         
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,))
314
315         self.assertTrue(netpipe_stats, "Unavailable netpipe stats")
316
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()
320         
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")
346
347         comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
348
349 --- .* ping statistics ---
350 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
351 """
352
353         try:
354             instance.do_setup()
355             instance.do_create()
356             instance.do_connect_init()
357             instance.do_connect_compl()
358             instance.do_preconfigure()
359             
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") )
365             )
366             
367             instance.do_configure()
368             
369             instance.do_prestart()
370             instance.start()
371             while instance.status(9) != AS.STATUS_FINISHED:
372                 time.sleep(0.5)
373             ping_result = instance.trace(9, "stdout") or ""
374             packets1 = instance.trace(7, "packets") or ""
375             packets2 = instance.trace(8, "packets") or ""
376             instance.stop()
377         finally:
378             instance.shutdown()
379
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" % (
383                 ping_result,
384                 packets1,
385                 packets2))
386
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")
390
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")
394
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_tap_ping(self):
397         self._pingtest("TapInterface", "tcp")
398
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_udp(self):
401         self._pingtest("TapInterface", "udp")
402
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_nepi_depends(self):
405         instance = self.make_instance()
406         
407         instance.defer_create(2, "Node")
408         instance.defer_create_set(2, "hostname", self.host1)
409         instance.defer_create(3, "NodeInterface")
410         instance.defer_connect(2, "devs", 3, "node")
411         instance.defer_create(4, "Internet")
412         instance.defer_connect(3, "inet", 4, "devs")
413         instance.defer_create(5, "NepiDependency")
414         instance.defer_connect(5, "node", 2, "deps")
415         instance.defer_create(12, "Application")
416         instance.defer_connect(12, "node", 2, "apps")
417         instance.defer_create_set(12, "command", "python -c 'import nepi'")
418         instance.defer_add_trace(12, "stderr")
419
420         try:
421             instance.do_setup()
422             instance.do_create()
423             instance.do_connect_init()
424             instance.do_connect_compl()
425             instance.do_preconfigure()
426             instance.do_configure()
427             
428             instance.do_prestart()
429             instance.start()
430             while instance.status(12) != AS.STATUS_FINISHED:
431                 time.sleep(0.5)
432             ping_result = (instance.trace(12, "stderr") or "").strip()
433             instance.stop()
434         finally:
435             instance.shutdown()
436         
437         # asserts at the end, to make sure there's proper cleanup
438         self.assertEqual(ping_result, "")
439
440     @test_util.skipUnless(test_util.pl_auth() is not None, 
441         "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
442     @test_util.skipUnless(os.environ.get('NEPI_FULL_TESTS','').lower() in ('1','yes','true','on'),
443         "Test is expensive, requires NEPI_FULL_TESTS=yes")
444     def test_ns3_depends(self):
445         instance = self.make_instance()
446         
447         instance.defer_create(2, "Node")
448         instance.defer_create_set(2, "hostname", self.host1)
449         instance.defer_create(3, "NodeInterface")
450         instance.defer_connect(2, "devs", 3, "node")
451         instance.defer_create(4, "Internet")
452         instance.defer_connect(3, "inet", 4, "devs")
453         instance.defer_create(5, "NepiDependency")
454         instance.defer_connect(5, "node", 2, "deps")
455         instance.defer_create(6, "NS3Dependency")
456         instance.defer_connect(6, "node", 2, "deps")
457         instance.defer_create(12, "Application")
458         instance.defer_connect(12, "node", 2, "apps")
459         instance.defer_create_set(12, "command", "python -c 'import nepi.testbeds.ns3.execute ; tb = nepi.testbeds.ns3.execute.TestbedController() ; mod = tb._load_ns3_module()'")
460         instance.defer_add_trace(12, "stderr")
461
462         try:
463             instance.do_setup()
464             instance.do_create()
465             instance.do_connect_init()
466             instance.do_connect_compl()
467             instance.do_preconfigure()
468             instance.do_configure()
469             
470             instance.do_prestart()
471             instance.start()
472             while instance.status(12) != AS.STATUS_FINISHED:
473                 time.sleep(0.5)
474             ping_result = (instance.trace(12, "stderr") or "").strip()
475             instance.stop()
476         finally:
477             instance.shutdown()
478         
479         # asserts at the end, to make sure there's proper cleanup
480         self.assertEqual(ping_result, "")
481
482     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
483     def test_discovery(self):
484         instance = self.make_instance()
485         
486         instance.defer_create(2, "Node")
487         instance.defer_create_set(2, "operatingSystem", "f12")
488         instance.defer_create(3, "Node")
489         instance.defer_create_set(3, "operatingSystem", "f12")
490         instance.defer_create(4, "NodeInterface")
491         instance.defer_connect(2, "devs", 4, "node")
492         instance.defer_create(5, "NodeInterface")
493         instance.defer_connect(3, "devs", 5, "node")
494         instance.defer_create(6, "Internet")
495         instance.defer_connect(4, "inet", 6, "devs")
496         instance.defer_connect(5, "inet", 6, "devs")
497         instance.defer_create(7, "Application")
498         instance.defer_create_set(7, "command", "ping -qc1 {#[GUID-5].addr[0].[Address]#}")
499         instance.defer_add_trace(7, "stdout")
500         instance.defer_add_trace(7, "stderr")
501         instance.defer_connect(7, "node", 2, "apps")
502
503         comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
504
505 --- .* ping statistics ---
506 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
507 """
508
509         try:
510             instance.do_setup()
511             instance.do_create()
512             instance.do_connect_init()
513             instance.do_connect_compl()
514             instance.do_preconfigure()
515             
516             # Manually replace netref
517             instance.set(7, "command",
518                 instance.get(7, "command")
519                     .replace("{#[GUID-5].addr[0].[Address]#}", 
520                         instance.get_address(5, 0, "Address") )
521             )
522
523             instance.do_configure()
524             
525             instance.do_prestart()
526             instance.start()
527             while instance.status(7) != AS.STATUS_FINISHED:
528                 time.sleep(0.5)
529             ping_result = instance.trace(7, "stdout") or ""
530             instance.stop()
531         finally:
532             instance.shutdown()
533
534         # asserts at the end, to make sure there's proper cleanup
535         self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
536             "Unexpected trace:\n" + ping_result)
537         
538         
539
540 if __name__ == '__main__':
541     unittest.main()
542