Debian Codegraph @

networking_l2gw/tests/unit/db/test_l2gw_db.py

raw
1 # Copyright 2015 OpenStack Foundation
2 # Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 from unittest import mock
17
18 from neutron.tests.unit import testlib_api
19 from neutron_lib.callbacks import events
20 from neutron_lib.callbacks import resources
21 from neutron_lib import context
22 from neutron_lib.db import api as db_api
23
24 from networking_l2gw.db.l2gateway import l2gateway_db
25 from networking_l2gw.services.l2gateway.common import constants
26 from networking_l2gw.services.l2gateway.common import l2gw_validators
27 from networking_l2gw.services.l2gateway import exceptions
28
29 from neutron_lib import exceptions as exc
30 from neutron_lib.plugins import directory
31 from oslo_utils import importutils
32 from oslo_utils import uuidutils
33
34 DB_PLUGIN_KLASS = 'neutron.db.db_base_plugin_v2.NeutronDbPluginV2'
35 _uuid = uuidutils.generate_uuid
36
37
38 class L2GWTestCase(testlib_api.SqlTestCase):
39
40 """Unit test for l2 Gateway DB support."""
41
42 def setUp(self):
43 super().setUp()
44 self.ctx = context.get_admin_context()
45 self.mixin = l2gateway_db.L2GatewayMixin()
46 self.gw_resource = constants.L2_GATEWAYS
47 self.con_resource = constants.CONNECTION_RESOURCE_NAME
48 self.plugin = importutils.import_object(DB_PLUGIN_KLASS)
49
50 def _create_l2gateway(self, l2gateway):
51 """Create l2gateway helper method."""
52 with db_api.CONTEXT_WRITER.using(self.ctx):
53 return self.mixin.create_l2_gateway(self.ctx, l2gateway)
54
55 def _get_l2_gateway_data(self, name, device_name):
56 """Get l2 gateway data helper method."""
57 data = {"l2_gateway": {"name": name,
58 "devices":
59 [{"interfaces": [{"name": "port1",
60 "segmentation_id": ["111"]}],
61 "device_name": device_name}]}}
62 return data
63
64 def _get_l2_gateway_data_with_multiple_segid(self, name, device_name):
65 """Get l2 gateway data helper method for multiple seg id."""
66 data = {"l2_gateway": {"name": name,
67 "devices":
68 [{"interfaces": [{"name": "port1",
69 "segmentation_id": ["111",
70 "123"]}],
71 "device_name": device_name}]}}
72 return data
73
74 def _get_l2_gateway_multiple_interface_data(self, name, device_name):
75 """Get l2 gateway data helper method with multiple interface data."""
76 data = {"l2_gateway": {"name": name,
77 "devices":
78 [{"interfaces": [{"name": "port1",
79 "segmentation_id": ["4076"]},
80 {"name": "port1",
81 "segmentation_id": ["4074"]}],
82 "device_name": device_name}]}}
83 return data
84
85 def _get_l2_gw_multiple_interface_partial_seg_id_data(self, name,
86 device_name):
87 """Get l2 gateway data helper method with partial seg id."""
88 data = {"l2_gateway": {"name": name,
89 "devices":
90 [{"interfaces": [{"name": "port1",
91 "segmentation_id": ["4076"]},
92 {"name": "port1"}],
93 "device_name": device_name}]}}
94 return data
95
96 def _get_l2_gw_multiple_interface_without_seg_id_data(self, name,
97 device_name):
98 """Get l2 gateway data helper method with partial seg id."""
99 return {"l2_gateway": {"name": name,
100 "devices":
101 [{"interfaces": [{"name": "port1"},
102 {"name": "port2"}],
103 "device_name": device_name}]}}
104
105 def _get_l2_gw_invalid_seg_id_data(self, name,
106 device_name):
107 """Get l2 gateway data helper method with invalid seg id."""
108 data = {"interfaces": [{"name": "port1",
109 "segmentation_id": ["test"]}],
110 "device_name": device_name}
111 return [data]
112
113 def _get_nw_data(self):
114 return {'network': {'id': _uuid(),
115 'name': 'net1',
116 'admin_state_up': True,
117 'tenant_id': 'test-tenant',
118 'shared': False}}
119
120 def _get_l2_gateway_data_without_seg_id(self, name, device_name):
121 """Get l2 gateway data helper method."""
122 data = {"l2_gateway": {"name": name,
123 "devices":
124 [{"interfaces": [{"name": "port1"}],
125 "device_name": device_name}]}}
126 return data
127
128 def test_l2_gateway_get(self):
129 """Test l2 gateway get."""
130 name = "l2gw_1"
131 device_name = "device1"
132 data = self._get_l2_gateway_data(name, device_name)
133 result = self._create_l2gateway(data)
134 get_result = self._get_l2_gateway(result['id'])
135 self.assertEqual(name, get_result['name'])
136
137 def test_l2_gateway_get_invalid_id_failure(self):
138 """Test l2 gateway get for an invalid L2 gateway UUID."""
139 # Generate a random UUID and try to retrieve a L2 gateway
140 # using that UUID.
141 self.assertRaises(exceptions.L2GatewayNotFound,
142 self._get_l2_gateway, _uuid())
143
144 def test_l2_gateway_create(self):
145 """Test l2 gateway create."""
146 name = "l2gw_1"
147 device_name = "device1"
148 data = self._get_l2_gateway_data(name, device_name)
149 result = self._create_l2gateway(data)
150 self.assertEqual(result['name'], name)
151
152 def _get_l2_gateway(self, l2gw_id):
153 with db_api.CONTEXT_READER.using(self.ctx):
154 return self.mixin.get_l2_gateway(self.ctx, l2gw_id)
155
156 def _get_l2_gateways(self):
157 """Update l2gateway helper."""
158 with db_api.CONTEXT_READER.using(self.ctx):
159 return self.mixin.get_l2_gateways(self.ctx)
160
161 def test_l2gateway_list(self):
162 """Test l2 gateway list."""
163 name = "l2gw_1"
164 device_name = "device1"
165 data = self._get_l2_gateway_data(name, device_name)
166 self._create_l2gateway(data)
167 result2 = self._get_l2_gateways()
168 self.assertIn('id', result2[0])
169
170 def test_l2gateway_show(self):
171 """Test l2 gateway show."""
172 name = "l2gw_1"
173 device_name = "device1"
174 data = self._get_l2_gateway_data(name, device_name)
175 gw = self._create_l2gateway(data)
176 l2gw_id = gw['id']
177 result = self._get_l2_gateway(l2gw_id)
178 self.assertEqual(name, result['name'])
179
180 def _update_l2_gateway(self, id, l2gateway):
181 """Update l2gateway helper."""
182 with db_api.CONTEXT_WRITER.using(self.ctx):
183 return self.mixin.update_l2_gateway(self.ctx, id, l2gateway)
184
185 def test_l2_gateway_update(self):
186 """Test l2 gateway update."""
187 name_create = "l2gw_1"
188 name_update = "l2gw_2"
189 device_name = "device1"
190 data_l2gw_create = self._get_l2_gateway_data(name_create,
191 device_name)
192 gw_org = self._create_l2gateway(data_l2gw_create)
193 l2gw_id = gw_org['id']
194 l2_gw_update_dict = self._get_l2_gateway_data(name_update,
195 device_name)
196 result = self._update_l2_gateway(l2gw_id, l2_gw_update_dict)
197 self.assertNotEqual(result['name'], name_create)
198
199 def test_l2_gateway_update_without_devices(self):
200 """Test l2 gateway update without devices."""
201 name_create = "l2gw_1"
202 name_update = "l2gw_updated"
203 device_name = "device1"
204 data_l2gw_create = self._get_l2_gateway_data(name_create,
205 device_name)
206 gw_org = self._create_l2gateway(data_l2gw_create)
207 l2gw_id = gw_org['id']
208 l2_gw_update_dict = {"l2_gateway": {"name": name_update}}
209 result = self._update_l2_gateway(l2gw_id, l2_gw_update_dict)
210 self.assertNotEqual(result['name'], name_create)
211 self.assertEqual(result['name'], name_update)
212
213 def _create_l2gateway_connection(self, l2gateway_con):
214 """Create L2 gateway connection resource helper method."""
215 with db_api.CONTEXT_WRITER.using(self.ctx):
216 return self.mixin.create_l2_gateway_connection(self.ctx,
217 l2gateway_con)
218
219 def _list_l2gateway_connection(self):
220 """Create L2 gateway connection resource helper method."""
221 with db_api.CONTEXT_READER.using(self.ctx):
222 return self.mixin.get_l2_gateway_connections(self.ctx)
223
224 def _get_no_l2gateway_connections(self):
225 """Create L2 gateway connection resource helper method."""
226 with db_api.CONTEXT_READER.using(self.ctx):
227 return self.mixin.get_l2_gateway_connections_count(self.ctx)
228
229 def _delete_l2gw_connection_by_l2gw_id(self, l2gw_id):
230 """Delete l2 gateway connection."""
231 with db_api.CONTEXT_WRITER.using(self.ctx):
232 return self.mixin._delete_connection_by_l2gw_id(self.ctx, l2gw_id)
233
234 def test_get_l2gw_ids_by_interface_switch(self):
235 """Test get L2 gateway ids by interface and switch name."""
236 name = "l2gw_con1"
237 device_name = "device1"
238 data_l2gw = self._get_l2_gateway_data(name, device_name)
239 gw = self._create_l2gateway(data_l2gw)
240 net_data = self._get_nw_data()
241 net = self.plugin.create_network(self.ctx, net_data)
242 l2gw_id = gw['id']
243 data_con = {self.con_resource: {'l2_gateway_id': l2gw_id,
244 'network_id': net['id']}}
245
246 self._create_l2gateway_connection(data_con)
247 l2gw_id_list = self.mixin._get_l2gw_ids_by_interface_switch(
248 self.ctx, 'port1', 'device1')
249 self.assertEqual(l2gw_id_list[0], l2gw_id)
250
251 def test_l2gateway_connection_create_delete_list(self):
252 """Test l2 gateway connection create and delete."""
253 name = "l2gw_con1"
254 device_name = "device_name1"
255 data_l2gw = self._get_l2_gateway_data(name, device_name)
256 gw = self._create_l2gateway(data_l2gw)
257 net_data = self._get_nw_data()
258 net = self.plugin.create_network(self.ctx, net_data)
259 l2gw_id = gw['id']
260 data_con = {self.con_resource: {'l2_gateway_id': l2gw_id,
261 'network_id': net['id']}}
262 gw_con = self._create_l2gateway_connection(data_con)
263 exp_net_id = gw_con['network_id']
264 self.assertEqual(net['id'], exp_net_id)
265 list_con = self._list_l2gateway_connection()
266 self.assertEqual(1, self._get_no_l2gateway_connections())
267 self.assertIn('id', list_con[0])
268 result = self._delete_l2gw_connection_by_l2gw_id(l2gw_id)
269 self.assertIsNone(result)
270
271 def _validate_l2_gateway_for_delete(self, l2gw_id):
272 """Delete l2 gateway helper method."""
273 with db_api.CONTEXT_READER.using(self.ctx):
274 return self.mixin.validate_l2_gateway_for_delete(self.ctx, l2gw_id)
275
276 def _validate_l2_gateway_for_create(self, l2gw):
277 """Create l2 gateway helper method."""
278 with db_api.CONTEXT_READER.using(self.ctx):
279 return self.mixin.validate_l2_gateway_for_create(self.ctx, l2gw)
280
281 def _validate_l2_gateway_for_update(self, l2gw_id, l2gw):
282 """Update l2 gateway helper method."""
283 with db_api.CONTEXT_READER.using(self.ctx):
284 return self.mixin.validate_l2_gateway_for_update(self.ctx,
285 l2gw_id, l2gw)
286
287 def _validate_l2_gateway_connection_for_create(self, l2gw_con):
288 """Create l2 gateway connection helper method."""
289 with db_api.CONTEXT_READER.using(self.ctx):
290 return self.mixin.validate_l2_gateway_connection_for_create(
291 self.ctx, l2gw_con)
292
293 def test_l2gateway_con_create_and_delete_in_use_without_seg_id(self):
294 """Test l2 gateway connection create without seg id when use."""
295 name = "l2gw_con2"
296 device_name = "device_name2"
297 data_l2gw = self._get_l2_gateway_data(name,
298 device_name)
299 gw = self._create_l2gateway(data_l2gw)
300 net_data = self._get_nw_data()
301 net = self.plugin.create_network(self.ctx, net_data)
302 l2gw_id = gw['id']
303 data_con = {self.con_resource: {'l2_gateway_id': l2gw_id,
304 'network_id': net['id']}}
305 self._create_l2gateway_connection(data_con)
306 self.assertRaises(exceptions.L2GatewayInUse,
307 self._validate_l2_gateway_for_delete, l2gw_id)
308
309 def test_l2gateway_con_create_with_invalid_net_id(self):
310 """Test l2 gateway connection create with invalid net id."""
311 name = "l2gw_con2"
312 device_name = "device_name2"
313 data_l2gw = self._get_l2_gateway_data(name,
314 device_name)
315 gw = self._create_l2gateway(data_l2gw)
316 net_id = 'invalid_net_id'
317 l2gw_id = gw['id']
318 data_con = {self.con_resource: {'l2_gateway_id': l2gw_id,
319 'network_id': net_id}}
320 directory.add_plugin('CORE', self.plugin)
321 self.assertRaises(exc.NetworkNotFound,
322 self._validate_l2_gateway_connection_for_create,
323 data_con)
324
325 def _delete_l2gateway(self, l2gw_id):
326 """Delete l2 gateway helper method."""
327 with db_api.CONTEXT_WRITER.using(self.ctx):
328 return self.mixin.delete_l2_gateway(self.ctx,
329 l2gw_id)
330
331 def test_l2gateway_delete(self):
332 """Test l2 gateway delete."""
333 data_l2gw = self._get_l2_gateway_data("gateway_delete",
334 "device_name")
335 gw_actual = self._create_l2gateway(data_l2gw)
336 l2gw_id = gw_actual['id']
337 result = self._delete_l2gateway(l2gw_id)
338 self.assertIsNone(result)
339
340 def _delete_l2gw_connection(self, con_id):
341 """Delete l2 gateway connection."""
342 with db_api.CONTEXT_WRITER.using(self.ctx):
343 return self.mixin.delete_l2_gateway_connection(self.ctx, con_id)
344
345 def test_l2_gateway_create_with_mul_interfaces(self):
346 """Test l2 gateway create with multiple interfaces all seg id."""
347 name = "l2gw_1"
348 device_name = "device1"
349 data = self._get_l2_gateway_multiple_interface_data(name, device_name)
350 result = self._create_l2gateway(data)
351 self.assertEqual(result['name'], name)
352
353 def test_l2_gateway_create_with_mul_interfaces_inconsistent_seg_id(self):
354 """Test l2 gateway create with multiple interfaces."""
355 name = "l2gw_1"
356 dev_name = "device1"
357 data = self._get_l2_gw_multiple_interface_partial_seg_id_data(name,
358 dev_name)
359 self.assertRaises(exceptions.L2GatewaySegmentationRequired,
360 self._validate_l2_gateway_for_create, data)
361
362 def test_l2_gateway_update_with_mul_interfaces_inconsistent_seg_id(self):
363 """Test l2 gateway update with multiple interfaces."""
364 name = "l2gw_1"
365 dev_name = "device1"
366 data_orig = self._get_l2_gateway_multiple_interface_data(name,
367 dev_name)
368 data = self._get_l2_gw_multiple_interface_partial_seg_id_data(name,
369 dev_name)
370 gw_org = self._create_l2gateway(data_orig)
371 l2gw_id = gw_org['id']
372 self.assertRaises(exceptions.L2GatewaySegmentationRequired,
373 self._validate_l2_gateway_for_update, l2gw_id, data)
374
375 def test_l2_gateway_update_with_mul_interfaces_without_seg_id(self):
376 """Test l2 gateway update with multiple interfaces without seg_id."""
377 name = "l2gw_1"
378 dev_name = "device1"
379 data1 = self._get_l2_gateway_multiple_interface_data(
380 name, dev_name)
381 data2 = self._get_l2_gw_multiple_interface_without_seg_id_data(
382 name, dev_name)
383 gw_org1 = self._create_l2gateway(data1)
384 l2gw_id1 = gw_org1['id']
385 gw_org2 = self._create_l2gateway(data2)
386 l2gw_id2 = gw_org2['id']
387 self.assertRaises(exceptions.L2GatewaySegmentationIDExists,
388 self._validate_l2_gateway_for_update,
389 l2gw_id1, data2)
390 self.assertRaises(exceptions.L2GatewaySegmentationIDNotExists,
391 self._validate_l2_gateway_for_update,
392 l2gw_id2, data1)
393
394 def test_l2_gateway_create_with_invalid_seg_id(self):
395 """Test l2 gateway create with invalid seg-id."""
396 name = "l2gw_1"
397 dev_name = "device1"
398 data = self._get_l2_gw_invalid_seg_id_data(name, dev_name)
399 self.assertRaises(exc.InvalidInput,
400 l2gw_validators.validate_gwdevice_list, data)
401
402 def test_l2_gateway_create_with_multiple_segid(self):
403 """Test l2 gateway create with multiple seg id."""
404 name = "l2gw_1"
405 device_name = "device1"
406 data = self._get_l2_gateway_data_with_multiple_segid(name, device_name)
407 result = self._create_l2gateway(data)
408 self.assertEqual(result['name'], name)
409
410 def test_l2_gateway_update_invalid_device_name(self):
411 """Test l2 gateway update with invalid device name."""
412 name_create = "l2gw_1"
413 device_name = "device1"
414 invalid_device_name = "invalid_device"
415 data_l2gw_create = self._get_l2_gateway_data(name_create,
416 device_name)
417 data_l2gw_update = self._get_l2_gateway_data(name_create,
418 invalid_device_name)
419 gw_org = self._create_l2gateway(data_l2gw_create)
420 l2gw_id = gw_org['id']
421 self.assertRaises(exceptions.L2GatewayDeviceNotFound,
422 self._validate_l2_gateway_for_update, l2gw_id,
423 data_l2gw_update)
424
425 def test_l2gw_callback_update_port(self):
426 service_plugin = mock.Mock()
427 directory.add_plugin(constants.L2GW, service_plugin)
428 fake_context = mock.Mock()
429 fake_port = mock.Mock()
430 # fake_kwargs = {'context': fake_context,
431 # 'port': fake_port}
432 fake_pl = events.DBEventPayload(
433 context=fake_context,
434 states=[fake_port])
435 l2gateway_db.l2gw_callback(resources.PORT,
436 events.AFTER_UPDATE,
437 mock.Mock(),
438 fake_pl)
439 self.assertTrue(service_plugin.add_port_mac.called)
440
441 def test_l2gw_callback_delete_port(self):
442 service_plugin = mock.Mock()
443 directory.add_plugin(constants.L2GW, service_plugin)
444 fake_context = mock.Mock()
445 fake_port = mock.Mock()
446
447 fake_pl = events.DBEventPayload(
448 context=fake_context,
449 states=[fake_port])
450 l2gateway_db.l2gw_callback(resources.PORT,
451 events.AFTER_DELETE,
452 mock.Mock(),
453 fake_pl)
454 self.assertTrue(service_plugin.delete_port_mac.called)
455
456 def test_l2_gateway_create_output_aligned_with_input(self):
457 """Test l2 gateway create output that is aligned with input dict."""
458 name = "l2gw_1"
459 device_name = "device1"
460 data = self._get_l2_gateway_data_with_multiple_segid(name, device_name)
461 result = self._create_l2gateway(data)
462 gw_input = data['l2_gateway']
463 devices_input = gw_input['devices']
464 devices_output = result['devices']
465 input_seg_list = devices_input[0]['interfaces'][0]['segmentation_id']
466 output_seg_list = devices_output[0]['interfaces'][0]['segmentation_id']
467 self.assertEqual(len(input_seg_list), len(output_seg_list))
468
469 def test_l2gateway_show_update_delete_invalid_id(self):
470 """Test l2 gateway show, update and delete with invalid id."""
471 name = "l2gw_1"
472 name_update = "l2gw_2"
473 device_name = "device1"
474 invalid_l2gw_id = "invalid_id"
475 data_l2gw_create = self._get_l2_gateway_data(name, device_name)
476 data_l2gw_update = self._get_l2_gateway_data(name_update, device_name)
477 self._create_l2gateway(data_l2gw_create)
478 self.assertRaises(exceptions.L2GatewayNotFound,
479 self._get_l2_gateway, invalid_l2gw_id)
480 self.assertRaises(exceptions.L2GatewayNotFound,
481 self._validate_l2_gateway_for_update,
482 invalid_l2gw_id, data_l2gw_update)
483 self.assertRaises(exceptions.L2GatewayNotFound,
484 self._validate_l2_gateway_for_delete,
485 invalid_l2gw_id)