OpenStack Networking (Neutron)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

446 lines
18KB

  1. # Copyright (c) 2014 OpenStack Foundation, all rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. # implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import warnings
  16. import mock
  17. import netaddr
  18. from neutron_lib.api.definitions import portbindings
  19. from neutron_lib import constants
  20. from neutron_lib import context
  21. from neutron_lib.plugins.ml2 import api
  22. from oslo_utils import uuidutils
  23. from sqlalchemy.orm import exc
  24. from sqlalchemy.orm import query
  25. from neutron.db import api as db_api
  26. from neutron.db import db_base_plugin_v2
  27. from neutron.db.models import l3 as l3_models
  28. from neutron.db import models_v2
  29. from neutron.db import segments_db
  30. from neutron.objects import network as network_obj
  31. from neutron.objects import ports as port_obj
  32. from neutron.plugins.ml2 import db as ml2_db
  33. from neutron.plugins.ml2 import models
  34. from neutron.tests.unit import testlib_api
  35. PLUGIN_NAME = 'ml2'
  36. class Ml2DBTestCase(testlib_api.SqlTestCase):
  37. def setUp(self):
  38. super(Ml2DBTestCase, self).setUp()
  39. self.ctx = context.get_admin_context()
  40. self.setup_coreplugin(PLUGIN_NAME)
  41. def _setup_neutron_network(self, network_id):
  42. network_obj.Network(self.ctx, id=network_id).create()
  43. def _setup_neutron_port(self, network_id, port_id):
  44. mac_address = db_base_plugin_v2.NeutronDbPluginV2._generate_mac()
  45. port = port_obj.Port(self.ctx,
  46. id=port_id,
  47. network_id=network_id,
  48. mac_address=netaddr.EUI(mac_address),
  49. admin_state_up=True,
  50. status='DOWN',
  51. device_id='',
  52. device_owner='')
  53. port.create()
  54. return port
  55. def _setup_neutron_portbinding(self, port_id, vif_type, host):
  56. with db_api.context_manager.writer.using(self.ctx):
  57. self.ctx.session.add(models.PortBinding(port_id=port_id,
  58. vif_type=vif_type,
  59. host=host))
  60. @staticmethod
  61. def _sort_segments(segments):
  62. return sorted(segments, key=lambda d: d['segmentation_id'])
  63. def _create_segments(self, segments, is_seg_dynamic=False,
  64. network_id=uuidutils.generate_uuid()):
  65. self._setup_neutron_network(network_id)
  66. for segment in segments:
  67. segments_db.add_network_segment(
  68. self.ctx, network_id, segment,
  69. is_dynamic=is_seg_dynamic)
  70. segment['network_id'] = network_id
  71. net_segments = segments_db.get_network_segments(
  72. self.ctx, network_id,
  73. filter_dynamic=is_seg_dynamic)
  74. net_segments = self._sort_segments(net_segments)
  75. for segment_index, segment in enumerate(segments):
  76. self.assertEqual(segment, net_segments[segment_index])
  77. return net_segments
  78. def test_network_segments_for_provider_network(self):
  79. segment = {api.NETWORK_TYPE: 'vlan',
  80. api.PHYSICAL_NETWORK: 'physnet1',
  81. api.SEGMENTATION_ID: 1}
  82. self._create_segments([segment])
  83. def test_network_segments_is_dynamic_true(self):
  84. segment = {api.NETWORK_TYPE: 'vlan',
  85. api.PHYSICAL_NETWORK: 'physnet1',
  86. api.SEGMENTATION_ID: 1}
  87. self._create_segments([segment], is_seg_dynamic=True)
  88. def test_network_segments_for_multiprovider_network(self):
  89. segments = [{api.NETWORK_TYPE: 'vlan',
  90. api.PHYSICAL_NETWORK: 'physnet1',
  91. api.SEGMENTATION_ID: 1},
  92. {api.NETWORK_TYPE: 'vlan',
  93. api.PHYSICAL_NETWORK: 'physnet1',
  94. api.SEGMENTATION_ID: 2}]
  95. self._create_segments(segments)
  96. def test_get_networks_segments(self):
  97. net_id1 = uuidutils.generate_uuid()
  98. net_id2 = uuidutils.generate_uuid()
  99. segments1 = [{api.NETWORK_TYPE: 'vlan',
  100. api.PHYSICAL_NETWORK: 'physnet1',
  101. api.SEGMENTATION_ID: 1},
  102. {api.NETWORK_TYPE: 'vlan',
  103. api.PHYSICAL_NETWORK: 'physnet1',
  104. api.SEGMENTATION_ID: 2}]
  105. segments2 = [{api.NETWORK_TYPE: 'vlan',
  106. api.PHYSICAL_NETWORK: 'physnet1',
  107. api.SEGMENTATION_ID: 3},
  108. {api.NETWORK_TYPE: 'vlan',
  109. api.PHYSICAL_NETWORK: 'physnet1',
  110. api.SEGMENTATION_ID: 4}]
  111. net1segs = self._create_segments(segments1, network_id=net_id1)
  112. net2segs = self._create_segments(segments2, network_id=net_id2)
  113. segs = segments_db.get_networks_segments(
  114. self.ctx, [net_id1, net_id2])
  115. self.assertEqual(net1segs, self._sort_segments(segs[net_id1]))
  116. self.assertEqual(net2segs, self._sort_segments(segs[net_id2]))
  117. def test_get_networks_segments_no_segments(self):
  118. net_id1 = uuidutils.generate_uuid()
  119. net_id2 = uuidutils.generate_uuid()
  120. self._create_segments([], network_id=net_id1)
  121. self._create_segments([], network_id=net_id2)
  122. segs = segments_db.get_networks_segments(
  123. self.ctx, [net_id1, net_id2])
  124. self.assertEqual([], segs[net_id1])
  125. self.assertEqual([], segs[net_id2])
  126. def test_get_segment_by_id(self):
  127. segment = {api.NETWORK_TYPE: 'vlan',
  128. api.PHYSICAL_NETWORK: 'physnet1',
  129. api.SEGMENTATION_ID: 1}
  130. net_segment = self._create_segments([segment])[0]
  131. segment_uuid = net_segment[api.ID]
  132. net_segment = segments_db.get_segment_by_id(self.ctx,
  133. segment_uuid)
  134. self.assertEqual(segment, net_segment)
  135. def test_get_segment_by_id_result_not_found(self):
  136. segment_uuid = uuidutils.generate_uuid()
  137. net_segment = segments_db.get_segment_by_id(self.ctx,
  138. segment_uuid)
  139. self.assertIsNone(net_segment)
  140. def test_delete_network_segment(self):
  141. segment = {api.NETWORK_TYPE: 'vlan',
  142. api.PHYSICAL_NETWORK: 'physnet1',
  143. api.SEGMENTATION_ID: 1}
  144. net_segment = self._create_segments([segment])[0]
  145. segment_uuid = net_segment[api.ID]
  146. segments_db.delete_network_segment(self.ctx, segment_uuid)
  147. # Get segment and verify its empty
  148. net_segment = segments_db.get_segment_by_id(self.ctx,
  149. segment_uuid)
  150. self.assertIsNone(net_segment)
  151. def test_get_dynamic_segment(self):
  152. net_id = uuidutils.generate_uuid()
  153. segment1 = {api.NETWORK_TYPE: 'vlan',
  154. api.PHYSICAL_NETWORK: 'physnet1',
  155. api.SEGMENTATION_ID: 1}
  156. self._create_segments(
  157. [segment1], is_seg_dynamic=True, network_id=net_id)
  158. segs1 = segments_db.get_dynamic_segment(
  159. self.ctx, net_id)
  160. self.assertEqual('vlan', segs1[api.NETWORK_TYPE])
  161. self.assertEqual('physnet1', segs1[api.PHYSICAL_NETWORK])
  162. self.assertEqual(1, segs1[api.SEGMENTATION_ID])
  163. segs2 = segments_db.get_dynamic_segment(
  164. self.ctx, net_id, physical_network='physnet1')
  165. self.assertEqual('vlan', segs2[api.NETWORK_TYPE])
  166. self.assertEqual('physnet1', segs2[api.PHYSICAL_NETWORK])
  167. self.assertEqual(1, segs2[api.SEGMENTATION_ID])
  168. segs3 = segments_db.get_dynamic_segment(
  169. self.ctx, net_id, segmentation_id=1)
  170. self.assertEqual('vlan', segs3[api.NETWORK_TYPE])
  171. self.assertEqual('physnet1', segs3[api.PHYSICAL_NETWORK])
  172. self.assertEqual(1, segs3[api.SEGMENTATION_ID])
  173. def test_add_port_binding(self):
  174. network_id = uuidutils.generate_uuid()
  175. port_id = uuidutils.generate_uuid()
  176. self._setup_neutron_network(network_id)
  177. self._setup_neutron_port(network_id, port_id)
  178. port = ml2_db.add_port_binding(self.ctx, port_id)
  179. self.assertEqual(port_id, port.port_id)
  180. self.assertEqual(portbindings.VIF_TYPE_UNBOUND, port.vif_type)
  181. def test_get_port_binding_host(self):
  182. network_id = uuidutils.generate_uuid()
  183. port_id = uuidutils.generate_uuid()
  184. host = 'fake_host'
  185. vif_type = portbindings.VIF_TYPE_UNBOUND
  186. self._setup_neutron_network(network_id)
  187. self._setup_neutron_port(network_id, port_id)
  188. self._setup_neutron_portbinding(port_id, vif_type, host)
  189. port_host = ml2_db.get_port_binding_host(self.ctx, port_id)
  190. self.assertEqual(host, port_host)
  191. def test_get_port_binding_host_multiple_results_found(self):
  192. network_id = uuidutils.generate_uuid()
  193. port_id = uuidutils.generate_uuid()
  194. port_id_one = uuidutils.generate_uuid()
  195. port_id_two = uuidutils.generate_uuid()
  196. # NOTE(manjeets) to check startswith testcase we
  197. # need port ids with same prefix
  198. port_id_one = port_id[:8] + port_id_one[8:]
  199. port_id_two = port_id[:8] + port_id_two[8:]
  200. host = 'fake_host'
  201. vif_type = portbindings.VIF_TYPE_UNBOUND
  202. self._setup_neutron_network(network_id)
  203. self._setup_neutron_port(network_id, port_id_one)
  204. self._setup_neutron_portbinding(port_id_one, vif_type, host)
  205. self._setup_neutron_port(network_id, port_id_two)
  206. self._setup_neutron_portbinding(port_id_two, vif_type, host)
  207. port_host = ml2_db.get_port_binding_host(self.ctx, port_id[:8])
  208. self.assertIsNone(port_host)
  209. def test_get_port_binding_host_result_not_found(self):
  210. port_id = uuidutils.generate_uuid()
  211. port_host = ml2_db.get_port_binding_host(self.ctx, port_id)
  212. self.assertIsNone(port_host)
  213. def test_get_port(self):
  214. network_id = uuidutils.generate_uuid()
  215. port_id = uuidutils.generate_uuid()
  216. self._setup_neutron_network(network_id)
  217. self._setup_neutron_port(network_id, port_id)
  218. port = ml2_db.get_port(self.ctx, port_id)
  219. self.assertEqual(port_id, port.id)
  220. def test_get_port_multiple_results_found(self):
  221. with mock.patch(
  222. 'sqlalchemy.orm.query.Query.one',
  223. side_effect=exc.MultipleResultsFound):
  224. port = ml2_db.get_port(self.ctx, 'unused')
  225. self.assertIsNone(port)
  226. def test_get_port_result_not_found(self):
  227. port_id = uuidutils.generate_uuid()
  228. port = ml2_db.get_port(self.ctx, port_id)
  229. self.assertIsNone(port)
  230. def test_get_port_from_device_mac(self):
  231. network_id = uuidutils.generate_uuid()
  232. port_id = uuidutils.generate_uuid()
  233. self._setup_neutron_network(network_id)
  234. port = self._setup_neutron_port(network_id, port_id)
  235. observed_port = ml2_db.get_port_from_device_mac(self.ctx,
  236. port['mac_address'])
  237. self.assertEqual(port_id, observed_port.id)
  238. class Ml2DvrDBTestCase(testlib_api.SqlTestCase):
  239. def setUp(self):
  240. super(Ml2DvrDBTestCase, self).setUp()
  241. self.ctx = context.get_admin_context()
  242. self.setup_coreplugin(PLUGIN_NAME)
  243. def _setup_neutron_network(self, network_id, port_ids):
  244. with db_api.context_manager.writer.using(self.ctx):
  245. network_obj.Network(self.ctx, id=network_id).create()
  246. ports = []
  247. for port_id in port_ids:
  248. mac_address = (db_base_plugin_v2.NeutronDbPluginV2.
  249. _generate_mac())
  250. port = port_obj.Port(self.ctx,
  251. id=port_id,
  252. network_id=network_id,
  253. mac_address=netaddr.EUI(mac_address),
  254. admin_state_up=True,
  255. status='ACTIVE',
  256. device_id='',
  257. device_owner='')
  258. port.create()
  259. ports.append(port)
  260. return ports
  261. def _setup_neutron_router(self):
  262. with self.ctx.session.begin(subtransactions=True):
  263. router = l3_models.Router()
  264. self.ctx.session.add(router)
  265. return router
  266. def _setup_distributed_binding(self, network_id,
  267. port_id, router_id, host_id):
  268. with db_api.context_manager.writer.using(self.ctx):
  269. record = models.DistributedPortBinding(
  270. port_id=port_id,
  271. host=host_id,
  272. router_id=router_id,
  273. vif_type=portbindings.VIF_TYPE_UNBOUND,
  274. vnic_type=portbindings.VNIC_NORMAL,
  275. status='DOWN')
  276. self.ctx.session.add(record)
  277. return record
  278. def test_ensure_distributed_port_binding_deals_with_db_duplicate(self):
  279. network_id = uuidutils.generate_uuid()
  280. port_id = uuidutils.generate_uuid()
  281. router_id = 'foo_router_id'
  282. host_id = 'foo_host_id'
  283. self._setup_neutron_network(network_id, [port_id])
  284. self._setup_distributed_binding(network_id, port_id,
  285. router_id, host_id)
  286. with mock.patch.object(query.Query, 'first') as query_first:
  287. query_first.return_value = []
  288. with mock.patch.object(ml2_db.LOG, 'debug') as log_trace:
  289. binding = ml2_db.ensure_distributed_port_binding(
  290. self.ctx, port_id, host_id, router_id)
  291. self.assertTrue(query_first.called)
  292. self.assertTrue(log_trace.called)
  293. self.assertEqual(port_id, binding.port_id)
  294. def test_ensure_distributed_port_binding(self):
  295. network_id = uuidutils.generate_uuid()
  296. port_id = uuidutils.generate_uuid()
  297. self._setup_neutron_network(network_id, [port_id])
  298. router = self._setup_neutron_router()
  299. ml2_db.ensure_distributed_port_binding(
  300. self.ctx, port_id, 'foo_host', router.id)
  301. expected = (self.ctx.session.query(models.DistributedPortBinding).
  302. filter_by(port_id=port_id).one())
  303. self.assertEqual(port_id, expected.port_id)
  304. def test_ensure_distributed_port_binding_multiple_bindings(self):
  305. network_id = uuidutils.generate_uuid()
  306. port_id = uuidutils.generate_uuid()
  307. self._setup_neutron_network(network_id, [port_id])
  308. router = self._setup_neutron_router()
  309. ml2_db.ensure_distributed_port_binding(
  310. self.ctx, port_id, 'foo_host_1', router.id)
  311. ml2_db.ensure_distributed_port_binding(
  312. self.ctx, port_id, 'foo_host_2', router.id)
  313. bindings = (self.ctx.session.query(models.DistributedPortBinding).
  314. filter_by(port_id=port_id).all())
  315. self.assertEqual(2, len(bindings))
  316. def test_delete_distributed_port_binding_if_stale(self):
  317. network_id = uuidutils.generate_uuid()
  318. port_id = uuidutils.generate_uuid()
  319. self._setup_neutron_network(network_id, [port_id])
  320. binding = self._setup_distributed_binding(
  321. network_id, port_id, None, 'foo_host_id')
  322. ml2_db.delete_distributed_port_binding_if_stale(self.ctx,
  323. binding)
  324. count = (self.ctx.session.query(models.DistributedPortBinding).
  325. filter_by(port_id=binding.port_id).count())
  326. self.assertFalse(count)
  327. def test_get_distributed_port_binding_by_host_not_found(self):
  328. port = ml2_db.get_distributed_port_binding_by_host(
  329. self.ctx, 'foo_port_id', 'foo_host_id')
  330. self.assertIsNone(port)
  331. def test_get_distributed_port_bindings_not_found(self):
  332. port = ml2_db.get_distributed_port_bindings(self.ctx,
  333. 'foo_port_id')
  334. self.assertFalse(len(port))
  335. def test_get_distributed_port_bindings(self):
  336. network_id = uuidutils.generate_uuid()
  337. port_id_1 = uuidutils.generate_uuid()
  338. port_id_2 = uuidutils.generate_uuid()
  339. self._setup_neutron_network(network_id, [port_id_1, port_id_2])
  340. router = self._setup_neutron_router()
  341. self._setup_distributed_binding(
  342. network_id, port_id_1, router.id, 'foo_host_id_1')
  343. self._setup_distributed_binding(
  344. network_id, port_id_1, router.id, 'foo_host_id_2')
  345. ports = ml2_db.get_distributed_port_bindings(self.ctx,
  346. port_id_1)
  347. self.assertEqual(2, len(ports))
  348. def test_distributed_port_binding_deleted_by_port_deletion(self):
  349. network_id = uuidutils.generate_uuid()
  350. network_obj.Network(self.ctx, id=network_id).create()
  351. with db_api.context_manager.writer.using(self.ctx):
  352. device_owner = constants.DEVICE_OWNER_DVR_INTERFACE
  353. port = models_v2.Port(
  354. id='port_id',
  355. network_id=network_id,
  356. mac_address='00:11:22:33:44:55',
  357. admin_state_up=True,
  358. status=constants.PORT_STATUS_ACTIVE,
  359. device_id='device_id',
  360. device_owner=device_owner)
  361. self.ctx.session.add(port)
  362. binding_kwarg = {
  363. 'port_id': 'port_id',
  364. 'host': 'host',
  365. 'vif_type': portbindings.VIF_TYPE_UNBOUND,
  366. 'vnic_type': portbindings.VNIC_NORMAL,
  367. 'router_id': 'router_id',
  368. 'status': constants.PORT_STATUS_DOWN
  369. }
  370. self.ctx.session.add(models.DistributedPortBinding(
  371. **binding_kwarg))
  372. binding_kwarg['host'] = 'another-host'
  373. self.ctx.session.add(models.DistributedPortBinding(
  374. **binding_kwarg))
  375. with warnings.catch_warnings(record=True) as warning_list:
  376. with db_api.context_manager.writer.using(self.ctx):
  377. self.ctx.session.delete(port)
  378. self.assertEqual(
  379. [], warning_list,
  380. 'Warnings: %s' % ';'.join([str(w) for w in warning_list]))
  381. ports = ml2_db.get_distributed_port_bindings(self.ctx,
  382. 'port_id')
  383. self.assertEqual(0, len(ports))