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