d6d0b449188d490f03c03928867441f0a221f7f5
[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
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     def t_ping(self, host, user = None, identity = None):
55         ec = ExperimentController(exp_id = "test-netns-p2p-ping")
56         
57         node = ec.register_resource("LinuxNode")
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("LinuxNetNSEmulation")
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", "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