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 as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
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.
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/>.
19 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
21 # Test based on netns test/test_core.py file test_run_ping_routing test
24 from nepi.resources.netns.netnsserver import run_server
25 from nepi.resources.linux.netns.netnsclient import LinuxNetNSClient
27 from test_utils import skipIf
34 class DummyEmulation(object):
35 def __init__(self, socket_name):
36 self.socket_name = socket_name
37 self.node = dict({'hostname': 'localhost'})
40 def remote_socket(self):
41 return self.socket_name
43 class LinuxNetNSClientTest(unittest.TestCase):
45 self.socket_name = os.path.join("/", "tmp", "NetNSWrapperServer.sock")
46 if os.path.exists(self.socket_name):
47 os.remove(self.socket_name)
50 os.remove(self.socket_name)
52 @skipIf(os.getuid() != 0, "Test requires root privileges")
53 def test_run_ping_routing(self):
54 thread = threading.Thread(target = run_server,
55 args = [self.socket_name])
57 thread.setDaemon(True)
62 # Verify that the communication socket was created
63 self.assertTrue(os.path.exists(self.socket_name))
65 # Create a dummy simulation object
66 emulation = DummyEmulation(self.socket_name)
68 # Instantiate the NS3 client
69 client = LinuxNetNSClient(emulation)
75 n1 = client.create("Node")
76 n2 = client.create("Node")
77 n3 = client.create("Node")
79 ### add interfaces to nodes
84 i1 = client.invoke(n1, "add_if")
85 i2a = client.invoke(n2, "add_if")
86 i2b = client.invoke(n2, "add_if")
87 i3 = client.invoke(n3, "add_if")
90 # i1.up = i2a.up = i2b.up = i3.up = True
91 client.set(i1, "up", True)
92 client.set(i2a, "up", True)
93 client.set(i2b, "up", True)
94 client.set(i3, "up", True)
99 l1 = client.create("Switch")
100 l2 = client.create("Switch")
102 ### connect interfaces to switches
107 client.invoke(l1, "connect", i1)
108 client.invoke(l1, "connect", i2a)
109 client.invoke(l2, "connect", i2b)
110 client.invoke(l2, "connect", i3)
113 # l1.up = l2.up = True
114 client.set(l1, "up", True)
115 client.set(l2, "up", True)
117 ## add ip addresses to interfaces
118 #i1.add_v4_address('10.0.0.1', 24)
119 #i2a.add_v4_address('10.0.0.2', 24)
120 #i2b.add_v4_address('10.0.1.1', 24)
121 #i3.add_v4_address('10.0.1.2', 24)
122 client.invoke(i1, "add_v4_address", "10.0.0.1", 24)
123 client.invoke(i2a, "add_v4_address", "10.0.0.2", 24)
124 client.invoke(i2b, "add_v4_address", "10.0.1.1", 24)
125 client.invoke(i3, "add_v4_address", "10.0.1.2", 24)
127 ## add routes to nodes
128 #n1.add_route(prefix = '10.0.1.0', prefix_len = 24,
129 # nexthop = '10.0.0.2')
130 #n3.add_route(prefix = '10.0.0.0', prefix_len = 24,
131 # nexthop = '10.0.1.1')
132 client.invoke(n1, "add_route", prefix="10.0.1.0", prefix_len=24,
134 client.invoke(n3, "add_route", prefix "10.0.0.0", prefix_len=24,
138 #a1 = n1.Popen(['ping', '-qc1', '10.0.1.2'], stdout = null)
139 #a2 = n3.Popen(['ping', '-qc1', '10.0.0.1'], stdout = null)
140 path1 = "/tmp/netns_file1"
141 path2 = "/tmp/netns_file2"
142 file1 = client.create("open", path1, "w")
143 file2 = client.create("open", path2, "w")
144 a1 = client.invoke(n1, "Popen", ["ping", "-qc1", "10.0.1.2"], stdout=file1)
145 a2 = client.invoke(n3, "Popen", ["ping", "-qc1", "10.0.0.1"], stdout=file2)
150 while p1 is None or p2 is None:
151 p1 = client.invoke(a1, "poll")
152 p2 = client.invoke(a2, "poll")
154 stdout1 = open(path1, "r")
155 stdout2 = open(path2, "r")
162 expected = "1 packets transmitted, 1 received, 0% packet loss"
163 self.assertTrue(s1.find(expected) > -1)
164 self.assertTrue(s2.find(expected) > -1)
166 # wait until emulation is over
169 if __name__ == '__main__':