From da02fc6e4191bdbbb2015b78f9c3fe5045bb0460 Mon Sep 17 00:00:00 2001
From: Vishvananda Ishaya <vishvananda@gmail.com>
Date: Tue, 23 Aug 2011 15:14:09 -0700
Subject: [PATCH 1/3] Fix not found exceptions to properly use ec2_ips for not
 found

---
 nova/api/ec2/__init__.py | 10 ++++++----
 nova/exception.py        |  1 +
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/nova/api/ec2/__init__.py b/nova/api/ec2/__init__.py
index 5430f443dabf..363dad7cd1e1 100644
--- a/nova/api/ec2/__init__.py
+++ b/nova/api/ec2/__init__.py
@@ -392,17 +392,19 @@ class Executor(wsgi.Application):
         except exception.InstanceNotFound as ex:
             LOG.info(_('InstanceNotFound raised: %s'), unicode(ex),
                      context=context)
-            return self._error(req, context, type(ex).__name__, ex.message)
+            ec2_id = ec2utils.id_to_ec2_id(ex.kwargs['instance_id'])
+            message = ex.message % {'instance_id': ec2_id}
+            return self._error(req, context, type(ex).__name__, message)
         except exception.VolumeNotFound as ex:
             LOG.info(_('VolumeNotFound raised: %s'), unicode(ex),
                      context=context)
-            ec2_id = ec2utils.id_to_ec2_vol_id(ex.volume_id)
-            message = _('Volume %s not found') % ec2_id
+            ec2_id = ec2utils.id_to_ec2_vol_id(ex.kwargs['volume_id'])
+            message = ex.message % {'volume_id': ec2_id}
             return self._error(req, context, type(ex).__name__, message)
         except exception.SnapshotNotFound as ex:
             LOG.info(_('SnapshotNotFound raised: %s'), unicode(ex),
                      context=context)
-            ec2_id = ec2utils.id_to_ec2_snap_id(ex.snapshot_id)
+            ec2_id = ec2utils.id_to_ec2_snap_id(ex.kwargs['snapshot_id'])
             message = _('Snapshot %s not found') % ec2_id
             return self._error(req, context, type(ex).__name__, message)
         except exception.NotFound as ex:
diff --git a/nova/exception.py b/nova/exception.py
index 66740019b5ea..5b86059d8ebd 100644
--- a/nova/exception.py
+++ b/nova/exception.py
@@ -146,6 +146,7 @@ class NovaException(Exception):
     message = _("An unknown exception occurred.")
 
     def __init__(self, **kwargs):
+        self.kwargs = kwargs
         try:
             self._error_string = self.message % kwargs
 

From 0343a328e66557abda9d0817558ad09a73962eb9 Mon Sep 17 00:00:00 2001
From: Vishvananda Ishaya <vishvananda@gmail.com>
Date: Wed, 24 Aug 2011 14:39:47 -0700
Subject: [PATCH 2/3] change snapshot msg too

---
 nova/api/ec2/__init__.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/nova/api/ec2/__init__.py b/nova/api/ec2/__init__.py
index 363dad7cd1e1..1e176e52d6f8 100644
--- a/nova/api/ec2/__init__.py
+++ b/nova/api/ec2/__init__.py
@@ -405,7 +405,7 @@ class Executor(wsgi.Application):
             LOG.info(_('SnapshotNotFound raised: %s'), unicode(ex),
                      context=context)
             ec2_id = ec2utils.id_to_ec2_snap_id(ex.kwargs['snapshot_id'])
-            message = _('Snapshot %s not found') % ec2_id
+            message = ex.message % {'snapshot_id': ec2_id}
             return self._error(req, context, type(ex).__name__, message)
         except exception.NotFound as ex:
             LOG.info(_('NotFound raised: %s'), unicode(ex), context=context)

From 470b9dc73c5e27ef8716436fe22e9f32dbdffd28 Mon Sep 17 00:00:00 2001
From: Vishvananda Ishaya <vishvananda@gmail.com>
Date: Fri, 26 Aug 2011 17:40:22 -0700
Subject: [PATCH 3/3] add tests to verify NotFound exceptions are wrapped with
 the proper ids

