Serious refactoring of TUN/TAP and tunnel code. Linux udp/gre tunnels not yet functional
[nepi.git] / test / resources / linux / 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 LinuxUdpTunnelTestCase(unittest.TestCase):
30     def setUp(self):
31         self.host = "roseval.pl.sophia.inria.fr"
32         self.user = "inria_nepi"
33         self.identity = "%s/.ssh/id_rsa" % (os.environ['HOME'])
34
35     @skipIfAnyNotAliveWithIdentity
36     def t_tap_udp_tunnel(self, user, host, identity):
37
38         ec = ExperimentController(exp_id="test-tap-udp-tunnel")
39         
40         node1 = ec.register_resource("linux::Node")
41         ec.set(node1, "hostname", "localhost")
42         ec.set(node1, "cleanExperiment", True)
43         ec.set(node1, "cleanProcesses", True)
44
45         tap1 = ec.register_resource("linux::Tap")
46         ec.set(tap1, "ip", "192.168.3.1")
47         ec.set(tap1, "prefix", "30")
48         ec.register_connection(tap1, node1)
49
50         node2 = ec.register_resource("linux::Node")
51         ec.set(node2, "hostname", host)
52         ec.set(node2, "username", user)
53         ec.set(node2, "identity", identity)
54         ec.set(node2, "cleanExperiment", True)
55         ec.set(node2, "cleanProcesses", True)
56
57         tap2 = ec.register_resource("linux::Tap")
58         ec.set(tap2, "ip", "192.168.3.2")
59         ec.set(tap2, "prefix", "30")
60         ec.register_connection(tap2, node2)
61
62         udptun = ec.register_resource("linux::UdpTunnel")
63         ec.register_connection(tap1, udptun)
64         ec.register_connection(tap2, udptun)
65
66         app = ec.register_resource("linux::Application")
67         cmd = "ping -c3 192.168.3.2"
68         ec.set(app, "command", cmd)
69         ec.register_connection(app, node1)
70
71         ec.deploy()
72
73         ec.wait_finished(app)
74
75         ping = ec.trace(app, "stdout")
76         print ping
77         expected = """3 packets transmitted, 3 received, 0% packet loss"""
78         self.assertTrue(ping.find(expected) > -1)
79         
80         vif_name = ec.get(tap1, "deviceName")
81         self.assertTrue(vif_name.startswith("tap"))
82         
83         vif_name = ec.get(tap2, "deviceName")
84         self.assertTrue(vif_name.startswith("tap"))
85
86         ec.shutdown()
87
88     @skipIfAnyNotAliveWithIdentity
89     def t_tun_udp_tunnel(self, user, host, identity):
90
91         ec = ExperimentController(exp_id="test-tun-udp-tunnel")
92         
93         node1 = ec.register_resource("linux::Node")
94         ec.set(node1, "hostname", "localhost")
95         ec.set(node1, "cleanExperiment", True)
96         ec.set(node1, "cleanProcesses", True)
97
98         tun1 = ec.register_resource("linux::Tun")
99         ec.set(tun1, "ip", "192.168.3.1")
100         ec.set(tun1, "prefix", "30")
101         ec.register_connection(tun1, node1)
102
103         node2 = ec.register_resource("linux::Node")
104         ec.set(node2, "hostname", host)
105         ec.set(node2, "username", user)
106         ec.set(node2, "identity", identity)
107         ec.set(node2, "cleanExperiment", True)
108         ec.set(node2, "cleanProcesses", True)
109
110         tun2 = ec.register_resource("linux::Tun")
111         ec.set(tun2, "ip", "192.168.3.2")
112         ec.set(tun2, "prefix", "30")
113         ec.register_connection(tun2, node2)
114
115         udptun = ec.register_resource("linux::UdpTunnel")
116         ec.register_connection(tun1, udptun)
117         ec.register_connection(tun2, udptun)
118
119         app = ec.register_resource("linux::Application")
120         cmd = "ping -c3 192.168.3.2"
121         ec.set(app, "command", cmd)
122         ec.register_connection(app, node1)
123
124         ec.deploy()
125
126         ec.wait_finished(app)
127
128         ping = ec.trace(app, "stdout")
129
130         print ping
131         expected = """3 packets transmitted, 3 received, 0% packet loss"""
132         self.assertTrue(ping.find(expected) > -1)
133         
134         vif_name = ec.get(tun1, "deviceName")
135         self.assertTrue(vif_name.startswith("tun"))
136         
137         vif_name = ec.get(tun2, "deviceName")
138         self.assertTrue(vif_name.startswith("tun"))
139
140         ec.shutdown()
141
142     def test_tap_udp_tunnel(self):
143         self.t_tap_udp_tunnel(self.user, self.host, self.identity)
144
145     def ztest_tun_udp_tunnel(self):
146         self.t_tun_udp_tunnel(self.user, self.host, self.identity)
147
148 if __name__ == '__main__':
149     unittest.main()
150