From aef708188554948f84be4ce3cb1b1a8778f6153e Mon Sep 17 00:00:00 2001 From: Ann Kamyshnikova Date: Wed, 19 Nov 2014 11:51:14 +0300 Subject: [PATCH] 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 --- neutron/plugins/cisco/common/cisco_exceptions.py | 5 +++++ neutron/plugins/cisco/db/n1kv_db_v2.py | 7 +++++-- neutron/tests/unit/cisco/n1kv/test_n1kv_plugin.py | 12 ++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/neutron/plugins/cisco/common/cisco_exceptions.py b/neutron/plugins/cisco/common/cisco_exceptions.py index b0c297370..fd7a38f54 100644 --- a/neutron/plugins/cisco/common/cisco_exceptions.py +++ b/neutron/plugins/cisco/common/cisco_exceptions.py @@ -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 " diff --git a/neutron/plugins/cisco/db/n1kv_db_v2.py b/neutron/plugins/cisco/db/n1kv_db_v2.py index d753d1cbc..eaaff10fa 100644 --- a/neutron/plugins/cisco/db/n1kv_db_v2.py +++ b/neutron/plugins/cisco/db/n1kv_db_v2.py @@ -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): """ diff --git a/neutron/tests/unit/cisco/n1kv/test_n1kv_plugin.py b/neutron/tests/unit/cisco/n1kv/test_n1kv_plugin.py index 44c7b2f13..d0b272292 100644 --- a/neutron/tests/unit/cisco/n1kv/test_n1kv_plugin.py +++ b/neutron/tests/unit/cisco/n1kv/test_n1kv_plugin.py @@ -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): -- 2.45.2