]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Handle bool correctly during _extend_extra_router_dict
authorarmando-migliaccio <armamig@gmail.com>
Fri, 25 Jul 2014 04:44:47 +0000 (21:44 -0700)
committerarmando-migliaccio <armamig@gmail.com>
Fri, 25 Jul 2014 06:26:41 +0000 (23:26 -0700)
Ensure that extension attributes are always used to
override the chosen defaults. This was not working
in the case of default boolean True, as the testing
condition was wrong.

Closes-bug: #1348479

Change-Id: I22bce82c6078a96c0eb4a67e6decb6e9205721a8

neutron/db/l3_attrs_db.py
neutron/tests/unit/test_l3_plugin.py

index 18757bf62132ab6cb8bb96e4f7a32d70f95eddf8..b04778ab7e9425acec08e0619dc631a21a5b67c1 100644 (file)
@@ -48,12 +48,12 @@ class ExtraAttributesMixin(object):
     extra_attributes = []
 
     def _extend_extra_router_dict(self, router_res, router_db):
-        extra_attrs = router_db['extra_attributes']
+        extra_attrs = router_db['extra_attributes'] or {}
         for attr in self.extra_attributes:
             name = attr['name']
             default = attr['default']
             router_res[name] = (
-                extra_attrs and extra_attrs[name] or default)
+                extra_attrs[name] if name in extra_attrs else default)
 
     def _get_extra_attributes(self, router, extra_attributes):
         return (dict((attr['name'],
index ccce434800207df94cbedfddc5367b631388bf4e..c1a62ae1d16c66647f0f76926e4137b44fbb19c5 100644 (file)
@@ -31,6 +31,7 @@ from neutron.db import common_db_mixin
 from neutron.db import db_base_plugin_v2
 from neutron.db import external_net_db
 from neutron.db import l3_agentschedulers_db
+from neutron.db import l3_attrs_db
 from neutron.db import l3_db
 from neutron.db import l3_dvr_db
 from neutron.db import l3_rpc_base
@@ -42,6 +43,7 @@ from neutron.openstack.common import importutils
 from neutron.openstack.common import log as logging
 from neutron.openstack.common import uuidutils
 from neutron.plugins.common import constants as service_constants
+from neutron.tests import base
 from neutron.tests import fake_notifier
 from neutron.tests.unit import test_agent_ext_plugin
 from neutron.tests.unit import test_api_v2
@@ -510,6 +512,57 @@ class L3NatTestCaseMixin(object):
                 yield f
 
 
+class ExtraAttributesMixinTestCase(base.BaseTestCase):
+
+    def setUp(self):
+        super(ExtraAttributesMixinTestCase, self).setUp()
+        self.mixin = l3_attrs_db.ExtraAttributesMixin()
+
+    def _test__extend_extra_router_dict(
+        self, extra_attributes, attributes, expected_attributes):
+        self.mixin._extend_extra_router_dict(
+            attributes, {'extra_attributes': extra_attributes})
+        self.assertEqual(expected_attributes, attributes)
+
+    def test__extend_extra_router_dict_string_default(self):
+        self.mixin.extra_attributes = [{
+            'name': "foo_key",
+            'default': 'foo_default'
+        }]
+        extension_attributes = {'foo_key': 'my_fancy_value'}
+        self._test__extend_extra_router_dict(
+            extension_attributes, {}, extension_attributes)
+
+    def test__extend_extra_router_dict_booleans_false_default(self):
+        self.mixin.extra_attributes = [{
+            'name': "foo_key",
+            'default': False
+        }]
+        extension_attributes = {'foo_key': True}
+        self._test__extend_extra_router_dict(
+            extension_attributes, {}, extension_attributes)
+
+    def test__extend_extra_router_dict_booleans_true_default(self):
+        self.mixin.extra_attributes = [{
+            'name': "foo_key",
+            'default': True
+        }]
+        # Test that the default is overridden
+        extension_attributes = {'foo_key': False}
+        self._test__extend_extra_router_dict(
+            extension_attributes, {}, extension_attributes)
+
+    def test__extend_extra_router_dict_no_extension_attributes(self):
+        self.mixin.extra_attributes = [{
+            'name': "foo_key",
+            'default': 'foo_value'
+        }]
+        self._test__extend_extra_router_dict({}, {}, {'foo_key': 'foo_value'})
+
+    def test__extend_extra_router_dict_none_extension_attributes(self):
+        self._test__extend_extra_router_dict(None, {}, {})
+
+
 class L3NatTestCaseBase(L3NatTestCaseMixin):
 
     def test_router_create(self):