Lots of cross-connection fixes, TUN synchronization, etc
[nepi.git] / test / testbeds / planetlab / integration_multi.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, ATTR_NEPI_TESTBED_ENVIRONMENT_SETUP
9 import os
10 import shutil
11 import tempfile
12 import test_util
13 import time
14 import unittest
15 import re
16
17 class PlanetLabMultiIntegrationTestCase(unittest.TestCase):
18     testbed_id = "planetlab"
19     testbed_version = "01"
20     
21     slicename1 = "inria_nepi"
22     plchost1 = "nepiplc.pl.sophia.inria.fr"
23
24     slicename2 = "inria_nepi12"
25     plchost2 = "www.planet-lab.eu"
26     
27     host1pl1 = "nepi1.pl.sophia.inria.fr"
28     host2pl1 = "nepi2.pl.sophia.inria.fr"
29
30     host1pl2 = "onelab11.pl.sophia.inria.fr"
31     host2pl2 = "onelab10.pl.sophia.inria.fr"
32
33     def setUp(self):
34         self.root_dir = tempfile.mkdtemp()
35
36     def tearDown(self):
37         try:
38             shutil.rmtree(self.root_dir)
39         except:
40             # retry
41             time.sleep(0.1)
42             shutil.rmtree(self.root_dir)
43
44     def make_experiment_desc(self):
45         testbed_id = self.testbed_id
46         testbed_version = self.testbed_version
47         
48         slicename1 = self.slicename1
49         plchost1 = self.plchost1
50         
51         slicename2 = self.slicename2
52         plchost2 = self.plchost2
53         
54         pl_ssh_key = os.environ.get(
55             "PL_SSH_KEY",
56             "%s/.ssh/id_rsa_planetlab" % (os.environ['HOME'],) )
57         pl_user, pl_pwd = test_util.pl_auth()
58
59         exp_desc = ExperimentDescription()
60         pl_provider = FactoriesProvider(testbed_id, testbed_version)
61         pl_desc = exp_desc.add_testbed_description(pl_provider)
62         pl_desc.set_attribute_value("homeDirectory", self.root_dir)
63         pl_desc.set_attribute_value("slice", slicename1)
64         pl_desc.set_attribute_value("sliceSSHKey", pl_ssh_key)
65         pl_desc.set_attribute_value("authUser", pl_user)
66         pl_desc.set_attribute_value("authPass", pl_pwd)
67         pl_desc.set_attribute_value("plcHost", plchost1)
68
69         pl_desc2 = exp_desc.add_testbed_description(pl_provider)
70         pl_desc2.set_attribute_value("homeDirectory", self.root_dir+"v2")
71         pl_desc2.set_attribute_value("slice", slicename2)
72         pl_desc2.set_attribute_value("sliceSSHKey", pl_ssh_key)
73         pl_desc2.set_attribute_value("authUser", pl_user)
74         pl_desc2.set_attribute_value("authPass", pl_pwd)
75         pl_desc2.set_attribute_value("plcHost", plchost2)
76         
77         return pl_desc, pl_desc2, exp_desc
78     
79     def make_pl_tapnode(self, pl, tapip, hostname, label_prefix):
80         node1 = pl.create("Node")
81         node1.set_attribute_value("hostname", hostname)
82         node1.set_attribute_value("label", label_prefix)
83         node1.set_attribute_value("emulation", True) # require emulation
84         iface1 = pl.create("NodeInterface")
85         iface1.set_attribute_value("label", label_prefix+"iface")
86         tap1 = pl.create("TapInterface")
87         tap1.enable_trace("packets") # for error output
88         tap1.set_attribute_value("label", label_prefix+"tap")
89         inet = pl.create("Internet")
90         node1.connector("devs").connect(iface1.connector("node"))
91         node1.connector("devs").connect(tap1.connector("node"))
92         iface1.connector("inet").connect(inet.connector("devs"))
93         
94         tap1ip = tap1.add_address()
95         tap1ip.set_attribute_value("Address", tapip)
96         tap1ip.set_attribute_value("NetPrefix", 24)
97         tap1ip.set_attribute_value("Broadcast", False)
98         
99         return node1, iface1, tap1, tap1ip, inet
100     
101     def _test_plpl_crossconnect(self, proto):
102         pl, pl2, exp = self.make_experiment_desc()
103         
104         # Create PL node, ifaces, assign addresses
105         node1, iface1, tap1, tap1ip, inet1 = self.make_pl_tapnode(pl, 
106             "192.168.2.2", self.host1pl1, "node1")
107         node2, iface2, tap2, tap2ip, inet2 = self.make_pl_tapnode(pl2, 
108             "192.168.2.3", self.host1pl2, "node2")
109             
110         # Connect the two
111         tap1.connector(proto).connect(tap2.connector(proto))
112         
113         # Create PlanetLab ping application, pinging the from one PL to another
114         ping = pl.create("Application")
115         ping.set_attribute_value("command", "ping -qc10 {#[node2tap].addr[0].[Address]#}")
116         ping.enable_trace("stdout")
117         ping.enable_trace("stderr")
118         ping.connector("node").connect(node1.connector("apps"))
119
120         comp_result = r"""PING .* \(.*\) \d*\(\d*\) bytes of data.
121
122 --- .* ping statistics ---
123 10 packets transmitted, 10 received, 0% packet loss, time \d*ms.*
124 """
125
126         xml = exp.to_xml()
127
128         controller = ExperimentController(xml, self.root_dir)
129         controller.start()
130
131         while not controller.is_finished(ping.guid):
132             time.sleep(0.5)
133           
134         ping_result = controller.trace(pl.guid, ping.guid, "stdout")
135         tap_trace = controller.trace(pl.guid, tap1.guid, "packets")
136         tap2_trace = controller.trace(pl2.guid, tap2.guid, "packets")
137
138         controller.stop()
139         controller.shutdown()
140
141         # asserts at the end, to make sure there's proper cleanup
142         self.assertTrue(re.match(comp_result, ping_result, re.MULTILINE),
143             "Unexpected trace:\n%s\nTap trace at origin:\n%s\nTap trace at destination:\n%s\n" % (
144                 ping_result,
145                 tap_trace,
146                 tap2_trace) )
147
148     @test_util.skipUnless(test_util.pl_auth() is not None, 
149         "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
150     @test_util.skipUnless(os.environ.get('NEPI_FULL_TESTS','').lower() in ('1','yes','true','on'),
151         "Test is expensive, requires NEPI_FULL_TESTS=yes")
152     def test_plpl_crossconnect_udp(self):
153         self._test_plpl_crossconnect("udp")
154
155     @test_util.skipUnless(test_util.pl_auth() is not None, 
156         "Test requires PlanetLab authentication info (PL_USER and PL_PASS environment variables)")
157     @test_util.skipUnless(os.environ.get('NEPI_FULL_TESTS','').lower() in ('1','yes','true','on'),
158         "Test is expensive, requires NEPI_FULL_TESTS=yes")
159     def test_plpl_crossconnect_tcp(self):
160         self._test_plpl_crossconnect("tcp")
161
162
163 if __name__ == '__main__':
164     unittest.main()
165