Fixing wrong license
[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 version 2 as
8 #    published by the Free Software Foundation;
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.execution.ec import ExperimentController 
21
22 from test_utils import skipIfAnyNotAliveWithIdentity
23
24 import os
25 import time
26 import unittest
27
28 ## TODO: VALIDATE THIS TEST!
29
30 class LinuxGRETunnelTestCase(unittest.TestCase):
31     def setUp(self):
32         self.host = "roseval.pl.sophia.inria.fr"
33         self.user = "inria_nepi"
34         self.identity = "%s/.ssh/id_rsa" % (os.environ['HOME'])
35         self.netblock = "192.168.1"
36
37     @skipIfAnyNotAliveWithIdentity
38     def t_tap_gre_tunnel(self, user, host, identity):
39         ec = ExperimentController(exp_id = "test-tap-gre-tunnel")
40         
41         node1 = ec.register_resource("linux::Node")
42         ec.set(node1, "hostname", "localhost")
43         ec.set(node1, "cleanExperiment", True)
44         ec.set(node1, "cleanProcesses", True)
45
46         tap1 = ec.register_resource("linux::Tap")
47         ec.set(tap1, "ip", "%s.1" % self.netblock)
48         ec.set(tap1, "prefix", "32")
49         ec.register_connection(tap1, node1)
50
51         node2 = ec.register_resource("linux::Node")
52         ec.set(node2, "hostname", host)
53         ec.set(node2, "username", user)
54         ec.set(node2, "identity", identity)
55         ec.set(node2, "cleanExperiment", True)
56         ec.set(node2, "cleanProcesses", True)
57
58         tap2 = ec.register_resource("linux::Tap")
59         ec.set(tap2, "ip", "%s.2" % self.netblock)
60         ec.set(tap2, "prefix", "32")
61         ec.register_connection(tap2, node2)
62
63         gretun = ec.register_resource("linux::GRETunnel")
64         ec.register_connection(tap1, gretun)
65         ec.register_connection(tap2, gretun)
66
67         app = ec.register_resource("linux::Application")
68         cmd = "ping -c3 %s.2" % self.netblock
69         ec.set(app, "command", cmd)
70         ec.register_connection(app, node1)
71
72         ec.deploy()
73
74         ec.wait_finished(app)
75
76         ping = ec.trace(app, 'stdout')
77         expected = """3 packets transmitted, 3 received, 0% packet loss"""
78         self.assertTrue(ping.find(expected) > -1)
79         
80         if_name = ec.get(tap1, "deviceName")
81         self.assertTrue(if_name.startswith("tap"))
82         
83         if_name = ec.get(tap2, "deviceName")
84         self.assertTrue(if_name.startswith("tap"))
85
86         ec.shutdown()
87
88     @skipIfAnyNotAliveWithIdentity
89     def t_tun_gre_tunnel(self, user, host, identity):
90         ec = ExperimentController(exp_id = "test-tun-gre-tunnel")
91         
92         node1 = ec.register_resource("linux::Node")
93         ec.set(node1, "hostname", "localhost")
94         ec.set(node1, "cleanExperiment", True)
95         ec.set(node1, "cleanProcesses", True)
96
97         tun1 = ec.register_resource("linux::Tun")
98         ec.set(tun1, "ip", "%s.1" % self.netblock)
99         ec.set(tun1, "prefix", "32")
100         ec.register_connection(tun1, node1)
101
102         node2 = ec.register_resource("linux::Node")
103         ec.set(node2, "hostname", host)
104         ec.set(node2, "username", user)
105         ec.set(node2, "identity", identity)
106         ec.set(node2, "cleanExperiment", True)
107         ec.set(node2, "cleanProcesses", True)
108
109         tun2 = ec.register_resource("linux::Tun")
110         ec.set(tun2, "ip", "%s.2" % self.netblock)
111         ec.set(tun2, "prefix", "32")
112         ec.register_connection(tun2, node2)
113
114         udptun = ec.register_resource("linux::GRETunnel")
115         ec.register_connection(tun1, udptun)
116         ec.register_connection(tun2, udptun)
117
118         app = ec.register_resource("linux::Application")
119         cmd = "ping -c3 %s.2" % self.netblock
120         ec.set(app, "command", cmd)
121         ec.register_connection(app, node1)
122
123         ec.deploy()
124
125         ec.wait_finished(app)
126
127         ping = ec.trace(app, 'stdout')
128         expected = """3 packets transmitted, 3 received, 0% packet loss"""
129         self.assertTrue(ping.find(expected) > -1)
130         
131         if_name = ec.get(tun1, "deviceName")
132         self.assertTrue(if_name.startswith("tun"))
133         
134         if_name = ec.get(tun2, "deviceName")
135         self.assertTrue(if_name.startswith("tun"))
136
137         ec.shutdown()
138
139     def test_tap_gre_tunnel(self):
140         self.t_tap_gre_tunnel(self.user, self.host, self.identity)
141
142     def ztest_tun_gre_tunnel(self):
143         self.t_tun_gre_tunnel(self.user, self.host, self.identity)
144
145 if __name__ == '__main__':
146     unittest.main()
147