Bug fixing Netns emulation
[nepi.git] / test / resources / linux / netns / netnsemulation.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 from nepi.execution.trace import TraceAttr
23
24 from test_utils import skipIfNotAlive, skipIf
25
26 import os
27 import time
28 import unittest
29
30 def add_node(ec, emu, addr, prefix):
31     node = ec.register_resource("netns::Node")
32     ec.register_connection(node, emu)
33     
34     iface = ec.register_resource("netns::NodeInterface")
35     ec.register_connection(iface, node)
36
37     ip = ec.register_resource("netns::IPv4Address")
38     ec.set(ip, "ip", addr)
39     ec.set(ip, "prefix", prefix)
40     ec.register_connection(ip, iface)
41
42     print ec.get(ip, "ip"), addr
43     print ec.get(ip, "prefix"), prefix
44
45     return node, iface
46
47 class LinuxNetNSEmulationTest(unittest.TestCase):
48     def setUp(self):
49         self.fedora_host = "mimas.inria.fr"
50         self.fedora_user = "aquereil"
51         self.fedora_identity = "%s/.ssh/id_rsa" % (os.environ['HOME'])
52
53     @skipIfNotAlive
54     @skipIf(os.getuid() != 0, "Test requires root privileges")
55     def t_ping(self, host, user = None, identity = None):
56         ec = ExperimentController(exp_id = "test-netns-p2p-ping")
57         
58         node = ec.register_resource("linux::Node")
59         if host == "localhost":
60             ec.set(node, "hostname", "localhost")
61         else:
62             ec.set(node, "hostname", host)
63             ec.set(node, "username", user)
64             ec.set(node, "identity", identity)
65         
66         ec.set(node, "cleanProcesses", True)
67         #ec.set(node, "cleanHome", True)
68
69         emu = ec.register_resource("linux::netns::Emulation")
70         ec.set(emu, "verbose", True)
71         ec.register_connection(emu, node)
72
73         netnode1, iface1 = add_node(ec, emu, "10.0.0.1", "24")
74         netnode2, iface2 = add_node(ec, emu, "10.0.0.2", "24")
75          
76         switch = ec.register_resource("netns::Switch")
77         ec.register_connection(iface1, switch)
78         ec.register_connection(iface2, switch)
79  
80         ping = ec.register_resource("netns::Application")
81         ec.set(ping, "command", "ping -c20 10.0.0.2")
82         ec.register_connection(ping, netnode1)
83
84         ec.deploy()
85
86         ec.wait_finished([ping])
87         
88         stdout = ec.trace(ping, "stdout") 
89
90         expected = "20 packets transmitted, 20 received, 0% packet loss"
91         self.assertTrue(stdout.find(expected) > -1)
92
93         ec.shutdown()
94
95     def ztest_ping_fedora(self):
96         self.t_ping(self.fedora_host, self.fedora_user, self.fedora_identity)
97
98     def test_ping_local(self):
99         self.t_ping("localhost")
100
101
102 if __name__ == '__main__':
103     unittest.main()
104