From: Kevin Benton Date: Sun, 6 Apr 2014 11:29:07 +0000 (-0700) Subject: Enforce required config params for ODL driver X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=2da351af7d2a10a3055020d572d357c56ba2689b;p=openstack-build%2Fneutron-build.git Enforce required config params for ODL driver 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 --- diff --git a/neutron/plugins/ml2/drivers/mechanism_odl.py b/neutron/plugins/ml2/drivers/mechanism_odl.py index 87d89afcb..cecc06c01 100644 --- a/neutron/plugins/ml2/drivers/mechanism_odl.py +++ b/neutron/plugins/ml2/drivers/mechanism_odl.py @@ -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 " diff --git a/neutron/tests/unit/ml2/test_mechanism_odl.py b/neutron/tests/unit/ml2/test_mechanism_odl.py index 423e6db85..8034d6496 100644 --- a/neutron/tests/unit/ml2/test_mechanism_odl.py +++ b/neutron/tests/unit/ml2/test_mechanism_odl.py @@ -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