da576fd3ba6011ed5741362ee5d3b6e1abce28fe
[nepi.git] / test / resources / linux / netns / netnsclient.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 # Test based on netns test/test_core.py file test_run_ping_routing test
21 #
22
23 from nepi.resources.netns.netnsserver import run_server
24 from nepi.resources.linux.netns.netnsclient import LinuxNetNSClient
25
26 from test_utils import skipIf
27
28 import logging
29 import os
30 import threading
31 import time
32 import unittest
33
34 class DummyEmulation(object):
35     def __init__(self, socket_name):
36         self.socket_name = socket_name
37         self.node = dict({'hostname': 'localhost'})
38
39     @property
40     def remote_socket(self):
41         return self.socket_name
42
43 class LinuxNetNSClientTest(unittest.TestCase):
44     def setUp(self):
45         self.socket_name = os.path.join("/", "tmp", "NetNSWrapperServer.sock")
46         if os.path.exists(self.socket_name):
47             os.remove(self.socket_name) 
48
49     def tearDown(self):
50         os.remove(self.socket_name) 
51
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], kwargs={"level":logging.DEBUG})
56
57         thread.setDaemon(True)
58         thread.start()
59
60         time.sleep(3)
61
62         # Verify that the communication socket was created
63         self.assertTrue(os.path.exists(self.socket_name))
64
65         # Create a dummy simulation object
66         emulation = DummyEmulation(self.socket_name) 
67
68         # Instantiate the NS3 client
69         client = LinuxNetNSClient(emulation)
70
71         ### create 3 nodes
72         #n1 = netns.Node()
73         #n2 = netns.Node()
74         #n3 = netns.Node()
75         n1 = client.create("Node")
76         n2 = client.create("Node")
77         n3 = client.create("Node")
78
79         ### add interfaces to nodes
80         #i1 = n1.add_if()
81         #i2a = n2.add_if()
82         #i2b = n2.add_if()
83         #i3 = n3.add_if()
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")
88
89         ### set interfaces up
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)
95
96         ### create 2 switches
97         #l1 = netns.Switch()
98         #l2 = netns.Switch()
99         l1 = client.create("Switch")
100         l2 = client.create("Switch")
101
102         ### connect interfaces to switches
103         #l1.connect(i1)
104         #l1.connect(i2a)
105         #l2.connect(i2b)
106         #l2.connect(i3)
107         client.invoke(l1, "connect", i1)
108         client.invoke(l1, "connect", i2a)
109         client.invoke(l2, "connect", i2b)
110         client.invoke(l2, "connect", i3)
111
112         ### set switched up
113         # l1.up = l2.up = True
114         client.set(l1, "up", True)
115         client.set(l2, "up", True)
116
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)
126
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,
133                 nexthop="10.0.0.2")
134         client.invoke(n3, "add_route", prefix="10.0.0.0", prefix_len=24,
135                 nexthop="10.0.1.1")
136
137         ## launch pings
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)
146
147         ## get ping status
148         p1 = None
149         p2 = None
150         while p1 is None or p2 is None:
151             p1 = client.invoke(a1, "poll")
152             p2 = client.invoke(a2, "poll")
153
154         stdout1 = open(path1, "r")
155         stdout2 = open(path2, "r")
156
157         s1 = stdout1.read()
158         s2 = stdout2.read()
159
160         print s1, s2
161
162         expected = "1 packets transmitted, 1 received, 0% packet loss"
163         self.assertTrue(s1.find(expected) > -1)
164         self.assertTrue(s2.find(expected) > -1)
165
166         # wait until emulation is over
167         client.shutdown()
168
169 if __name__ == '__main__':
170     unittest.main()
171