Debian Codegraph @

vif_plug_ovs/tests/functional/ovsdb/test_ovsdb_lib.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 import fixtures
14 import random
15
16
17 from unittest import mock
18
19 import testscenarios
20
21 from oslo_concurrency import processutils
22 from oslo_config import cfg
23 from oslo_utils import uuidutils
24
25 from vif_plug_ovs import constants
26 from vif_plug_ovs import linux_net
27 from vif_plug_ovs import ovs
28 from vif_plug_ovs.ovsdb import ovsdb_lib
29 from vif_plug_ovs import privsep
30 from vif_plug_ovs.tests.functional import base
31
32
33 CONF = cfg.CONF
34
35
36 @privsep.vif_plug_test.entrypoint
37 def run_privileged(*full_args):
38 return processutils.execute(*full_args)[0].rstrip()
39
40
41 class TestOVSDBLib(testscenarios.WithScenarios,
42 base.VifPlugOvsBaseFunctionalTestCase):
43
44 scenarios = [
45 ('native', {'interface': 'native'}),
46 ('vsctl', {'interface': 'vsctl'})
47 ]
48
49 def setUp(self):
50 super(TestOVSDBLib, self).setUp()
51 run_privileged('ovs-vsctl', 'set-manager', 'ptcp:6640')
52
53 # NOTE: (ralonsoh) load default configuration variables "CONFIG_OPTS"
54 ovs.OvsPlugin.load('ovs')
55 self.flags(ovsdb_interface=self.interface, group='os_vif_ovs')
56
57 self.ovs = ovsdb_lib.BaseOVS(CONF.os_vif_ovs)
58 self._ovsdb = self.ovs.ovsdb
59 self.brname = ('br' + str(random.randint(1000, 9999)) + '-' +
60 self.interface)
61
62 # Make sure exceptions pass through by calling do_post_commit directly
63 post_commit = (
64 'ovsdbapp.schema.open_vswitch.impl_idl.'
65 'OvsVsctlTransaction.post_commit'
66 )
67 # "this" is the self parmater which is a reference to the
68 # OvsVsctlTransaction instance on which do_post_commit is defiend.
69
70 def direct_post_commit(this, transaction):
71 this.do_post_commit(transaction)
72
73 self.useFixture(fixtures.MonkeyPatch(post_commit, direct_post_commit))
74
75 def _add_port(self, bridge, port, may_exist=True):
76 with self._ovsdb.transaction() as txn:
77 txn.add(self._ovsdb.add_port(bridge, port, may_exist=may_exist))
78 txn.add(self._ovsdb.db_set('Interface', port,
79 ('type', 'internal')))
80 self.assertIn(port, self._list_ports_in_bridge(bridge))
81
82 def _list_ports_in_bridge(self, bridge):
83 return self._ovsdb.list_ports(bridge).execute()
84
85 def test__set_mtu_request(self):
86 port_name = 'port1-' + self.interface
87 self._add_bridge(self.brname)
88 self.addCleanup(self._del_bridge, self.brname)
89 self._add_port(self.brname, port_name)
90 if self.ovs._ovs_supports_mtu_requests():
91 with self._ovsdb.transaction() as txn:
92 self.ovs._set_mtu_request(txn, port_name, 1000)
93 self._check_parameter('Interface', port_name, 'mtu', 1000)
94 with self._ovsdb.transaction() as txn:
95 self.ovs._set_mtu_request(txn, port_name, 1500)
96 self._check_parameter('Interface', port_name, 'mtu', 1500)
97 else:
98 self.skipTest('Current version of Open vSwitch does not support '
99 '"mtu_request" parameter')
100
101 def test_create_ovs_vif_port(self):
102 port_name = 'port2-' + self.interface
103 iface_id = 'iface_id'
104 mac = 'ca:fe:ca:fe:ca:fe'
105 instance_id = uuidutils.generate_uuid()
106 interface_type = constants.OVS_VHOSTUSER_INTERFACE_TYPE
107 vhost_server_path = '/fake/path'
108 mtu = 1500
109 self._add_bridge(self.brname)
110 self.addCleanup(self._del_bridge, self.brname)
111
112 self.ovs.create_ovs_vif_port(self.brname, port_name, iface_id, mac,
113 instance_id, mtu=mtu,
114 interface_type=interface_type,
115 vhost_server_path=vhost_server_path,
116 tag=2000)
117
118 expected_external_ids = {'iface-status': 'active',
119 'iface-id': iface_id,
120 'attached-mac': mac,
121 'vm-uuid': instance_id}
122 self._check_parameter('Interface', port_name, 'external_ids',
123 expected_external_ids)
124 self._check_parameter('Interface', port_name, 'type', interface_type)
125 expected_vhost_server_path = {'vhost-server-path': vhost_server_path}
126 self._check_parameter(
127 'Interface', port_name, 'options', expected_vhost_server_path
128 )
129 self._check_parameter('Port', port_name, 'tag', 2000)
130 self._check_parameter('Port', port_name, 'qos', [])
131
132 @mock.patch.object(linux_net, 'delete_net_dev')
133 def test_delete_ovs_vif_port(self, *mock):
134 port_name = 'port3-' + self.interface
135 self._add_bridge(self.brname)
136 self.addCleanup(self._del_bridge, self.brname)
137
138 self._add_port(self.brname, port_name)
139 self.ovs.delete_ovs_vif_port(self.brname, port_name)
140 self.assertNotIn(port_name, self._list_ports_in_bridge(self.brname))
141
142 def test_ensure_ovs_bridge(self):
143 bridge_name = 'bridge2-' + self.interface
144 self.ovs.ensure_ovs_bridge(bridge_name, constants.OVS_DATAPATH_SYSTEM)
145 self.assertTrue(self._check_bridge(bridge_name))
146 self.addCleanup(self._del_bridge, bridge_name)
147
148 def test_create_patch_port_pair(self):
149 port_bridge = 'fake-pb'
150 port_bridge_port = 'fake-pbp'
151 int_bridge = 'pb-int'
152 int_bridge_port = 'fake-ibp'
153 iface_id = 'iface_id'
154 mac = 'ca:fe:ca:fe:ca:fe'
155 instance_id = uuidutils.generate_uuid()
156
157 # deleting a bridge deletes all ports on bridges so we register the
158 # bridge cleanup first so if we fail anywhere it runs.
159 self.addCleanup(self._del_bridge, port_bridge)
160 self.addCleanup(self._del_bridge, int_bridge)
161 self.ovs.ensure_ovs_bridge(port_bridge, constants.OVS_DATAPATH_SYSTEM)
162 self.ovs.ensure_ovs_bridge(int_bridge, constants.OVS_DATAPATH_SYSTEM)
163 self.ovs.create_patch_port_pair(
164 port_bridge, port_bridge_port, int_bridge, int_bridge_port,
165 iface_id, mac, instance_id, tag=2000)
166 self.assertTrue(self._check_bridge(port_bridge))
167 self.assertTrue(self._check_bridge(int_bridge))
168
169 expected_external_ids = {'iface-status': 'active',
170 'iface-id': iface_id,
171 'attached-mac': mac,
172 'vm-uuid': instance_id}
173 self._check_parameter(
174 'Interface', int_bridge_port, 'external_ids',
175 expected_external_ids)
176 self._check_parameter('Interface', int_bridge_port, 'type', 'patch')
177 port_opts = {'peer': port_bridge_port}
178 self._check_parameter(
179 'Interface', int_bridge_port, 'options', port_opts)
180 self._check_parameter('Port', int_bridge_port, 'tag', 2000)
181 port_opts = {'peer': int_bridge_port}
182 self._check_parameter(
183 'Interface', port_bridge_port, 'options', port_opts)
184
185 def test_create_ovs_vif_port_with_default_qos(self):
186 port_name = 'def-qos-port-' + self.interface
187 iface_id = 'iface_id'
188 mac = 'ca:fe:ca:fe:ca:fe'
189 instance_id = uuidutils.generate_uuid()
190 mtu = 1500
191 interface_type = 'internal'
192 qos_type = CONF.os_vif_ovs.default_qos_type
193
194 self.addCleanup(self._del_bridge, self.brname)
195 self._add_bridge(self.brname)
196
197 self.addCleanup(
198 self.ovs.delete_ovs_vif_port, self.brname, port_name,
199 delete_netdev=False, qos_type=qos_type
200 )
201 self.ovs.create_ovs_vif_port(
202 self.brname, port_name, iface_id, mac,
203 instance_id, mtu=mtu, interface_type=interface_type,
204 tag=2000, qos_type=qos_type
205 )
206
207 # first we assert that the standard parameters are set correctly
208 expected_external_ids = {'iface-status': 'active',
209 'iface-id': iface_id,
210 'attached-mac': mac,
211 'vm-uuid': instance_id}
212 self._check_parameter('Interface', port_name, 'external_ids',
213 expected_external_ids)
214 self._check_parameter('Interface', port_name, 'type', interface_type)
215 self._check_parameter('Port', port_name, 'tag', 2000)
216
217 # now we check that the port has a qos policy attached
218 qos_uuid = self.ovs.get_qos(
219 port_name, qos_type
220 )[0]['_uuid']
221 self._check_parameter('Port', port_name, 'qos', qos_uuid)
222
223 # finally we check that the qos policy has the correct parameters
224 self._check_parameter(
225 'QoS', str(qos_uuid), 'type', qos_type
226 )
227
228 def test_delete_qos_if_exists(self):
229 port_name = 'del-qos-port-' + self.interface
230 iface_id = 'iface_id'
231 mac = 'ca:fe:ca:fe:ca:fe'
232 instance_id = uuidutils.generate_uuid()
233 interface_type = 'internal'
234 qos_type = CONF.os_vif_ovs.default_qos_type
235
236 # setup test by creating a bridge and port, and register
237 # cleanup funcitons to avoid leaking them.
238 self.addCleanup(self._del_bridge, self.brname)
239 self._add_bridge(self.brname)
240 self.addCleanup(
241 self.ovs.delete_ovs_vif_port, self.brname, port_name,
242 delete_netdev=False, qos_type=qos_type
243 )
244 self.ovs.create_ovs_vif_port(
245 self.brname, port_name, iface_id, mac,
246 instance_id, interface_type=interface_type,
247 qos_type=qos_type
248 )
249
250 # now we check that the port has a qos policy attached
251 qos_uuid = self.ovs.get_qos(
252 port_name, CONF.os_vif_ovs.default_qos_type
253 )[0]['_uuid']
254 self._check_parameter('Port', port_name, 'qos', qos_uuid)
255
256 # finally we check that the qos policy has the correct parameters
257 self._check_parameter(
258 'QoS', str(qos_uuid), 'type', qos_type
259 )
260
261 # we need to delete the port directly in the db to remove
262 # any references to the qos policy
263 self.ovs.ovsdb.del_port(
264 port_name, bridge=self.brname, if_exists=True).execute()
265 # then we can delete the qos policy
266 self.ovs.delete_qos_if_exists(port_name, qos_type)
267 self._check_parameter(
268 'QoS', str(qos_uuid), 'type', None
269 )
270 # invoking the delete when the policy does not exist
271 # should not result in an error
272 self.ovs.delete_qos_if_exists(port_name, qos_type)
273 self._check_parameter(
274 'QoS', str(qos_uuid), 'type', None
275 )
276
277 def test_get_qos(self):
278 port_name = 'get-qos-' + self.interface
279 iface_id = 'iface_id'
280 mac = 'ca:fe:ca:fe:ca:fe'
281 instance_id = uuidutils.generate_uuid()
282 interface_type = 'internal'
283 qos_type = CONF.os_vif_ovs.default_qos_type
284 # initally no qos policy should exist
285 self.assertEqual(0, len(self.ovs.get_qos(port_name, qos_type)))
286
287 # if we create a port with a qos policy get_qos should
288 # return the policy
289 self.addCleanup(self._del_bridge, self.brname)
290 self._add_bridge(self.brname)
291 self.addCleanup(
292 self.ovs.delete_ovs_vif_port, self.brname, port_name,
293 delete_netdev=False, qos_type=qos_type
294 )
295 self.ovs.create_ovs_vif_port(
296 self.brname, port_name, iface_id, mac,
297 instance_id, interface_type=interface_type,
298 qos_type=qos_type
299 )
300 # result should be a list of lenght 1 containing the
301 # qos policy created for the port we defied.
302 result = self.ovs.get_qos(port_name, qos_type)
303 self.assertEqual(1, len(result))
304 self.assertIn('_uuid', result[0])
305 self._check_parameter(
306 'Port', port_name, 'qos', result[0]['_uuid']
307 )
308 # if we delete the port and its qos policy get_qos should
309 # not return it.
310 self.ovs.delete_ovs_vif_port(
311 self.brname, port_name,
312 delete_netdev=False, qos_type=qos_type
313 )
314 self.assertEqual(0, len(self.ovs.get_qos(port_name, qos_type)))
315
316 def test_port_exists(self):
317 port_name = 'port-exists-' + self.interface
318 iface_id = 'iface_id'
319 mac = 'ca:fe:ca:fe:ca:fe'
320 instance_id = uuidutils.generate_uuid()
321 interface_type = 'internal'
322
323 self.assertFalse(self.ovs.port_exists(port_name, self.brname))
324
325 self.addCleanup(self._del_bridge, self.brname)
326 self._add_bridge(self.brname)
327 self.addCleanup(
328 self.ovs.delete_ovs_vif_port, self.brname, port_name,
329 delete_netdev=False,
330 )
331 self.ovs.create_ovs_vif_port(
332 self.brname, port_name, iface_id, mac,
333 instance_id, interface_type=interface_type,
334 )
335
336 self.assertTrue(self.ovs.port_exists(port_name, self.brname))