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