Serious refactoring of TUN/TAP and tunnel code. Linux udp/gre tunnels not yet functional
[nepi.git] / test / resources / linux / gretunnel.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 ## TODO: VALIDATE THIS TEST!
30
31 class LinuxGRETunnelTestCase(unittest.TestCase):
32     def setUp(self):
33         self.host = "roseval.pl.sophia.inria.fr"
34         self.user = "inria_nepi"
35         self.identity = "%s/.ssh/id_rsa" % (os.environ['HOME'])
36         self.netblock = "192.168.1"
37
38     @skipIfAnyNotAliveWithIdentity
39     def t_tap_gre_tunnel(self, user, host, identity):
40         ec = ExperimentController(exp_id = "test-tap-gre-tunnel")
41         
42         node1 = ec.register_resource("linux::Node")
43         ec.set(node1, "hostname", "localhost")
44         ec.set(node1, "cleanExperiment", True)
45         ec.set(node1, "cleanProcesses", True)
46
47         tap1 = ec.register_resource("linux::Tap")
48         ec.set(tap1, "ip", "%s.1" % self.netblock)
49         ec.set(tap1, "prefix", "32")
50         ec.register_connection(tap1, node1)
51
52         node2 = ec.register_resource("linux::Node")
53         ec.set(node2, "hostname", host)
54         ec.set(node2, "username", user)
55         ec.set(node2, "identity", identity)
56         ec.set(node2, "cleanExperiment", True)
57         ec.set(node2, "cleanProcesses", True)
58
59         tap2 = ec.register_resource("linux::Tap")
60         ec.set(tap2, "ip", "%s.2" % self.netblock)
61         ec.set(tap2, "prefix", "32")
62         ec.register_connection(tap2, node2)
63
64         gretun = ec.register_resource("linux::GRETunnel")
65         ec.register_connection(tap1, gretun)
66         ec.register_connection(tap2, gretun)
67
68         app = ec.register_resource("linux::Application")
69         cmd = "ping -c3 %s.2" % self.netblock
70         ec.set(app, "command", cmd)
71         ec.register_connection(app, node1)
72
73         ec.deploy()
74
75         ec.wait_finished(app)
76
77         ping = ec.trace(app, 'stdout')
78         expected = """3 packets transmitted, 3 received, 0% packet loss"""
79         self.assertTrue(ping.find(expected) > -1)
80         
81         if_name = ec.get(tap1, "deviceName")
82         self.assertTrue(if_name.startswith("tap"))
83         
84         if_name = ec.get(tap2, "deviceName")
85         self.assertTrue(if_name.startswith("tap"))
86
87         ec.shutdown()
88
89     @skipIfAnyNotAliveWithIdentity
90     def t_tun_gre_tunnel(self, user, host, identity):
91         ec = ExperimentController(exp_id = "test-tun-gre-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", "%s.1" % self.netblock)
100         ec.set(tun1, "prefix", "32")
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", "%s.2" % self.netblock)
112         ec.set(tun2, "prefix", "32")
113         ec.register_connection(tun2, node2)
114
115         udptun = ec.register_resource("linux::GRETunnel")
116         ec.register_connection(tun1, udptun)
117         ec.register_connection(tun2, udptun)
118
119         app = ec.register_resource("linux::Application")
120         cmd = "ping -c3 %s.2" % self.netblock
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         expected = """3 packets transmitted, 3 received, 0% packet loss"""
130         self.assertTrue(ping.find(expected) > -1)
131         
132         if_name = ec.get(tun1, "deviceName")
133         self.assertTrue(if_name.startswith("tun"))
134         
135         if_name = ec.get(tun2, "deviceName")
136         self.assertTrue(if_name.startswith("tun"))
137
138         ec.shutdown()
139
140     def test_tap_gre_tunnel(self):
141         self.t_tap_gre_tunnel(self.user, self.host, self.identity)
142
143     def ztest_tun_gre_tunnel(self):
144         self.t_tun_gre_tunnel(self.user, self.host, self.identity)
145
146 if __name__ == '__main__':
147     unittest.main()
148