Merge
[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         
57         return instance
58
59     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
60     def test_simple(self):
61         instance = self.make_instance()
62         
63         instance.defer_create(2, "Node")
64         instance.defer_create_set(2, "hostname", self.host1)
65         instance.defer_create(3, "Node")
66         instance.defer_create_set(3, "hostname", self.host2)
67         instance.defer_create(4, "NodeInterface")
68         instance.defer_connect(2, "devs", 4, "node")
69         instance.defer_create(5, "NodeInterface")
70         instance.defer_connect(3, "devs", 5, "node")
71         instance.defer_create(6, "Internet")
72         instance.defer_connect(4, "inet", 6, "devs")
73         instance.defer_connect(5, "inet", 6, "devs")
74         instance.defer_create(7, "Application")
75         instance.defer_create_set(7, "command", "ping -qc1 {#[GUID-5].addr[0].[Address]#}")
76         instance.defer_add_trace(7, "stdout")
77         instance.defer_add_trace(7, "stderr")
78         instance.defer_connect(7, "node", 2, "apps")
79
80         comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
81
82 --- .* ping statistics ---
83 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
84 """
85
86         try:
87             instance.do_setup()
88             instance.do_create()
89             instance.do_connect_init()
90             instance.do_connect_compl()
91             instance.do_preconfigure()
92             
93             # Manually replace netref
94             instance.set(7, "command",
95                 instance.get(7, "command")
96                     .replace("{#[GUID-5].addr[0].[Address]#}", 
97                         instance.get_address(5, 0, "Address") )
98             )
99
100             instance.do_configure()
101             
102             instance.do_prestart()
103             instance.start()
104             while instance.status(7) != AS.STATUS_FINISHED:
105                 time.sleep(0.5)
106             ping_result = instance.trace(7, "stdout") or ""
107             instance.stop()
108         finally:
109             instance.shutdown()
110
111         # asserts at the end, to make sure there's proper cleanup
112         self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
113             "Unexpected trace:\n" + ping_result)
114         
115     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
116     def test_depends(self):
117         instance = self.make_instance()
118         
119         instance.defer_create(2, "Node")
120         instance.defer_create_set(2, "hostname", self.host1)
121         instance.defer_create(3, "NodeInterface")
122         instance.defer_connect(2, "devs", 3, "node")
123         instance.defer_create(4, "Internet")
124         instance.defer_connect(3, "inet", 4, "devs")
125         instance.defer_create(5, "Application")
126         instance.defer_create_set(5, "command", "gfortran --version")
127         instance.defer_create_set(5, "depends", "gcc-gfortran")
128         instance.defer_add_trace(5, "stdout")
129         instance.defer_add_trace(5, "stderr")
130         instance.defer_connect(5, "node", 2, "apps")
131
132         try:
133             instance.do_setup()
134             instance.do_create()
135             instance.do_connect_init()
136             instance.do_connect_compl()
137             instance.do_preconfigure()
138             instance.do_configure()
139             
140             instance.do_prestart()
141             instance.start()
142             while instance.status(5) != AS.STATUS_FINISHED:
143                 time.sleep(0.5)
144             ping_result = instance.trace(5, "stdout") or ""
145             comp_result = r".*GNU Fortran \(GCC\).*"
146             instance.stop()
147         finally:
148             instance.shutdown()
149
150         # asserts at the end, to make sure there's proper cleanup
151         self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
152             "Unexpected trace:\n" + ping_result)
153         
154     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
155     def test_build(self):
156         instance = self.make_instance()
157         
158         instance.defer_create(2, "Node")
159         instance.defer_create_set(2, "hostname", self.host1)
160         instance.defer_create(3, "NodeInterface")
161         instance.defer_connect(2, "devs", 3, "node")
162         instance.defer_create(4, "Internet")
163         instance.defer_connect(3, "inet", 4, "devs")
164         instance.defer_create(10, "Application")
165         instance.defer_create_set(10, "command", "./consts")
166         instance.defer_create_set(10, "buildDepends", "gcc")
167         instance.defer_create_set(10, "build", "gcc ${SOURCES}/consts.c -o consts")
168         instance.defer_create_set(10, "install", "cp consts ${SOURCES}/consts")
169         instance.defer_create_set(10, "sources", os.path.join(os.path.dirname(planetlab.__file__),'scripts','consts.c'))
170         instance.defer_add_trace(10, "stdout")
171         instance.defer_add_trace(10, "stderr")
172         instance.defer_connect(10, "node", 2, "apps")
173
174         comp_result = \
175 r""".*ETH_P_ALL = 0x[0-9a-fA-F]{8}
176 ETH_P_IP = 0x[0-9a-fA-F]{8}
177 TUNGETIFF = 0x[0-9a-fA-F]{8}
178 TUNSETIFF = 0x[0-9a-fA-F]{8}
179 IFF_NO_PI = 0x[0-9a-fA-F]{8}
180 IFF_TAP = 0x[0-9a-fA-F]{8}
181 IFF_TUN = 0x[0-9a-fA-F]{8}
182 IFF_VNET_HDR = 0x[0-9a-fA-F]{8}
183 TUN_PKT_STRIP = 0x[0-9a-fA-F]{8}
184 IFHWADDRLEN = 0x[0-9a-fA-F]{8}
185 IFNAMSIZ = 0x[0-9a-fA-F]{8}
186 IFREQ_SZ = 0x[0-9a-fA-F]{8}
187 FIONREAD = 0x[0-9a-fA-F]{8}.*
188 """
189
190         try:
191             instance.do_setup()
192             instance.do_create()
193             instance.do_connect_init()
194             instance.do_connect_compl()
195             instance.do_preconfigure()
196             instance.do_configure()
197             
198             instance.do_prestart()
199             instance.start()
200             while instance.status(10) != AS.STATUS_FINISHED:
201                 time.sleep(0.5)
202             ping_result = instance.trace(10, "stdout") or ""
203             instance.stop()
204         finally:
205             instance.shutdown()
206
207         # asserts at the end, to make sure there's proper cleanup
208         self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
209             "Unexpected trace:\n" + ping_result)
210         
211     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
212     def test_simple_vsys(self):
213         instance = self.make_instance()
214         
215         instance.defer_create(2, "Node")
216         instance.defer_create_set(2, "hostname", self.host1)
217         instance.defer_create(3, "NodeInterface")
218         instance.defer_connect(2, "devs", 3, "node")
219         instance.defer_create(4, "Internet")
220         instance.defer_connect(3, "inet", 4, "devs")
221         instance.defer_create(5, "TunInterface")
222         instance.defer_add_address(5, "192.168.2.2", 24, False)
223         instance.defer_connect(2, "devs", 5, "node")
224         instance.defer_create(6, "Application")
225         instance.defer_create_set(6, "command", """
226 set -e
227 netconfig help > /dev/null
228 test -e /vsys/vif_up.in > /dev/null
229 test -e /vsys/vif_up.out > /dev/null
230 test -e /vsys/fd_tuntap.control > /dev/null
231 echo 'OKIDOKI'
232 """)
233         instance.defer_create_set(6, "sudo", True) # only sudo has access to /vsys
234         instance.defer_add_trace(6, "stdout")
235         instance.defer_add_trace(6, "stderr")
236         instance.defer_connect(6, "node", 2, "apps")
237
238         try:
239             instance.do_setup()
240             instance.do_create()
241             instance.do_connect_init()
242             instance.do_connect_compl()
243             instance.do_preconfigure()
244             instance.do_configure()
245             
246             instance.do_prestart()
247             instance.start()
248             while instance.status(6) != AS.STATUS_FINISHED:
249                 time.sleep(0.5)
250             test_result = (instance.trace(6, "stdout") or "").strip()
251             comp_result = "OKIDOKI"
252             instance.stop()
253         finally:
254             instance.shutdown()
255
256         # asserts at the end, to make sure there's proper cleanup
257         self.assertEqual(comp_result, test_result)
258
259     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
260     def test_emulation(self):
261         instance = self.make_instance()
262         
263         instance.defer_create(2, "Node")
264         instance.defer_create_set(2, "hostname", self.host1)
265         instance.defer_create(3, "NodeInterface")
266         instance.defer_connect(2, "devs", 3, "node")
267         instance.defer_create(4, "Internet")
268         instance.defer_connect(3, "inet", 4, "devs")
269         instance.defer_create(7, "NetPipe")
270         instance.defer_create_set(7, "mode", "CLIENT")
271         instance.defer_create_set(7, "portList", "80")
272         instance.defer_create_set(7, "bwOut", 12.0/1024.0) # 12kbps
273         instance.defer_create_set(7, "bwIn", 64.0/1024.0) # 64kbps
274         instance.defer_create_set(7, "plrOut", 0.01) # 1% plr outbound - high loss
275         instance.defer_create_set(7, "plrIn", 0.001) # 0.1% plr inbound - regular loss
276         instance.defer_create_set(7, "delayOut", int(1500 * 8 / (12.0/1024.0) / 1000)) # tx delay at 12kbps in ms
277         instance.defer_create_set(7, "delayIn", int(1500 * 8 / (64.0/1024.0) / 1000)) # rx delay at 64kbps in ms
278         instance.defer_add_trace(7, "netpipeStats")
279         instance.defer_connect(2, "pipes", 7, "node")
280         instance.defer_create(8, "Application")
281         instance.defer_create_set(8, "command", "time wget -q -O /dev/null http://www.google.com/") # Fetch ~10kb
282         instance.defer_add_trace(8, "stdout")
283         instance.defer_add_trace(8, "stderr")
284         instance.defer_connect(8, "node", 2, "apps")
285
286         try:
287             instance.do_setup()
288             instance.do_create()
289             instance.do_connect_init()
290             instance.do_connect_compl()
291             instance.do_preconfigure()
292             instance.do_configure()
293             
294             instance.do_prestart()
295             instance.start()
296             while instance.status(8) != AS.STATUS_FINISHED:
297                 time.sleep(0.5)
298             test_result = (instance.trace(8, "stderr") or "").strip()
299             comp_result = r".*real\s*(?P<min>[0-9]+)m(?P<sec>[0-9]+[.][0-9]+)s.*"
300             netpipe_stats = instance.trace(7, "netpipeStats")
301             
302             instance.stop()
303         finally:
304             instance.shutdown()
305
306         # asserts at the end, to make sure there's proper cleanup
307         match = re.match(comp_result, test_result, re.MULTILINE)
308         self.assertTrue(match, "Unexpected output: %s" % (test_result,))
309         
310         minutes = int(match.group("min"))
311         seconds = float(match.group("sec"))
312         self.assertTrue((minutes * 60 + seconds) > 1.0, "Emulation not effective: %s" % (test_result,))
313
314         self.assertTrue(netpipe_stats, "Unavailable netpipe stats")
315
316     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
317     def _pingtest(self, TunClass, ConnectionProto):
318         instance = self.make_instance()
319         
320         instance.defer_create(2, "Node")
321         instance.defer_create_set(2, "hostname", self.host1)
322         instance.defer_create(3, "Node")
323         instance.defer_create_set(3, "hostname", self.host2)
324         instance.defer_create(4, "NodeInterface")
325         instance.defer_connect(2, "devs", 4, "node")
326         instance.defer_create(5, "Internet")
327         instance.defer_connect(4, "inet", 5, "devs")
328         instance.defer_create(6, "NodeInterface")
329         instance.defer_connect(3, "devs", 6, "node")
330         instance.defer_connect(6, "inet", 5, "devs")
331         instance.defer_create(7, TunClass)
332         instance.defer_add_trace(7, "packets")
333         instance.defer_add_address(7, "192.168.2.2", 24, False)
334         instance.defer_connect(2, "devs", 7, "node")
335         instance.defer_create(8, TunClass)
336         instance.defer_add_trace(8, "packets")
337         instance.defer_add_address(8, "192.168.2.3", 24, False)
338         instance.defer_connect(3, "devs", 8, "node")
339         instance.defer_connect(7, ConnectionProto, 8, ConnectionProto)
340         instance.defer_create(9, "Application")
341         instance.defer_create_set(9, "command", "ping -qc1 {#[GUID-8].addr[0].[Address]#}")
342         instance.defer_add_trace(9, "stdout")
343         instance.defer_add_trace(9, "stderr")
344         instance.defer_connect(9, "node", 2, "apps")
345
346         comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
347
348 --- .* ping statistics ---
349 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
350 """
351
352         try:
353             instance.do_setup()
354             instance.do_create()
355             instance.do_connect_init()
356             instance.do_connect_compl()
357             instance.do_preconfigure()
358             
359             # Manually replace netref
360             instance.set(9, "command",
361                 instance.get(9, "command")
362                     .replace("{#[GUID-8].addr[0].[Address]#}", 
363                         instance.get_address(8, 0, "Address") )
364             )
365             
366             instance.do_configure()
367             
368             instance.do_prestart()
369             instance.start()
370             while instance.status(9) != AS.STATUS_FINISHED:
371                 time.sleep(0.5)
372             ping_result = instance.trace(9, "stdout") or ""
373             packets1 = instance.trace(7, "packets") or ""
374             packets2 = instance.trace(8, "packets") or ""
375             instance.stop()
376         finally:
377             instance.shutdown()
378
379         # asserts at the end, to make sure there's proper cleanup
380         self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
381             "Unexpected trace:\n%s\nPackets @ source:\n%s\nPackets @ target:\n%s" % (
382                 ping_result,
383                 packets1,
384                 packets2))
385
386     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
387     def test_tun_ping(self):
388         self._pingtest("TunInterface", "tcp")
389
390     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
391     def test_tun_ping_udp(self):
392         self._pingtest("TunInterface", "udp")
393
394     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
395     def test_tap_ping(self):
396         self._pingtest("TapInterface", "tcp")
397
398     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
399     def test_tap_ping_udp(self):
400         self._pingtest("TapInterface", "udp")
401
402     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
403     def test_nepi_depends(self):
404         instance = self.make_instance()
405         
406         instance.defer_create(2, "Node")
407         instance.defer_create_set(2, "hostname", self.host1)
408         instance.defer_create(3, "NodeInterface")
409         instance.defer_connect(2, "devs", 3, "node")
410         instance.defer_create(4, "Internet")
411         instance.defer_connect(3, "inet", 4, "devs")
412         instance.defer_create(5, "NepiDependency")
413         instance.defer_connect(5, "node", 2, "deps")
414         instance.defer_create(12, "Application")
415         instance.defer_connect(12, "node", 2, "apps")
416         instance.defer_create_set(12, "command", "python -c 'import nepi'")
417         instance.defer_add_trace(12, "stderr")
418
419         try:
420             instance.do_setup()
421             instance.do_create()
422             instance.do_connect_init()
423             instance.do_connect_compl()
424             instance.do_preconfigure()
425             instance.do_configure()
426             
427             instance.do_prestart()
428             instance.start()
429             while instance.status(12) != AS.STATUS_FINISHED:
430                 time.sleep(0.5)
431             ping_result = (instance.trace(12, "stderr") or "").strip()
432             instance.stop()
433         finally:
434             instance.shutdown()
435         
436         # asserts at the end, to make sure there's proper cleanup
437         self.assertEqual(ping_result, "")
438
439     @test_util.skipUnless(test_util.pl_auth() is not None, 
440         "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
441     @test_util.skipUnless(os.environ.get('NEPI_FULL_TESTS','').lower() in ('1','yes','true','on'),
442         "Test is expensive, requires NEPI_FULL_TESTS=yes")
443     def test_ns3_depends(self):
444         instance = self.make_instance()
445         
446         instance.defer_create(2, "Node")
447         instance.defer_create_set(2, "hostname", self.host1)
448         instance.defer_create(3, "NodeInterface")
449         instance.defer_connect(2, "devs", 3, "node")
450         instance.defer_create(4, "Internet")
451         instance.defer_connect(3, "inet", 4, "devs")
452         instance.defer_create(5, "NepiDependency")
453         instance.defer_connect(5, "node", 2, "deps")
454         instance.defer_create(6, "NS3Dependency")
455         instance.defer_connect(6, "node", 2, "deps")
456         instance.defer_create(12, "Application")
457         instance.defer_connect(12, "node", 2, "apps")
458         instance.defer_create_set(12, "command", "python -c 'import nepi.testbeds.ns3.execute ; tb = nepi.testbeds.ns3.execute.TestbedController() ; mod = tb._load_ns3_module()'")
459         instance.defer_add_trace(12, "stderr")
460
461         try:
462             instance.do_setup()
463             instance.do_create()
464             instance.do_connect_init()
465             instance.do_connect_compl()
466             instance.do_preconfigure()
467             instance.do_configure()
468             
469             instance.do_prestart()
470             instance.start()
471             while instance.status(12) != AS.STATUS_FINISHED:
472                 time.sleep(0.5)
473             ping_result = (instance.trace(12, "stderr") or "").strip()
474             instance.stop()
475         finally:
476             instance.shutdown()
477         
478         # asserts at the end, to make sure there's proper cleanup
479         self.assertEqual(ping_result, "")
480
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_discovery(self):
483         instance = self.make_instance()
484         
485         instance.defer_create(2, "Node")
486         instance.defer_create_set(2, "operatingSystem", "f12")
487         instance.defer_create(3, "Node")
488         instance.defer_create_set(3, "operatingSystem", "f12")
489         instance.defer_create(4, "NodeInterface")
490         instance.defer_connect(2, "devs", 4, "node")
491         instance.defer_create(5, "NodeInterface")
492         instance.defer_connect(3, "devs", 5, "node")
493         instance.defer_create(6, "Internet")
494         instance.defer_connect(4, "inet", 6, "devs")
495         instance.defer_connect(5, "inet", 6, "devs")
496         instance.defer_create(7, "Application")
497         instance.defer_create_set(7, "command", "ping -qc1 {#[GUID-5].addr[0].[Address]#}")
498         instance.defer_add_trace(7, "stdout")
499         instance.defer_add_trace(7, "stderr")
500         instance.defer_connect(7, "node", 2, "apps")
501
502         comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
503
504 --- .* ping statistics ---
505 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
506 """
507
508         try:
509             instance.do_setup()
510             instance.do_create()
511             instance.do_connect_init()
512             instance.do_connect_compl()
513             instance.do_preconfigure()
514             
515             # Manually replace netref
516             instance.set(7, "command",
517                 instance.get(7, "command")
518                     .replace("{#[GUID-5].addr[0].[Address]#}", 
519                         instance.get_address(5, 0, "Address") )
520             )
521
522             instance.do_configure()
523             
524             instance.do_prestart()
525             instance.start()
526             while instance.status(7) != AS.STATUS_FINISHED:
527                 time.sleep(0.5)
528             ping_result = instance.trace(7, "stdout") or ""
529             instance.stop()
530         finally:
531             instance.shutdown()
532
533         # asserts at the end, to make sure there's proper cleanup
534         self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
535             "Unexpected trace:\n" + ping_result)
536         
537         
538
539 if __name__ == '__main__':
540     unittest.main()
541