all the python scripts are for python2, and fedora31 requires to be specific
[vsys-scripts.git] / slice-context / test / test_vsys.py
1 #!/usr/bin/env python
2
3 #
4 # Copyright (c) 2012 INRIA
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the Free
8 # Software Foundation; either version 2 of the License, or (at your option)
9 # any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14 # more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # this program; if not, write to the Free Software Foundation, Inc., 51
18 # Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 #
20 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
21 #
22
23 import subprocess
24 import unittest
25 import time
26
27 from vsys import fd_tuntap, vif_up, vif_down, vroute, IFF_TAP, IFF_TUN
28 import _vsys
29
30 class TestVsys(unittest.TestCase):
31     def setUp(self):
32         self._network = "192.168.2.0"
33         self._prefix  = 30
34         self._ip = "192.168.2.2"
35         self._remote_net = "192.168.2.4"
36
37     def _create_vif(self, if_type, no_pi = False):
38         ####### create virtual device 
39         (fd, if_name) = fd_tuntap(if_type, no_pi)
40         self.assertTrue(fd > 0)
41         
42         ###### configure virtual device
43         vif_up(if_name, self._ip, self._prefix)
44
45         # wait for prcocess to see the new configuration...
46         time.sleep(5)
47
48         ###### test ip responds to pings
49         p = subprocess.Popen(["ping", "-qc3", self._ip], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
50         out, err = p.communicate()
51         
52         self.assertFalse(err)
53
54         expected = """PING %(ip)s (%(ip)s) 56(84) bytes of data.
55
56 --- %(ip)s ping statistics ---
57 3 packets transmitted, 3 received, 0%% packet loss""" % {'ip': self._ip}
58         
59         self.assertTrue(out.startswith(expected), out)
60
61         ###### add route
62         vroute ("add", self._remote_net, self._prefix, self._ip, if_name)
63
64         # wait for prcocess to see the new configuration...
65         time.sleep(5)
66
67         ###### test routes
68         p = subprocess.Popen(["ip", "r"], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
69         out, err = p.communicate()
70         
71         self.assertFalse(err)
72         self.assertTrue(out.find(self._remote_net) >= 0 )
73
74         ###### del route
75         vroute ("del", self._remote_net, self._prefix, self._ip, if_name)
76
77         # wait for prcocess to see the new configuration...
78         time.sleep(5)
79
80         ##### test routes
81         p = subprocess.Popen(["ip", "r"], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
82         out, err = p.communicate()
83         
84         self.assertFalse(err)
85         self.assertTrue(out.find(self._remote_net) < 0 )
86
87         ##### delete interface
88         vif_down(if_name)
89
90         # wait for prcocess to see the new configuration...
91         time.sleep(5)
92
93         ###### test ip NOT responds to pings
94         p = subprocess.Popen(["ping", "-qc3", self._ip], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
95         out, err = p.communicate()
96         
97         self.assertFalse(err)
98
99         expected = """PING %(ip)s (%(ip)s) 56(84) bytes of data.
100
101 --- %(ip)s ping statistics ---
102 3 packets transmitted, 0 received, 100%% packet loss""" % {'ip': self._ip}
103         
104         self.assertTrue(out.startswith(expected), out)
105
106
107     def test_create_tun(self):
108         self._create_vif(IFF_TUN)
109
110     def test_create_tap(self):
111         self._create_vif(IFF_TAP)
112
113     def test_create_tun_no_pi(self):
114         self._create_vif(IFF_TUN, no_pi = True)
115
116     def test_create_tap_no_pi(self):
117         self._create_vif(IFF_TAP, no_pi = True)
118  
119
120
121 if __name__ == '__main__':
122     unittest.main()
123