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