cef57b74b552dba395f0d8771ecf4d16e65b4b90
[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 STATUS_FINISHED
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
15 class PlanetLabExecuteTestCase(unittest.TestCase):
16     def setUp(self):
17         self.root_dir = tempfile.mkdtemp()
18         
19     def tearDown(self):
20         try:
21             shutil.rmtree(self.root_dir)
22         except:
23             # retry
24             time.sleep(0.1)
25             shutil.rmtree(self.root_dir)
26
27     def make_instance(self):
28         testbed_version = "01"
29         instance = planetlab.TestbedController(testbed_version)
30         slicename = "inria_nepi12"
31         pl_ssh_key = os.environ.get(
32             "PL_SSH_KEY",
33             "%s/.ssh/id_rsa_planetlab" % (os.environ['HOME'],) )
34         pl_user, pl_pwd = test_util.pl_auth()
35         
36         instance.defer_configure("homeDirectory", self.root_dir)
37         instance.defer_configure("slice", slicename)
38         instance.defer_configure("sliceSSHKey", pl_ssh_key)
39         instance.defer_configure("authUser", pl_user)
40         instance.defer_configure("authPass", pl_pwd)
41         
42         return instance
43
44     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
45     def test_simple(self):
46         instance = self.make_instance()
47         
48         instance.defer_create(2, "Node")
49         instance.defer_create_set(2, "hostname", "onelab11.pl.sophia.inria.fr")
50         instance.defer_create(3, "Node")
51         instance.defer_create_set(3, "hostname", "onelab10.pl.sophia.inria.fr")
52         instance.defer_create(4, "NodeInterface")
53         instance.defer_connect(2, "devs", 4, "node")
54         instance.defer_create(5, "NodeInterface")
55         instance.defer_connect(3, "devs", 5, "node")
56         instance.defer_create(6, "Internet")
57         instance.defer_connect(4, "inet", 6, "devs")
58         instance.defer_connect(5, "inet", 6, "devs")
59         instance.defer_create(7, "Application")
60         instance.defer_create_set(7, "command", "ping -qc1 {#[GUID-5].addr[0].[Address]#}")
61         instance.defer_add_trace(7, "stdout")
62         instance.defer_add_trace(7, "stderr")
63         instance.defer_connect(7, "node", 2, "apps")
64
65         comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
66
67 --- .* ping statistics ---
68 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
69 """
70
71         try:
72             instance.do_setup()
73             instance.do_create()
74             instance.do_connect_init()
75             instance.do_connect_compl()
76             instance.do_preconfigure()
77             
78             # Manually replace netref
79             instance.set(7, "command",
80                 instance.get(7, "command")
81                     .replace("{#[GUID-5].addr[0].[Address]#}", 
82                         instance.get_address(5, 0, "Address") )
83             )
84
85             instance.do_configure()
86             
87             instance.start()
88             while instance.status(7) != STATUS_FINISHED:
89                 time.sleep(0.5)
90             ping_result = instance.trace(7, "stdout") or ""
91             instance.stop()
92         finally:
93             instance.shutdown()
94
95         # asserts at the end, to make sure there's proper cleanup
96         self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
97             "Unexpected trace:\n" + ping_result)
98         
99     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
100     def test_depends(self):
101         instance = self.make_instance()
102         
103         instance.defer_create(2, "Node")
104         instance.defer_create_set(2, "hostname", "onelab11.pl.sophia.inria.fr")
105         instance.defer_create(3, "NodeInterface")
106         instance.defer_connect(2, "devs", 3, "node")
107         instance.defer_create(4, "Internet")
108         instance.defer_connect(3, "inet", 4, "devs")
109         instance.defer_create(5, "Application")
110         instance.defer_create_set(5, "command", "gfortran --version")
111         instance.defer_create_set(5, "depends", "gcc-gfortran")
112         instance.defer_add_trace(5, "stdout")
113         instance.defer_add_trace(5, "stderr")
114         instance.defer_connect(5, "node", 2, "apps")
115
116         try:
117             instance.do_setup()
118             instance.do_create()
119             instance.do_connect_init()
120             instance.do_connect_compl()
121             instance.do_preconfigure()
122             instance.do_configure()
123             
124             instance.start()
125             while instance.status(5) != STATUS_FINISHED:
126                 time.sleep(0.5)
127             ping_result = instance.trace(5, "stdout") or ""
128             comp_result = r".*GNU Fortran \(GCC\).*"
129             instance.stop()
130         finally:
131             instance.shutdown()
132
133         # asserts at the end, to make sure there's proper cleanup
134         self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
135             "Unexpected trace:\n" + ping_result)
136         
137     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
138     def test_build(self):
139         instance = self.make_instance()
140         
141         instance.defer_create(2, "Node")
142         instance.defer_create_set(2, "hostname", "onelab11.pl.sophia.inria.fr")
143         instance.defer_create(3, "NodeInterface")
144         instance.defer_connect(2, "devs", 3, "node")
145         instance.defer_create(4, "Internet")
146         instance.defer_connect(3, "inet", 4, "devs")
147         instance.defer_create(10, "Application")
148         instance.defer_create_set(10, "command", "./consts")
149         instance.defer_create_set(10, "buildDepends", "gcc")
150         instance.defer_create_set(10, "build", "gcc ${SOURCES}/consts.c -o consts")
151         instance.defer_create_set(10, "install", "cp consts ${SOURCES}/consts")
152         instance.defer_create_set(10, "sources", os.path.join(os.path.dirname(planetlab.__file__),'scripts','consts.c'))
153         instance.defer_add_trace(10, "stdout")
154         instance.defer_add_trace(10, "stderr")
155         instance.defer_connect(10, "node", 2, "apps")
156
157         comp_result = \
158 r""".*ETH_P_ALL = 0x[0-9a-fA-F]{8}
159 ETH_P_IP = 0x[0-9a-fA-F]{8}
160 TUNSETIFF = 0x[0-9a-fA-F]{8}
161 IFF_NO_PI = 0x[0-9a-fA-F]{8}
162 IFF_TAP = 0x[0-9a-fA-F]{8}
163 IFF_TUN = 0x[0-9a-fA-F]{8}
164 IFF_VNET_HDR = 0x[0-9a-fA-F]{8}
165 TUN_PKT_STRIP = 0x[0-9a-fA-F]{8}
166 IFHWADDRLEN = 0x[0-9a-fA-F]{8}
167 IFNAMSIZ = 0x[0-9a-fA-F]{8}
168 IFREQ_SZ = 0x[0-9a-fA-F]{8}
169 FIONREAD = 0x[0-9a-fA-F]{8}.*
170 """
171
172         try:
173             instance.do_setup()
174             instance.do_create()
175             instance.do_connect_init()
176             instance.do_connect_compl()
177             instance.do_preconfigure()
178             instance.do_configure()
179             
180             instance.start()
181             while instance.status(10) != STATUS_FINISHED:
182                 time.sleep(0.5)
183             ping_result = instance.trace(10, "stdout") or ""
184             instance.stop()
185         finally:
186             instance.shutdown()
187
188         # asserts at the end, to make sure there's proper cleanup
189         self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
190             "Unexpected trace:\n" + ping_result)
191         
192     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
193     def test_simple_vsys(self):
194         instance = self.make_instance()
195         
196         instance.defer_create(2, "Node")
197         instance.defer_create_set(2, "hostname", "onelab11.pl.sophia.inria.fr")
198         instance.defer_create_set(2, "emulation", True) # require emulation
199         instance.defer_create(3, "NodeInterface")
200         instance.defer_connect(2, "devs", 3, "node")
201         instance.defer_create(4, "Internet")
202         instance.defer_connect(3, "inet", 4, "devs")
203         instance.defer_create(5, "TunInterface")
204         instance.defer_add_address(5, "192.168.2.2", 24, False)
205         instance.defer_connect(2, "devs", 5, "node")
206         instance.defer_create(6, "Application")
207         instance.defer_create_set(6, "command", """
208 set -e
209 netconfig help > /dev/null
210 test -e /vsys/vif_up.in > /dev/null
211 test -e /vsys/vif_up.out > /dev/null
212 test -e /vsys/fd_tuntap.control > /dev/null
213 echo 'OKIDOKI'
214 """)
215         instance.defer_create_set(6, "sudo", True) # only sudo has access to /vsys
216         instance.defer_add_trace(6, "stdout")
217         instance.defer_add_trace(6, "stderr")
218         instance.defer_connect(6, "node", 2, "apps")
219
220         try:
221             instance.do_setup()
222             instance.do_create()
223             instance.do_connect_init()
224             instance.do_connect_compl()
225             instance.do_preconfigure()
226             instance.do_configure()
227             
228             instance.start()
229             while instance.status(6) != STATUS_FINISHED:
230                 time.sleep(0.5)
231             test_result = (instance.trace(6, "stdout") or "").strip()
232             comp_result = "OKIDOKI"
233             instance.stop()
234         finally:
235             instance.shutdown()
236
237         # asserts at the end, to make sure there's proper cleanup
238         self.assertEqual(comp_result, test_result)
239
240     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
241     def test_emulation(self):
242         instance = self.make_instance()
243         
244         instance.defer_create(2, "Node")
245         instance.defer_create_set(2, "hostname", "onelab11.pl.sophia.inria.fr")
246         instance.defer_create_set(2, "emulation", True) # require emulation
247         instance.defer_create(3, "NodeInterface")
248         instance.defer_connect(2, "devs", 3, "node")
249         instance.defer_create(4, "Internet")
250         instance.defer_connect(3, "inet", 4, "devs")
251         instance.defer_create(7, "NetPipe")
252         instance.defer_create_set(7, "mode", "CLIENT")
253         instance.defer_create_set(7, "portList", "80")
254         instance.defer_create_set(7, "bwOut", 12.0/1024.0) # 12kbps
255         instance.defer_create_set(7, "bwIn", 64.0/1024.0) # 64kbps
256         instance.defer_create_set(7, "plrOut", 0.01) # 1% plr outbound - high loss
257         instance.defer_create_set(7, "plrIn", 0.001) # 0.1% plr inbound - regular loss
258         instance.defer_create_set(7, "delayOut", int(1500 * 8 / (12.0/1024.0) / 1000)) # tx delay at 12kbps in ms
259         instance.defer_create_set(7, "delayIn", int(1500 * 8 / (64.0/1024.0) / 1000)) # rx delay at 64kbps in ms
260         instance.defer_add_trace(7, "netpipeStats")
261         instance.defer_connect(2, "pipes", 7, "node")
262         instance.defer_create(8, "Application")
263         instance.defer_create_set(8, "command", "time wget -q -O /dev/null http://www.google.com/") # Fetch ~10kb
264         instance.defer_add_trace(8, "stdout")
265         instance.defer_add_trace(8, "stderr")
266         instance.defer_connect(8, "node", 2, "apps")
267
268         try:
269             instance.do_setup()
270             instance.do_create()
271             instance.do_connect_init()
272             instance.do_connect_compl()
273             instance.do_preconfigure()
274             instance.do_configure()
275             
276             instance.start()
277             while instance.status(8) != STATUS_FINISHED:
278                 time.sleep(0.5)
279             test_result = (instance.trace(8, "stderr") or "").strip()
280             comp_result = r".*real\s*(?P<min>[0-9]+)m(?P<sec>[0-9]+[.][0-9]+)s.*"
281             netpipe_stats = instance.trace(7, "netpipeStats")
282             
283             instance.stop()
284         finally:
285             instance.shutdown()
286
287         # asserts at the end, to make sure there's proper cleanup
288         match = re.match(comp_result, test_result, re.MULTILINE)
289         self.assertTrue(match, "Unexpected output: %s" % (test_result,))
290         
291         minutes = int(match.group("min"))
292         seconds = float(match.group("sec"))
293         self.assertTrue((minutes * 60 + seconds) > 1.0, "Emulation not effective: %s" % (test_result,))
294
295         self.assertTrue(netpipe_stats, "Unavailable netpipe stats")
296
297     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
298     def test_tun_emulation_requirement(self):
299         instance = self.make_instance()
300         
301         instance.defer_create(2, "Node")
302         instance.defer_create_set(2, "hostname", "onelab11.pl.sophia.inria.fr")
303         instance.defer_create(3, "NodeInterface")
304         instance.defer_connect(2, "devs", 3, "node")
305         instance.defer_create(4, "Internet")
306         instance.defer_connect(3, "inet", 4, "devs")
307         instance.defer_create(5, "TunInterface")
308         instance.defer_add_address(5, "192.168.2.2", 24, False)
309         instance.defer_connect(2, "devs", 5, "node")
310         instance.defer_create(6, "Application")
311         instance.defer_create_set(6, "command", "false")
312         instance.defer_add_trace(6, "stdout")
313         instance.defer_add_trace(6, "stderr")
314         instance.defer_connect(6, "node", 2, "apps")
315
316         try:
317             instance.do_setup()
318             instance.do_create()
319             instance.do_connect_init()
320             instance.do_connect_compl()
321             instance.do_preconfigure()
322             instance.do_configure()
323             self.fail("Usage of TUN without emulation should fail")
324         except Exception,e:
325             pass
326
327     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
328     def _pingtest(self, TunClass, ConnectionProto):
329         instance = self.make_instance()
330         
331         instance.defer_create(2, "Node")
332         instance.defer_create_set(2, "hostname", "onelab11.pl.sophia.inria.fr")
333         instance.defer_create_set(2, "emulation", True) # require emulation
334         instance.defer_create(3, "Node")
335         instance.defer_create_set(3, "hostname", "onelab10.pl.sophia.inria.fr")
336         instance.defer_create_set(3, "emulation", True) # require emulation
337         instance.defer_create(4, "NodeInterface")
338         instance.defer_connect(2, "devs", 4, "node")
339         instance.defer_create(5, "Internet")
340         instance.defer_connect(4, "inet", 5, "devs")
341         instance.defer_create(6, "NodeInterface")
342         instance.defer_connect(3, "devs", 6, "node")
343         instance.defer_connect(6, "inet", 5, "devs")
344         instance.defer_create(7, TunClass)
345         instance.defer_add_address(7, "192.168.2.2", 24, False)
346         instance.defer_connect(2, "devs", 7, "node")
347         instance.defer_create(8, TunClass)
348         instance.defer_add_address(8, "192.168.2.3", 24, False)
349         instance.defer_connect(3, "devs", 8, "node")
350         instance.defer_connect(7, ConnectionProto, 8, ConnectionProto)
351         instance.defer_create(9, "Application")
352         instance.defer_create_set(9, "command", "ping -qc1 {#[GUID-8].addr[0].[Address]#}")
353         instance.defer_add_trace(9, "stdout")
354         instance.defer_add_trace(9, "stderr")
355         instance.defer_connect(9, "node", 2, "apps")
356
357         comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
358
359 --- .* ping statistics ---
360 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
361 """
362
363         try:
364             instance.do_setup()
365             instance.do_create()
366             instance.do_connect_init()
367             instance.do_connect_compl()
368             instance.do_preconfigure()
369             
370             # Manually replace netref
371             instance.set(9, "command",
372                 instance.get(9, "command")
373                     .replace("{#[GUID-8].addr[0].[Address]#}", 
374                         instance.get_address(8, 0, "Address") )
375             )
376             
377             instance.do_configure()
378             
379             instance.start()
380             while instance.status(9) != STATUS_FINISHED:
381                 time.sleep(0.5)
382             ping_result = instance.trace(9, "stdout") or ""
383             instance.stop()
384         finally:
385             instance.shutdown()
386
387         # asserts at the end, to make sure there's proper cleanup
388         self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
389             "Unexpected trace:\n" + ping_result)
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(self):
393         self._pingtest("TunInterface", "tcp")
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_tun_ping_udp(self):
397         self._pingtest("TunInterface", "udp")
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(self):
401         self._pingtest("TapInterface", "tcp")
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_tap_ping_udp(self):
405         self._pingtest("TapInterface", "udp")
406
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_nepi_depends(self):
409         instance = self.make_instance()
410         
411         instance.defer_create(2, "Node")
412         instance.defer_create_set(2, "hostname", "onelab11.pl.sophia.inria.fr")
413         instance.defer_create(3, "NodeInterface")
414         instance.defer_connect(2, "devs", 3, "node")
415         instance.defer_create(4, "Internet")
416         instance.defer_connect(3, "inet", 4, "devs")
417         instance.defer_create(5, "NepiDependency")
418         instance.defer_connect(5, "node", 2, "deps")
419         instance.defer_create(12, "Application")
420         instance.defer_connect(12, "node", 2, "apps")
421         instance.defer_create_set(12, "command", "python -c 'import nepi'")
422         instance.defer_add_trace(12, "stderr")
423
424         try:
425             instance.do_setup()
426             instance.do_create()
427             instance.do_connect_init()
428             instance.do_connect_compl()
429             instance.do_preconfigure()
430             instance.do_configure()
431             
432             instance.start()
433             while instance.status(12) != STATUS_FINISHED:
434                 time.sleep(0.5)
435             ping_result = (instance.trace(12, "stderr") or "").strip()
436             instance.stop()
437         finally:
438             instance.shutdown()
439         
440         # asserts at the end, to make sure there's proper cleanup
441         self.assertEqual(ping_result, "")
442
443     @test_util.skipUnless(test_util.pl_auth() is not None, 
444         "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
445     @test_util.skipUnless(os.environ.get('NEPI_FULL_TESTS','').lower() in ('1','yes','true','on'),
446         "Test is expensive, requires NEPI_FULL_TESTS=yes")
447     def test_ns3_depends(self):
448         instance = self.make_instance()
449         
450         instance.defer_create(2, "Node")
451         instance.defer_create_set(2, "hostname", "onelab11.pl.sophia.inria.fr")
452         instance.defer_create(3, "NodeInterface")
453         instance.defer_connect(2, "devs", 3, "node")
454         instance.defer_create(4, "Internet")
455         instance.defer_connect(3, "inet", 4, "devs")
456         instance.defer_create(5, "NepiDependency")
457         instance.defer_connect(5, "node", 2, "deps")
458         instance.defer_create(6, "NS3Dependency")
459         instance.defer_connect(6, "node", 2, "deps")
460         instance.defer_create(12, "Application")
461         instance.defer_connect(12, "node", 2, "apps")
462         instance.defer_create_set(12, "command", "python -c 'import nepi.testbeds.ns3.execute ; tb = nepi.testbeds.ns3.execute.TestbedController(\"3_9_RC3\") ; mod = tb._load_ns3_module()'")
463         instance.defer_add_trace(12, "stderr")
464
465         try:
466             instance.do_setup()
467             instance.do_create()
468             instance.do_connect_init()
469             instance.do_connect_compl()
470             instance.do_preconfigure()
471             instance.do_configure()
472             
473             instance.start()
474             while instance.status(12) != STATUS_FINISHED:
475                 time.sleep(0.5)
476             ping_result = (instance.trace(12, "stderr") or "").strip()
477             instance.stop()
478         finally:
479             instance.shutdown()
480         
481         # asserts at the end, to make sure there's proper cleanup
482         self.assertEqual(ping_result, "")
483         
484
485 if __name__ == '__main__':
486     unittest.main()
487