]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Catch NoResultFound in _get_policy_profile_by_name
authorAnn Kamyshnikova <akamyshnikova@mirantis.com>
Wed, 19 Nov 2014 08:51:14 +0000 (11:51 +0300)
committerAnn Kamyshnikova <akamyshnikova@mirantis.com>
Wed, 19 Nov 2014 14:13:52 +0000 (17:13 +0300)
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
neutron/plugins/cisco/db/n1kv_db_v2.py
neutron/tests/unit/cisco/n1kv/test_n1kv_plugin.py

index b0c2973704b12f0bed35d5639c4d968294667f90..fd7a38f546301d0ad0f1c6deb8781a7184f9fdfa 100644 (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 "
index d753d1cbc2919b9d8218ab92508ccea865921a77..eaaff10fa85b4f1dbdee2b7f8f6b906c561943a1 100644 (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):
         """
index 44c7b2f13a6c0cd854c015c1970a33aee13c9026..d0b272292bd4d686a9cb9dc15551ab0b7104e059 100644 (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):