]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Enforce required config params for ODL driver
authorKevin Benton <blak111@gmail.com>
Sun, 6 Apr 2014 11:29:07 +0000 (04:29 -0700)
committerGerrit Code Review <review@openstack.org>
Tue, 8 Apr 2014 06:41:31 +0000 (06:41 +0000)
Raise a config error during initialization if
there is no URL, username, or password specified
in the config for the OpenDayLight ML2 driver.

Closes-Bug: #1301432
Change-Id: I65fc94d3eaaade3d1402d1c82d2c1edfa7133d5a

neutron/plugins/ml2/drivers/mechanism_odl.py
neutron/tests/unit/ml2/test_mechanism_odl.py

index 87d89afcb3a6340d0506e64ee0caa19caad280c1..cecc06c01aa4b1be94f298d666bdac4e681e1b53 100644 (file)
@@ -129,6 +129,10 @@ class OpenDaylightMechanismDriver(api.MechanismDriver):
         self.timeout = cfg.CONF.ml2_odl.timeout
         self.username = cfg.CONF.ml2_odl.username
         self.password = cfg.CONF.ml2_odl.password
+        required_opts = ('url', 'username', 'password')
+        for opt in required_opts:
+            if not getattr(self, opt):
+                raise cfg.RequiredOptError(opt, 'ml2_odl')
         self.auth = JsessionId(self.url, self.username, self.password)
         self.vif_type = portbindings.VIF_TYPE_OVS
         self.vif_details = {portbindings.CAP_PORT_FILTER: True}
@@ -311,18 +315,17 @@ class OpenDaylightMechanismDriver(api.MechanismDriver):
 
         headers = {'Content-Type': 'application/json'}
         data = jsonutils.dumps(obj, indent=2) if obj else None
-        if self.url:
-            url = '/'.join([self.url, urlpath])
-            LOG.debug(_('ODL-----> sending URL (%s) <-----ODL') % url)
-            LOG.debug(_('ODL-----> sending JSON (%s) <-----ODL') % obj)
-            r = requests.request(method, url=url,
-                                 headers=headers, data=data,
-                                 auth=self.auth, timeout=self.timeout)
-
-            # ignorecodes contains a list of HTTP error codes to ignore.
-            if r.status_code in ignorecodes:
-                return
-            r.raise_for_status()
+        url = '/'.join([self.url, urlpath])
+        LOG.debug(_('ODL-----> sending URL (%s) <-----ODL') % url)
+        LOG.debug(_('ODL-----> sending JSON (%s) <-----ODL') % obj)
+        r = requests.request(method, url=url,
+                             headers=headers, data=data,
+                             auth=self.auth, timeout=self.timeout)
+
+        # ignorecodes contains a list of HTTP error codes to ignore.
+        if r.status_code in ignorecodes:
+            return
+        r.raise_for_status()
 
     def bind_port(self, context):
         LOG.debug(_("Attempting to bind port %(port)s on "
index 423e6db8545f146939adc7436d8bd547b568e392..8034d6496b46ea8367be1fc784d2523b2d1dbedd 100644 (file)
@@ -32,6 +32,12 @@ class OpenDaylightTestCase(test_plugin.NeutronDbPluginV2TestCase):
         config.cfg.CONF.set_override('mechanism_drivers',
                                      ['logger', 'opendaylight'],
                                      'ml2')
+        # Set URL/user/pass so init doesn't throw a cfg required error.
+        # They are not used in these tests since sendjson is overwritten.
+        config.cfg.CONF.set_override('url', 'http://127.0.0.1:9999', 'ml2_odl')
+        config.cfg.CONF.set_override('username', 'someuser', 'ml2_odl')
+        config.cfg.CONF.set_override('password', 'somepass', 'ml2_odl')
+
         super(OpenDaylightTestCase, self).setUp(PLUGIN_NAME)
         self.port_create_status = 'DOWN'
         self.segment = {'api.NETWORK_TYPE': ""}
@@ -59,6 +65,32 @@ class OpenDaylightTestCase(test_plugin.NeutronDbPluginV2TestCase):
         self.assertFalse(self.mech.check_segment(self.segment))
 
 
+class OpenDayLightMechanismConfigTests(test_plugin.NeutronDbPluginV2TestCase):
+
+    def _setUp(self):
+        config.cfg.CONF.set_override('mechanism_drivers',
+                                     ['logger', 'opendaylight'],
+                                     'ml2')
+        config.cfg.CONF.set_override('url', 'http://127.0.0.1:9999', 'ml2_odl')
+        config.cfg.CONF.set_override('username', 'someuser', 'ml2_odl')
+        config.cfg.CONF.set_override('password', 'somepass', 'ml2_odl')
+
+    def test_url_required(self):
+        self._setUp()
+        config.cfg.CONF.set_override('url', None, 'ml2_odl')
+        self.assertRaises(config.cfg.RequiredOptError, self.setUp, PLUGIN_NAME)
+
+    def test_username_required(self):
+        self._setUp()
+        config.cfg.CONF.set_override('username', None, 'ml2_odl')
+        self.assertRaises(config.cfg.RequiredOptError, self.setUp, PLUGIN_NAME)
+
+    def test_password_required(self):
+        self._setUp()
+        config.cfg.CONF.set_override('password', None, 'ml2_odl')
+        self.assertRaises(config.cfg.RequiredOptError, self.setUp, PLUGIN_NAME)
+
+
 class OpenDaylightMechanismTestBasicGet(test_plugin.TestBasicGet,
                                         OpenDaylightTestCase):
     pass