---
 nova/tests/api/ec2/__init__.py              | 19 +++++++++
 nova/tests/{ => api/ec2}/test_middleware.py | 45 +++++++++++++++++++++
 nova/tests/test_cloud.py                    |  7 ----
 3 files changed, 64 insertions(+), 7 deletions(-)
 create mode 100644 nova/tests/api/ec2/__init__.py
 rename nova/tests/{ => api/ec2}/test_middleware.py (67%)

diff --git a/nova/tests/api/ec2/__init__.py b/nova/tests/api/ec2/__init__.py
new file mode 100644
index 000000000000..6dab802f201a
--- /dev/null
+++ b/nova/tests/api/ec2/__init__.py
@@ -0,0 +1,19 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2011 Openstack LLC.
+# 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.
+
+# NOTE(vish): this forces the fixtures from tests/__init.py:setup() to work
+from nova.tests import *
diff --git a/nova/tests/test_middleware.py b/nova/tests/api/ec2/test_middleware.py
similarity index 67%
rename from nova/tests/test_middleware.py
rename to nova/tests/api/ec2/test_middleware.py
index 40d117c4563c..295f6c4ea56b 100644
--- a/nova/tests/test_middleware.py
+++ b/nova/tests/api/ec2/test_middleware.py
@@ -21,10 +21,13 @@ import webob.dec
 import webob.exc
 
 from nova.api import ec2
+from nova import context
+from nova import exception
 from nova import flags
 from nova import test
 from nova import utils
 
+from xml.etree.ElementTree import fromstring as xml_to_tree
 
 FLAGS = flags.FLAGS
 
@@ -83,3 +86,45 @@ class LockoutTestCase(test.TestCase):
         utils.advance_time_seconds(FLAGS.lockout_window * 60)
         self._send_bad_attempts('test', FLAGS.lockout_attempts - 1)
         self.assertFalse(self._is_locked_out('test'))
+
+
+class ExecutorTestCase(test.TestCase):
+    def setUp(self):
+        super(ExecutorTestCase, self).setUp()
+        self.executor = ec2.Executor()
+
+    def _execute(self, invoke):
+        class Fake(object):
+            pass
+        fake_ec2_request = Fake()
+        fake_ec2_request.invoke = invoke
+
+        fake_wsgi_request = Fake()
+
+        fake_wsgi_request.environ = {
+                'nova.context': context.get_admin_context(),
+                'ec2.request': fake_ec2_request,
+        }
+        return self.executor(fake_wsgi_request)
+
+    def _extract_message(self, result):
+        tree = xml_to_tree(result.body)
+        return tree.findall('./Errors')[0].find('Error/Message').text
+
+    def test_instance_not_found(self):
+        def not_found(context):
+            raise exception.InstanceNotFound(instance_id=5)
+        result = self._execute(not_found)
+        self.assertIn('i-00000005', self._extract_message(result))
+
+    def test_snapshot_not_found(self):
+        def not_found(context):
+            raise exception.SnapshotNotFound(snapshot_id=5)
+        result = self._execute(not_found)
+        self.assertIn('snap-00000005', self._extract_message(result))
+
+    def test_volume_not_found(self):
+        def not_found(context):
+            raise exception.VolumeNotFound(volume_id=5)
+        result = self._execute(not_found)
+        self.assertIn('vol-00000005', self._extract_message(result))
diff --git a/nova/tests/test_cloud.py b/nova/tests/test_cloud.py
index 0793784f8b1f..1bf12a06fcc9 100644
--- a/nova/tests/test_cloud.py
+++ b/nova/tests/test_cloud.py
@@ -86,13 +86,6 @@ class CloudTestCase(test.TestCase):
 
         self.stubs.Set(rpc, 'cast', finish_cast)
 
-    def tearDown(self):
-        networks = db.project_get_networks(self.context, self.project_id,
-                                           associate=False)
-        for network in networks:
-            db.network_disassociate(self.context, network['id'])
-        super(CloudTestCase, self).tearDown()
-
     def _create_key(self, name):
         # NOTE(vish): create depends on pool, so just call helper directly
         return cloud._gen_key(self.context, self.context.user_id, name)