From 7156ebfc2853ceeeeb94b7a4f9046eeb256051c1 Mon Sep 17 00:00:00 2001 From: Slawek Kaplonski Date: Fri, 30 Aug 2019 22:32:19 +0200 Subject: [PATCH] Fix creation of vlan network with segmentation_id set to 0 In case when vlan network was created with segmentation_id=0 and without physical_network given, it was passing validation of provider segment and first available segmentation_id was choosen for network. Problem was that in such case all available segmentation ids where allocated and no other vlan network could be created later. This patch fixes validation of segmentation_id when it is set to value 0. Change-Id: Ic768deb84d544db832367f9a4b84a92729eee620 Closes-bug: #1840895 (cherry picked from commit f01f3ae5dd0dd7dd9aa513a1b50e04e20a08b97b) --- neutron/plugins/ml2/drivers/type_vlan.py | 4 ++-- .../plugins/ml2/drivers/test_type_vlan.py | 21 ++++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/neutron/plugins/ml2/drivers/type_vlan.py b/neutron/plugins/ml2/drivers/type_vlan.py index 2e2471e2116..7847612583e 100644 --- a/neutron/plugins/ml2/drivers/type_vlan.py +++ b/neutron/plugins/ml2/drivers/type_vlan.py @@ -241,7 +241,7 @@ class VlanTypeDriver(helpers.SegmentTypeDriver): msg = (_("physical_network '%s' unknown " "for VLAN provider network") % physical_network) raise exc.InvalidInput(error_message=msg) - if segmentation_id: + if segmentation_id is not None: if not plugin_utils.is_valid_vlan_tag(segmentation_id): msg = (_("segmentation_id out of range (%(min)s through " "%(max)s)") % @@ -254,7 +254,7 @@ class VlanTypeDriver(helpers.SegmentTypeDriver): "to be specified when creating a provider " "network") % physical_network) raise exc.InvalidInput(error_message=msg) - elif segmentation_id: + elif segmentation_id is not None: msg = _("segmentation_id requires physical_network for VLAN " "provider network") raise exc.InvalidInput(error_message=msg) diff --git a/neutron/tests/unit/plugins/ml2/drivers/test_type_vlan.py b/neutron/tests/unit/plugins/ml2/drivers/test_type_vlan.py index 657edea9d85..92b8af58cd8 100644 --- a/neutron/tests/unit/plugins/ml2/drivers/test_type_vlan.py +++ b/neutron/tests/unit/plugins/ml2/drivers/test_type_vlan.py @@ -100,6 +100,13 @@ class VlanTypeTest(testlib_api.SqlTestCase): segment = {api.NETWORK_TYPE: p_const.TYPE_VLAN} self.driver.validate_provider_segment(segment) + def test_validate_provider_segment_no_phys_network_seg_id_0(self): + segment = {api.NETWORK_TYPE: p_const.TYPE_VLAN, + api.SEGMENTATION_ID: 0} + self.assertRaises(exc.InvalidInput, + self.driver.validate_provider_segment, + segment) + def test_validate_provider_segment_with_missing_physical_network(self): segment = {api.NETWORK_TYPE: p_const.TYPE_VLAN, api.SEGMENTATION_ID: 1} @@ -117,11 +124,15 @@ class VlanTypeTest(testlib_api.SqlTestCase): def test_validate_provider_segment_with_invalid_segmentation_id(self): segment = {api.NETWORK_TYPE: p_const.TYPE_VLAN, - api.PHYSICAL_NETWORK: PROVIDER_NET, - api.SEGMENTATION_ID: 5000} - self.assertRaises(exc.InvalidInput, - self.driver.validate_provider_segment, - segment) + api.PHYSICAL_NETWORK: PROVIDER_NET} + segmentation_ids = [ + p_const.MIN_VLAN_TAG - 1, + p_const.MAX_VLAN_TAG + 1] + for segmentation_id in segmentation_ids: + segment[api.SEGMENTATION_ID] = segmentation_id + self.assertRaises(exc.InvalidInput, + self.driver.validate_provider_segment, + segment) def test_validate_provider_segment_with_invalid_input(self): segment = {api.NETWORK_TYPE: p_const.TYPE_VLAN,