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
35 class DummyEmulation(object):
36 def __init__(self, socket_name):
37 self.socket_name = socket_name
38 self.node = dict({'hostname': 'localhost'})
41 def remote_socket(self):
42 return self.socket_name
44 class LinuxNetNSClientTest(unittest.TestCase):
46 self.socket_name = os.path.join("/", "tmp", "NetNSWrapperServer.sock")
47 if os.path.exists(self.socket_name):
48 os.remove(self.socket_name)
51 os.remove(self.socket_name)
53 @skipIf(os.getuid() != 0, "Test requires root privileges")
54 def test_run_ping_routing(self):
55 thread = threading.Thread(target = run_server,
56 args = [self.socket_name], kwargs={"level":logging.DEBUG})
58 thread.setDaemon(True)
63 # Verify that the communication socket was created
64 self.assertTrue(os.path.exists(self.socket_name))
66 # Create a dummy simulation object
67 emulation = DummyEmulation(self.socket_name)
69 # Instantiate the NS3 client
70 client = LinuxNetNSClient(emulation)
76 n1 = client.create("Node")
77 n2 = client.create("Node")
78 n3 = client.create("Node")
80 ### add interfaces to nodes
85 i1 = client.invoke(n1, "add_if")
86 i2a = client.invoke(n2, "add_if")
87 i2b = client.invoke(n2, "add_if")
88 i3 = client.invoke(n3, "add_if")
91 # i1.up = i2a.up = i2b.up = i3.up = True
92 client.set(i1, "up", True)
93 client.set(i2a, "up", True)
94 client.set(i2b, "up", True)
95 client.set(i3, "up", True)
100 l1 = client.create("Switch")
101 l2 = client.create("Switch")
103 ### connect interfaces to switches
108 client.invoke(l1, "connect", i1)
109 client.invoke(l1, "connect", i2a)
110 client.invoke(l2, "connect", i2b)
111 client.invoke(l2, "connect", i3)
114 # l1.up = l2.up = True
115 client.set(l1, "up", True)
116 client.set(l2, "up", True)
118 ## add ip addresses to interfaces
119 #i1.add_v4_address('10.0.0.1', 24)
120 #i2a.add_v4_address('10.0.0.2', 24)
121 #i2b.add_v4_address('10.0.1.1', 24)
122 #i3.add_v4_address('10.0.1.2', 24)
123 client.invoke(i1, "add_v4_address", "10.0.0.1", 24)
124 client.invoke(i2a, "add_v4_address", "10.0.0.2", 24)
125 client.invoke(i2b, "add_v4_address", "10.0.1.1", 24)
126 client.invoke(i3, "add_v4_address", "10.0.1.2", 24)
128 ## add routes to nodes
129 #n1.add_route(prefix = '10.0.1.0', prefix_len = 24,
130 # nexthop = '10.0.0.2')
131 #n3.add_route(prefix = '10.0.0.0', prefix_len = 24,
132 # nexthop = '10.0.1.1')
133 client.invoke(n1, "add_route", prefix="10.0.1.0", prefix_len=24,
135 client.invoke(n3, "add_route", prefix="10.0.0.0", prefix_len=24,
139 #a1 = n1.Popen(['ping', '-qc1', '10.0.1.2'], stdout = null)
140 #a2 = n3.Popen(['ping', '-qc1', '10.0.0.1'], stdout = null)
141 path1 = "/tmp/netns_file1"
142 path2 = "/tmp/netns_file2"
143 file1 = client.create("open", path1, "w")
144 file2 = client.create("open", path2, "w")
145 a1 = client.invoke(n1, "Popen", ["ping", "-qc1", "10.0.1.2"], stdout=file1)
146 a2 = client.invoke(n3, "Popen", ["ping", "-qc1", "10.0.0.1"], stdout=file2)
151 while p1 is None or p2 is None:
152 p1 = client.invoke(a1, "poll")
153 p2 = client.invoke(a2, "poll")
155 stdout1 = open(path1, "r")
156 stdout2 = open(path2, "r")
163 expected = "1 packets transmitted, 1 received, 0% packet loss"
164 self.assertTrue(s1.find(expected) > -1)
165 self.assertTrue(s2.find(expected) > -1)
167 # wait until emulation is over
170 if __name__ == '__main__':