cc580f9c608b05f024331aaacfc3ca66eb6d16ee
[nepi.git] / test / resources / netns / netnswrapper.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
22 # Test based on netns test/test_core.py file test_run_ping_routing test
23 #
24
25 from nepi.resources.netns.netnswrapper import NetNSWrapper
26
27 from test_utils import skipIf
28
29 import os
30 import subprocess
31 import sys
32 import time
33 import unittest
34
35 class NetNSWrapperTest(unittest.TestCase):
36     def setUp(self):
37         pass
38
39     @skipIf(os.getuid() != 0, "Test requires root privileges")
40     def test_run_ping_routing(self):
41         wrapper = NetNSWrapper()
42
43         ### create 3 nodes
44         #n1 = netns.Node()
45         #n2 = netns.Node()
46         #n3 = netns.Node()
47         n1 = wrapper.create("Node")
48         n2 = wrapper.create("Node")
49         n3 = wrapper.create("Node")
50
51         ### add interfaces to nodes
52         #i1 = n1.add_if()
53         #i2a = n2.add_if()
54         #i2b = n2.add_if()
55         #i3 = n3.add_if()
56         i1 = wrapper.invoke(n1, "add_if")
57         i2a = wrapper.invoke(n2, "add_if")
58         i2b = wrapper.invoke(n2, "add_if")
59         i3 = wrapper.invoke(n3, "add_if")
60
61         ### set interfaces up
62         # i1.up = i2a.up = i2b.up = i3.up = True
63         wrapper.set(i1, "up", True)
64         wrapper.set(i2a, "up", True)
65         wrapper.set(i2b, "up", True)
66         wrapper.set(i3, "up", True)
67
68         ### create 2 switches
69         #l1 = netns.Switch()
70         #l2 = netns.Switch()
71         l1 = wrapper.create("Switch")
72         l2 = wrapper.create("Switch")
73
74         ### connect interfaces to switches
75         #l1.connect(i1)
76         #l1.connect(i2a)
77         #l2.connect(i2b)
78         #l2.connect(i3)
79         wrapper.invoke(l1, "connect", i1)
80         wrapper.invoke(l1, "connect", i2a)
81         wrapper.invoke(l2, "connect", i2b)
82         wrapper.invoke(l2, "connect", i3)
83
84         ### set switched up
85         # l1.up = l2.up = True
86         wrapper.set(l1, "up", True)
87         wrapper.set(l2, "up", True)
88
89         ## add ip addresses to interfaces
90         #i1.add_v4_address('10.0.0.1', 24)
91         #i2a.add_v4_address('10.0.0.2', 24)
92         #i2b.add_v4_address('10.0.1.1', 24)
93         #i3.add_v4_address('10.0.1.2', 24)
94         wrapper.invoke(i1, "add_v4_address", "10.0.0.1", 24)
95         wrapper.invoke(i2a, "add_v4_address", "10.0.0.2", 24)
96         wrapper.invoke(i2b, "add_v4_address", "10.0.1.1", 24)
97         wrapper.invoke(i3, "add_v4_address", "10.0.1.2", 24)
98
99         ## add routes to nodes
100         #n1.add_route(prefix = '10.0.1.0', prefix_len = 24,
101         #        nexthop = '10.0.0.2')
102         #n3.add_route(prefix = '10.0.0.0', prefix_len = 24,
103         #        nexthop = '10.0.1.1')
104         wrapper.invoke(n1, "add_route", prefix = "10.0.1.0", prefix_len = 24,
105                 nexthop = "10.0.0.2")
106         wrapper.invoke(n3, "add_route", prefix = "10.0.0.0", prefix_len = 24,
107                 nexthop = "10.0.1.1")
108
109         ## launch pings
110         #a1 = n1.Popen(['ping', '-qc1', '10.0.1.2'], stdout = null)
111         #a2 = n3.Popen(['ping', '-qc1', '10.0.0.1'], stdout = null)
112         path1 = "/tmp/netns_file1"
113         path2 = "/tmp/netns_file2"
114         file1 = wrapper.create("open", path1, "w")
115         file2 = wrapper.create("open", path2, "w")
116         a1 = wrapper.invoke(n1, "Popen", ["ping", "-qc1", "10.0.1.2"], stdout = file1)
117         a2 = wrapper.invoke(n3, "Popen", ["ping", "-qc1", "10.0.0.1"], stdout = file2)
118
119         ## get ping status
120         p1 = None
121         p2 = None
122         while p1 is None or p2 is None:
123             p1 = wrapper.invoke(a1, "poll")
124             p2 = wrapper.invoke(a2, "poll")
125
126         stdout1 = open(path1, "r")
127         stdout2 = open(path2, "r")
128
129         s1 = stdout1.read()
130         s2 = stdout2.read()
131
132         expected = "1 packets transmitted, 1 received, 0% packet loss"
133         self.assertTrue(s1.find(expected) > -1)
134         self.assertTrue(s2.find(expected) > -1)
135         
136         wrapper.shutdown()
137
138 if __name__ == '__main__':
139     unittest.main()
140