3 # NEPI, a framework to manage network experiments
4 # Copyright (C) 2013 INRIA
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;
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 from __future__ import print_function
22 from nepi.execution.ec import ExperimentController
23 from nepi.execution.trace import TraceAttr
25 from test_utils import skipIfNotAlive, skipIf
31 def add_node(ec, emu, addr, prefix):
32 node = ec.register_resource("netns::Node")
33 ec.register_connection(node, emu)
35 iface = ec.register_resource("netns::NodeInterface")
36 ec.register_connection(iface, node)
38 ip = ec.register_resource("netns::IPv4Address")
39 ec.set(ip, "ip", addr)
40 ec.set(ip, "prefix", prefix)
41 ec.register_connection(ip, iface)
43 print(ec.get(ip, "ip"), addr)
44 print(ec.get(ip, "prefix"), prefix)
48 class LinuxNetNSEmulationTest(unittest.TestCase):
50 self.fedora_host = "mimas.inria.fr"
51 self.fedora_user = "aquereil"
52 self.fedora_identity = "%s/.ssh/id_rsa" % (os.environ['HOME'])
55 @skipIf(os.getuid() != 0, "Test requires root privileges")
56 def t_ping(self, host, user = None, identity = None):
57 ec = ExperimentController(exp_id = "test-netns-p2p-ping")
59 node = ec.register_resource("linux::Node")
60 if host == "localhost":
61 ec.set(node, "hostname", "localhost")
63 ec.set(node, "hostname", host)
64 ec.set(node, "username", user)
65 ec.set(node, "identity", identity)
67 ec.set(node, "cleanProcesses", True)
68 #ec.set(node, "cleanHome", True)
70 emu = ec.register_resource("linux::netns::Emulation")
71 ec.set(emu, "verbose", True)
72 ec.register_connection(emu, node)
74 netnode1, iface1 = add_node(ec, emu, "10.0.0.1", "24")
75 netnode2, iface2 = add_node(ec, emu, "10.0.0.2", "24")
77 switch = ec.register_resource("netns::Switch")
78 ec.register_connection(iface1, switch)
79 ec.register_connection(iface2, switch)
81 ping = ec.register_resource("netns::Application")
82 ec.set(ping, "command", "ping -c20 10.0.0.2")
83 ec.register_connection(ping, netnode1)
87 ec.wait_finished([ping])
89 stdout = ec.trace(ping, "stdout")
91 expected = "20 packets transmitted, 20 received, 0% packet loss"
92 self.assertTrue(stdout.find(expected) > -1)
96 def ztest_ping_fedora(self):
97 self.t_ping(self.fedora_host, self.fedora_user, self.fedora_identity)
99 def test_ping_local(self):
100 self.t_ping("localhost")
103 if __name__ == '__main__':