Removed version number from testbed file names. Only 1 version permitted at the time.
[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 import os
9 import shutil
10 import tempfile
11 import test_util
12 import time
13 import unittest
14 import re
15 import sys
16
17 class PlanetLabIntegrationTestCase(unittest.TestCase):
18     testbed_id = "planetlab"
19     slicename = "inria_nepi"
20     plchost = "nepiplc.pl.sophia.inria.fr"
21     
22     host1 = "nepi1.pl.sophia.inria.fr"
23     host2 = "nepi2.pl.sophia.inria.fr"
24     host3 = "nepi3.pl.sophia.inria.fr"
25     host4 = "nepi5.pl.sophia.inria.fr"
26
27     def setUp(self):
28         self.root_dir = tempfile.mkdtemp()
29
30     def tearDown(self):
31         return
32         try:
33             shutil.rmtree(self.root_dir)
34         except:
35             # retry
36             time.sleep(0.1)
37             shutil.rmtree(self.root_dir)
38
39     def make_experiment_desc(self):
40         testbed_id = self.testbed_id
41         slicename = self.slicename
42         plchost = self.plchost
43         pl_ssh_key = os.environ.get(
44             "PL_SSH_KEY",
45             "%s/.ssh/id_rsa_planetlab" % (os.environ['HOME'],) )
46         pl_user, pl_pwd = test_util.pl_auth()
47
48         exp_desc = ExperimentDescription()
49         pl_provider = FactoriesProvider(testbed_id)
50         pl_desc = exp_desc.add_testbed_description(pl_provider)
51         pl_desc.set_attribute_value("homeDirectory", self.root_dir)
52         pl_desc.set_attribute_value("slice", slicename)
53         pl_desc.set_attribute_value("sliceSSHKey", pl_ssh_key)
54         pl_desc.set_attribute_value("authUser", pl_user)
55         pl_desc.set_attribute_value("authPass", pl_pwd)
56         pl_desc.set_attribute_value("plcHost", plchost)
57         
58         return pl_desc, exp_desc
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         pl, exp = self.make_experiment_desc()
63         
64         node1 = pl.create("Node")
65         node2 = pl.create("Node")
66         node1.set_attribute_value("hostname", self.host1)
67         node2.set_attribute_value("hostname", self.host2)
68         iface1 = pl.create("NodeInterface")
69         iface2 = pl.create("NodeInterface")
70         iface2.set_attribute_value("label", "node2iface")
71         inet = pl.create("Internet")
72         node1.connector("devs").connect(iface1.connector("node"))
73         node2.connector("devs").connect(iface2.connector("node"))
74         iface1.connector("inet").connect(inet.connector("devs"))
75         iface2.connector("inet").connect(inet.connector("devs"))
76         app = pl.create("Application")
77         app.set_attribute_value("command", "ping -qc1 {#[node2iface].addr[0].[Address]#}")
78         app.enable_trace("stdout")
79         app.connector("node").connect(node1.connector("apps"))
80
81         xml = exp.to_xml()
82
83         controller = ExperimentController(xml, self.root_dir)
84         try:
85             controller.start()
86             while not controller.is_finished(app.guid):
87                 time.sleep(0.5)
88             ping_result = controller.trace(app.guid, "stdout")
89             comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
90
91 --- .* ping statistics ---
92 1 packets transmitted, 1 received, 0% packet loss, time \d*ms.*
93 """
94             self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
95                 "Unexpected trace:\n" + ping_result)
96         
97         finally:
98             controller.stop()
99             controller.shutdown()
100
101
102     @test_util.skipUnless(test_util.pl_auth() is not None, "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
103     def test_spanning_deployment(self):
104         pl, exp = self.make_experiment_desc()
105         
106         from nepi.testbeds import planetlab as plpackage
107         
108         nodes = [ pl.create("Node") for i in xrange(4) ]
109         ifaces = [ pl.create("NodeInterface") for node in nodes ]
110         inet = pl.create("Internet")
111         for node, iface in zip(nodes,ifaces):
112             node.connector("devs").connect(iface.connector("node"))
113             iface.connector("inet").connect(inet.connector("devs"))
114         
115         apps = []
116         for node in nodes:
117             app = pl.create("Application")
118             app.set_attribute_value("command", "./consts")
119             app.set_attribute_value("buildDepends", "gcc")
120             app.set_attribute_value("build", "gcc ${SOURCES}/consts.c -o consts")
121             app.set_attribute_value("install", "cp consts ${SOURCES}/consts")
122             app.set_attribute_value("sources", os.path.join(
123                 os.path.dirname(plpackage.__file__),'scripts','consts.c'))
124             app.enable_trace("stdout")
125             app.enable_trace("stderr")
126             app.enable_trace("buildlog")
127             node.connector("apps").connect(app.connector("node"))
128             apps.append(app)
129
130         comp_result = \
131 r""".*ETH_P_ALL = 0x[0-9a-fA-F]{8}
132 ETH_P_IP = 0x[0-9a-fA-F]{8}
133 TUNGETIFF = 0x[0-9a-fA-F]{8}
134 TUNSETIFF = 0x[0-9a-fA-F]{8}
135 IFF_NO_PI = 0x[0-9a-fA-F]{8}
136 IFF_TAP = 0x[0-9a-fA-F]{8}
137 IFF_TUN = 0x[0-9a-fA-F]{8}
138 IFF_VNET_HDR = 0x[0-9a-fA-F]{8}
139 TUN_PKT_STRIP = 0x[0-9a-fA-F]{8}
140 IFHWADDRLEN = 0x[0-9a-fA-F]{8}
141 IFNAMSIZ = 0x[0-9a-fA-F]{8}
142 IFREQ_SZ = 0x[0-9a-fA-F]{8}
143 FIONREAD = 0x[0-9a-fA-F]{8}.*
144 """
145
146         comp_build = r".*(Identity added|gcc).*"
147
148         xml = exp.to_xml()
149
150         controller = ExperimentController(xml, self.root_dir)
151         try:
152             controller.start()
153             while not all(controller.is_finished(app.guid) for app in apps):
154                 time.sleep(0.5)
155             
156             for app in apps:
157                 app_result = controller.trace(app.guid, "stdout") or ""
158                 self.assertTrue(re.match(comp_result, app_result, re.MULTILINE),
159                     "Unexpected trace:\n" + app_result)
160
161                 build_result = controller.trace(app.guid, "buildlog") or ""
162                 self.assertTrue(re.match(comp_build, build_result, re.MULTILINE | re.DOTALL),
163                     "Unexpected trace:\n" + build_result)
164         
165         finally:
166             controller.stop()
167             controller.shutdown()
168         
169
170 if __name__ == '__main__':
171     unittest.main()
172