]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Validate network config (vlan)
authorManish Godara <manishg@yahoo-inc.com>
Mon, 6 Oct 2014 17:03:28 +0000 (10:03 -0700)
committerManish Godara <manishg@yahoo-inc.com>
Wed, 22 Oct 2014 17:29:33 +0000 (10:29 -0700)
Validate to make sure empty physical networks
are not permitted. Added unit test as well.

Change-Id: Ie55fab3c53276ed48d5e9d603e8c1dc59fa9cc32
Closes-Bug: 1325664

neutron/common/exceptions.py
neutron/plugins/common/utils.py
neutron/plugins/ml2/drivers/type_vlan.py
neutron/tests/unit/ml2/test_type_vlan.py
neutron/tests/unit/test_common_utils.py

index be62388aa6e5d62488cd9089134dfc093f386572..81c1e6e6aef778b61dee0c7ca6590a752b7fefc4 100644 (file)
@@ -305,6 +305,10 @@ class NetworkVlanRangeError(NeutronException):
         super(NetworkVlanRangeError, self).__init__(**kwargs)
 
 
+class PhysicalNetworkNameError(NeutronException):
+    message = _("Empty physical network name.")
+
+
 class NetworkTunnelRangeError(NeutronException):
     message = _("Invalid network Tunnel range: "
                 "'%(tunnel_range)s' - %(error)s")
index 6abd52554cbe29b681cee51e7925054a1e6c4b1e..7addb639a4d63506533f2c9926c31a53cba92d73 100644 (file)
@@ -61,6 +61,8 @@ def parse_network_vlan_range(network_vlan_range):
             vlan_range = (int(vlan_min), int(vlan_max))
         except ValueError as ex:
             raise n_exc.NetworkVlanRangeError(vlan_range=entry, error=ex)
+        if not network:
+            raise n_exc.PhysicalNetworkNameError()
         verify_vlan_range(vlan_range)
         return network, vlan_range
     else:
index 5cd3c3c8be5f802df9b43fed107b63745bb221ff..efe17a4c3f2165a90fe1d7f593149fdc8fb0be0b 100644 (file)
@@ -87,8 +87,6 @@ class VlanTypeDriver(helpers.TypeDriverHelper):
         try:
             self.network_vlan_ranges = plugin_utils.parse_network_vlan_ranges(
                 cfg.CONF.ml2_type_vlan.network_vlan_ranges)
-            # TODO(rkukura): Validate that each physical_network name
-            # is neither empty nor too long.
         except Exception:
             LOG.exception(_("Failed to parse network_vlan_ranges. "
                             "Service terminated!"))
index 2a78152120704cdd4d3f99add85b40bd4b8e16a0..4bf73e688a160422c3dffe6654767810f001f856 100644 (file)
@@ -19,18 +19,18 @@ from testtools import matchers
 from neutron.common import exceptions as exc
 import neutron.db.api as db
 from neutron.plugins.common import constants as p_const
+from neutron.plugins.common import utils as plugin_utils
 from neutron.plugins.ml2 import driver_api as api
 from neutron.plugins.ml2.drivers import type_vlan
 from neutron.tests.unit import testlib_api
+from oslo.config import cfg
 
 PROVIDER_NET = 'phys_net1'
 TENANT_NET = 'phys_net2'
 VLAN_MIN = 200
 VLAN_MAX = 209
-NETWORK_VLAN_RANGES = {
-    PROVIDER_NET: [],
-    TENANT_NET: [(VLAN_MIN, VLAN_MAX)],
-}
+NETWORK_VLAN_RANGES = [PROVIDER_NET, "%s:%s:%s" %
+                       (TENANT_NET, VLAN_MIN, VLAN_MAX)]
 UPDATED_VLAN_RANGES = {
     PROVIDER_NET: [],
     TENANT_NET: [(VLAN_MIN + 5, VLAN_MAX + 5)],
@@ -41,11 +41,21 @@ class VlanTypeTest(testlib_api.SqlTestCase):
 
     def setUp(self):
         super(VlanTypeTest, self).setUp()
+        cfg.CONF.set_override('network_vlan_ranges', NETWORK_VLAN_RANGES,
+                              group='ml2_type_vlan')
+        self.network_vlan_ranges = plugin_utils.parse_network_vlan_ranges(
+            NETWORK_VLAN_RANGES)
         self.driver = type_vlan.VlanTypeDriver()
-        self.driver.network_vlan_ranges = NETWORK_VLAN_RANGES
         self.driver._sync_vlan_allocations()
         self.session = db.get_session()
 
+    def test_parse_network_exception_handling(self):
+        with mock.patch.object(plugin_utils,
+                               'parse_network_vlan_ranges') as parse_ranges:
+            parse_ranges.side_effect = Exception('any exception')
+            self.assertRaises(SystemExit,
+                              self.driver._parse_network_vlan_ranges)
+
     def _get_allocation(self, session, segment):
         return session.query(type_vlan.VlanAllocation).filter_by(
             physical_network=segment[api.PHYSICAL_NETWORK],
@@ -128,7 +138,7 @@ class VlanTypeTest(testlib_api.SqlTestCase):
             self.assertFalse(
                 self._get_allocation(self.session, segment).allocated)
 
-        check_in_ranges(NETWORK_VLAN_RANGES)
+        check_in_ranges(self.network_vlan_ranges)
         self.driver.network_vlan_ranges = UPDATED_VLAN_RANGES
         self.driver._sync_vlan_allocations()
         check_in_ranges(UPDATED_VLAN_RANGES)
index 5534e380dcf14e48cc8074278ba37b9f464ed33c..c4dfb5a1c0094c8a8b003797df25ceb75c35739c 100644 (file)
@@ -161,6 +161,22 @@ class UtilTestParseVlanRanges(base.BaseTestCase):
         return self._err_prefix + v_range_str + self._err_range
 
 
+class TestVlanNetworkNameValid(base.BaseTestCase):
+    def parse_vlan_ranges(self, vlan_range):
+        return plugin_utils.parse_network_vlan_ranges(vlan_range)
+
+    def test_validate_provider_phynet_name_mixed(self):
+        self.assertRaises(n_exc.PhysicalNetworkNameError,
+                          self.parse_vlan_ranges,
+                          ['', ':23:30', 'physnet1',
+                           'tenant_net:100:200'])
+
+    def test_validate_provider_phynet_name_bad(self):
+        self.assertRaises(n_exc.PhysicalNetworkNameError,
+                          self.parse_vlan_ranges,
+                          [':1:34'])
+
+
 class TestVlanRangeVerifyValid(UtilTestParseVlanRanges):
     def verify_range(self, vlan_range):
         return plugin_utils.verify_vlan_range(vlan_range)