Restore extraroute dict after OVO change

A recently merged OVO change [1] made the dict have netaddr objects.
This change restores plain strings so that it can be JSON
serializable again.

[1] I2439116abf051d0e19821ba53895fa0c724f6a96

Partially-Implements: blueprint adopt-oslo-versioned-objects-for-db
Closes-Bug: #1649733
Change-Id: Ie64aa161f1a2bb17098d88db20195b7e84bb0eae
This commit is contained in:
YAMAMOTO Takashi 2016-12-14 09:03:08 +09:00
parent 920ddeaf58
commit 7b6e236c7c
2 changed files with 75 additions and 2 deletions

View File

@ -132,8 +132,9 @@ class ExtraRoute_dbonly_mixin(l3_db.L3_NAT_dbonly_mixin):
@staticmethod
def _make_extra_route_list(extra_routes):
return [{'destination': route['destination'],
'nexthop': route['nexthop']}
# NOTE(yamamoto): the extra_routes argument is either object or db row
return [{'destination': str(route['destination']),
'nexthop': str(route['nexthop'])}
for route in extra_routes]
def _get_extra_routes_by_router_id(self, context, id):

View File

@ -0,0 +1,72 @@
# Copyright (c) 2016 Midokura SARL
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from neutron import context
from neutron.db import common_db_mixin
from neutron.db import extraroute_db
from neutron.tests.unit import testlib_api
class _Plugin(common_db_mixin.CommonDbMixin,
extraroute_db.ExtraRoute_dbonly_mixin):
pass
class TestExtraRouteDb(testlib_api.SqlTestCase):
def setUp(self):
super(TestExtraRouteDb, self).setUp()
self._plugin = _Plugin()
get_plugin = mock.patch('neutron_lib.plugins.directory.get_plugin',
return_value=self._plugin)
get_plugin.start()
def test_update(self):
ctx = context.get_admin_context()
create_request = {
'router': {
'name': 'my router',
'tenant_id': 'my tenant',
'admin_state_up': True,
}
}
router = self._plugin.create_router(ctx, create_request)
self.assertItemsEqual(router['routes'], [])
router_id = router['id']
routes = [
{'destination': '10.0.0.0/24', 'nexthop': '1.1.1.4'},
{'destination': '10.1.0.0/24', 'nexthop': '1.1.1.3'},
{'destination': '10.2.0.0/24', 'nexthop': '1.1.1.2'},
]
self._test_update_routes(ctx, router_id, router, routes)
routes = [
{'destination': '10.0.0.0/24', 'nexthop': '1.1.1.4'},
{'destination': '10.2.0.0/24', 'nexthop': '1.1.1.2'},
{'destination': '10.3.0.0/24', 'nexthop': '1.1.1.1'},
]
self._test_update_routes(ctx, router_id, router, routes)
def _test_update_routes(self, ctx, router_id, router, routes):
router['routes'] = routes
update_request = {
'router': router,
}
with mock.patch.object(self._plugin, '_validate_routes'):
updated_router = self._plugin.update_router(ctx, router_id,
update_request)
self.assertItemsEqual(updated_router['routes'], routes)
got_router = self._plugin.get_router(ctx, router_id)
self.assertItemsEqual(got_router['routes'], routes)