From 6bd147df43b1d352230d94f52e0fb4c56e7885d6 Mon Sep 17 00:00:00 2001 From: armando-migliaccio Date: Thu, 24 Jul 2014 21:44:47 -0700 Subject: [PATCH] Handle bool correctly during _extend_extra_router_dict 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 | 4 +-- neutron/tests/unit/test_l3_plugin.py | 53 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/neutron/db/l3_attrs_db.py b/neutron/db/l3_attrs_db.py index 18757bf62..b04778ab7 100644 --- a/neutron/db/l3_attrs_db.py +++ b/neutron/db/l3_attrs_db.py @@ -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'], diff --git a/neutron/tests/unit/test_l3_plugin.py b/neutron/tests/unit/test_l3_plugin.py index ccce43480..c1a62ae1d 100644 --- a/neutron/tests/unit/test_l3_plugin.py +++ b/neutron/tests/unit/test_l3_plugin.py @@ -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): -- 2.45.2