Serious refactoring of TUN/TAP and tunnel code. Linux udp/gre tunnels not yet functional
[nepi.git] / test / resources / planetlab / udptunnel.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20
21 from nepi.execution.ec import ExperimentController 
22
23 from test_utils import skipIfAnyNotAliveWithIdentity
24
25 import os
26 import time
27 import unittest
28
29 class PlanetlabUdpTunnelTestCase(unittest.TestCase):
30     def setUp(self):
31         #self.host1 = "nepi2.pl.sophia.inria.fr"
32         #self.host2 = "nepi5.pl.sophia.inria.fr"
33         #self.host2 = "planetlab1.informatik.uni-goettingen.de"
34         self.host1 = "onelab4.warsaw.rd.tp.pl"
35         self.host2 = "inriarennes1.irisa.fr"
36         self.user = "inria_nepi"
37         #self.identity = "%s/.ssh/id_rsa_planetlab" % (os.environ['HOME'])
38         self.identity = "%s/.ssh/id_rsa" % (os.environ['HOME'])
39         #self.netblock = "192.168.1"
40         self.netblock = "192.168.3"
41
42     @skipIfAnyNotAliveWithIdentity
43     def t_tap_udp_tunnel(self, user1, host1, identity1, user2, host2, 
44             identity2):
45
46         ec = ExperimentController(exp_id="test-tap-udp-tunnel")
47         
48         node1 = ec.register_resource("planetlab::Node")
49         ec.set(node1, "hostname", host1)
50         ec.set(node1, "username", user1)
51         ec.set(node1, "identity", identity1)
52         ec.set(node1, "cleanExperiment", True)
53         ec.set(node1, "cleanProcesses", True)
54
55         tap1 = ec.register_resource("planetlab::Tap")
56         ec.set(tap1, "ip", "%s.1" % self.netblock)
57         ec.set(tap1, "prefix", "24")
58         ec.register_connection(tap1, node1)
59
60         node2 = ec.register_resource("planetlab::Node")
61         ec.set(node2, "hostname", host2)
62         ec.set(node2, "username", user2)
63         ec.set(node2, "identity", identity2)
64         ec.set(node2, "cleanExperiment", True)
65         ec.set(node2, "cleanProcesses", True)
66
67         tap2 = ec.register_resource("planetlab::Tap")
68         ec.set(tap2, "ip", "%s.2" % self.netblock)
69         ec.set(tap2, "prefix", "24")
70         ec.register_connection(tap2, node2)
71
72         udptun = ec.register_resource("linux::UdpTunnel")
73         ec.register_connection(tap1, udptun)
74         ec.register_connection(tap2, udptun)
75
76         app = ec.register_resource("linux::Application")
77         cmd = "ping -c3 %s.2" % self.netblock
78         ec.set(app, "command", cmd)
79         ec.register_connection(app, node1)
80
81         ec.deploy()
82
83         ec.wait_finished(app)
84
85         ping = ec.trace(app, 'stdout')
86         expected = """3 packets transmitted, 3 received, 0% packet loss"""
87         self.assertTrue(ping.find(expected) > -1)
88         
89         vif_name = ec.get(tap1, "deviceName")
90         self.assertTrue(vif_name.startswith("tap"))
91         
92         vif_name = ec.get(tap2, "deviceName")
93         self.assertTrue(vif_name.startswith("tap"))
94
95         ec.shutdown()
96
97     @skipIfAnyNotAliveWithIdentity
98     def t_tun_udp_tunnel(self, user1, host1, identity1, user2, host2, identity2):
99
100         ec = ExperimentController(exp_id="test-tun-udp-tunnel")
101         
102         node1 = ec.register_resource("planetlab::Node")
103         ec.set(node1, "hostname", host1)
104         ec.set(node1, "username", user1)
105         ec.set(node1, "identity", identity1)
106         ec.set(node1, "cleanExperiment", True)
107         ec.set(node1, "cleanProcesses", True)
108
109         tun1 = ec.register_resource("planetlab::Tun")
110         ec.set(tun1, "ip", "%s.1" % self.netblock)
111         ec.set(tun1, "prefix", "24")
112         ec.register_connection(tun1, node1)
113
114         node2 = ec.register_resource("planetlab::Node")
115         ec.set(node2, "hostname", host2)
116         ec.set(node2, "username", user2)
117         ec.set(node2, "identity", identity2)
118         ec.set(node2, "cleanExperiment", True)
119         ec.set(node2, "cleanProcesses", True)
120
121         tun2 = ec.register_resource("planetlab::Tun")
122         ec.set(tun2, "ip", "%s.2" % self.netblock)
123         ec.set(tun2, "prefix", "24")
124         ec.register_connection(tun2, node2)
125
126         udptun = ec.register_resource("linux::UdpTunnel")
127         ec.register_connection(tun1, udptun)
128         ec.register_connection(tun2, udptun)
129
130         app = ec.register_resource("linux::Application")
131         cmd = "ping -c3 %s.2" % self.netblock
132         ec.set(app, "command", cmd)
133         ec.register_connection(app, node1)
134
135         ec.deploy()
136
137         ec.wait_finished(app)
138
139         ping = ec.trace(app, 'stdout')
140         expected = """3 packets transmitted, 3 received, 0% packet loss"""
141         self.assertTrue(ping.find(expected) > -1)
142         
143         vif_name = ec.get(tun1, "deviceName")
144         self.assertTrue(vif_name.startswith("tun"))
145         
146         vif_name = ec.get(tun2, "deviceName")
147         self.assertTrue(vif_name.startswith("tun"))
148
149         ec.shutdown()
150
151     def test_tap_udp_tunnel(self):
152         self.t_tap_udp_tunnel(self.user, self.host1, self.identity,
153                 self.user, self.host2, self.identity)
154
155     def test_tun_udp_tunnel(self):
156         self.t_tun_udp_tunnel(self.user, self.host1, self.identity,
157                 self.user, self.host2, self.identity)
158
159 if __name__ == '__main__':
160     unittest.main()
161