Catch NoResultFound in _get_policy_profile_by_name

Add try-except in _get_policy_profile_by_name to raise
proper PolicyProfileNameNotFound if profile not found in
database.

Also unittest is added.

Closes-bug: #1393399

Change-Id: I3ab50a6870ff77a95e0977c75255e9cbd7286fbb
This commit is contained in:
Ann Kamyshnikova 2014-11-19 11:51:14 +03:00
parent a67d68e862
commit 81c2730de9
3 changed files with 22 additions and 2 deletions

View File

@ -153,6 +153,11 @@ class PolicyProfileIdNotFound(exceptions.NotFound):
message = _("Policy Profile %(profile_id)s could not be found.")
class PolicyProfileNameNotFound(exceptions.NotFound):
"""Policy Profile with the given name cannot be found."""
message = _("Policy Profile %(profile_name)s could not be found.")
class NetworkProfileAlreadyExists(exceptions.NeutronException):
"""Network Profile cannot be created since it already exists."""
message = _("Network Profile %(profile_id)s "

View File

@ -1599,8 +1599,11 @@ class PolicyProfile_db_mixin(object):
"""
db_session = db.get_session()
with db_session.begin(subtransactions=True):
return (db_session.query(n1kv_models_v2.PolicyProfile).
filter_by(name=name).one())
try:
return (db_session.query(n1kv_models_v2.PolicyProfile).
filter_by(name=name).one())
except exc.NoResultFound:
raise c_exc.PolicyProfileNameNotFound(profile_name=name)
def _remove_all_fake_policy_profiles(self):
"""

View File

@ -941,6 +941,18 @@ class TestN1kvPolicyProfiles(N1kvPluginTestCase):
# Request the list using admin and verify it returns
self._test_get_policy_profiles(expected_profiles=profiles, admin=True)
def test_get_policy_profiles_by_name(self):
with mock.patch(n1kv_client.__name__ + ".Client",
new=fake_client.TestClient):
instance = n1kv_neutron_plugin.N1kvNeutronPluginV2()
profile = instance._get_policy_profile_by_name('pp-1')
self.assertEqual('pp-1', profile['name'])
self.assertEqual('00000000-0000-0000-0000-000000000001',
profile['id'])
self.assertRaises(c_exc.PolicyProfileNameNotFound,
instance._get_policy_profile_by_name,
"name")
class TestN1kvNetworks(test_plugin.TestNetworksV2,
N1kvPluginTestCase):