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