Various fixes:
[nepi.git] / test / testbeds / planetlab / integration.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import getpass
5 from nepi.core.design import ExperimentDescription, FactoriesProvider
6 from nepi.core.execute import ExperimentController
7 from nepi.util import proxy
8 from nepi.util.constants import DeploymentConfiguration as DC
9 import os
10 import shutil
11 import tempfile
12 import test_util
13 import time
14 import unittest
15 import re
16 import sys
17
18 class PlanetLabIntegrationTestCase(unittest.TestCase):
19     testbed_id = "planetlab"
20     slicename = "inria_nepi"
21     plchost = "nepiplc.pl.sophia.inria.fr"
22     
23     host1 = "nepi1.pl.sophia.inria.fr"
24     host2 = "nepi2.pl.sophia.inria.fr"
25     host3 = "nepi3.pl.sophia.inria.fr"
26     host4 = "nepi5.pl.sophia.inria.fr"
27
28     port_base = 2000 + (os.getpid() % 1000) * 13
29     
30     def setUp(self):
31         self.root_dir = tempfile.mkdtemp()
32         self.__class__.port_base = self.port_base + 100
33
34     def tearDown(self):
35         try:
36             shutil.rmtree(self.root_dir)
37         except:
38             # retry
39             time.sleep(0.1)
40             shutil.rmtree(self.root_dir)
41
42     def make_experiment_desc(self):
43         testbed_id = self.testbed_id
44         slicename = self.slicename
45         plchost = self.plchost
46         pl_ssh_key = os.environ.get(
47             "PL_SSH_KEY",
48             "%s/.ssh/id_rsa_planetlab" % (os.environ['HOME'],) )
49         pl_user, pl_pwd = test_util.pl_auth()
50
51         exp_desc = ExperimentDescription()
52         pl_provider = FactoriesProvider(testbed_id)
53         pl_desc = exp_desc.add_testbed_description(pl_provider)
54         pl_desc.set_attribute_value("homeDirectory", self.root_dir)
55         pl_desc.set_attribute_value("slice", slicename)
56         pl_desc.set_attribute_value("sliceSSHKey", pl_ssh_key)
57         pl_desc.set_attribute_value("authUser", pl_user)
58         pl_desc.set_attribute_value("authPass", pl_pwd)
59         pl_desc.set_attribute_value("plcHost", plchost)
60         pl_desc.set_attribute_value("tapPortBase", self.port_base)
61         
62         return pl_desc, exp_desc
63     
64     def _test_simple(self, daemonize_testbed, controller_access_configuration, environ = None):
65         pl, exp = self.make_experiment_desc()
66         
67         node1 = pl.create("Node")
68         node2 = pl.create("Node")
69         node1.set_attribute_value("hostname", self.host1)
70         node2.set_attribute_value("hostname", self.host2)
71         iface1 = pl.create("NodeInterface")
72         iface2 = pl.create("NodeInterface")
73         iface2.set_attribute_value("label", "node2iface")
74         inet = pl.create("Internet")
75         node1.connector("devs").connect(iface1.connector("node"))
76         node2.connector("devs").connect(iface2.connector("node"))
77         iface1.connector("inet").connect(inet.connector("devs"))
78         iface2.connector("inet").connect(inet.connector("devs"))
79         app = pl.create("Application")
80         app.set_attribute_value("command", "ping -qc1 {#[node2iface].addr[0].[Address]#}")
81         app.enable_trace("stdout")
82         app.connector("node").connect(node1.connector("apps"))
83
84         if daemonize_testbed:
85             pl.set_attribute_value(DC.DEPLOYMENT_MODE, DC.MODE_DAEMON)
86             inst_root_dir = os.path.join(self.root_dir, "instance")
87             os.mkdir(inst_root_dir)
88             pl.set_attribute_value(DC.ROOT_DIRECTORY, inst_root_dir)
89             pl.set_attribute_value(DC.LOG_LEVEL, DC.DEBUG_LEVEL)
90
91             if environ:
92                 pl.set_attribute_value(DC.DEPLOYMENT_ENVIRONMENT_SETUP, environ)
93
94         xml = exp.to_xml()
95
96         if controller_access_configuration:
97             controller = proxy.create_experiment_controller(xml, 
98                 controller_access_configuration)
99         else:
100             controller = ExperimentController(xml, self.root_dir)
101         
102         try:
103             controller.start()
104             while not controller.is_finished(app.guid):
105                 time.sleep(0.5)
106             ping_result = controller.trace(app.guid, "stdout")
107             comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
108
109 --- .* ping statistics ---
110 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
111 """
112             self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
113                 "Unexpected trace:\n" + ping_result)
114         
115         finally:
116             controller.stop()
117             controller.shutdown()
118
119     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
120     def test_spanning_deployment(self):
121         pl, exp = self.make_experiment_desc()
122         
123         from nepi.testbeds import planetlab as plpackage
124         
125         nodes = [ pl.create("Node") for i in xrange(4) ]
126         ifaces = [ pl.create("NodeInterface") for node in nodes ]
127         inet = pl.create("Internet")
128         for node, iface in zip(nodes,ifaces):
129             node.connector("devs").connect(iface.connector("node"))
130             iface.connector("inet").connect(inet.connector("devs"))
131         
132         apps = []
133         for node in nodes:
134             app = pl.create("Application")
135             app.set_attribute_value("command", "./consts")
136             app.set_attribute_value("buildDepends", "gcc")
137             app.set_attribute_value("build", "gcc ${SOURCES}/consts.c -o consts")
138             app.set_attribute_value("install", "cp consts ${SOURCES}/consts")
139             app.set_attribute_value("sources", os.path.join(
140                 os.path.dirname(plpackage.__file__),'scripts','consts.c'))
141             app.enable_trace("stdout")
142             app.enable_trace("stderr")
143             app.enable_trace("buildlog")
144             node.connector("apps").connect(app.connector("node"))
145             apps.append(app)
146
147         comp_result = \
148 r""".*ETH_P_ALL = 0x[0-9a-fA-F]{8}
149 ETH_P_IP = 0x[0-9a-fA-F]{8}
150 TUNGETIFF = 0x[0-9a-fA-F]{8}
151 TUNSETIFF = 0x[0-9a-fA-F]{8}
152 IFF_NO_PI = 0x[0-9a-fA-F]{8}
153 IFF_TAP = 0x[0-9a-fA-F]{8}
154 IFF_TUN = 0x[0-9a-fA-F]{8}
155 IFF_VNET_HDR = 0x[0-9a-fA-F]{8}
156 TUN_PKT_STRIP = 0x[0-9a-fA-F]{8}
157 IFHWADDRLEN = 0x[0-9a-fA-F]{8}
158 IFNAMSIZ = 0x[0-9a-fA-F]{8}
159 IFREQ_SZ = 0x[0-9a-fA-F]{8}
160 FIONREAD = 0x[0-9a-fA-F]{8}.*
161 """
162
163         comp_build = r".*(Identity added|gcc).*"
164
165         xml = exp.to_xml()
166
167         controller = ExperimentController(xml, self.root_dir)
168         try:
169             controller.start()
170             while not all(controller.is_finished(app.guid) for app in apps):
171                 time.sleep(0.5)
172             
173             for app in apps:
174                 app_result = controller.trace(app.guid, "stdout") or ""
175                 self.assertTrue(re.match(comp_result, app_result, re.MULTILINE),
176                     "Unexpected trace:\n" + app_result)
177
178                 build_result = controller.trace(app.guid, "buildlog") or ""
179                 self.assertTrue(re.match(comp_build, build_result, re.MULTILINE | re.DOTALL),
180                     "Unexpected trace:\n" + build_result)
181         
182         finally:
183             controller.stop()
184             controller.shutdown()
185
186     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
187     def test_simple(self):
188         self._test_simple(
189             daemonize_testbed = False,
190             controller_access_configuration = None)
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_daemonized(self):
194         access_config = proxy.AccessConfiguration({
195             DC.DEPLOYMENT_MODE : DC.MODE_DAEMON,
196             DC.ROOT_DIRECTORY : self.root_dir,
197         })
198
199         self._test_simple(
200             daemonize_testbed = False,
201             controller_access_configuration = access_config)
202
203     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
204     def test_z_simple_ssh(self): # _z_ cause we want it last - it messes up the process :(
205         # Recreate environment
206         environ = ' ; '.join( map("export %s=%r".__mod__, os.environ.iteritems()) )
207
208         env = test_util.test_environment()
209
210         access_config = proxy.AccessConfiguration({
211             DC.DEPLOYMENT_MODE : DC.MODE_DAEMON,
212             DC.ROOT_DIRECTORY : self.root_dir,
213             DC.LOG_LEVEL : DC.DEBUG_LEVEL,
214             DC.DEPLOYMENT_COMMUNICATION : DC.ACCESS_SSH,
215             DC.DEPLOYMENT_PORT : env.port,
216             DC.USE_AGENT : True,
217             DC.DEPLOYMENT_ENVIRONMENT_SETUP : environ,
218         })
219
220         self._test_simple(
221             daemonize_testbed = False,
222             controller_access_configuration = access_config,
223             environ = environ)
224         
225
226 if __name__ == '__main__':
227     unittest.main()
228