Debian Codegraph @

os_vif/tests/unit/internal/ip/linux/test_impl_pyroute2.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from unittest import mock
14
15 from pyroute2 import iproute
16 from pyroute2.netlink import exceptions as ipexc
17 from pyroute2.netlink.rtnl import ifinfmsg
18
19 from os_vif import exception
20 from os_vif.internal.ip.linux import impl_pyroute2
21 from os_vif.tests.unit import base
22
23
24 class TestIpCommand(base.TestCase):
25
26 ERROR_CODE = 40
27 OTHER_ERROR_CODE = 50
28 DEVICE = 'device'
29 MTU = 1500
30 MAC = 'ca:fe:ca:fe:ca:fe'
31 UP = 'up'
32 TYPE_VETH = 'veth'
33 TYPE_VLAN = 'vlan'
34 TYPE_BRIDGE = 'bridge'
35 LINK = 'device2'
36 VLAN_ID = 14
37
38 def setUp(self):
39 super(TestIpCommand, self).setUp()
40 self.ip = impl_pyroute2.PyRoute2()
41 self.ip_link_p = mock.patch.object(iproute.IPRoute, 'link',
42 create=True)
43 self.ip_link = self.ip_link_p.start()
44
45 def test_set(self):
46 with mock.patch.object(
47 iproute.IPRoute, 'link_lookup', return_value=[1],
48 create=True) as mock_link_lookup:
49 self.ip_link.return_value = [{'flags': 0x4000}]
50 self.ip.set(self.DEVICE, state=self.UP, mtu=self.MTU,
51 address=self.MAC, promisc=True)
52 mock_link_lookup.assert_called_once_with(ifname=self.DEVICE)
53 args = {'state': self.UP,
54 'mtu': self.MTU,
55 'address': self.MAC,
56 'flags': 0x4000 | ifinfmsg.IFF_PROMISC}
57 calls = [mock.call('get', index=1),
58 mock.call('set', index=1, **args)]
59 self.ip_link.assert_has_calls(calls)
60
61 def test_set_exit_code(self):
62 with mock.patch.object(
63 iproute.IPRoute, 'link_lookup', return_value=[1],
64 create=True) as mock_link_lookup:
65 self.ip_link.side_effect = ipexc.NetlinkError(self.ERROR_CODE,
66 msg="Error message")
67
68 self.ip.set(self.DEVICE, check_exit_code=[self.ERROR_CODE])
69 mock_link_lookup.assert_called_once_with(ifname=self.DEVICE)
70 self.ip_link.assert_called_once_with('set', index=1)
71
72 self.assertRaises(ipexc.NetlinkError, self.ip.set, self.DEVICE,
73 check_exit_code=[self.OTHER_ERROR_CODE])
74
75 def test_set_no_interface_found(self):
76 with mock.patch.object(
77 iproute.IPRoute, 'link_lookup', return_value=[],
78 create=True) as mock_link_lookup:
79 self.assertRaises(exception.NetworkInterfaceNotFound, self.ip.set,
80 self.DEVICE)
81 mock_link_lookup.assert_called_once_with(ifname=self.DEVICE)
82 self.ip_link.assert_not_called()
83
84 def test_add_veth(self):
85 self.ip.add(self.DEVICE, self.TYPE_VETH, peer='peer')
86 self.ip_link.assert_called_once_with(
87 'add', ifname=self.DEVICE, kind=self.TYPE_VETH, peer='peer')
88
89 def test_add_vlan(self):
90 with mock.patch.object(
91 iproute.IPRoute, 'link_lookup', return_value=[1],
92 create=True) as mock_link_lookup:
93 self.ip.add(self.DEVICE, self.TYPE_VLAN, link=self.LINK,
94 vlan_id=self.VLAN_ID)
95 mock_link_lookup.assert_called_once_with(ifname=self.LINK)
96 args = {'ifname': self.DEVICE,
97 'kind': self.TYPE_VLAN,
98 'vlan_id': self.VLAN_ID,
99 'link': 1}
100 self.ip_link.assert_called_once_with('add', **args)
101
102 def test_add_bridge(self):
103 self.ip.add(self.DEVICE, self.TYPE_BRIDGE)
104 args = {'ifname': self.DEVICE,
105 'kind': self.TYPE_BRIDGE,
106 'IFLA_BR_FORWARD_DELAY': 0,
107 'IFLA_BR_STP_STATE': 0,
108 'IFLA_BR_MCAST_SNOOPING': 0}
109 self.ip_link.assert_called_once_with('add', **args)
110
111 def test_add_bridge_with_ageing(self):
112 self.ip.add(self.DEVICE, self.TYPE_BRIDGE, ageing=0)
113 args = {'ifname': self.DEVICE,
114 'kind': self.TYPE_BRIDGE,
115 'IFLA_BR_AGEING_TIME': 0,
116 'IFLA_BR_FORWARD_DELAY': 0,
117 'IFLA_BR_STP_STATE': 0,
118 'IFLA_BR_MCAST_SNOOPING': 0}
119 self.ip_link.assert_called_once_with('add', **args)
120
121 def test_add_vlan_no_interface_found(self):
122 with mock.patch.object(
123 iproute.IPRoute, 'link_lookup', return_value=[],
124 create=True) as mock_link_lookup:
125 self.assertRaises(exception.NetworkInterfaceNotFound, self.ip.add,
126 self.DEVICE, self.TYPE_VLAN, link=self.LINK)
127 mock_link_lookup.assert_called_once_with(ifname=self.LINK)
128 self.ip_link.assert_not_called()
129
130 def test_add_other_type(self):
131 self.assertRaises(exception.NetworkInterfaceTypeNotDefined,
132 self.ip.add, self.DEVICE, 'type_not_defined')
133
134 def test_add_exit_code(self):
135 self.ip_link.side_effect = ipexc.NetlinkError(self.ERROR_CODE,
136 msg="Error message")
137
138 self.ip.add(self.DEVICE, self.TYPE_VETH, peer='peer',
139 check_exit_code=[self.ERROR_CODE])
140 self.ip_link.assert_called_once_with(
141 'add', ifname=self.DEVICE, kind=self.TYPE_VETH, peer='peer')
142
143 self.assertRaises(
144 exception.NetworkInterfaceNotFound,
145 self.ip.add, self.DEVICE,
146 self.TYPE_VLAN, peer='peer',
147 check_exit_code=[self.OTHER_ERROR_CODE])
148
149 def test_delete(self):
150 with mock.patch.object(
151 iproute.IPRoute, 'link_lookup', return_value=[1],
152 create=True) as mock_link_lookup:
153 self.ip.delete(self.DEVICE)
154 mock_link_lookup.assert_called_once_with(ifname=self.DEVICE)
155 self.ip_link.assert_called_once_with('del', index=1)
156
157 def test_delete_no_interface_found(self):
158 with mock.patch.object(
159 iproute.IPRoute, 'link_lookup', return_value=[],
160 create=True) as mock_link_lookup:
161 self.assertRaises(exception.NetworkInterfaceNotFound,
162 self.ip.delete, self.DEVICE)
163 mock_link_lookup.assert_called_once_with(ifname=self.DEVICE)
164
165 def test_delete_exit_code(self):
166 with mock.patch.object(
167 iproute.IPRoute, 'link_lookup', return_value=[1],
168 create=True) as mock_link_lookup:
169 self.ip_link.side_effect = ipexc.NetlinkError(self.ERROR_CODE,
170 msg="Error message")
171
172 self.ip.delete(self.DEVICE, check_exit_code=[self.ERROR_CODE])
173 mock_link_lookup.assert_called_once_with(ifname=self.DEVICE)
174 self.ip_link.assert_called_once_with('del', index=1)
175
176 self.assertRaises(ipexc.NetlinkError, self.ip.delete, self.DEVICE,
177 check_exit_code=[self.OTHER_ERROR_CODE])
178
179 def test_add_tap(self):
180 """Test creating a basic tap device."""
181 self.ip.add(self.DEVICE, 'tuntap', mode='tap')
182 args = {'ifname': self.DEVICE,
183 'kind': 'tuntap',
184 'mode': 'tap'}
185 self.ip_link.assert_called_once_with('add', **args)
186
187 def test_add_tap_default_mode(self):
188 """Test creating tap device with default mode (should be 'tap')."""
189 self.ip.add(self.DEVICE, 'tuntap')
190 args = {'ifname': self.DEVICE,
191 'kind': 'tuntap',
192 'mode': 'tap'}
193 self.ip_link.assert_called_once_with('add', **args)
194
195 def test_add_tap_with_multiqueue(self):
196 """Test creating tap device with multiqueue enabled."""
197 self.ip.add(self.DEVICE, 'tuntap', mode='tap', multiqueue=True)
198
199 # Verify the call includes multiqueue flag
200 args = {'ifname': self.DEVICE,
201 'kind': 'tuntap',
202 'mode': 'tap',
203 'multi_queue': True}
204 self.ip_link.assert_called_once_with('add', **args)
205
206 def test_add_tun_device(self):
207 """Test creating a tun device (not tap)."""
208 self.ip.add(self.DEVICE, 'tuntap', mode='tun')
209 args = {'ifname': self.DEVICE,
210 'kind': 'tuntap',
211 'mode': 'tun'}
212 self.ip_link.assert_called_once_with('add', **args)
213
214 def test_add_tap_exit_code(self):
215 """Test tap device creation with exit code handling."""
216 self.ip_link.side_effect = ipexc.NetlinkError(self.ERROR_CODE,
217 msg="Error message")
218
219 # Should not raise when error code matches
220 self.ip.add(self.DEVICE, 'tuntap', mode='tap',
221 check_exit_code=[self.ERROR_CODE])
222 self.ip_link.assert_called_once()
223
224 # Should raise when exit code doesn't match
225 self.assertRaises(
226 ipexc.NetlinkError,
227 self.ip.add, self.DEVICE, 'tuntap', mode='tap',
228 check_exit_code=[self.OTHER_ERROR_CODE])
229
230 def test_set_no_multiqueue_parameter(self):
231 """Verify set() no longer accepts multiqueue parameter."""
232 with mock.patch.object(
233 iproute.IPRoute, 'link_lookup', return_value=[1],
234 create=True):
235 self.ip_link.return_value = [{'flags': 0}]
236
237 # Should work without multiqueue
238 self.ip.set(self.DEVICE, state=self.UP, mtu=self.MTU,
239 address=self.MAC)
240
241 # Should raise TypeError if multiqueue is passed
242 self.assertRaises(TypeError, self.ip.set, self.DEVICE,
243 multiqueue=True)