44e4493313
Due to the fact that plugin port operations are not transactional (as they involve non synchronized DB and Controller operations), concurrent (interleaved) port requests may accidentally cause the insertion of the mapping entry (neutron-port-id, nvp-port-id) more than once. In case this occurs, it's safe to expect the failure and continue the normal process of the operation being requested. Closes-bug: #1265081 Change-Id: Ifcf5b453fa08145df844c2de3cbb08bf2f4baa59
80 lines
3.5 KiB
Python
80 lines
3.5 KiB
Python
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
# Copyright 2013 VMware, Inc.
|
|
#
|
|
# 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.
|
|
|
|
from neutron import context
|
|
from neutron.db import api as db
|
|
from neutron.db import models_v2
|
|
from neutron.openstack.common.db import exception as d_exc
|
|
from neutron.plugins.nicira.dbexts import nicira_db
|
|
from neutron.plugins.nicira.dbexts import nicira_models
|
|
from neutron.tests import base
|
|
|
|
|
|
class NiciraDBTestCase(base.BaseTestCase):
|
|
|
|
def setUp(self):
|
|
super(NiciraDBTestCase, self).setUp()
|
|
db.configure_db()
|
|
self.ctx = context.get_admin_context()
|
|
self.addCleanup(db.clear_db)
|
|
|
|
def _setup_neutron_network_and_port(self, network_id, port_id):
|
|
with self.ctx.session.begin(subtransactions=True):
|
|
self.ctx.session.add(models_v2.Network(id=network_id))
|
|
port = models_v2.Port(id=port_id,
|
|
network_id=network_id,
|
|
mac_address='foo_mac_address',
|
|
admin_state_up=True,
|
|
status='ACTIVE',
|
|
device_id='',
|
|
device_owner='')
|
|
self.ctx.session.add(port)
|
|
|
|
def test_add_neutron_nsx_port_mapping_handle_duplicate_constraint(self):
|
|
neutron_net_id = 'foo_neutron_network_id'
|
|
neutron_port_id = 'foo_neutron_port_id'
|
|
nsx_port_id = 'foo_nsx_port_id'
|
|
nsx_switch_id = 'foo_nsx_switch_id'
|
|
self._setup_neutron_network_and_port(neutron_net_id, neutron_port_id)
|
|
|
|
nicira_db.add_neutron_nsx_port_mapping(
|
|
self.ctx.session, neutron_port_id, nsx_switch_id, nsx_port_id)
|
|
# Call the method twice to trigger a db duplicate constraint error
|
|
nicira_db.add_neutron_nsx_port_mapping(
|
|
self.ctx.session, neutron_port_id, nsx_switch_id, nsx_port_id)
|
|
result = (self.ctx.session.query(nicira_models.NeutronNsxPortMapping).
|
|
filter_by(neutron_id=neutron_port_id).one())
|
|
self.assertEqual(nsx_port_id, result.nsx_port_id)
|
|
self.assertEqual(neutron_port_id, result.neutron_id)
|
|
|
|
def test_add_neutron_nsx_port_mapping_raise_on_duplicate_constraint(self):
|
|
neutron_net_id = 'foo_neutron_network_id'
|
|
neutron_port_id = 'foo_neutron_port_id'
|
|
nsx_port_id_1 = 'foo_nsx_port_id_1'
|
|
nsx_port_id_2 = 'foo_nsx_port_id_2'
|
|
nsx_switch_id = 'foo_nsx_switch_id'
|
|
self._setup_neutron_network_and_port(neutron_net_id, neutron_port_id)
|
|
|
|
nicira_db.add_neutron_nsx_port_mapping(
|
|
self.ctx.session, neutron_port_id, nsx_switch_id, nsx_port_id_1)
|
|
# Call the method twice to trigger a db duplicate constraint error,
|
|
# this time with a different nsx port id!
|
|
self.assertRaises(d_exc.DBDuplicateEntry,
|
|
nicira_db.add_neutron_nsx_port_mapping,
|
|
self.ctx.session, neutron_port_id,
|
|
nsx_switch_id, nsx_port_id_2)
|